How do I create a new graph editor in Maya using Python?
I want to add a graph editor part that shows curves and curve names to the menu I made. Does anyone have any suggestions how I can do this?
UPDATE
I am trying to get something similar to the image above. Adding to the chart and outliner. I managed to find the code for the outliner (cmds.outlinerPanel), but I'm not sure about this graph: \
I am using maya 2014.
source to share
OK. Super quick example of creating a graph editor panel using may cmds:
import maya.cmds as cmds
cmds.window()
cmds.frameLayout()
cmds.animCurveEditor()
cmds.showWindow()
But for the future, if you want to add any Maya widget that is not available as a Maya command, you can use the power of PySide to do so. Here's an example. This is the code I slightly modified from Nathan Horne's excellent post on embedding the Maya UI widget in Qt UI and PySide / Shiboken :
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
from PySide import QtCore, QtGui
import shiboken
def wrapinstance(ptr, base=None):
"""
Utility to convert a pointer to a Qt class instance (PySide/PyQt compatible)
:param ptr: Pointer to QObject in memory
:type ptr: long or Swig instance
:param base: (Optional) Base class to wrap with (Defaults to QObject, which should handle anything)
:type base: QtGui.QWidget
:return: QWidget or subclass instance
:rtype: QtGui.QWidget
"""
if not globals().has_key('QtCore') or not globals().has_key('QtGui'):
return None
if ptr is None:
return None
ptr = long(ptr) # Ensure type
if globals().has_key('shiboken'):
if base is None:
qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject)
metaObj = qObj.metaObject()
cls = metaObj.className()
superCls = metaObj.superClass().className()
if hasattr(QtGui, cls):
base = getattr(QtGui, cls)
elif hasattr(QtGui, superCls):
base = getattr(QtGui, superCls)
else:
base = QtGui.QWidget
return shiboken.wrapInstance(long(ptr), base)
elif globals().has_key('sip'):
base = QtCore.QObject
return sip.wrapinstance(long(ptr), base)
else:
return None
def getMayaWindow():
ptr = apiUI.MQtUtil.mainWindow()
return wrapinstance(long(ptr), QtCore.QObject)
def toQtObject(mayaName):
'''
Given the name of a Maya UI element of any type,
return the corresponding QWidget or QAction.
If the object does not exist, returns None
'''
ptr = apiUI.MQtUtil.findControl(mayaName)
if ptr is None:
ptr = apiUI.MQtUtil.findLayout(mayaName)
if ptr is None:
ptr = apiUI.MQtUtil.findMenuItem(mayaName)
if ptr is not None:
return wrapinstance(long(ptr), QtCore.QObject)
class MayaSubWindow(QtGui.QMainWindow):
def __init__(self, parent=getMayaWindow()):
super(MayaSubWindow, self).__init__(parent)
qtObj = toQtObject(cmds.animCurveEditor())
#Fill the window, could use qtObj.setParent
#and then add it to a layout.
self.setCentralWidget(qtObj)
myWindow = MayaSubWindow()
myWindow.show()
Even though this appears to be a long piece of code, you can safely add wrapinstance()
, getMayaWindow()
and toQtObject()
to your module, utilities for reuse.
Hope this was helpful.
source to share