0

I should first let you know that im trying to re-create the aimconstraint UI from maya. this is the first time ive used python so im just doing a small project to wrap my head around it.

the problem children are the maintain offset bool and the world up vector enum. i cant figure out how to call on their results to drive the variables of the aim constraint.

Ok. so im having some trouble getting my if/else statements working. every time i try and use them (Maintain Offset Row, World Up Type Row) i get "invalid syntax" or "unexpected indent" or some other error that i never get in unity while using Csharp. on top of that, i cant work out how to call one definition through another.

(ive cut out a lot of the less important items for your ease of viewing.)

#AimConstrain.py

import maya.cmds as cmds
import functools

#main
maintainOffsetBool = False
OffsetX = 0.0
OffsetY = 0.0
OffsetZ = 0.0
aimVectorX = 1.0
aimVectorY = 0.0
aimVectorZ = 0.0
upVectorX = 0.0
upVectorY = 1.0
upVectorZ = 0.0
worldUpTypeField = ''
worldUpVectorX = 0.0
worldUpVectorY = 1.0
worldUpVectorZ = 0.0
weightFloat = 1.0 

def createUI( pWindowTitle, pApplyCallback ):

    windowID = 'CustomAimConstraint'

    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

    cmds.window( windowID, title=pWindowTitle, sizeable=False, resizeToFitChildren=True )

    cmds.rowColumnLayout( numberOfColumns=6, columnWidth=[ (1,60), (2,90), (3,75), (4,75), (5,75), (6,60) ], columnOffset=[ (1, 'right',3) ] )


    # Maintain Offset Row
    cmds.separator( h=10, style='none' )

    cmds.text( label='Maintain Offset: ')

    maintainOffsetCB = cmds.checkBox( value = False, label='' ):
    if(maintainOffsetCB):
        maintainOffsetBool = True
    else():
        maintainOffsetBool = False

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # The XYZ of the Offset
    cmds.separator( h=10, style='none' )

    cmds.text( label='Offset:' )

    OffsetX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    OffsetY = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    OffsetZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # The XYZ of the Aim Vector
    cmds.separator( h=10, style='none' )

    cmds.text( label='Aim Vector:' )

    aimVectorX = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    aimVectorY = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    aimVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # The XYZ of the Up Vector
    cmds.separator( h=10, style='none' )

    cmds.text( label='Up Vector:' )

    upVectorX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    upVectorY = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    upVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # World Up Type Row
    cmds.separator( h=10, style='none' )

    cmds.text( label='World Up Type:' )

    cmds.optionMenu("worldUpTypeMenu", width=2 )
    cmds.menuItem( label = 'Vector' )
    cmds.menuItem( label = 'World' )
    cmds.menuItem( label = 'None' )

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # World Up XYZ
    cmds.separator( h=10, style='none' )

    cmds.text( label='World Up Vector:' )

    worldUpVectorX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    worldUpVectorY = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    worldUpVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # Weight Setting
    cmds.separator( h=10, style='none' )

    cmds.text( label='Weight: ' )

    weightFloat = cmds.floatField( value=1 )

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # Bottom Row / Buttons and shit
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    cmds.button( label='Add', command=addCallBack )

    cmds.button( label='Apply', command=applyCallBack )

    cmds.button( label='Cancel', command=cancelCallBack )

    cmds.showWindow()

# this is for the enum, go to World Up Type row.
def worldUpTypeDef():
    currentValue = cmds.optionMenu("worldUpTypeMenu", query=True, value=True):
    if currentValue == 'Vector':
        worldUpTypeField = 'Vector'
    elif currentValue == 'World':
        worldUpTypeField = 'World'
    elif currentValue == 'None':
        worldUpTypeField = 'None'

def cancelCallBack():
    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

def applyCallBack(applyConstraint, worldUpTypeDef):


def addCallBack(applyConstraint, worldUpTypeDef):
    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

createUI( 'Custom Aim Constraint', applyCallBack )

