ValueError: No object matches name: in Maya Python
ValueError: No object matches name: in Maya Python
我有一个 returns 错误
的代码
ValueError: No object matches name: s
我不确定它为什么要寻找对象 s
。
代码如下
import maya.cmds as cmds
def createOffsetGrp(objSel):
for obj in objSel:
p = cmds.listRelatives(obj,parent=True)
print (p)
createOffsetGrp('spine02_jnt')
按预期,打印命令应该吐出 Spine01_jnt
,它是 Spine02_jnt
的父级
有什么我遗漏的吗?
多亏了鸭子输入 Python,有时这样的错误很难发现。这里发生的是你的函数期望一个数组作为参数,但你传递的是一个字符串。
Python 也支持通过列出单个字符来遍历字符串,这就是它在 spine02_jnt
中寻找 s
的原因。在数组中传递您的字符串应该可以解决您的问题:
createOffsetGrp(['spine02_jnt'])
除了 crazyGamer 之外,您还可以像这样对字符串提供一些支持:
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')
我有一个 returns 错误
的代码ValueError: No object matches name: s
我不确定它为什么要寻找对象 s
。
代码如下
import maya.cmds as cmds
def createOffsetGrp(objSel):
for obj in objSel:
p = cmds.listRelatives(obj,parent=True)
print (p)
createOffsetGrp('spine02_jnt')
按预期,打印命令应该吐出 Spine01_jnt
,它是 Spine02_jnt
有什么我遗漏的吗?
多亏了鸭子输入 Python,有时这样的错误很难发现。这里发生的是你的函数期望一个数组作为参数,但你传递的是一个字符串。
Python 也支持通过列出单个字符来遍历字符串,这就是它在 spine02_jnt
中寻找 s
的原因。在数组中传递您的字符串应该可以解决您的问题:
createOffsetGrp(['spine02_jnt'])
除了 crazyGamer 之外,您还可以像这样对字符串提供一些支持:
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')