Python maya.cmds 模块,listRelatives() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用maya.cmds.listRelatives()

项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def _output_node(source, type, suffix):
    newname = lib.unique(name=source.rsplit("_", 1)[0] + suffix)

    node = cmds.createNode(type)
    node = [cmds.listRelatives(node, parent=True) or node][0]
    node = cmds.rename(node, newname)

    try:
        cmds.parent(node, source)
        match_transform(node, source)

    except Exception:
        cmds.warning("Could not create %s" % node)
        cmds.delete(node)

    return node
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def shape_from_element(element):
    """Return shape of given 'element'

    Supports components, meshes, and surfaces

    """

    try:
        # Get either shape or transform, based on element-type
        node = cmds.ls(element, objectsOnly=True)[0]
    except Exception:
        cmds.warning("Could not find node in %s" % element)
        return None

    if cmds.nodeType(node) == 'transform':
        try:
            return cmds.listRelatives(node, shapes=True)[0]
        except Exception:
            cmds.warning("Could not find shape in %s" % element)
            return None

    else:
        return node
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def clone_special(*args):
    """Clone in localspace, and preserve user-defined attributes"""

    for transform in cmds.ls(selection=True, long=True):
        if cmds.nodeType(transform) != "transform":
            cmds.warning("Skipping '%s', not a `transform`" % transform)
            continue

        shape = _find_shape(transform)
        type = cmds.nodeType(shape)

        if type not in ("mesh", "nurbsSurface", "nurbsCurve"):
            cmds.warning("Skipping '{transform}': cannot clone nodes "
                         "of type '{type}'".format(**locals()))
            continue

        cloned = commands.clone(shape, worldspace=False)
        new_transform = cmds.listRelatives(cloned,
                                           parent=True,
                                           fullPath=True)[0]

        new_transform = cmds.rename(new_transform,
                                    new_transform.rsplit(":", 1)[-1])

        for attr in cmds.listAttr(transform,
                                  userDefined=True) or list():
            try:
                cmds.addAttr(new_transform, longName=attr, dataType="string")
            except Exception:
                continue

            value = cmds.getAttr(transform + "." + attr)
            cmds.setAttr(new_transform + "." + attr, value, type="string")

        # Connect visibility
        cmds.connectAttr(transform + ".visibility",
                         new_transform + ".visibility")
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def _clone(worldspace=False):
    """Clone selected objects in viewport

    Arguments:
        worldspace (bool): Whether or not to append a transformGeometry to
            resulting clone.

    """

    clones = list()

    for node in cmds.ls(selection=True, long=True):
        shape = _find_shape(node)
        type = cmds.nodeType(shape)

        if type not in ("mesh", "nurbsSurface", "nurbsCurve"):
            cmds.warning("Skipping '{node}': cannot clone nodes "
                         "of type '{type}'".format(**locals()))
            continue

        cloned = commands.clone(shape, worldspace=worldspace)
        clones.append(cloned)

    if not clones:
        return

    # Select newly created transform nodes in the viewport
    transforms = list()

    for clone in clones:
        transform = cmds.listRelatives(clone, parent=True, fullPath=True)[0]
        transforms.append(transform)

    cmds.select(transforms, replace=True)
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def _find_shape(element):
    """Return shape of given 'element'

    Supports components, meshes, and surfaces

    Arguments:
        element (str): Path to component, mesh or surface

    Returns:
        str of path if found, None otherwise

    """

    # Get either shape or transform, based on element-type
    node = cmds.ls(element, objectsOnly=True, long=True)[0]

    if cmds.nodeType(node) == "transform":
        try:
            return cmds.listRelatives(node, shapes=True, fullPath=True)[0]
        except IndexError:
            return cmds.warning("Could not find shape in %s" % element)
    else:
        return node
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def on_save(_):
    """Automatically add IDs to new nodes

    Any transform of a mesh, without an exising ID,
    is given one automatically on file save.

    """

    avalon.logger.info("Running callback on save..")

    nodes = (set(cmds.ls(type="mesh", long=True)) -
             set(cmds.ls(long=True, readOnly=True)) -
             set(cmds.ls(long=True, lockedNodes=True)))

    transforms = cmds.listRelatives(list(nodes), parent=True) or list()

    # Add unique identifiers
    for node in transforms:
        _set_uuid(node)
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def getObjects(self, model, includeShapes=True):
        """Get the objects of the component.

        Args:
            model(dagNode): The root of the component.
            includeShapes (boo): If True, will include the shapes.

        Returns:
            list of dagNode: The list of the objects.

        """
        objects = {}
        if includeShapes:
            children = pm.listRelatives(model, ad=True)
        else:
            children = pm.listRelatives(model, ad=True, typ='transform')
        pm.select(children)
        for child in pm.ls(self.fullName + "_*", selection=True):
            objects[child[child.index(
                self.fullName + "_") + len(self.fullName + "_"):]] = child

        return objects
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def getObjects2(self, model, includeShapes=True):
        """Get the objects of the component.

        Args:
            model(dagNode): The root of the component.
            includeShapes (boo): If True, will include the shapes.

        Returns:
            list of dagNode: The list of the objects.

        """
        objects = {}
        if includeShapes:
            children = [pm.PyNode(x) for x in cmds.listRelatives(
                model.longName(), ad=True, fullPath=True)]
        else:
            children = [pm.PyNode(x) for x in cmds.listRelatives(
                model.longName(), ad=True, typ='transform', fullPath=True)]
        for child in children:
            cName = child.longName()
            if cName.startswith(self.fullName):
                objects[cName.split("_")[-1]] = child

        return objects
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def __findChildren(node, name, firstOnly=False, partialName=False):

    if partialName:
        children = [item for item
                    in node.listRelatives(allDescendents=True,
                                          type="transform")
                    if item.name().split("|")[-1].split("_")[-1] == name]
    else:
        children = [item for item
                    in node.listRelatives(allDescendents=True,
                                          type="transform")
                    if item.name().split("|")[-1] == name]
    if not children:
        return False
    if firstOnly:
        return children[0]

    return children
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def __findChild(node, name):
    """This find children function will stop search after firs child found.child

    This is a faster version of __findchildren

    Arguments:
        node (dagNode): The input node to search
        name (str): The name to search

    Returns:
        dagNode: Children node
    """
    try:
        for item in cmds.listRelatives(node.name(),
                                       allDescendents=True,
                                       type="transform"):
            if item.split("|")[-1] == name:
                return pm.PyNode(item)
    except pm.MayaNodeError:
        for item in node.listRelatives(allDescendents=True,
                                       type="transform"):
            if item.split("|")[-1] == name:
                return item

    return False
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def make_planar(joints):
    for joint in joints:
        parent = cmds.listRelatives(joint, parent=True, path=True)
        if not parent:
            log.warning('Cannot make %s planar because it does not have a parent.', joint)
            continue
        children = _unparent_children(joint)
        if not children:
            log.warning('Cannot make %s planar because it does not have any children.', joint)
            continue
        cmds.delete(cmds.aimConstraint(children[0], joint, aim=(1, 0, 0), u=(0, 1, 0), worldUpType='object', worldUpObject=parent[0]))
        cmds.makeIdentity(joint, apply=True)
        _reparent_children(joint, children)

    if joints:
        cmds.select(joints)
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def orient_to_world(joints):
    """Orients the given joints with the world.

    @param joints: Joints to orient.
    """
    for joint in joints:
        children = _unparent_children(joint)
        print children
        parent = cmds.listRelatives(joint, parent=True, path=True)
        orig_joint = joint.split('|')[-1]
        if parent:
            joint = cmds.parent(joint, world=True)[0]
        cmds.joint(joint, e=True, oj='none', zso=True)
        if parent:
            joint = cmds.parent(joint, parent)[0]
            print 'Renaming {0} to {1}'.format(joint, orig_joint)
            joint = cmds.rename(joint, orig_joint)
        _reparent_children(joint, children)

    if joints:
        cmds.select(joints)
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def mirror(joint, search_for, replace_with):
    joints = [joint, ] + (cmds.listRelatives(joint, ad=True, path=True) or [])
    for joint in joints:
        mirrored_joint = joint.replace(search_for, replace_with)
        if cmds.objExists(mirrored_joint):
            translate = list(cmds.getAttr('{0}.t'.format(joint))[0])
            parent = cmds.listRelatives(joint, parent=True, path=True)
            if parent and search_for not in parent[0]:
                translate[2] *= -1.0
            else:
                translate = [x * -1.0 for x in translate]
            cmds.setAttr('{0}.t'.format(mirrored_joint), *translate)

            rotate = cmds.getAttr('{0}.r'.format(joint))[0]
            cmds.setAttr('{0}.r'.format(mirrored_joint), *rotate)

            scale = cmds.getAttr('{0}.s'.format(joint))[0]
            cmds.setAttr('{0}.s'.format(mirrored_joint), *scale)
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def get_skin_clusters(nodes):
    """Get the skinClusters attached to the specified node and all nodes in descendents.

    :param nodes: List of dag nodes.
    @return A list of the skinClusters in the hierarchy of the specified root node.
    """
    if isinstance(nodes, basestring):
        nodes = [nodes, ]
    all_skins = []
    for node in nodes:
        relatives = cmds.listRelatives(node, ad=True, path=True) or []
        relatives.insert(0, node)
        relatives = [shortcuts.get_shape(node) for node in relatives]
        for relative in relatives:
            history = cmds.listHistory(relative, pruneDagObjects=True, il=2) or []
            skins = [x for x in history if cmds.nodeType(x) == 'skinCluster']
            if skins:
                all_skins.append(skins[0])
    return list(set(all_skins))
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def test_set_transform_stack(self):
        loc = cmds.spaceLocator(name='spine_ctrl')[0]
        nulls = control.create_transform_stack(loc, 2)
        self.assertEqual(2, len(nulls))
        parent = cmds.listRelatives(loc, parent=True, path=True)[0]
        self.assertEqual('spine_1nul', parent)
        parent = cmds.listRelatives(parent, parent=True, path=True)[0]
        self.assertEqual('spine_2nul', parent)

        nulls = control.create_transform_stack(loc, 3)
        self.assertEqual(3, len(nulls))
        parent = cmds.listRelatives(loc, parent=True, path=True)[0]
        self.assertEqual('spine_1nul', parent)
        parent = cmds.listRelatives(parent, parent=True, path=True)[0]
        self.assertEqual('spine_2nul', parent)
        parent = cmds.listRelatives(parent, parent=True, path=True)[0]
        self.assertEqual('spine_3nul', parent)
