{"id":513,"date":"2020-09-08T13:20:18","date_gmt":"2020-09-08T13:20:18","guid":{"rendered":"https:\/\/half4.xyz\/?p=513"},"modified":"2024-01-24T22:27:11","modified_gmt":"2024-01-24T22:27:11","slug":"how-i-work-maya-qt-pyside","status":"publish","type":"post","link":"https:\/\/half4.xyz\/index.php\/2020\/09\/08\/how-i-work-maya-qt-pyside\/","title":{"rendered":"How I Work: Maya, Qt &#038; PySide"},"content":{"rendered":"<p class=\"has-small-font-size\"><em>[I\u2019m improving this page, WIP]<\/em><\/p>\n<p>There are plenty of ways to get up to speed with an IDE for developing Maya tools &amp; plugins. Anecdotally, I\u2019ve found not many studios enforce a particular IDE for Maya development, so this is how I\u2019ve usually got mine configured.<\/p>\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n<ul>\n<li>An IDE \u2013 I use Visual Studio, but VS Code &amp; Eclipse work fine.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Getting Started<\/h2>\n<p>Once you\u2019ve installed those tools, locate where you installed Qt to, and find this application:<\/p>\n<p>~\\Qt\\Qt5.6.1\\5.6\\mingw49_32\\bin\\designer.exe<\/p>\n<p>Put a shortcut to this on your Desktop (or wherever). You can run this to build Maya-compatible .UI files<\/p>\n<p>Next, run Maya and run this command in Python:<\/p>\n<pre class=\"wp-block-preformatted\">import maya.cmds as cmds\ncmds.commandPort(name=\":7001\", sourceType=\"mel\")\ncmds.commandPort(name=\":7002\", sourceType=\"python\")<\/pre>\n<p>You can, if you wish, add this to you userSetup.py\/userSetup.mel file in your Maya scripts path.<\/p>\n<p>I also head into VS and add in the Maya Python paths, so you get all the Maya-specific PyMEL commands. You can do this by adding Maya\u2019s python interpreters to a new environment:<\/p>\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-517\" src=\"https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-1024x598.png\" sizes=\"(max-width: 1024px) 100vw, 1024px\" srcset=\"https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-1024x598.png 1024w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-300x175.png 300w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-768x449.png 768w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-1536x897.png 1536w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-15-2048x1196.png 2048w\" alt=\"\" width=\"1024\" height=\"598\" \/><\/figure>\n<p>&nbsp;<\/p>\n<p>Open up Designer and make yourself a basic UI, like so:<\/p>\n<p><img decoding=\"async\" class=\"wp-image-515\" src=\"https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-14-1024x592.png\" sizes=\"(max-width: 1024px) 100vw, 1024px\" srcset=\"https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-14-1024x592.png 1024w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-14-300x173.png 300w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-14-768x444.png 768w, https:\/\/half4.xyz\/wp-content\/uploads\/2020\/09\/image-14.png 1421w\" alt=\"\" width=\"1024\" height=\"592\" \/><\/p>\n<p>Save this as \u201cMyCustomUiFile.ui\u201d in:<br \/>\nC:\\Users\\&lt;username&gt;\\Documents\\Maya\\&lt;maya version, i,e, \u20182019\u2019&gt;\\scripts\\<\/p>\n<p>&nbsp;<\/p>\n<p>Now, you can use the following termplate to open the UI and hook up events:<\/p>\n<pre class=\"wp-block-code\"><code>import os\nimport sys\nimport site\nfrom PySide2.QtCore import SIGNAL\ntry:\n  from PySide2.QtCore import *\n  from PySide2.QtGui import *\n  from PySide2.QtWidgets import *\n  from PySide2 import __version__\n  from shiboken2 import wrapInstance\nexcept ImportError:\n  from PySide.QtCore import *\n  from PySide.QtGui import *\n  from PySide import __version__\n  from shiboken import wrapInstance\nfrom maya import OpenMayaUI as omui\nfrom PySide2.QtUiTools import  *\nfrom pymel.all import *\nimport maya\nimport maya.cmds as cmds\nimport pymel.core as pm\nimport pymel.mayautils as mutil\n \nmayaMainWindowPtr = omui.MQtUtil.mainWindow()\nmayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)\n \nwindowId = \"MyCustomUI\"\nUSERNAME = os.getenv('USERNAME')\n \ndef loadUiWidget(uifilename, parent=None):\n    \"\"\"Properly Loads and returns UI files - by BarryPye on stackOverflow\"\"\"\n    loader = QUiLoader()\n    uifile = QFile(uifilename)\n    uifile.open(QFile.ReadOnly)\n    ui = loader.load(uifile, parent)\n    uifile.close()\n    return ui\n \nclass createMyCustomUI(QMainWindow):\n \n    uiPath = \"C:\\\\Users\\\\\" + USERNAME + \"\\\\Documents\\\\maya\\\\2019\\\\scripts\\\\MyCustomScript\\\\UI\\\\MyCustomUiFile.ui\"\n \n    def onExitCode(self):\n        print(\"DEBUG: UI Closed!\\n\")\n \n    def __init__(self):\n        print(\"Opening UI at \" + self.uiPath)\n        mainUI = self.uiPath\n        MayaMain = wrapInstance(long(omui.MQtUtil.mainWindow()), QWidget)\n        super(createMyCustomUI, self).__init__(MayaMain)\n         \n        # main window load \/ settings\n        self.MainWindowUI = loadUiWidget(mainUI, MayaMain)\n        self.MainWindowUI.setAttribute(Qt.WA_DeleteOnClose, True) \n        self.MainWindowUI.destroyed.connect(self.onExitCode)\n        self.MainWindowUI.show()\n        # You can use code like below to implement functions on the UI itself:\n        #self.MainWindowUI.buttonBox.accepted.connect(self.doOk)\n    #def doOk(self):\n        #print(\"Ok Button Pressed\")\n     \nif not (cmds.window(windowId, exists=True)):\n    createMyCustomUI()\nelse:\n    sys.stdout.write(\"tool is already open!\\n\")<\/code><\/pre>\n<p>That should at least get you going with a basic interface to work from.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[I\u2019m improving this page, WIP] There are plenty of ways to get up to speed with an IDE for developing Maya tools &amp; plugins. Anecdotally, I\u2019ve found not many studios enforce a particular IDE for Maya development, so this is how I\u2019ve usually got mine configured. Prerequisites An IDE \u2013 I use Visual Studio, but VS Code &amp; Eclipse work fine. Getting Started Once you\u2019ve installed those tools, locate where you installed Qt to, and find this application: ~\\Qt\\Qt5.6.1\\5.6\\mingw49_32\\bin\\designer.exe Put a shortcut to this on your Desktop (or wherever). You can run this to build Maya-compatible .UI files Next, run Maya and run this command in Python: import maya.cmds as cmds cmds.commandPort(name=&#8221;:7001&#8243;, sourceType=&#8221;mel&#8221;) cmds.commandPort(name=&#8221;:7002&#8243;, sourceType=&#8221;python&#8221;) You can, if you wish, add this to you userSetup.py\/userSetup.mel file in your Maya scripts path. I also head into VS and add in the Maya Python paths, so you get all the Maya-specific PyMEL commands. You can do this by adding Maya\u2019s python interpreters to a new environment: &nbsp; Open up Designer and make yourself a basic UI, like so: Save this as \u201cMyCustomUiFile.ui\u201d in: C:\\Users\\&lt;username&gt;\\Documents\\Maya\\&lt;maya version, i,e, \u20182019\u2019&gt;\\scripts\\ &nbsp; Now, you can use the following termplate to open the UI and hook up events: import os import sys import site from PySide2.QtCore import SIGNAL try: from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * from PySide2 import __version__ from shiboken2 import wrapInstance except ImportError: from PySide.QtCore import * from PySide.QtGui import * from PySide import __version__ from shiboken import wrapInstance from maya import OpenMayaUI as omui from PySide2.QtUiTools import * from pymel.all import * import maya import maya.cmds as cmds import pymel.core as pm import pymel.mayautils as mutil mayaMainWindowPtr = omui.MQtUtil.mainWindow() mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget) windowId = &#8220;MyCustomUI&#8221; USERNAME = os.getenv(&#8216;USERNAME&#8217;) def loadUiWidget(uifilename, parent=None): &#8220;&#8221;&#8221;Properly Loads and returns UI files &#8211; by BarryPye on stackOverflow&#8221;&#8221;&#8221; loader = QUiLoader() uifile = QFile(uifilename) uifile.open(QFile.ReadOnly) ui = loader.load(uifile, parent) uifile.close() return ui class createMyCustomUI(QMainWindow): uiPath = &#8220;C:\\\\Users\\\\&#8221; + USERNAME + &#8220;\\\\Documents\\\\maya\\\\2019\\\\scripts\\\\MyCustomScript\\\\UI\\\\MyCustomUiFile.ui&#8221; def onExitCode(self): print(&#8220;DEBUG: UI Closed!\\n&#8221;) def __init__(self): print(&#8220;Opening UI at &#8221; + self.uiPath) mainUI = self.uiPath MayaMain = wrapInstance(long(omui.MQtUtil.mainWindow()), QWidget) super(createMyCustomUI, self).__init__(MayaMain) # main window load \/ settings self.MainWindowUI = loadUiWidget(mainUI, MayaMain) self.MainWindowUI.setAttribute(Qt.WA_DeleteOnClose, True) self.MainWindowUI.destroyed.connect(self.onExitCode) self.MainWindowUI.show() # You can use code like below to implement functions on the UI itself: #self.MainWindowUI.buttonBox.accepted.connect(self.doOk) #def doOk(self): #print(&#8220;Ok Button Pressed&#8221;) if not (cmds.window(windowId, exists=True)): createMyCustomUI() else: sys.stdout.write(&#8220;tool is already open!\\n&#8221;) That should at least get you going with a basic interface to work from.<\/p>\n","protected":false},"author":1,"featured_media":517,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,6],"tags":[],"class_list":["post-513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-quick-tips","category-tuts"],"_links":{"self":[{"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/posts\/513"}],"collection":[{"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":12,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":1248,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/posts\/513\/revisions\/1248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/media\/517"}],"wp:attachment":[{"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/half4.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}