Speaking Between Definitions

Speaking Between Definitions

Im having trouble getting two definitions to speak to each other using a UI. I would like to create a bunch of locators place them in the scene and name them. Then in a different definition create some joints and use the locators name and position to position the joints and name them according to the locators I previously created.

My problem is the variable named rootJnt is not being recognized in the joint creation definition. All others are except rootJnt. I don't understand why, all the other variables are being recognized. Can some one please explain?

from maya import cmds

def proxySkel (self):
    target = cmds.ls (selection = True)
    DDD = cmds.textField (textNam,
                          query = True,
                          text = True)
                          
    # CREATE THE LOCATOR 
    _endOutLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locEndOut')
    _startOutLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locStartOut')
    _rootLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locRoot')                          

    # MOVE END LOCATOR IN SPACE
    cmds.move (5, 0, 3, _endOutLoc, absolute = True)
    cmds.move (0, 0, 3, _startOutLoc, absolute = True)

    return _endOutLoc, _startOutLoc, _rootLoc    


def jointsCreate (self):
    target = cmds.ls (selection = True)
    DDD = cmds.textField (textNam,
                          query = True,
                          text = True)
    
    # CREATE THE JOINTS 
    rootJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _rootLoc + '_jnt')
    endOutJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _endOutLoc[0] + '_jnt')
    startOutJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _startOutLoc[0] + '_jnt')
    
    # PLACE THE JOINTS INTO SPACE
    rootConst = cmds.parentConstraint (_rootLoc, rootJnt, mo = False)
    endOutConst = cmds.parentConstraint (_endOutLoc, endOutJnt, mo = False)
    startInConst = cmds.parentConstraint (_startInLoc, startInJnt, mo = False)
    
    # CREATE THE JOINT HIERARCHY
    cmds.parent (startInJnt, rootJnt)
    cmds.parent (startInJnt, startOutJnt)




if(cmds.window('window1',q=1,ex=1)):
    cmds.deleteUI('window1')
    cmds.window('window1',menuBar=1)

cmds.columnLayout()

textNam = cmds.textField(width=150,
               text='Name',
               backgroundColor=[0.6233005264362554, 1.0, 0.9765011062790875])

cmds.separator()

cmds.rowLayout(numberOfColumns=3)

cmds.button(width=65,
            command = proxySkel,
            backgroundColor=[0.0, 0.2187533379110399, 1.0],
            label='Locators')
            
cmds.separator(style='none',
               width=3)
               
cmds.button(width=78,
            command = jointsCreate,
            backgroundColor=[0.7971007858396277, 0.0, 0.0],
            label='Joints')
            
cmds.showWindow('window1')

4

1 Answer

To have access to your variables between two functions, you have to define them in an outer scope. In your case _endOutLoc, _startOutLoc, _rootLoc are defined locally to ProxySkel function and they are not visible to jointsCreate function In additional to that, there are several other mistakes

  • as it was mentioned in the comments to your question, _rootLoc have to be passed with index 0, because it stores value as a list, not as a string
  • if you are using parent constraint for the alignment, I would guess you want to delete them once the alignment is finished
  • there is no need to call parent command for the joints, as they are already parented in the order of cmds.joint calls. Here is a note from documentation "In the creation mode, a new joint will be created as a child of a selected transform or starts a hierarchy by itself if no transform is selected." if you don't want such behaviour, you should clear the selection
  • cmds.window('window1',menuBar=1) have to be always called, not inside the existance window condition, otherwise the UI elements will be attached to the script editor window

here is an updated script

from maya import cmds

global _endOutLoc
global _startOutLoc
global _rootLoc

def proxySkel (self):
    global _endOutLoc, _startOutLoc, _rootLoc
    
    target = cmds.ls (selection = True)
    DDD = cmds.textField (textNam,
                          query = True,
                          text = True)
                          
    # CREATE THE LOCATOR 
    _endOutLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locEndOut')
    _startOutLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locStartOut')
    _rootLoc = cmds.spaceLocator (position = (0, 0, 0), name = DDD + '_locRoot')                          

    # MOVE END LOCATOR IN SPACE
    cmds.move (5, 0, 3, _endOutLoc, absolute = True)
    cmds.move (0, 0, 3, _startOutLoc, absolute = True)

    return _endOutLoc, _startOutLoc, _rootLoc    


def jointsCreate (self):
    global _endOutLoc, _startOutLoc, _rootLoc
    
    if None in [_endOutLoc, _startOutLoc, _rootLoc]:
        print('locators are not created yet')
        return
    
    target = cmds.ls (selection = True)
    DDD = cmds.textField (textNam,
                          query = True,
                          text = True)
    
    # CREATE THE JOINTS 
    rootJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _rootLoc[0] + '_jnt')
    endOutJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _endOutLoc[0] + '_jnt')
    startOutJnt = cmds.joint (position = (0.0, 0.0, 0.0), name = _startOutLoc[0] + '_jnt')
    
    # PLACE THE JOINTS INTO SPACE
    rootConst = cmds.parentConstraint (_rootLoc, rootJnt, mo = False)
    endOutConst = cmds.parentConstraint (_endOutLoc, endOutJnt, mo = False)
    startInConst = cmds.parentConstraint (_startOutLoc, startOutJnt, mo = False)
    
    # DELETE CONSTRAINTS AFTER ALIGNMENT
    cmds.delete (rootConst, endOutConst, startInConst)
    
    # CREATE THE JOINT HIERARCHY
    #cmds.parent (endOutJnt, rootJnt)
    #cmds.parent (startOutJnt, endOutJnt)




if(cmds.window('window1',q=1,ex=1)):
    cmds.deleteUI('window1')
    
cmds.window('window1',menuBar=1)

cmds.columnLayout()

textNam = cmds.textField(width=150,
               text='Name',
               backgroundColor=[0.6233005264362554, 1.0, 0.9765011062790875])

cmds.separator()

cmds.rowLayout(numberOfColumns=3)

cmds.button(width=65,
            command = proxySkel,
            backgroundColor=[0.0, 0.2187533379110399, 1.0],
            label='Locators')
            
cmds.separator(style='none',
               width=3)
               
cmds.button(width=78,
            command = jointsCreate,
            backgroundColor=[0.7971007858396277, 0.0, 0.0],
            label='Joints')
            
cmds.showWindow('window1')
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.