项目:cmt    作者:chadmv    | 项目源码 | 文件源码
def assert_hierarachies_match(self):
        self.assertEqual(7, len(cmds.ls(type='joint')))
        # Make sure the joint orients are the same
        translates = [cmds.getAttr('{0}.t'.format(x))[0] for x in cmds.ls(type='joint')]
        rotates = [cmds.getAttr('{0}.r'.format(x))[0] for x in cmds.ls(type='joint')]
        orients = [cmds.getAttr('{0}.jo'.format(x))[0] for x in cmds.ls(type='joint')]
        for orient, new_orient in zip(self.orients, orients):
            self.assertListAlmostEqual(orient, new_orient)
        for translate, new_translate in zip(self.translates, translates):
            self.assertListAlmostEqual(translate, new_translate)
        for rotate, new_rotate in zip(self.rotates, rotates):
            self.assertListAlmostEqual(rotate, new_rotate)
        # The geometry should not have been exported
        self.assertFalse(cmds.objExists(self.cube))
        self.assertTrue(cmds.objExists(self.group))
        self.assertEqual('joint1', cmds.listRelatives(self.group, children=True)[0])
项目:mayakit    作者:danbradham    | 项目源码 | 文件源码
def to_curve_fn(curve):

    shape = None
    if cmds.nodeType(curve, 'nurbsCurve'):
        shape = curve
    else:
        child = cmds.listRelatives(curve, shapes=True, noIntermediate=True)
        if child:
            shape = child[0]
        else:
            cmds.warning('Not a proper nurbsCurve: {}'.format(curve))
            raise Exception('Not a proper nurbsCurve: {}'.format(curve))

    sel = om.MSelectionList()
    sel.add(shape)
    dep = sel.getDagPath(0)
    fn = om.MFnNurbsCurve(dep)
    return fn