# Defines the Aim Constraint itself.
def applyConstraint():
    selectionList = cmds.ls( orderedSelection=True )

    if len( selectionList ) >= 2:

        print 'Selected items: %s' % ( selectionList )

        targetName = selectionList[0]

        selectionList.remove( targetName )

        for objectName in selectionList:

            print 'Constraining %s towards %s' % ( objectName, targetName )

            cmds.aimConstraint( targetName, objectName, aimVector = [aimVectorX, aimVectorY, aimVectorZ], maintainOffset = maintainOffestBool, offset = [OffsetX, OffsetY, OffsetZ], upVector = [upVectorX, upVectorY, upVectorZ], weight = weightFloat, worldUpType = worldUpTypeField, worldUpVector = [worldUpVectorX, worldUpVectorY, worldUpVectorZ] )
                #aimConstraint( [target...] object , [aimVector=[float, float, float]], [maintainOffset=boolean], [name=string], [offset=[float, float, float]], [remove=boolean], [skip=string], [targetList=boolean], [upVector=[float, float, float]], [weight=float], [weightAliasList=boolean], [worldUpObject=name], [worldUpType=string], [worldUpVector=[float, float, float]]) 

    else:

        print 'Please select two or more objects.'

Can anyone help me fix this mess of code?

Calavid
  • 13
  • 2

2 Answers2

1

The new error is well explained :

# Error: addCallBack() takes exactly 2 arguments (1 given) # 

You defined :

def addCallBack(applyConstraint, worldUpTypeDef):

So you have to use partial to feed your def here :

line 131 : cmds.button( label='Add', command=addCallBack )

[...etc]

cmds.button( label='Add', command=partial(addCallBack, "First Arg", "Scnd Arg") )

[...etc]

def addCallBack(applyConstraint, worldUpTypeDef, *args):

Note that maya ui return with command flag a Value of True in addition of yours, that why we add "*args" to the def

--- EDIT ---

for more explanation about partial : Maya Python - Using data from UI

Note that i wrote : "First Arg" but it can be any type of data : string, list, value, argument, dictionnary, def, class...etc

