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:

  1. +command="myPythonInstance.pythonCommand()"
  1.  

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.

  1. +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().

  1. import maya.cmds as cmds
  2.  
  3. class myUi(object):
  4. def __init__(self, *args):
  5. uiFile = '/path/to/uiFile.ui'
  6. rlDialog = cmds.loadUI(f=uiFile)
  7. cmds.showWindow(rlDialog)
  8. cmds.button( "ovrAdd_btn", edit=True, command=self.overrideAdd )
  9.  
  10. def overrideAdd(self, *args):
  11. 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!

  1. import maya.cmds as cmds
  2.  
  3. def widgetPath(windowName, widgetNames):
  4. """
  5.  
  6. @param windowName: Window instance name to search
  7. @param widgetNames: list of names to search for
  8. """
  9.  
  10. returnDict = {}
  11. mayaWidgetList = cmds.lsUI(dumpWidgets=True)
  12.  
  13. for widget in widgetNames:
  14. for mayaWidge in mayaWidgetList:
  15. if windowName in mayaWidge:
  16. if mayaWidge.endswith(widget):
  17. returnDict[widget] = mayaWidge
  18.  
  19. return returnDict
  20.  
  21. class myUi(object):
  22. def __init__(self, *args):
  23. uiWidgetList = ['ovrAdd_btn']
  24.  
  25. uiFile = '/path/to/uiFile.ui'
  26. rlDialog = cmds.loadUI(f=uiFile)
  27. self.uiObjects = widgetPath(rlDialog, uiWidgetList)
  28.  
  29. cmds.showWindow(rlDialog)
  30.  
  31. cmds.button( self.uiObjects["ovrAdd_btn"], edit=True, command=self.overrideAdd )
  32.  
  33. def overrideAdd(self, *args):
  34. 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. 《高级Perl编程》 读书笔记

    http://blog.chinaunix.net/uid-20767124-id-1849881.html

  2. [Effective C++ --009]确定对象被使用前已先被初始化

    在确保对象在使用前已先被初始化这一条款的编码实践中,作者为我们总结了三条经验,它们分别是: ------------------------------------------------------ ...

  3. javascript笔记02:严格模式的特定要求

    1.严格模式变量必须声明,不然会报错: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  4. 简书APP

    找第三方的时候看到简书这个APP,上网搜了一下发现网页版非常的干净,开头的一篇文章就是"你没实力就别心存侥幸",看完也挺有有同感的.文章网址:http://www.jianshu. ...

  5. 修改UILabel的行间距

    在iOS开发中  有时候为了调整一些UI效果  我们需要调整UILabel之间的行间距: contentLabel.text:label上显示的文字内容; 5:label行间距; contentLab ...

  6. 关于SQL语言分类

    从功能上划分,SQL语言可以分为DDL,DML和DCL三大类. 事务:可以作用在DML(update.insert.delete)语句上. 1. DDL(Data Definition Languag ...

  7. Nodejs v4.x.0API文档学习(1)简介

    文档参考地址:https://nodejs.org/dist/latest-v4.x/docs/api/ 简介 下面是用nodejs编写的一个web服务的例子,返回"Hello World& ...

  8. dede-搜索

    <form name="formsearch" action="{dede:global.cfg_cmsurl/}/plus/search.php"> ...

  9. 第五章 jQuery中的动画

    通过jQuery中的动画方法,能轻松地为网页添加精彩的视觉效果,给用户一种全新体验. 1.show()方法和hide()方法 该方法的功能与css()方法设置display属性效果相同. 给show( ...

  10. HashMap 与HashTable的区别

    我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Cloneable, java.io.Serializable ...