项目:mayakit    作者:danbradham    | 项目源码 | 文件源码
def curve_between(a, b, num_points=24, degree=3, name='curve#'):
    '''Create a nurbsCurve between two MVectors

    :param a: start of curve
    :param b: end of curve
    :param num_points: number of points on curve
    :param degree: degree of curve
    '''

    v = b - a

    cvs = []
    for t in linspace(0, 1, num_points):
        cvs.append(a + v * t)
    knots = compute_knots(num_points, degree)

    curve = cmds.curve(point=cvs, degree=degree, knot=knots)
    curve = cmds.rename(curve, name)
    curve_shape = cmds.listRelatives(curve, shapes=True)[0]
    return curve, curve_shape
项目:mayakit    作者:danbradham    | 项目源码 | 文件源码
def curve_to_hair(curve_shape, hair_system):

    curve = cmds.listRelatives(curve_shape, parent=True, f=True)[0]
    curve_name = curve.split('|')[-1]

    # Create follicle
    follicle_shape = cmds.createNode('follicle')
    follicle = cmds.listRelatives(follicle_shape, parent=True, f=True)[0]
    follicle = cmds.rename(follicle, curve_name + '_follicle#')
    follicle_shape = cmds.listRelatives(follicle, shapes=True, f=True)[0]
    cmds.connectAttr(curve + '.worldMatrix', follicle_shape + '.startPositionMatrix')
    cmds.connectAttr(curve_shape + '.local', follicle_shape + '.startPosition')

    # # Create output curve
    out_curve_shape = cmds.createNode('nurbsCurve')
    out_curve = cmds.listRelatives(out_curve_shape, parent=True, f=True)[0]
    out_curve = cmds.rename(out_curve, curve_name + '_out#')
    out_curve_shape = cmds.listRelatives(out_curve, shapes=True, f=True)[0]
    cmds.connectAttr(follicle + '.outCurve', out_curve_shape + '.create')

    # Add follicle to hair system
    add_follicle(follicle_shape, hair_system)

    return [[follicle, follicle_shape], [out_curve, out_curve_shape]]
