0

我应该首先让您知道我正在尝试从 Maya 重新创建目标约束 UI。这是我第一次使用 python,所以我只是做了一个小项目来围绕它。

问题儿童是保持偏移布尔和世界向上向量枚举。我不知道如何调用他们的结果来驱动目标约束的变量。

行。所以我在让我的 if/else 语句正常工作时遇到了一些麻烦。每次我尝试使用它们(维护偏移行,世界向上类型行)时,我都会收到“无效的语法”或“意外的缩进”或其他一些我在使用 Csharp 时从未统一的错误。最重要的是,我无法弄清楚如何通过另一个定义来调用一个定义。

(为了方便查看,我删掉了很多不太重要的项目。)

#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.'

谁能帮我解决这个乱七八糟的代码?

4

2 回答 2

1

新错误得到了很好的解释:

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

你定义了:

def addCallBack(applyConstraint, worldUpTypeDef):

所以你必须在这里使用 partial 来提供你的 def :

第 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):

请注意,maya ui 除了您的命令标志之外还返回 True 值,这就是为什么我们将“*args”添加到 def

- - 编辑 - -

有关部分的更多解释:Maya Python - 使用 UI 中的数据

请注意,我写的是:“First Arg”,但它可以是任何类型的数据:字符串、列表、值、参数、字典、def、类...等

于 2017-02-06T10:33:37.197 回答
0

我已经修复了你所有的语法错误,但我没有安装 maya 模块,我不知道你的代码做了什么,所以我无法告诉你是否有任何问题。如果有任何问题,请告诉我

#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.')
于 2017-02-05T23:59:05.270 回答