Search code examples
pythonpluginsmaya

show window on menuItem clicked Python Maya


I'am new to python scripting using Maya I want to autoload a plugin that I created to display a custom menu in Maya main Window. When clicking the menu item, it shows a window using a function that I create. So the problem: When I run my code simply in Maya interface, it runs correctly but when I put my code in a python file and autoload it, it shows each time a different error : Sometimes it shows this error on clicking the menu item the first time it's loaded when opening Maya:

# Error: NameError: file <maya console> line 1: name 'createUI' is not defined #

And sometimes it shows this error:

// Warning: file: C:/Program Files/Autodesk/MAYA20185/Maya2018/scripts/others/pluginWin.mel line 290: No initializePlugin() or initializePlugin2() function
 // 
// Error: file: C:/Program Files/Autodesk/MAYA20185/Maya2018/scripts/others/pluginWin.mel line 290:  (userInterface) //

This is my code: userInterface.py

import maya.cmds as cmds
import functools
import pymel.core as pm

def createUI(pWindowTitle):
    windowID = 'myWindowID'
    if cmds.window(windowID, exists=True):
        cmds.deleteUI(windowID)
    cmds.window(windowID, title=pWindowTitle, sizeable=False, resizeToFitChildren=True)
    cmds.rowColumnLayout(numberOfColumns=3, columnWidth=[(1,75), (2,60), (3,60) ], columnOffset=[(1,'right',3)])
    cmds.text(label='Time Range:')
    startTimeField = cmds.intField(value=cmds.playbackOptions(q=True, minTime=True))
    endTimeField = cmds.intField(value=cmds.playbackOptions(q=True, maxTime=True))
    cmds.text( label='Attribute:' )
    targetAttributeField = cmds.textField( text='rotateY' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    def cancelCallback(*pArgs):
        if cmds.window(windowID, exists=True ):
            cmds.deleteUI(windowID)
    cmds.button(label='Cancel',command=cancelCallback)
    cmds.showWindow()

def keyFullRotation(pObjectName, pStartTime, pEndTime, pTargetAttribute):
    cmds.cutKey( pObjectName, time=(pStartTime, pEndTime), attribute=pTargetAttribute)
    cmds.setKeyframe( pObjectName, time=pStartTime, attribute=pTargetAttribute, value=0)
    cmds.setKeyframe( pObjectName, time=pEndTime, attribute=pTargetAttribute, value=360)    
    cmds.selectKey( pObjectName, time=(pStartTime, pEndTime), attribute=pTargetAttribute, keyframe=True)    
    cmds.keyTangent( inTangentType='linear', outTangentType='linear' )

MainMayaWindow = pm.language.melGlobals['gMainWindow'] 
customMenu = pm.menu('TestMenu', parent=MainMayaWindow)
pm.menuItem(label="menu item 'hihi'", command="createUI('My Title')", parent=customMenu)

Solution

  • First never write your command as a string, I suppose you have a problem of namespacing. If you really want to parse so variable, please, try to use partial. If you look at my posts, Im giving tones of advice about it.

    So try to change your lines :

    def createUI():
        pWindowTitle = 'somethingFix'
        windowID = 'myWindowID'
        if cmds.window(windowID, exists=True):
            cmds.deleteUI(windowID)
        cmds.window(windowID, title=pWindowTitle, sizeable=False, resizeToFitChildren=True)
    
    
    
    pm.menuItem(label="menu item 'hihi'", command=createUI, parent=customMenu)