项目:OBB    作者:chrisdevito    | 项目源码 | 文件源码
def getShape(self,  node):
        """
        Gets the shape node from the input node.

        :param node (str): string name of input node

        Raises:
            `RuntimeError` if no shape node.
        Returns:
            (str) shape node name
        """
        if cmds.nodeType(node) == 'transform':
            shapes = cmds.listRelatives(node, shapes=True)

            if not shapes:
                raise RuntimeError('%s has no shape' % node)

            return shapes[0]

        elif cmds.nodeType(node) == "mesh":
            return node
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def insertGroupAbove(obj, *args):
    par = cmds.listRelatives(obj, p=True)

    grp = cmds.group(em=True, n="{}_Grp".format(obj))

    pos = cmds.xform(obj, q=True, ws=True, rp=True)
    rot = cmds.xform(obj, q=True, ws=True, ro=True)

    cmds.xform(grp, ws=True, t=pos)
    cmds.xform(grp, ws=True, ro=rot)

    cmds.parent(obj, grp)
    if par:
        cmds.parent(grp, par[0])

    return (grp)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def insertGroupAbove(*args):
    sel = cmds.ls(sl=True)

    for obj in sel:
        par = cmds.listRelatives(obj, p=True)

        grp = cmds.group(em=True, n="{}_Grp".format(obj))

        # grp = nameCheck(grp)

        pos = cmds.xform(obj, q=True, ws=True, rp=True)
        rot = cmds.xform(obj, q=True, ws=True, ro=True)

        cmds.xform(grp, ws=True, t=pos)
        cmds.xform(grp, ws=True, ro=rot) 

        cmds.parent(obj, grp)
        if par:
            cmds.parent(grp, par[0])
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def selectComponents(*args):
    sel = cmds.ls(sl=True)

    if sel:

        for obj in sel:
            shape = cmds.listRelatives(obj, s=True)[0]

            if cmds.objectType(shape) == "nurbsCurve":
                cmds.select(cmds.ls("{}.cv[*]".format(obj), fl=True))

            elif cmds.objectType(shape) == "mesh":
                cmds.select(cmds.ls("{}.vtx[*]".format(obj), fl=True))

            else:
                return
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def capReplace(*args):
    sel = cmds.ls(sl=True, type="transform")

    if sel < 2:
        cmds.warning("You don't have two things selected (cap and one ctrl minimum)!")
        return

    newCap = sel[0]
    ctrls = sel[1:]
    for ctrl in ctrls:
        oldCap = cmds.connectionInfo("{0}.capRig".format(ctrl), sfd=True).partition(".")[0]
        dupe = rig.swapDupe(newCap, oldCap, delete=True, name=oldCap)
        cmds.connectAttr("{0}.rotateCap".format(ctrl), "{0}.rotateY".format(dupe))
        cmds.connectAttr("{0}.message".format(dupe), "{0}.capRig".format(ctrl))
        cmds.setAttr("{0}.v".format(dupe), 1)

    # if not already, parent cap replace obj in folder and hide
    par = cmds.listRelatives(newCap, p=True)
    if not par or par[0] != "pastaRigSetupComponents_Grp":
        cmds.parent(newCap, "pastaRigSetupComponents_Grp")

    cmds.setAttr("{0}.v".format(newCap), 0)
    cmds.select(ctrls, r=True)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def addBaseCap(*args):
    sel = cmds.ls(sl=True, type="transform")

    if sel < 2:
        cmds.warning("You don't have two things selected (cap and one ctrl minimum)!")
        return

    newCap = sel[0]
    ctrls = sel[1:]

    for ctrl in ctrls:
        tempCap = cmds.connectionInfo("{0}.tempBaseCap".format(ctrl), sfd=True).partition(".")[0]

        dupe = rig.swapDupe(newCap, tempCap, delete=True, name="{0}_baseCap".format(ctrl))
        cmds.setAttr("{0}.v".format(dupe), 1)
        cmds.connectAttr("{0}.rotateBaseCap".format(ctrl), "{0}.rotateY".format(dupe))
        cmds.connectAttr("{0}.message".format(dupe), "{0}.tempBaseCap".format(ctrl))

    # if not already, parent cap replace obj in folder and hide
    par = cmds.listRelatives(newCap, p=True)
    if not par or par[0] != "pastaRigSetupComponents_Grp":
        cmds.parent(newCap, "pastaRigSetupComponents_Grp")

    cmds.setAttr("{0}.v".format(newCap), 0)
    cmds.select(ctrls, r=True)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def addGroupAbove(obj="none", suff="none", *args):
    """name of existing obj, new group suffix. New group will be oriented to the object BELOW it"""
    #FIX THE OBJ, SUFIX TO BE EITHER SELECTED OR ENTERED
    sel = cmds.ls(sl=True, type = "transform")
    for obj in sel:
        suff = "_new"
        name = obj + suff + "_GRP"
        #get worldspace location of existing obj
        loc = cmds.xform(obj, q=True, ws=True, rp=True)
        #create new group, name it, move it to new postion in ws and Orient it
        grp = cmds.group(empty=True, name=name)
        cmds.move(loc[0], loc[1], loc[2], grp, ws=True)
        oc = cmds.orientConstraint(obj, grp)
        cmds.delete(oc)
        #check if there's a parent to the old group
        par = cmds.listRelatives(obj, p=True)
        print(par)
        if par:
            cmds.parent(grp, par)
        cmds.parent(obj, grp)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def positionsAlongCurve(crv="", numPts = 3, *args):
    """
    returns list of numPts evenly distributed world positions along given nurbs crv
    """
    if not crv:
        return

    if isType(crv, "nurbsCurve"):
        posList = []
        shp = cmds.listRelatives(crv, s=True)[0]
        poc = cmds.shadingNode("pointOnCurveInfo", asUtility=True, name="tmpPOCInfo")
        cmds.connectAttr("{}.local".format(shp), "{}.inputCurve".format(poc))
        cmds.setAttr("{}.turnOnPercentage".format(poc), 1)
        lineLen = cmds.arclen(crv, ch=False)
        dist = float(numPts)/lineLen

        for x in range(0, numPts+1):
            perc = 1.0/numPts
            cmds.setAttr("{}.parameter".format(poc),x*perc)
            print x*perc
            pos = cmds.getAttr("{}.position".format(poc))[0]
            posList.append(pos)

        cmds.delete(poc)
        return(posList)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def insertGroupAbove(obj, *args):
    par = cmds.listRelatives(obj, p=True)

    grp = cmds.group(em=True, n="{}_Grp".format(obj))

    # grp = nameCheck(grp)

    pos = cmds.xform(obj, q=True, ws=True, rp=True)
    rot = cmds.xform(obj, q=True, ws=True, ro=True)

    cmds.xform(grp, ws=True, t=pos)
    cmds.xform(grp, ws=True, ro=rot) 

    cmds.parent(obj, grp)
    if par:
        cmds.parent(grp, par[0])

    return(grp)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def options_pass(*args):
    xfersDo, shpsDo = cmds.checkBoxGrp(widgets["shapeCBG"], q=True, valueArray2=True)
    inputs, outputs = cmds.checkBoxGrp(widgets["inOutCBG"], q=True, valueArray2=True)

    sel = cmds.ls(sl=True)
    if len(sel) != 2:
        cmds.warning("You don't have two things selected! (source, then target)")
        return()    

    srcX = sel[0]
    tgtX = sel[1]

    if xfersDo:
        transfer_connections_do(srcX, tgtX, inputs, outputs)

    if shpsDo:
        # options for checkbox would go here. . . 
        srcShp = cmds.listRelatives(srcX, s=True)[0]
        tgtShp = cmds.listRelatives(tgtX, s=True)[0]
        transfer_connections_do(srcShp, tgtShp, inputs, outputs)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def toggle_sel_shape_vis(*args):
    """toggles the selected transforms' shape visibility"""
    sel = cmds.ls(sl=True, type="transform")
    if sel:
        for obj in sel:
            shp = cmds.listRelatives(obj, s=True)
            if not shp:
                return ()
            for s in shp:
                currVal = cmds.getAttr("{0}.visibility".format(s))
                newVal = 0
                if currVal == 0:
                    newVal = 1
                elif currVal == 1:
                    newVal = 0
                cmds.setAttr("{0}.visibility".format(s), newVal)
