在使用Maya的时候经常需要对大纲视图进行操作管理,比如下图中要删除选择的中间层级。直接删除的话会连带子节点一起删除,需要先改变链接关系再删除。如果数量过多的话还是比较让人烦恼的。
选择需要移除的中间层,运行脚本就可以删除中间层保留子级。
import pymel.core as py
def FindRoot(sl):
'获取所有根节点'
root = []
for obj in sl:
if not (py.listRelatives(obj, allParents=True, fullPath=1)[0] in sl):
# print py.listRelatives(obj, allParents = True,fullPath=1,noIntermediate=1)[0]
root.append(py.listRelatives(obj, allParents=True, fullPath=1, noIntermediate=1)[0])
return root
def FindChildren(sl):
'获取所有需要改变链接的子节点'
Children = []
for obj in sl:
if not ([False for chi in py.listRelatives(obj, children=True, fullPath=1, type='transform') if chi in sl]):
#print py.listRelatives(obj, allDescendents=True, fullPath=1, noIntermediate=1)[-1]
for x in (py.listRelatives(obj, children=True, fullPath=1, type='transform')):
Children.append(x)
return Children
def ChangeParent(sl):
py.select(clear=True)
root = FindRoot(sl)
children = FindChildren(sl)
if not (len(root) == 1):
for chi in children:
print "children is " + str(chi)
for roo in root:
print "Root is " + str(roo)
if (chi in py.listRelatives(roo, allDescendents=True, fullPath=True)):
print 'parent ' + str(chi) + ',' + str(roo)
py.parent(str(chi), str(roo))
py.select(clear=True)
break
else:
if not (len(children) == 0):
print 'parent ' + str(children) + ',' + str(root)
py.parent(*(children+root))
sl = py.ls(selection=1)
ChangeParent(sl)
py.delete(sl)