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 ...
随机推荐
- VS2005工程迁移到Eclipse CDT
原工程在VS2005下创建,后迁移到Eclipse CDT 3.3.2 + MingGW下,并增加makefile文件. 原VS2005下工程Sample,实现了对类SampleClass封装,生成S ...
- Ext.Net 问题收集
Ext.Net DateField只显示年月 <ext:DateField runat="server" Format="yyyy-MM"> < ...
- mysql控制流程函数
1.case语句 select case 2 when 1 then '男' when 2 then '女' else 'xoap' end as result; 2.if语句 select if(1 ...
- linux 命令学习日记 20160621
1.cat /proc/meminfo 观察Linux系统上虚拟内存的当前状态 2.ipcs -m ipcs命令专门用来查看系统上的当前共享内存页面 3.ls /dev -al sda* ttyS* ...
- 使用RecyclerView实现瀑布流的效果
主函数: public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; privat ...
- Android_listView
package com.example.app5; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...
- percona-toolkit工具检查MySQL复制一致性及修复
利用percona-toolkit工具检查MySQL数据库主从复制数据的一致性,以及修复. 一. pt-table-checksum检查主从库数据的一致性 pt-table-c ...
- java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...
- java和html的区别
Java 不是一门程序语言,它是一个平台,也可以说是一门技术. Java 包括 1.Java 程式语言 一个类似 C++ 或 Smalltalk 的物件导向程式语言.学习 Java 程式语言类似学人类 ...
- scala学习笔记:理解并行集合par
scala> (1 to 5).foreach(println(_)) 1 2 3 4 5 scala> (1 to 5).par.foreach(println(_)) 3 1 4 2 ...