Maya Python无效语法#Line 1

时间:2015-10-20 18:34:18

标签: python

我在Autodesk Maya中使用Python。我有这个脚本,我得到这个错误

错误:第1行:语法#

无效

脚本

from maya import cmds , OpenMaya
sel = cmds.ls(sl = 1 )
crv = "curveShape1"
for s in sel :
    pos = cmds.xform(s ,q = 1 , ws = 1 , t = 1)
    u = getUParam(pos , crv)
    name = s.replace("_LOC" , "_PCI")
    pci = cmds.createNode(""pointOnCurveInfo" , n = name )
    cmds.connectAttr(crv + '.worldSpace' , pci + '.inputCurve')
    cmds.setAttr(pci + '.parameter' , u )
    cmds.connectAttr( pci + '.position' , s + '.t')

1 个答案:

答案 0 :(得分:0)

Python中的字符串文字仅使用“或”表示一行字符串。对于多行字符串,您可以使用:“”“....

所以,要解决语法错误只需更改:

pci = cmds.createNode(""pointOnCurveInfo" , n = name )

要:

pci = cmds.createNode("pointOnCurveInfo" , n = name )

引发第二个错误是因为此时没有定义函数getUParam,所以你必须在使用之前定义这个getUParam函数,如下所示:

from maya import cmds , OpenMaya

def getUParam( pnt = [], crv = None):

    point = OpenMaya.MPoint(pnt[0],pnt[1],pnt[2])
    curveFn = OpenMaya.MFnNurbsCurve(getDagPath(crv))
    paramUtill=OpenMaya.MScriptUtil()
    paramPtr=paramUtill.asDoublePtr()
    isOnCurve = curveFn.isPointOnCurve(point)
    if isOnCurve == True:

        curveFn.getParamAtPoint(point , paramPtr,0.001,OpenMaya.MSpace.kObject )
    else :
        point = curveFn.closestPoint(point,paramPtr,0.001,OpenMaya.MSpace.kObject)
        curveFn.getParamAtPoint(point , paramPtr,0.001,OpenMaya.MSpace.kObject )

    param = paramUtill.getDouble(paramPtr)  
    return param

def getDagPath( objectName):

    if isinstance(objectName, list)==True:
        oNodeList=[]
        for o in objectName:
            selectionList = OpenMaya.MSelectionList()
            selectionList.add(o)
            oNode = OpenMaya.MDagPath()
            selectionList.getDagPath(0, oNode)
            oNodeList.append(oNode)
        return oNodeList
    else:
        selectionList = OpenMaya.MSelectionList()
        selectionList.add(objectName)
        oNode = OpenMaya.MDagPath()
        selectionList.getDagPath(0, oNode)
        return oNode

sel = cmds.ls(sl =1 )
crv = "curveShape1"
for s in sel :
    pos = cmds.xform(s ,q = 1 , ws = 1 , t = 1)
    u = getUParam(pos , crv)
    name = s.replace("_LOC" , "_PCI")
    pci = cmds.createNode("pointOnCurveInfo" , n = name )
    cmds.connectAttr(crv + '.worldSpace' , pci + '.inputCurve')
    cmds.setAttr(pci + '.parameter' , u )
    cmds.connectAttr( pci + '.position' , s + '.t')

希望它有所帮助,