项目:zTools    作者:zethwillie    | 项目源码 | 文件源码
def connectShapeVis(*args):
    """Connects the attr from the assoc. text field to the shape Visibility of selected objects"""

    sel = cmds.ls(sl=True, type="transform")
    driver = cmds.textFieldButtonGrp(widgets["toShapeVis"], q=True, tx=True)

    if sel:
        if driver:
            for obj in sel:
                shapes = cmds.listRelatives(obj, s=True)
                for shape in shapes:
                    try:
                        cmds.connectAttr(driver, "%s.v" % shape, f=True)
                        cmds.warning("Connected %s to %s" % (driver, shape))
                    except:
                        cmds.warning("Couldn't connect %s to %s. Sorry! Check the Script Editor." % (driver, shape))
    else:
        cmds.warning("You need to select an object to connect the shape.vis!")
项目:core    作者:getavalon    | 项目源码 | 文件源码
def shape_from_element(element):
    """Return shape of given 'element'

    Supports components, meshes, and surfaces

    """

    try:
        # Get either shape or transform, based on element-type
        node = cmds.ls(element, objectsOnly=True)[0]
    except:
        cmds.warning("Could not find node in %s" % element)
        return None

    if cmds.nodeType(node) == 'transform':
        try:
            return cmds.listRelatives(node, shapes=True)[0]
        except:
            cmds.warning("Could not find shape in %s" % element)
            return None

    else:
        return node
