Most tutorials online have suggested the way to fire commands inside QT interfaces launched n Maya (via cmds.loadUi – not involving pyQT) is to add a string property like:

+command="myPythonInstance.pythonCommand()"
 

Pretty craptastical – it can only fire commands inside a specific python module, the name of which has to be known when you’re laying out your interface.

Ideally, we’d like to have our interface instanced from a python class. This would allow us, amongst other things, so have multiple, independant instances. However it seems that a “self” reference can’t get passed to an interface, which is what you’d reallly like to do.

+command=self.pythonCommand

A (reasonably hacky) solution is to create the widget callbacks from inside our python class, after we’ve loaded the interface via cmds.loadUi().

import maya.cmds as cmds

class myUi(object):
def __init__(self, *args):
uiFile = '/path/to/uiFile.ui'
rlDialog = cmds.loadUI(f=uiFile)
cmds.showWindow(rlDialog)
cmds.button( "ovrAdd_btn", edit=True, command=self.overrideAdd ) def overrideAdd(self, *args):
print 'do something!'

Which is slightly better, but we’re still referring to our widget by a string name. This is problematic if you have multiple instances of this class – targeting “ovrAdd_btn” is going to get confusing. We need to find out exactly which control, in amongst the whole widget mess, is the one that we’ve spawned when we’re called cmds.loadUI().

Sadly, loadUI() doesn’t give us any help in this department. So, and here’s the hacky bit, we can trawl through the widgets to find those belonging to our class instance, store them in a dict and then we can refer back to them by a simplified name. Easy!

import maya.cmds as cmds

def widgetPath(windowName, widgetNames):
""" @param windowName: Window instance name to search
@param widgetNames: list of names to search for
""" returnDict = {}
mayaWidgetList = cmds.lsUI(dumpWidgets=True) for widget in widgetNames:
for mayaWidge in mayaWidgetList:
if windowName in mayaWidge:
if mayaWidge.endswith(widget):
returnDict[widget] = mayaWidge return returnDict class myUi(object):
def __init__(self, *args):
uiWidgetList = ['ovrAdd_btn'] uiFile = '/path/to/uiFile.ui'
rlDialog = cmds.loadUI(f=uiFile)
self.uiObjects = widgetPath(rlDialog, uiWidgetList) cmds.showWindow(rlDialog) cmds.button( self.uiObjects["ovrAdd_btn"], edit=True, command=self.overrideAdd ) def overrideAdd(self, *args):
print 'do something!'

Trawling through Maya’s widget list isn’t particularly elegant, but you only have to do it once per class initialisation, so there isn’t too much of a cost. The advantage is that you can now have per-instance widget names.

Why not just use pyQT proper, you ask? Installing external dependencies isn’t always an option for our less techy brethren, or easily done in a studio-wide fashion. Using pyQT would be far better (and give all sorts of other benefits), but if you’re constrained to using vanilla maya / python, this seems to do the trick.

Maya QT interfaces in a class的更多相关文章

  1. 使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)

    使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization) 前言: 这是 qt for python 的语言国际化,基于 UI 的,python 也有 ...

  2. 使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件

    使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件 前期准备: 安装 python:https://www ...

  3. 无法启动 Maya 集成的 qt designer 的解决方法和原因 以及 中英文切换

    无法启动 Maya 集成的 qt designer 的解决方法和原因 以及 中英文切换 前言: Maya 集成了 PySide,同时集成了qt designer,在 Maya 的安装目录下的 bin ...

  4. Qt Roadmap for 2018(对3D有很多改进)

    When it comes to new features, we have many things ongoing related to graphics, so I’ll start with t ...

  5. A Simple OpenCASCADE Qt Demo-occQt

    A Simple OpenCASCADE Qt Demo-occQt eryar@163.com Abstract. OpenCASCADE have provided the Qt samples ...

  6. Qt 设计师手册

    Qt设计师(Qt Designer)是使用Qt部件(Widgets)设计和使用图形用户界面(GUI)的工具.它允许我们以所见即所得的方式构建和定制自己的窗口(Windows)或对话框(Dialogs) ...

  7. CG资源网 - Maya教程

    Maya中mentalray灯光渲染终极训练视频教程 http://www.cgtsj.com/cg/f/vx3627/index.html Maya无人机建模制作训练视频教程第一季 http://w ...

  8. 使用Qt 开发图形界面的软件

    3DSlicer, a free open source software for visualization and medical image computing AcetoneISO:镜像文件挂 ...

  9. Qt Examples Qt实例汇总

    ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...

随机推荐

  1. 学习笔记之ulimit

    Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数.可以用ulimit -a 来显示当前的各种用户进程限制. Linux/Unix ul ...

  2. C#数据操作LINQ

    Linq是language integrated query的缩写,即"语言集成查询"之意. Linq主要包含四个组件,Linq to object.Linq to XML.Lin ...

  3. linux 常识笔记 20160621

    Linux分四部分 Linux内核 GNU工具组建 图形化桌面环境 应用软件 Linux系统的核心是内核,内核控制着计算机系统上的所有硬件和软件:必要时分配硬件,有需要时执行软件. 内核负责四项主要功 ...

  4. Asp.Net 之 获取GridView行的DataKeys

    1.后台任意一个位置获取GridView行的主键值 foreach (GridViewRow row in GridView1.Rows) { string order_Id=this.GridVie ...

  5. 2014年6月5日 深圳 IBM 安全解决方案会议通知

    2014年6月5日 深圳 IBM 安全解决方案会议通知 http://gdtesting.com/news.php?id=191 时间: 2014年6月5日 地点: 深圳大中华喜来登 议程: IBM安 ...

  6. CSS选择器优先级

    1.类的覆盖顺序和应用的时候引用的顺序没关系,覆盖顺序取决于类定义的顺序     例如: .a{ color:red } .b{ color:green }     由于b晚于a定义,所以b覆盖a,反 ...

  7. CentOS 简单命令

    一.启动级别 Linux系统设置系统启动模式的方式可以修改(必须要以root身份登录才能修改).修改系统启动模式的配置文件是   /etc/inittab. 我们在切换到root用户后,然后 vi / ...

  8. setTimeout和setImmediate以及process.nextTick的区别

    在javascript中我们了解到了setTimeout和setInterVal函数事件队列(任务队列)的相关知识,除了setTimeout和setInterval这两个方法外,Node.js还提供了 ...

  9. LeetCode里有特色的问题存档

    002* Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  10. 【C语言】5-结构体

    一.什么是结构体 * 在实际应用中,我们通常需要由不同类型的数据来构成一个整体,比如学生这个整体可以由姓名.年龄.身高等数据构成,这些数据都具有不同的类型,姓名可以是字符串类型,年龄可以是整型,身高可 ...