Maya QT interfaces in a class
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的更多相关文章
- 使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization)
使用 PySide2 开发 Maya 插件系列三:qt语言国际化(internationalization) 前言: 这是 qt for python 的语言国际化,基于 UI 的,python 也有 ...
- 使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件
使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件 前期准备: 安装 python:https://www ...
- 无法启动 Maya 集成的 qt designer 的解决方法和原因 以及 中英文切换
无法启动 Maya 集成的 qt designer 的解决方法和原因 以及 中英文切换 前言: Maya 集成了 PySide,同时集成了qt designer,在 Maya 的安装目录下的 bin ...
- 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 ...
- A Simple OpenCASCADE Qt Demo-occQt
A Simple OpenCASCADE Qt Demo-occQt eryar@163.com Abstract. OpenCASCADE have provided the Qt samples ...
- Qt 设计师手册
Qt设计师(Qt Designer)是使用Qt部件(Widgets)设计和使用图形用户界面(GUI)的工具.它允许我们以所见即所得的方式构建和定制自己的窗口(Windows)或对话框(Dialogs) ...
- CG资源网 - Maya教程
Maya中mentalray灯光渲染终极训练视频教程 http://www.cgtsj.com/cg/f/vx3627/index.html Maya无人机建模制作训练视频教程第一季 http://w ...
- 使用Qt 开发图形界面的软件
3DSlicer, a free open source software for visualization and medical image computing AcetoneISO:镜像文件挂 ...
- Qt Examples Qt实例汇总
ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...
随机推荐
- Linux下文件描述符
http://blog.csdn.net/kumu_linux/article/details/7877770 文件描述符是一个简单的整数,用以标明每一个被进程所打开的文件和socket.第一个打开的 ...
- python内存管理
python对象三要素: identity(值):对应于内存的地址,不可修改 type(类型):不可修改 value(值): mutable :可以修改 immutable:不可以修改 引用计数 当引 ...
- javascript中的一些偏门知识
undefined能够被重写 undefined = "now it's defined"; alert( undefined ); 浏览器测试结果: 浏览器 测试结果 结论 ie ...
- sql根据表名获取字段及对应说明
SELECT TableName = OBJECT_NAME(c.object_id), ColumnsName = c.name, Description = ex.value, ColumnTyp ...
- Supervised Learning-Regression
假设我们有一张房子属性及其价格之间的关系表(如下图所示) ,根据这些数据如何估计其他房子的价格?我们的第一个反应肯定是参考属性相似的房子的价格.在属性较少时这个方法还行得通,属性太复杂时就不那么简单了 ...
- GWT 中日期格式化 ,处置Date
GWT的view中不能用java原生的DateFormat 必须使用gwt封装的格式化方法,方法如下 import com.google.gwt.i18n.client.DateTimeFormat; ...
- 0x800a1391-Microsoft Jscript "JSON未定义"
本人在进行调试代码是遇到以下问题: 在运行到var result = JSON.parse(data);这句时,报错:JSON未定义.如下图:
- CentOS7 发布 ASP.NET MVC 4 --- mono 4.6.0 + jexus 5.8.1
yum -y install gcc gcc-c++ yum -y install bison pkgconfig glib2-devel gettext make libpng-devel libj ...
- Matlab中plot、fplot、ezplot的使用方法和区别
函数plot 是绘制二维图形的最基本函数,它是针对向量或矩阵的列来绘制曲线的.也就是说,使用plot 函数之前,必须首先定义好曲线上每一点的x 及y 坐标; 常用格式为: (1)plot(x) 当x ...
- SQL Server 2012 内存管理 (memory management) 改进
SQL Server 2012 的内存管理和以前的版本相比,有以下的一些变化. 一.内存分配器的变化 SQL Server 2012以前的版本,比如SQL Server 2008 R2等, 有sing ...