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. Linux-PAM(Linux下的密碼認證和安全机制)系統管理員指南(中文版)

    he Linux-PAM 系统管理员指南作者:Andrew G. Morgan, morgan@linux.kernel.org翻译:孙国清(Thomas Sun),thomassun@yeah.ne ...

  2. Android模拟器avd的创建、使用和调试相关命令

    1.创建AVD命令:android-sdk-windows\tools目录下面有android命令  android list 列出当前系统中已安装的Android版本以及已安装的AVD(Androi ...

  3. php测试代码工具类

      <?php error_reporting (E_ALL); ini_set ('display_errors', 'on'); ?> <meta http-equiv=&quo ...

  4. Middleware

    Middleware The middleware gives a single shot to the views associated into Controllers, before execu ...

  5. gdb显示内存命令用法简介

    http://blog.csdn.net/allenlinrui/article/details/5964046

  6. [Doc ID 1666646.1]如何使用功能管理员清除缓存?

    文档内容   目标   解决方案 适用于: Oracle iProcurement - 版本 11.5.1 到 12.0.0 [发行版 11.5 到 12] 本文档所含信息适用于所有平台 目标 怎样通 ...

  7. [转]Creating Unit Tests for ASP.NET MVC Applications (C#)

    本文转自:http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mv ...

  8. Rshare Pro是否可以放入至客户企业App Store?

    现在很多客户内部部署了苹果授权的企业内部的AppStore,我们的Rshare Pro 是完全允许放入企业搭建的AppStore平台中.但每份需要收费20美元,换成人民币是120元.

  9. DEDECMS 获取当前栏目及所有子栏目的文章数量

    因DEDEV5起,加强了对SQL注入和安全的检查,导致无法查询一些正常的子查询的SQL. 以下代码用来解决查询当前栏目及当前栏目下所有子栏目的文章总数,添加到/include/common.func. ...

  10. 点击 a 标签触发事件而不跳转页面

    有时候需要让 a 标签像 button 一样,被点击的时候触发事件而不跳转页面. <html> <body> <a id="a1" href=&quo ...