值错误:没有对象与Maya Python中的名称匹配

2024-04-28 19:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个返回错误的代码

ValueError: No object matches name: s

我不知道它为什么要寻找对象s。在

代码如下

^{pr2}$

按照预期,print命令应该输出Spine01_jnt,它是{}的父级

我错过了什么吗?在


Tags: 对象no代码name命令object错误print
2条回答

多亏了Python中的duck类型,有时这样的错误很难捕捉到。这里发生的是你的函数需要一个数组作为参数,但是你传递的是一个字符串。在

Python还通过列出单个字符来支持对字符串进行迭代,这就是它在spine02_jnt中查找s的原因。在数组中传递字符串可以解决您的问题:

createOffsetGrp(['spine02_jnt'])

你能提供什么样的支持

import maya.cmds as cmds

def createOffsetGrp(objSel):
    # isinstance is used to check the type of the variable :
    # i.e: isinstance(objSel, int)
    # basestring is a type combining unicode and string types
    if isinstance(objSel, basestring):
        objSel = [objSel]
    for obj in objSel:
        p = cmds.listRelatives(obj,parent=True)
        print (p)

createOffsetGrp('spine02_jnt')

相关问题 更多 >