项目:gozbruh    作者:LumaPictures    | 项目源码 | 文件源码
def get_goz_objs():
    """Grab meshes from selection, filter out extraneous DAG objects and
    freeze transforms on objects.
    """
    objs = cmds.ls(selection=True, type='mesh', dag=True)
    if objs:
        xforms = cmds.listRelatives(objs, parent=True, fullPath=True)
        # freeze transform
        cmds.makeIdentity(xforms, apply=True, t=1, r=1, s=1, n=0)
        cmds.select(xforms)
        objs = cmds.ls(selection=True)
    return objs

#------------------------------------------------------------------------------
# Renaming
#------------------------------------------------------------------------------
项目:gozbruh    作者:LumaPictures    | 项目源码 | 文件源码
def create(obj):
    """Tell ZBrush to treat `obj` as a new object.

    Under the hood this changes a gozbruhBrush ID to match object name.
    """
    # does not change selection:
    cmds.delete(obj, constructionHistory=True)
    shape = cmds.ls(obj, type='mesh', dag=True)[0]
    xform = cmds.listRelatives(shape, parent=True, fullPath=True)[0]
    goz_check_xform = cmds.attributeQuery(
        'gozbruhBrushID', node=xform, exists=True)
    goz_check_shape = cmds.attributeQuery(
        'gozbruhBrushID', node=shape, exists=True)

    if goz_check_shape:
        cmds.setAttr(shape + '.gozbruhBrushID', obj, type='string')
    if goz_check_xform:
        cmds.setAttr(xform + '.gozbruhBrushID', obj, type='string')
    return xform
项目:pw_mGeoExporter    作者:paulwinex    | 项目源码 | 文件源码
def getParent(self,name, filter=None):
        if cmds.objExists(name):
            if cmds.nodeType(name) == 'mesh':
                name = cmds.listRelatives(name, p=True, f=True)
            par = cmds.listRelatives(name, p=True, f=True)
            if par:
                parent = par[0].split('|')[-1]
                if filter:
                    tok = self.tokenPrefix(filter)
                    for t in tok:
                        if t in parent:
                            return parent
                    return self.getParent(par[0], filter)
                else:
                    return parent
            else:
                return 'NoParent'
项目:ml_tools    作者:morganloomis    | 项目源码 | 文件源码
def isNodeVisible(node):
    '''
    Simply return whether or not the node can be seen.
    '''

    if not mc.attributeQuery('visibility', node=node, exists=True):
        return False
    if not mc.getAttr(node+'.v'):
        return False
    if mc.attributeQuery('intermediateObject', node=node, exists=True):
        if mc.getAttr(node+'.intermediateObject'):
            return False
    if not mc.getAttr(node+'.lodVisibility'):
        return False
    if mc.getAttr(node+'.overrideEnabled') and not mc.getAttr(node+'.overrideVisibility'):
        return False

    parent = mc.listRelatives(node, parent=True, pa=True)
    if parent:
        return isNodeVisible(parent[0])
    return True
项目:ml_tools    作者:morganloomis    | 项目源码 | 文件源码
def copySkinComponents(source, destinationVerts):

    if not mc.listRelatives(source, shapes=True):
        raise RuntimeError('Source object must be geometry.')

    sourceSkin = getSkinCluster(source)

    if not sourceSkin:
        raise RuntimeError("Source mesh doesn't have a skinCluster to copy from.")

    destMesh = mc.ls(destinationVerts[0], o=True)[0]
    destMesh = mc.listRelatives(destMesh, parent=True)[0]
    destSkin = copySkinInfluences(source, destMesh)

    tempSet = mc.sets(destinationVerts)

    mc.select(source, tempSet)

    mc.copySkinWeights(noMirror=True,
                       surfaceAssociation='closestPoint', 
                       influenceAssociation='closestJoint', 
                       normalize=True)    

    mc.delete(tempSet)
    mc.select(destinationVerts)
项目:ml_tools    作者:morganloomis    | 项目源码 | 文件源码
def getSkinCluster(mesh):
    '''
    Return the first skinCluster affecting this mesh.
    '''

    if mc.nodeType(mesh) in ('mesh','nurbsSurface','nurbsCurve'):
        shapes = [mesh]
    else:
        shapes = mc.listRelatives(mesh, shapes=True, path=True)

    for shape in shapes:
        history = mc.listHistory(shape, groupLevels=True, pruneDagObjects=True)
        if not history:
            continue
        skins = mc.ls(history, type='skinCluster')
        if skins:
            return skins[0]
    return None
