ml_parentShape.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:ml_tools 作者: morganloomis 项目源码 文件源码
def unparentShape(objs=None):

    if not objs:
        objs = mc.ls(sl=True)
        if not objs:
            OpenMaya.MGlobal.displayWarning('Please select one or more nodes with shapes to unparent.')
            return
    elif not isinstance(objs, (list,tuple)):
        objs = [objs]

    #are these shapes or transforms
    transforms = list()
    shapes = list()
    for obj in objs:
        nodeType = mc.nodeType(obj)
        if nodeType in ('mesh','nurbsCurve','nurbsSurface','locator','annotationShape'):
            shapes.append(obj)
        elif nodeType in ('transform', 'joint', 'ikHandle'):
            if not mc.listRelatives(obj, shapes=True, path=True, noIntermediate=True):
                OpenMaya.MGlobal.displayWarning(obj+' has no shapes, skipping.')
                return
            transforms.append(obj)
        else:
            OpenMaya.MGlobal.displayWarning(obj+' must be a shape, or a transform with shapes. Skipping')
            return

    for each in transforms:
        childShapes = mc.listRelatives(each, shapes=True, path=True, noIntermediate=True)
        shapes.extend([x for x in childShapes if x not in shapes])

    #shapes that share a common parent get unparented together
    newTransforms = dict()
    for each in shapes:
        shapeParent = mc.listRelatives(each, parent=True, fullPath=True)[0]
        if not shapeParent in newTransforms:
            newTransforms[shapeParent] = mc.createNode('transform', name='unparentedShape#')
            newTransforms[shapeParent] = mc.parent(newTransforms[shapeParent], shapeParent)[0]
            mc.setAttr(newTransforms[shapeParent]+'.translate', 0,0,0)
            mc.setAttr(newTransforms[shapeParent]+'.rotate', 0,0,0)
            mc.setAttr(newTransforms[shapeParent]+'.scale', 1,1,1)
            newTransforms[shapeParent] = mc.parent(newTransforms[shapeParent], world=True)[0]

        shape = mc.parent(each, newTransforms[shapeParent], shape=True, relative=True)[0]
    return newTransforms.values()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号