Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a new Graph Editor in Maya using Python?

Tags:

python

maya

I want to add the part of the graph editor, that shows the curves and the names of the curves, to a menu I've made. Does anyone have any suggestions to how I could do this?

UPDATE

What I want added

I'm trying to get something similar to the image above. Adding on a graph and outliner. I've managed to find the code for the outliner (cmds.outlinerPanel), but i'm not so sure about the graph :\

I'm using maya 2014.

like image 733
Adam Sirrelle Avatar asked Aug 27 '26 07:08

Adam Sirrelle


1 Answers

Ok. Super quick example for creating a graph editor panel using Maya cmds:

import maya.cmds as cmds

cmds.window()
cmds.frameLayout()
cmds.animCurveEditor()
cmds.showWindow()

enter image description here

But, for future, if you want to add any Maya widget that isn't available as a Maya command, you could use PySide's power to do that. Here is an example. It is code that I slightly modified from Nathan Horne's awesome post on embedding Maya UI widgets on 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 is a seemingly long piece of code, you can safely add wrapinstance(), getMayaWindow() and toQtObject() to your utilities module for reuse.

Hope that was useful.

like image 88
kartikg3 Avatar answered Aug 29 '26 21:08

kartikg3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!