项目:ml_tools    作者:morganloomis    | 项目源码 | 文件源码
def colorShape(obj, rgb=None, hsv=None):

    if not rgb:
        if hsv and len(hsv) == 3:
            rgb = colorsys.hsv_to_rgb(*hsv)
        else:
            raise RuntimeError('colorShape requires an rgb or hsv input.')

    mc.setAttr('{}.overrideEnabled'.format(obj), 1)
    mc.setAttr('{}.overrideRGBColors'.format(obj), 1)
    mc.setAttr('{}.overrideColorRGB'.format(obj), *rgb)

    shapes = mc.listRelatives(obj, shapes=True, pa=True)
    for shape in shapes:
        #if mc.getAttr('{}.overrideEnabled'.format(shape)): 
        mc.setAttr('{}.overrideEnabled'.format(shape), 1)           
        mc.setAttr('{}.overrideRGBColors'.format(shape), 1)
        mc.setAttr('{}.overrideColorRGB'.format(shape), *rgb)
项目:ChainMaker    作者:XJZeng    | 项目源码 | 文件源码
def run_command(self, *args):
        curve_sel = self.curve_name()
        vert_sel_list = cmds.ls(sl=True, fl = True)
        if '.' in vert_sel_list[0]:
            dummy_item_index = vert_sel_list[0].index('.')
            self.geo_name = vert_sel_list[0][0:dummy_item_index]

        Chain_Constrain(curve_sel, vert_sel_list, self.geo_name)
        Spline_Rig_Chain(curve_sel, vert_sel_list)

        self.chain_list = cmds.listRelatives(str(self.geo_name + '_geo_grp'))

        joint_list = cmds.ls(type = 'joint')

        for dummy_geo in self.chain_list:
            cmds.select(dummy_geo, add = True)

        for dummy_joint in joint_list:
            if 'ikHandle' in dummy_joint:
                pass
            elif curve_sel in dummy_joint:
                 cmds.select(dummy_joint, add=True)
        cmds.select('ikHandle*', d = True)          
        mel.eval('newBindSkin " -byClosestPoint -toSkeleton";')
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def match_transform(src, dst):
    """Transform `src` to `dst`, taking worldspace into account

    Arguments:
        src (str): Absolute path to source transform
        dst (str): Absolute path to destination transform

    """

    try:
        parent = cmds.listRelatives(src, parent=True)[0]
    except Exception:
        parent = None

    node_decompose = cmds.createNode("decomposeMatrix")
    node_multmatrix = cmds.createNode("multMatrix")

    connections = {
        dst + ".worldMatrix": node_multmatrix + ".matrixIn[0]",
        node_multmatrix + ".matrixSum": node_decompose + ".inputMatrix",
        node_decompose + ".outputTranslate": src + ".translate",
        node_decompose + ".outputRotate": src + ".rotate",
        node_decompose + ".outputScale": src + ".scale",
    }

    if parent:
        connections.update({
            parent + ".worldInverseMatrix": node_multmatrix + ".matrixIn[1]"
        })

    for s, d in connections.iteritems():
        cmds.connectAttr(s, d, force=True)

    cmds.refresh()

    cmds.delete([node_decompose, node_multmatrix])
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def combine(nodes):
    """Produce a new mesh with the contents of `nodes`

    Arguments:
        nodes (list): Path to shapes

    """

    unite = cmds.createNode("polyUnite", n=nodes[0] + "_polyUnite")

    count = 0
    for node in nodes:
        # Are we dealing with transforms, or shapes directly?
        shapes = cmds.listRelatives(node, shapes=True) or [node]

        for shape in shapes:
            try:
                cmds.connectAttr(shape + ".outMesh",
                                 unite + ".inputPoly[%s]" % count, force=True)
                cmds.connectAttr(shape + ".worldMatrix",
                                 unite + ".inputMat[%s]" % count, force=True)
                count += 1

            except Exception:
                cmds.warning("'%s' is not a polygonal mesh" % shape)

    if count:
        output = cmds.createNode("mesh", n=nodes[0] + "_combinedShape")
        cmds.connectAttr(unite + ".output", output + ".inMesh", force=True)
        return output

    else:
        cmds.delete(unite)
        return None
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def parent_group(source, transferTransform=True):
    """Create and transfer transforms to parent group"""
    assert cmds.objExists(source), "%s does not exist" % source
    assert cmds.nodeType(source) == "transform", (
        "%s must be transform" % source)

    parent = cmds.listRelatives(source, parent=True)

    if transferTransform:
        group = cmds.createNode("transform", n="%s_parent" % source)
        match_transform(group, source)

        try:
            cmds.parent(source, group)
        except Exception:
            cmds.warning("Failed to parent child under new parent")
            cmds.delete(group)

        if parent:
            cmds.parent(group, parent[0])

    else:
        cmds.select(source)
        group = cmds.group(n="%s_parent" % source)

    return group
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def create_ncloth():
    selection = cmds.ls(selection=True)[0]

    input_mesh = cmds.listRelatives(selection, shapes=True)[0]
    current_mesh = commands.create_ncloth(input_mesh)

    # Optionally append suffix
    comp = selection.rsplit("_", 1)
    suffix = ("_" + comp[-1]) if len(comp) > 1 else ""

    cmds.rename(current_mesh, "currentMesh%sShape" % suffix)

    # Mimic default nCloth command
    cmds.hide(selection)
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def process(self, instance):
        from maya import cmds

        has_multiple_shapes = list()

        # Consider entire hierarchy of nodes included in an Instance
        hierarchy = cmds.listRelatives(instance, allDescendents=True)

        # Consider only nodes of type="mesh"
        meshes = cmds.ls(hierarchy, type="mesh", long=True)
        transforms = cmds.listRelatives(meshes, parent=True)

        for transform in set(transforms):
            shapes = cmds.listRelatives(transform, shapes=True) or list()

            # Ensure the one child is a shape
            has_single_shape = len(shapes) == 1
            self.log.info("has single shape: %s" % has_single_shape)

            # Ensure the one shape is of type "mesh"
            has_single_mesh = (
                has_single_shape and
                cmds.nodeType(shapes[0]) == "mesh"
            )
            self.log.info("has single mesh: %s" % has_single_mesh)

            if not all([has_single_shape, has_single_mesh]):
                has_multiple_shapes.append(transform)

        assert not has_multiple_shapes, (
            "\"%s\" has transforms with multiple shapes: %s" % (
                instance, ", ".join(
                    "\"" + member + "\"" for member in has_multiple_shapes))
        )
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def process(self, instance):
        from maya import cmds

        missing = list()

        for member in ("controls_SET",
                       "out_SET"):
            if member not in instance:
                missing.append(member)

        assert not missing, "\"%s\" is missing members: %s" % (
            instance, ", ".join("\"" + member + "\"" for member in missing))

        # Ensure all output has IDs.
        # As user may inadvertently add to the out_SET without
        # realising, and some of the new members may be non-meshes,
        # or meshes without and ID
        missing = list()

        for node in cmds.sets("out_SET", query=True) or list():

            # Only check transforms with shapes that are meshes
            shapes = cmds.listRelatives(node, shapes=True) or list()
            meshes = cmds.ls(shapes, type="mesh")

            if not meshes:
                continue

            try:
                self.log.info("Checking '%s'" % node)
                cmds.getAttr(node + ".mbID")
            except ValueError:
                missing.append(node)

        assert not missing, ("Missing ID attribute on: %s"
                             % ", ".join(missing))