Community
  • 1
  • 1
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • Thank you so much for your help :D i dont know why, but im really struggling with python... which is weird because everyones saying that its easy. oh well, ill get there. Thank you again for your time. – Calavid Feb 06 '17 at 23:58
  • i marked the first answer as correct because it answers the title, but you are just as appreciated :) – Calavid Feb 07 '17 at 00:12
  • 1
    check the [stackoverflow docs](http://stackoverflow.com/documentation/maya/7627/creating-maya-ui) for some more on hooking things to UI in maya python. – theodox Feb 07 '17 at 00:22
  • Python in maya is not really easy because it is mel/c++ driving by python. – DrWeeny Feb 07 '17 at 10:43
0

I've fixed all your syntax errors, but I don't have maya module installed and I have no idea of what your code does so I cannot tell you if anything is wrong. Do let me know if there is any issue

#AimConstrain.py

import maya.cmds as cmds
import functools

#main
maintainOffsetBool = False
OffsetX = 0.0
OffsetY = 0.0
OffsetZ = 0.0
aimVectorX = 1.0
aimVectorY = 0.0
aimVectorZ = 0.0
upVectorX = 0.0
upVectorY = 1.0
upVectorZ = 0.0
worldUpTypeField = ''
worldUpVectorX = 0.0
worldUpVectorY = 1.0
worldUpVectorZ = 0.0
weightFloat = 1.0 

def createUI( pWindowTitle, pApplyCallback ):

    windowID = 'CustomAimConstraint'

    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

    cmds.window( windowID, title=pWindowTitle, sizeable=False, resizeToFitChildren=True )

    cmds.rowColumnLayout( numberOfColumns=6, columnWidth=[ (1,60), (2,90), (3,75), (4,75), (5,75), (6,60) ], columnOffset=[ (1, 'right',3) ] )


    # Maintain Offset Row
    cmds.separator( h=10, style='none' )

    cmds.text( label='Maintain Offset: ')

    maintainOffsetCB = cmds.checkBox( value = False, label='' )
    if(maintainOffsetCB):
        maintainOffsetBool = True
    else:
        maintainOffsetBool = False

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # The XYZ of the Offset
    cmds.separator( h=10, style='none' )

    cmds.text( label='Offset:' )

    OffsetX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    OffsetY = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    OffsetZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # The XYZ of the Aim Vector
    cmds.separator( h=10, style='none' )

    cmds.text( label='Aim Vector:' )

    aimVectorX = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    aimVectorY = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    aimVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # The XYZ of the Up Vector
    cmds.separator( h=10, style='none' )

    cmds.text( label='Up Vector:' )

    upVectorX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    upVectorY = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    upVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # World Up Type Row
    cmds.separator( h=10, style='none' )

    cmds.text( label='World Up Type:' )

    cmds.optionMenu("worldUpTypeMenu", width=2 )
    cmds.menuItem( label = 'Vector' )
    cmds.menuItem( label = 'World' )
    cmds.menuItem( label = 'None' )

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # World Up XYZ
    cmds.separator( h=10, style='none' )

    cmds.text( label='World Up Vector:' )

    worldUpVectorX = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    worldUpVectorY = cmds.floatField( value=1, maxValue=1.0, minValue=0.0 )

    worldUpVectorZ = cmds.floatField( value=0, maxValue=1.0, minValue=0.0 )

    cmds.separator( h=10, style='none' )

    # Weight Setting
    cmds.separator( h=10, style='none' )

    cmds.text( label='Weight: ' )

    weightFloat = cmds.floatField( value=1 )

    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    # Bottom Row / Buttons and shit
    cmds.separator( h=10, style='none' )
    cmds.separator( h=10, style='none' )

    cmds.button( label='Add', command=addCallBack )

    cmds.button( label='Apply', command=applyCallBack )

    cmds.button( label='Cancel', command=cancelCallBack )

    cmds.showWindow()

# this is for the enum, go to World Up Type row.
def worldUpTypeDef():
    currentValue = cmds.optionMenu("worldUpTypeMenu", query=True, value=True)
    if currentValue == 'Vector':
        worldUpTypeField = 'Vector'
    elif currentValue == 'World':
        worldUpTypeField = 'World'
    elif currentValue == 'None':
        worldUpTypeField = 'None'

def cancelCallBack():
    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

def applyCallBack(applyConstraint, worldUpTypeDef):
    print()


def addCallBack(applyConstraint, worldUpTypeDef):
    if cmds.window( windowID, exists=True ):
        cmds.deleteUI( windowID )

createUI( 'Custom Aim Constraint', applyCallBack )

# Defines the Aim Constraint itself.
def applyConstraint():
    selectionList = cmds.ls( orderedSelection=True )

    if len( selectionList ) >= 2:

        print ('Selected items: %s' % ( selectionList ))

        targetName = selectionList[0]

        selectionList.remove( targetName )

        for objectName in selectionList:

            print ('Constraining %s towards %s' % ( objectName, targetName ))

            cmds.aimConstraint( targetName, objectName, aimVector = [aimVectorX, aimVectorY, aimVectorZ], maintainOffset = maintainOffestBool, offset = [OffsetX, OffsetY, OffsetZ], upVector = [upVectorX, upVectorY, upVectorZ], weight = weightFloat, worldUpType = worldUpTypeField, worldUpVector = [worldUpVectorX, worldUpVectorY, worldUpVectorZ] )
                #aimConstraint( [target...] object , [aimVector=[float, float, float]], [maintainOffset=boolean], [name=string], [offset=[float, float, float]], [remove=boolean], [skip=string], [targetList=boolean], [upVector=[float, float, float]], [weight=float], [weightAliasList=boolean], [worldUpObject=name], [worldUpType=string], [worldUpVector=[float, float, float]]) 

    else:

        print ('Please select two or more objects.')
Dinesh.hmn
  • 713
  • 7
  • 21