我正在尝试创建一个变量来保存Maya中的定位器和网格列表。所以我有这个
locators = cmds.listRelatives(cmds.ls(type= 'locator'), p=1)# Give me the list of locators
meshes = cmds.listRelatives(cmds.ls(type= 'mesh'), p=1) #Give me the list of all meshes
但只有当场景中有定位器或多边形可用时,这些方法才会起作用。Maya抛出一个错误:
line 1: Object [] is invalid
如果没有找到定位器或多边形。你知道吗
即使它们在现场是否可用,如何存储它们以使其工作?目的是创建搜索和替换工具。因此,如果艺术家愿意,他/她可以只搜索和替换网格。但现在即使我只是S&R,它也会出错。当我搜索网格时,定位器给出错误,而当我搜索定位器时,网格失败。你知道吗
以下是我的整个搜索和替换代码:
def searchAndReplace(self):
searchText = str(self.windowObj.myLookFor.text()) #My search text feild
replaceText = str(self.windowObj.myRepFor.text()) #My replace text feild
selection = cmds.ls(sl=True) #only selected items
locators = cmds.listRelatives(cmds.ls(type= 'locator'), p=1)# Give me the list of locators
meshes = cmds.listRelatives(cmds.ls(type= 'mesh'), p=1) #Give me the list of all meshes
joints = cmds.ls(type = 'joint')# Give me the list of my joints.
allObjects = locators, meshes, joints
if len(selection) > 0:
if self.windowObj.myRepAll.isChecked():
print "All is selected"
for object in meshes:
if object.find(searchText) != -1:
newName = object.replace(searchText, replaceText)
cmds.rename(object, newName)
self.listofMeshes.append(meshes)
else:
print "No mesh found. Skipping meshes"
for object in locators:
if object.find(searchText) != -1:
newName2 = object.replace(searchText, replaceText)
cmds.rename(object, newName2)
self.listofLocators.append(locators)
else:
"No locators found. Skipping locators"
for object in joints:
if object.find(searchText) != -1:
newName3 = object.replace(searchText, replaceText)
cmds.rename(object, newName3)
self.listofJoints.append(joints)
else:
print "No joints found. Skipping joints"
需要帮助正确地存储变量,以便它能够正确地存储定位器、网格和关节,并且能够在其中一个在场景中不可用时使用它。你知道吗
默认情况下,如果
cmds.listRelatives
没有找到任何内容,而不是像预期的那样返回空的[]
,它将返回None
。你知道吗解决这个问题的两种方法是将
None
转换为[]
:或者执行条件检查以查看变量是否为空:
你不应该像Carlos建议的那样包装一个
try
except
。这只是编程中的一个坏习惯,有一些例外,而且通常是一种懒惰的方式。你知道吗当我在一个新的空场景中运行你指出的两个语句时,两个变量的结果都是“无”。你知道吗
在这种情况下,在启动循环之前,可以通过将每个for循环缩进if meshes:或更好的if isinstance(meshes,list):来防止错误,该命令仅在meshes是列表时才执行代码:
如果在尝试执行语句时仍然出现相同的错误,请将其缩进try/catch块中,以查看关于发生了什么的更详细解释,并从Maya CMDS' documentation获得更多帮助:
相关问题 更多 >
编程相关推荐