项目:config    作者:mindbender-studio    | 项目源码 | 文件源码
def process(self, instance):
        from maya import cmds

        nodes = list(instance)
        nodes += cmds.listRelatives(instance, allDescendents=True) or list()
        missing = list()

        for node in nodes:

            # Only check transforms with shapes that are meshes
            if not cmds.nodeType(node) == "transform":
                continue

            shapes = cmds.listRelatives(node,
                                        shapes=True,
                                        type="mesh") or list()
            meshes = cmds.ls(shapes, type="mesh")

            if not meshes:
                continue

            try:
                self.log.info("Checking '%s'" % node)
                cmds.getAttr(node + ".mbID")
            except ValueError:
                missing.append(node)

        assert not missing, ("Missing ID attribute on: %s"
                             % ", ".join(missing))
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def getShapes(node):
    """Returns the shape of the dagNode

    Arguments:
        node (dagNode): The input node to search the shape

    Returns:
        list: The shapes of the node

    """
    return node.listRelatives(shapes=True)
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def findComponentChildren(node, name, sideIndex):
    """Returns the component children of input component root.

    Note:
        This method is specific to work with shifter guides naming conventions

    Arguments:
        node (dagNode): The input node to search
        name (str): The name to search
        sideIndex (str): the side

    Returns:
        dagNode list: The children dagNodes

    >>> objList =  dag.findComponentChildren(self.parent,
                                             oldName,
                                             oldSideIndex)

    """
    children = []
    for item in node.listRelatives(allDescendents=True,
                                   type="transform"):
        checkName = item.name().split("|")[-1].split("_")
        if checkName[0] == name and checkName[1] == sideIndex:
            children.append(item)

    return children
项目:mgear    作者:miquelcampos    | 项目源码 | 文件源码
def findComponentChildren2(node, name, sideIndex):
    """Returns the component children of input component root.

    This function is using Maya cmds instead of PyMel

    Note:
        This method is specific to work with shifter guides naming conventions

    Arguments:
        node (dagNode): The input node to search
        name (str): The name to search
        sideIndex (str): the side

    Returns:
        dagNode list: The children dagNodes

    >>> objList =  dag.findComponentChildren(self.parent,
                                             oldName,
                                             oldSideIndex)

    """
    children = []
    for item in cmds.listRelatives(node.name(), allDescendents=True,
                                   type="transform"):
        checkName = item.split("|")[-1].split("_")
        if checkName[0] == name and checkName[1] == sideIndex:
            children.append(item)

    return [pm.PyNode(x) for x in children]