def test_get_long_name(self):
        node = maya.cmds.createNode('transform', name='myNode1')
        self.assertEqual(node, 'myNode1')
        node = node_utils.get_long_name(node)
        self.assertEqual(node, '|myNode1')

        chd1 = maya.cmds.createNode('transform', name='myNode1', parent=node)
        self.assertEqual(chd1, '|myNode1|myNode1')
        chd1 = node_utils.get_long_name(chd1)
        self.assertEqual(chd1, '|myNode1|myNode1')

        chd2 = maya.cmds.createNode('transform', name='myNode1', parent=chd1)
        self.assertEqual(chd2, '|myNode1|myNode1|myNode1')
        chd2 = node_utils.get_long_name(chd2)
        self.assertEqual(chd2, '|myNode1|myNode1|myNode1')

        chd3 = maya.cmds.createNode('transform', name='myChild1', parent=node)
        self.assertEqual(chd3, 'myChild1')
        chd3 = node_utils.get_long_name(chd3)
        self.assertEqual(chd3, '|myNode1|myChild1')

        node2 = maya.cmds.createNode('transform', name='myChild1')
        self.assertEqual(node2, '|myChild1')
        node2 = node_utils.get_long_name(node2)
        self.assertEqual(node2, '|myChild1')
Exemple #2
0
 def create_camera(name):
     cam_tfm = maya.cmds.createNode('transform', name=name)
     cam_tfm = node_utils.get_long_name(cam_tfm)
     cam_shp = maya.cmds.createNode('camera', name=name+'Shape',
                                    parent=cam_tfm)
     cam_shp = node_utils.get_long_name(cam_shp)
     return cam_tfm, cam_shp
def get_camera(node):
    """
    Get both transform and shape node of a camera.

    :param node: Part of the camera node, must be either a transform
                 or shape node.
    :type node: str

    :returns: Two nodes in a tuple, transform then shape node.
    :rtype: (str, str)
    """
    cam_tfm = None
    cam_shp = None
    inherited_node_types = maya.cmds.nodeType(node, inherited=True) or []
    if 'camera' in inherited_node_types:
        cam_shp = node_utils.get_long_name(node)
        nodes = maya.cmds.listRelatives(cam_shp, parent=True,
                                        fullPath=True) or []
        cam_tfm = nodes[0]
    elif 'transform' in inherited_node_types:
        cam_tfm = node_utils.get_long_name(node)
        nodes = maya.cmds.listRelatives(
            cam_tfm, shapes=True, type='camera', fullPath=True) or []
        if len(nodes) > 0:
            cam_shp = nodes[0]
        else:
            cam_tfm = None
            cam_shp = None
    else:
        node_type = maya.cmds.nodeType(node)
        msg = ('Node type not recognised as a camera! ' 'node=%r node_type=%r')
        LOG.warn(msg, node, node_type)
    return cam_tfm, cam_shp
Exemple #4
0
    def test_init(self):
        x = camera.Camera()
        x_tfm = x.get_transform_node()
        x_shp = x.get_shape_node()
        self.assertEqual(x_tfm, None)
        self.assertEqual(x_shp, None)

        # Create nodes
        cam_tfm = maya.cmds.createNode('transform', name='myCamera1')
        cam_tfm = node_utils.get_long_name(cam_tfm)
        cam_shp = maya.cmds.createNode('camera', name='myCameraShape1',
                                       parent=cam_tfm)
        cam_shp = node_utils.get_long_name(cam_shp)

        y = camera.Camera(transform=cam_tfm, shape=cam_shp)
        y_tfm = y.get_transform_node()
        y_shp = y.get_shape_node()
        self.assertTrue(maya.cmds.objExists(y_tfm))
        self.assertTrue(maya.cmds.objExists(y_shp))
        self.assertEqual(y_tfm, cam_tfm)
        self.assertEqual(y_shp, cam_shp)

        z1 = camera.Camera(transform=cam_tfm)
        z1_tfm = z1.get_transform_node()
        z1_shp = z1.get_shape_node()
        self.assertEqual(z1_tfm, cam_tfm)
        self.assertEqual(z1_shp, cam_shp)

        z2 = camera.Camera(shape=cam_shp)
        z2_tfm = z2.get_transform_node()
        z2_shp = z2.get_shape_node()
        self.assertEqual(z2_tfm, cam_tfm)
        self.assertEqual(z2_shp, cam_shp)
Exemple #5
0
    def test_init_with_non_standard_transform_node(self):
        """
        Create a camera with a custom transform node.

        Some pipelines custom transform nodes for camera transform
        nodes.

        GitHub issue #123.
        """
        custom_node_type = 'mmMarkerGroupTransform'
        # Create nodes
        cam_tfm = maya.cmds.createNode(custom_node_type, name='myCamera1')
        cam_tfm = node_utils.get_long_name(cam_tfm)
        cam_shp = maya.cmds.createNode('camera', name='myCameraShape1',
                                       parent=cam_tfm)
        cam_shp = node_utils.get_long_name(cam_shp)

        y = camera.Camera(transform=cam_tfm, shape=cam_shp)
        y_tfm = y.get_transform_node()
        y_tfm_uid = y.get_transform_uid()
        y_tfm_uid_node = node_utils.get_long_name(y_tfm_uid)
        y_shp = y.get_shape_node()
        y_shp_uid = y.get_shape_uid()
        y_shp_uid_node = node_utils.get_long_name(y_shp_uid)
        self.assertTrue(maya.cmds.objExists(y_tfm))
        self.assertTrue(maya.cmds.objExists(y_shp))
        self.assertTrue(maya.cmds.objExists(y_tfm_uid_node))
        self.assertTrue(maya.cmds.objExists(y_shp_uid_node))
        self.assertEqual(y_tfm, cam_tfm)
        self.assertEqual(y_shp, cam_shp)
        self.assertEqual(y_tfm_uid_node, cam_tfm)
        self.assertEqual(y_shp_uid_node, cam_shp)
        return
Exemple #6
0
def get_camera(node):
    """
    Get both transform and shape node of a camera.

    :param node: Part of the camera node, must be either a transform
                 or shape node.
    :type node: str

    :returns: Two nodes in a tuple, transform then shape node.
    :rtype: (str, str)
    """
    cam_tfm = None
    cam_shp = None
    node_type = maya.cmds.nodeType(node)
    if node_type == 'camera':
        cam_shp = node_utils.get_long_name(node)
        nodes = maya.cmds.listRelatives(
            cam_shp,
            parent=True,
            fullPath=True
        ) or []
        cam_tfm = nodes[0]
    elif node_type == 'transform':
        cam_tfm = node_utils.get_long_name(node)
        nodes = maya.cmds.listRelatives(
            cam_tfm,
            shapes=True,
            fullPath=True
        ) or []
        cam_shp = nodes[0]
    else:
        msg = 'Node type not recognised as a camera! node_type=%r'
        LOG.error(msg, node_type)
    return cam_tfm, cam_shp
    def test_get_marker_group_above_node(self):
        mkr_grp = maya.cmds.createNode('mmMarkerGroupTransform')
        mkr_grp = node_utils.get_long_name(mkr_grp)

        node = maya.cmds.createNode('transform', parent=mkr_grp)
        node = node_utils.get_long_name(node)
        above_mkr_grp = api_utils.get_marker_group_above_node(node)
        self.assertEqual(above_mkr_grp, mkr_grp)
Exemple #8
0
 def create_camera(name):
     assert isinstance(name, basestring)
     cam_tfm = maya.cmds.createNode('transform', name=name)
     cam_tfm = node_utils.get_long_name(cam_tfm)
     shp_name = name + 'Shape'
     cam_shp = maya.cmds.createNode(
         'camera', name=shp_name, parent=cam_tfm)
     cam_shp = node_utils.get_long_name(cam_shp)
     return cam_tfm, cam_shp
Exemple #9
0
def create_screen_space_locator(cam, name=None):
    """
    Create a Screen-Space Maya Locator that may be solved in Screen XYZ.
    """
    if name is None:
        name = 'screenSpace'

    mkr_grp = mmapi.MarkerGroup().create_node(cam=cam, name=name)
    mkr_grp_node = mkr_grp.get_node()
    maya.cmds.setAttr(mkr_grp_node + '.depth', 1.0)
    maya.cmds.setAttr(mkr_grp_node + '.depth', lock=True)
    maya.cmds.setAttr(mkr_grp_node + '.overscanX', lock=True)
    maya.cmds.setAttr(mkr_grp_node + '.overscanY', lock=True)

    depth_tfm = maya.cmds.createNode('transform',
                                     name='depth',
                                     parent=mkr_grp_node)
    depth_tfm = node_utils.get_long_name(depth_tfm)
    maya.cmds.setAttr(depth_tfm + '.translateX', lock=True)
    maya.cmds.setAttr(depth_tfm + '.translateY', lock=True)
    maya.cmds.setAttr(depth_tfm + '.translateZ', lock=True)
    maya.cmds.setAttr(depth_tfm + '.rotateX', lock=True)
    maya.cmds.setAttr(depth_tfm + '.rotateY', lock=True)
    maya.cmds.setAttr(depth_tfm + '.rotateZ', lock=True)
    src = depth_tfm + '.scaleX'
    dst1 = depth_tfm + '.scaleY'
    dst2 = depth_tfm + '.scaleZ'
    maya.cmds.connectAttr(src, dst1)
    maya.cmds.connectAttr(src, dst2)
    maya.cmds.setAttr(depth_tfm + '.scaleY', lock=True)
    maya.cmds.setAttr(depth_tfm + '.scaleZ', lock=True)

    loc_name = 'point'
    loc_tfm = maya.cmds.createNode('transform',
                                   name=name,
                                   parent=depth_tfm)
    loc_tfm = node_utils.get_long_name(loc_tfm)
    loc_shp = maya.cmds.createNode('locator', parent=loc_tfm)
    loc_shp = node_utils.get_long_name(loc_shp)
    maya.cmds.setAttr(loc_tfm + '.localScaleX', 0.1)
    maya.cmds.setAttr(loc_tfm + '.localScaleY', 0.1)
    maya.cmds.setAttr(loc_tfm + '.localScaleZ', 0.0)
    maya.cmds.setAttr(loc_tfm + '.translateZ', -1.0)
    maya.cmds.setAttr(loc_tfm + '.translateZ', lock=True)
    maya.cmds.setAttr(loc_tfm + '.rotateX', lock=True)
    maya.cmds.setAttr(loc_tfm + '.rotateY', lock=True)
    maya.cmds.setAttr(loc_tfm + '.rotateZ', lock=True)
    maya.cmds.setAttr(loc_tfm + '.scaleX', lock=True)
    maya.cmds.setAttr(loc_tfm + '.scaleY', lock=True)
    maya.cmds.setAttr(loc_tfm + '.scaleZ', lock=True)
    maya.cmds.setAttr(loc_tfm + '.shearXY', lock=True)
    maya.cmds.setAttr(loc_tfm + '.shearXZ', lock=True)
    maya.cmds.setAttr(loc_tfm + '.shearYZ', lock=True)
    return mkr_grp_node, depth_tfm, loc_tfm, loc_shp
    def test_get_camera_above_node(self):
        root = maya.cmds.createNode('transform')
        root = node_utils.get_long_name(root)

        cam_tfm = maya.cmds.createNode('transform', parent=root)
        cam_tfm = node_utils.get_long_name(cam_tfm)
        cam_shp = maya.cmds.createNode('camera', parent=cam_tfm)
        cam_shp = node_utils.get_long_name(cam_shp)

        node = maya.cmds.createNode('transform', parent=cam_tfm)
        node = node_utils.get_long_name(node)
        above_cam_tfm, above_cam_shp = node_utils.get_camera_above_node(node)
        self.assertEqual(above_cam_tfm, cam_tfm)
        self.assertEqual(above_cam_shp, cam_shp)
    def test_get_object_type(self):
        """
        Test getting string object types from a given Maya node.

        TODO: Does not provide a 'collection' example.
        """
        # Camera Type
        cam_tfm = maya.cmds.createNode('transform')
        cam_tfm = node_utils.get_long_name(cam_tfm)
        cam_shp = maya.cmds.createNode('camera', parent=cam_tfm)
        cam_shp = node_utils.get_long_name(cam_shp)
        tfm_obj_type = api_utils.get_object_type(cam_tfm)
        shp_obj_type = api_utils.get_object_type(cam_shp)
        self.assertEqual(tfm_obj_type, const.OBJECT_TYPE_CAMERA)
        self.assertEqual(shp_obj_type, const.OBJECT_TYPE_CAMERA)

        # Marker Type
        mkr = marker.Marker().create_node()
        mkr_node = mkr.get_node()
        obj_type = api_utils.get_object_type(mkr_node)
        self.assertEqual(obj_type, const.OBJECT_TYPE_MARKER)

        # Marker shape node must also return as OBJECT_TYPE_MARKER
        mkr_shp_node = maya.cmds.listRelatives(mkr_node, shapes=True)[0]
        obj_type = api_utils.get_object_type(mkr_shp_node)
        self.assertEqual(obj_type, const.OBJECT_TYPE_UNKNOWN)

        # Marker Group
        node = maya.cmds.createNode('mmMarkerGroupTransform')
        obj_type = api_utils.get_object_type(node)
        self.assertEqual(obj_type, const.OBJECT_TYPE_MARKER_GROUP)

        # A Bundle must be more than just a transform and locator.
        # Use mmSolver.api.Bundle.create_node() to create a Bundle.
        #
        # GitHub Issue #6.
        bnd_node = maya.cmds.createNode('transform')
        bnd_shp_node = maya.cmds.createNode('locator', parent=bnd_node)
        obj_type = api_utils.get_object_type(bnd_node)
        self.assertEqual(obj_type, const.OBJECT_TYPE_UNKNOWN)

        # Giving a shape will not work.
        obj_type = api_utils.get_object_type(bnd_shp_node)
        self.assertEqual(obj_type, const.OBJECT_TYPE_UNKNOWN)

        # Attribute Type
        node_attr = node + '.scaleX'
        obj_type = api_utils.get_object_type(node_attr)
        self.assertEqual(obj_type, const.OBJECT_TYPE_ATTRIBUTE)
def get_image_plane_shapes_from_camera(cam_tfm, cam_shp):
    """
    Get the list of image plane shape nodes connected to the given camera.

    :param cam_tfm: Camera transform node.
    :type cam_tfm: str

    :param cam_shp: Camera shape node.
    :type cam_shp: str

    :returns: The list of image shape nodes, may be an empty list.
    :rtype: [str, ..]
    """
    assert isinstance(cam_tfm, pycompat.TEXT_TYPE)
    assert len(cam_tfm) > 0
    assert maya.cmds.objExists(cam_tfm)
    assert isinstance(cam_shp, pycompat.TEXT_TYPE)
    assert len(cam_shp) > 0
    assert maya.cmds.objExists(cam_shp)
    assert node_utils.attribute_exists('imagePlane', cam_shp)
    plug = '{0}.imagePlane'.format(cam_shp)
    img_pl_shps = maya.cmds.listConnections(plug, type='imagePlane') or []
    img_pl_shps = [node_utils.get_long_name(n) for n in img_pl_shps]
    img_pl_shps = [n for n in img_pl_shps if n is not None]
    return img_pl_shps
Exemple #13
0
    def get_plate_resolution(self):
        """
        Get the resolution of the image attached to this camera, or
        return a default value if no image is attached.

        :return: Tuple of X and Y resolution.
        :rtype: (int, int)
        """
        resolution = (const.DEFAULT_PLATE_WIDTH, const.DEFAULT_PLATE_HEIGHT)
        shp = self.get_shape_node()
        if shp is None:
            LOG.warning('Could not get Camera shape node.')
            return resolution
        plug = shp + '.imagePlane'
        img_planes = maya.cmds.listConnections(plug, type='imagePlane') or []
        img_planes = [node_utils.get_long_name(n) for n in img_planes]

        if len(img_planes) == 0:
            return resolution  # no image planes
        elif len(img_planes) > 1:
            msg = 'Multiple image planes on camera, using first;'
            msg += 'camera=%r nodes=%r'
            LOG.warning(msg, shp, img_planes)
        img_plane = img_planes[0]

        width = maya.cmds.getAttr(img_plane + '.coverageX')
        height = maya.cmds.getAttr(img_plane + '.coverageY')
        if width > 0 and height > 0:
            resolution = (width, height)
        else:
            msg = 'Get plate resolution failed, using to default values;'
            msg += 'camera=%r nodes=%r width=%r height=%r'
            LOG.debug(msg, shp, img_planes, width, height)
        return resolution
Exemple #14
0
def create_new_camera():
    """
    Create a new camera nodes and object.

    :returns: Camera object.
    :rtype: Camera
    """
    name = 'camera'
    cam_tfm = maya.cmds.createNode('transform', name=name)
    cam_tfm = node_utils.get_long_name(cam_tfm)
    cam_shp = maya.cmds.createNode('camera',
                                   name=name + 'Shape',
                                   parent=cam_tfm)
    cam_shp = node_utils.get_long_name(cam_shp)
    cam = mmapi.Camera(transform=cam_tfm, shape=cam_shp)
    return cam
Exemple #15
0
    def create_node(self, name='bundle1', colour=None):
        """
        Create a Bundle.

        :param name: The name of the newly created Bundle.
        :type name: str

        :param colour: Colour of bundle as R, G and B.
                       'None' will leave as default.
        :type colour: (float, float, float) or None

        :return: Bundle object attached to newly created node.
        :rtype: Bundle
        """
        assert isinstance(name, (str, unicode))
        if colour is not None:
            assert isinstance(colour, (tuple, list))
            assert len(colour) == 3

        # Transform
        tfm = maya.cmds.createNode(const.BUNDLE_TRANSFORM_NODE_TYPE, name=name)
        tfm = node_utils.get_long_name(tfm)
        maya.cmds.setAttr(tfm + '.rx', lock=True)
        maya.cmds.setAttr(tfm + '.ry', lock=True)
        maya.cmds.setAttr(tfm + '.rz', lock=True)
        maya.cmds.setAttr(tfm + '.sx', lock=True)
        maya.cmds.setAttr(tfm + '.sy', lock=True)
        maya.cmds.setAttr(tfm + '.sz', lock=True)
        maya.cmds.setAttr(tfm + '.shxy', lock=True)
        maya.cmds.setAttr(tfm + '.shxz', lock=True)
        maya.cmds.setAttr(tfm + '.shyz', lock=True)
        maya.cmds.setAttr(tfm + '.rx', keyable=False)
        maya.cmds.setAttr(tfm + '.ry', keyable=False)
        maya.cmds.setAttr(tfm + '.rz', keyable=False)
        maya.cmds.setAttr(tfm + '.sx', keyable=False)
        maya.cmds.setAttr(tfm + '.sy', keyable=False)
        maya.cmds.setAttr(tfm + '.sz', keyable=False)
        maya.cmds.setAttr(tfm + '.shxy', keyable=False)
        maya.cmds.setAttr(tfm + '.shxz', keyable=False)
        maya.cmds.setAttr(tfm + '.shyz', keyable=False)

        # Shape Node
        shp_name = tfm.rpartition('|')[-1] + 'Shape'
        shp = maya.cmds.createNode(const.BUNDLE_SHAPE_NODE_TYPE,
                                   name=shp_name,
                                   parent=tfm)
        maya.cmds.setAttr(shp + '.localScaleX', 0.1)
        maya.cmds.setAttr(shp + '.localScaleY', 0.1)
        maya.cmds.setAttr(shp + '.localScaleZ', 0.1)

        self.set_node(tfm)

        # Set Colour (default is green)
        if colour is not None:
            self.set_colour_rgb(colour)
        else:
            green = (0.0, 1.0, 0.0)
            self.set_colour_rgb(green)
        return self
Exemple #16
0
 def get_node(self, full_path=True):
     node = None
     if self._dependFn is not None:
         try:
             node = self._dependFn.name()
         except RuntimeError:
             pass
     if node is not None and full_path is True:
         node = node_utils.get_long_name(node)
     return node
    def test_init(self):
        # TODO: More exhaustive test.
        node = maya.cmds.createNode('transform')
        node = node_utils.get_long_name(node)
        x = attribute.Attribute(node=node, attr='translateX')
        y = attribute.Attribute(node=node, attr='ty')
        self.assertEqual(x.get_node(), node)
        self.assertEqual(x.get_attr(), 'translateX')
        self.assertEqual(x.get_name(), '|transform1.translateX')

        self.assertEqual(y.get_attr(), 'translateY')
        self.assertEqual(y.get_name(), '|transform1.translateY')
    def test_find_plugs_affecting_transform_hierachy(self):
        """
        Simple transform hierachy test.
        """
        cam_tfm, cam_shp = self.create_camera('myCamera')

        top_node = maya.cmds.createNode(
            'transform', name='top_node')
        top_node = node_utils.get_long_name(top_node)

        bot_node = maya.cmds.createNode(
            'transform', name='bottom_node', parent=top_node)
        bot_node = node_utils.get_long_name(bot_node)

        # Plugs should only be the 'top' node.
        top_plugs = nodeaffects.find_plugs_affecting_transform(top_node, None)
        nodes = self.get_node_names_from_plugs(top_plugs)
        self.assertIn(top_node, nodes)
        self.assertEqual(len(nodes), 1)

        # Plugs should contain both the 'bottom' and 'top' nodes,
        # because 'bottom' has a parent node.
        bot_plugs = nodeaffects.find_plugs_affecting_transform(bot_node, None)
        nodes = self.get_node_names_from_plugs(bot_plugs)
        self.assertIn(top_node, nodes)
        self.assertIn(bot_node, nodes)
        self.assertEqual(len(nodes), 2)

        # The same as 'bottom' nodes, but will contain the camera
        # hierachy too.
        with_cam_plugs = nodeaffects.find_plugs_affecting_transform(bot_node, cam_tfm)
        nodes = self.get_node_names_from_plugs(with_cam_plugs)
        self.assertIn(top_node, nodes)
        self.assertIn(bot_node, nodes)
        self.assertIn(cam_tfm, nodes)
        self.assertIn(cam_shp, nodes)
        self.assertEqual(len(nodes), 4)
        return
Exemple #19
0
 def get_node(self, full_path=True):
     node = None
     node_uuid = None
     if self._dependFn is not None:
         try:
             node_uuid = self._dependFn.uuid().asString()
         except RuntimeError:
             pass
     if node_uuid is not None:
         if full_path is True:
             node = node_utils.get_long_name(node_uuid)
         else:
             nodes = maya.cmds.ls(node_uuid) or []
             if len(nodes) > 0:
                 node = nodes[0]
     assert node is None or isinstance(node, basestring)
     return node
def _get_upstream_nodes(node_name):
    node_types = maya.cmds.nodeType(node_name, inherited=True)
    out_nodes = []
    if 'dagNode' in node_types:
        # DAG upstream
        out_nodes = maya.cmds.listConnections(node_name,
                                              source=True,
                                              destination=False,
                                              plugs=False,
                                              shapes=False,
                                              connections=False,
                                              skipConversionNodes=False) or []
    else:
        # DG upstream
        out_nodes = maya.cmds.listHistory(node_name,
                                          allConnections=True,
                                          leaf=False,
                                          levels=0,
                                          pruneDagObjects=False) or []
    out_nodes = [str(node_utils.get_long_name(n)) for n in out_nodes]
    return out_nodes
    def test_get_state(self):
        node = maya.cmds.createNode('transform')
        node = node_utils.get_long_name(node)

        # Animated
        maya.cmds.setKeyframe(node, attribute='rotateY', time=1, value=-1.0)
        maya.cmds.setKeyframe(node, attribute='rotateY', time=10, value=1.0)

        # Locked
        maya.cmds.setAttr(node + '.rotateZ', lock=True)

        # Connected
        multdiv = maya.cmds.createNode('multiplyDivide')
        maya.cmds.connectAttr(multdiv + '.outputX', node + '.translateX')

        # Create the Attribute objects
        tx = attribute.Attribute(node=node, attr='translateX')
        rx = attribute.Attribute(node=node, attr='rotateX')
        ry = attribute.Attribute(node=node, attr='rotateY')
        rz = attribute.Attribute(node=node, attr='rotateZ')

        tx_state = tx.get_state()
        rx_state = rx.get_state()
        ry_state = ry.get_state()
        rz_state = rz.get_state()

        # test returned states.
        self.assertEqual(tx_state, const.ATTR_STATE_LOCKED)
        self.assertEqual(rx_state, const.ATTR_STATE_STATIC)
        self.assertEqual(ry_state, const.ATTR_STATE_ANIMATED)
        self.assertEqual(rz_state, const.ATTR_STATE_LOCKED)

        # test 'is_*' functions.
        self.assertEqual(tx.is_locked(), True)
        self.assertEqual(rx.is_static(), True)
        self.assertEqual(ry.is_animated(), True)
        self.assertEqual(rz.is_locked(), True)
Exemple #22
0
def create_motion_trail_setup(node_tfm, trail_handle_grp, name,
                              use_frame_range, start_frame, end_frame,
                              pre_frame, post_frame, increment, update_mode):
    handle_tfm, trail_shp = maya.cmds.snapshot(
        node_tfm,
        name=name,
        constructionHistory=True,
        motionTrail=True,
        startTime=start_frame,
        endTime=end_frame,
        increment=increment,
        update=update_mode,
    )
    handle_shp = maya.cmds.listRelatives(
        handle_tfm,
        shapes=True,
        fullPath=True,
        type='motionTrailShape',
    )[0]

    # Create attributes on trail node, hidden to the channel box.
    _create_trail_frame_attrs(trail_shp,
                              use_frame_range,
                              start_frame,
                              end_frame,
                              pre_frame,
                              post_frame,
                              increment,
                              keyable=False)

    # Create attributes on handle transform node, visible to the
    # channel box.
    _create_trail_frame_attrs(handle_tfm,
                              use_frame_range,
                              start_frame,
                              end_frame,
                              pre_frame,
                              post_frame,
                              increment,
                              keyable=True)

    # Drive trail_shp by attributes on handle_tfm
    attr_names = [
        'useFrameRange',
        'preFrame',
        'postFrame',
        'frameRangeStart',
        'frameRangeEnd',
        'increment',
    ]
    for attr_name in attr_names:
        src = handle_tfm + '.' + attr_name
        dst = trail_shp + '.' + attr_name
        maya.cmds.connectAttr(src, dst)

    # Re-parent to the camera, so the user can see it.
    handle_tfm = maya.cmds.parent(handle_tfm, trail_handle_grp,
                                  relative=True)[0]
    handle_tfm = node_utils.get_long_name(handle_tfm)

    # Create Expression to control the calculated trail frame range.
    cmd = ('if (useFrameRange) {'
           '    startTime = frameRangeStart;'
           '    endTime = frameRangeEnd;'
           '} else {'
           '    startTime = frame - preFrame;'
           '    endTime = frame + postFrame;'
           '}')
    maya.cmds.expression(object=trail_shp, string=cmd)
    return handle_tfm, handle_shp, trail_shp
Exemple #23
0
def create(nodes, current_frame=None, eval_mode=None):
    """
    Create a Controller for the given nodes.

    :param nodes: The nodes to create Controller for.
    :type nodes: [str, ..]

    :param current_frame: What frame number is considered to be
                          'current' when evaluating transforms without
                          any keyframes.
    :type current_frame: float or int

    :param eval_mode: What type of transform evaluation method to use?
    :type eval_mode: mmSolver.utils.constant.EVAL_MODE_*

    :returns: List of controller transform nodes.
    :rtype: [str, ..]
    """
    if current_frame is None:
        current_frame = maya.cmds.currentTime(query=True)
    assert current_frame is not None
    sparse = False

    tfm_nodes = [tfm_utils.TransformNode(node=n) for n in nodes]

    # Force into long-names.
    nodes = [n.get_node() for n in tfm_nodes]

    # Ensure node attributes are editable.
    keyable_attrs = set()
    for node in nodes:
        keyable_attrs |= _get_keyable_attrs(node, const.TFM_ATTRS)

    # Query keyframe times on each node attribute
    start_frame, end_frame = time_utils.get_maya_timeline_range_outer()
    keytime_obj = keytime_utils.KeyframeTimes()
    for node in nodes:
        keytime_obj.add_node_attrs(node, const.TFM_ATTRS, start_frame,
                                   end_frame)
    fallback_frame_range = keytime_obj.sum_frame_range_for_nodes(nodes)
    fallback_times = list(
        range(fallback_frame_range[0], fallback_frame_range[1] + 1))
    key_times_map = time_utils.get_keyframe_times_for_node_attrs(
        nodes, const.TFM_ATTRS)

    # Query the transform matrix for the nodes
    cache = tfm_utils.TransformMatrixCache()
    for tfm_node in tfm_nodes:
        node = tfm_node.get_node()
        times = key_times_map.get(node, [current_frame])
        cache.add_node(tfm_node, times)
    cache.process(eval_mode=eval_mode)

    depth_to_tfm_node_map = _sort_hierarchy_depth_to_tfm_nodes(tfm_nodes)
    nodes_parent = _get_node_parent_map(nodes)

    # Create new (locator) node for each input node
    ctrl_list = []
    node_to_ctrl_map = {}
    node_to_ctrl_tfm_map = {}
    depths = sorted(depth_to_tfm_node_map.keys())
    for depth in depths:
        depth_tfm_nodes = depth_to_tfm_node_map.get(depth)
        assert depth_tfm_nodes is not None
        sorted_tfm_nodes = sorted(depth_tfm_nodes, key=lambda x: x.get_node())
        for tfm_node in sorted_tfm_nodes:
            node = tfm_node.get_node()
            node_parent = nodes_parent.get(node)
            if node_parent is not None:
                node_parent = node_to_ctrl_map.get(node_parent)
            name = node.rpartition('|')[-1]
            assert '|' not in name
            name = name.replace(':', '_')
            name = name + '_CTRL'
            name = mmapi.find_valid_maya_node_name(name)
            tfm = maya.cmds.createNode('transform',
                                       name=name,
                                       parent=node_parent)
            tfm = node_utils.get_long_name(tfm)
            shape_name = name + 'Shape'
            maya.cmds.createNode('locator', name=shape_name, parent=tfm)
            rot_order = maya.cmds.xform(node, query=True, rotateOrder=True)
            maya.cmds.xform(tfm, rotateOrder=rot_order, preserve=True)
            ctrl_tfm = tfm_utils.TransformNode(node=tfm)
            ctrl_list.append(tfm)
            node_to_ctrl_map[node] = tfm
            node_to_ctrl_tfm_map[node] = ctrl_tfm

    # Set transform matrix on new node
    anim_curves = []
    for src in tfm_nodes:
        src_node = src.get_node()
        src_times = key_times_map.get(src_node, [current_frame])
        dst = node_to_ctrl_tfm_map.get(src_node)
        assert len(src_times) > 0
        tfm_utils.set_transform_values(cache,
                                       src_times,
                                       src,
                                       dst,
                                       delete_static_anim_curves=False,
                                       eval_mode=eval_mode)
        src_had_keys = key_times_map.get(src_node) is not None
        if src_had_keys is True:
            continue
        # Maintain that destination node will not have keyframes now, the
        # same as the source node.
        dst_node = dst.get_node()
        keyable_attrs = _get_keyable_attrs(dst_node, const.TFM_ATTRS)
        anim_curves += anim_utils.get_anim_curves_from_nodes(
            list(keyable_attrs), )
    anim_curves = [
        n for n in anim_curves if node_utils.node_is_referenced(n) is False
    ]
    if len(anim_curves) > 0:
        maya.cmds.delete(anim_curves)

    # Delete all keyframes on controlled nodes
    keyable_attrs = set()
    for tfm_node in tfm_nodes:
        node = tfm_node.get_node()
        keyable_attrs |= _get_keyable_attrs(node, const.TFM_ATTRS)
    anim_curves = anim_utils.get_anim_curves_from_nodes(list(keyable_attrs), )
    anim_curves = [
        n for n in anim_curves if node_utils.node_is_referenced(n) is False
    ]
    if len(anim_curves) > 0:
        maya.cmds.delete(anim_curves)

    # Create constraint(s) to previous nodes.
    for tfm_node in tfm_nodes:
        src_node = tfm_node.get_node()
        ctrl = node_to_ctrl_tfm_map.get(src_node)
        dst_node = ctrl.get_node()
        _create_constraint(src_node, dst_node)
    return ctrl_list
    def test_get_all_members(self):
        x = sethelper.SetHelper().create_node('mySet')
        x.get_all_members()

        node1 = maya.cmds.createNode('transform', name='myParent')
        node1 = node_utils.get_long_name(node1)

        node2 = maya.cmds.createNode('transform', name='myChild', parent=node1)
        node2 = node_utils.get_long_name(node2)

        node3 = maya.cmds.createNode('transform', name='myChild')
        node3 = node_utils.get_long_name(node3)

        node4 = maya.cmds.createNode('multiplyDivide', name='myMathsNode')
        node4 = node_utils.get_long_name(node4)

        node_attr1 = node1 + '.tx'  # short attribute name
        node_attr2 = node2 + '.rotateY'
        node_attr3 = node3 + '.rotateX'
        node_attr4 = node4 + '.operation'

        x.add_member(node1)
        x.add_member(node2)
        x.add_member(node3)
        x.add_member(node4)
        x.add_member(node_attr1)
        x.add_member(node_attr2)
        x.add_member(node_attr3)
        x.add_member(node_attr4)

        # Get the full path node names
        members = x.get_all_members(full_path=True)
        self.assertEqual(len(members), 8)
        self.assertIn(node1, members)
        self.assertIn(node2, members)
        self.assertIn(node3, members)
        self.assertIn(node4, members)
        node_attr1 = node1 + '.translateX'  # full attribute name
        self.assertIn(node_attr1, members)
        self.assertIn(node_attr2, members)
        self.assertIn(node_attr3, members)
        self.assertIn(node_attr4, members)

        # Make sure short-names are handled too
        node1 = 'myParent'
        node2 = 'myParent|myChild'
        node3 = '|myChild'
        node4 = 'myMathsNode'
        node_attr1 = node1 + '.translateX'
        node_attr2 = node2 + '.rotateY'
        node_attr3 = node3 + '.rotateX'
        node_attr4 = node4 + '.operation'
        members = x.get_all_members(full_path=False)
        self.assertEqual(len(members), 8)
        self.assertIn(node1, members)
        self.assertIn(node2, members)
        self.assertIn(node3, members)
        self.assertIn(node4, members)
        self.assertIn(node_attr1, members)
        self.assertIn(node_attr2, members)
        self.assertIn(node_attr3, members)
        self.assertIn(node_attr4, members)
        x.clear_all_members()

        # Test 'flatten' argument, with nested sets.
        y = sethelper.SetHelper().create_node('myNestedSet')
        y.add_member(node2)
        y.add_member(node3)
        y.add_member(node_attr4)
        x.add_member(node1)
        x.add_member(y.get_node())

        # Query the results.
        x_members_flat = x.get_all_members(flatten=True)
        y_members_flat = y.get_all_members(flatten=True)
        x_members = x.get_all_members(flatten=False)
        y_members = y.get_all_members(flatten=False)
        self.assertEqual(len(x_members_flat), 4)
        self.assertEqual(len(x_members), 2)
        self.assertEqual(len(y_members_flat), 3)
        self.assertEqual(len(y_members), 3)
        self.assertEqual(y_members, y_members_flat)
    def create_node(self, name='bundle1', colour=None):
        """
        Create a Bundle.

        :param name: The name of the newly created Bundle.
        :type name: str

        :param colour: Colour of bundle as R, G and B.
                       'None' will leave as default.
        :type colour: (float, float, float) or None

        :return: Bundle object attached to newly created node.
        :rtype: Bundle
        """
        assert isinstance(name, pycompat.TEXT_TYPE)
        if colour is not None:
            assert isinstance(colour, (tuple, list))
            assert len(colour) == 3

        # Transform
        tfm = maya.cmds.createNode(const.BUNDLE_TRANSFORM_NODE_TYPE, name=name)
        tfm = node_utils.get_long_name(tfm)

        # Show the bundle transform attributes in the channel box, but
        # the attributes are locked, so this is compatible with
        # versions before v0.3.15.
        lock_rot_scale = True
        display_rot_scale = True
        lock_shear = True
        display_shear = False
        _lock_and_display_bundle_attributes(
            tfm,
            lock_translate=False,
            lock_rotate=lock_rot_scale,
            lock_scale=lock_rot_scale,
            lock_shear=lock_shear,
            display_translate=True,
            display_rotate=display_rot_scale,
            display_scale=display_rot_scale,
            display_shear=display_shear,
        )

        # Shape Node
        shp_name = tfm.rpartition('|')[-1] + 'Shape'
        shp = maya.cmds.createNode(const.BUNDLE_SHAPE_NODE_TYPE,
                                   name=shp_name,
                                   parent=tfm)
        maya.cmds.setAttr(shp + '.localScaleX', 0.1)
        maya.cmds.setAttr(shp + '.localScaleY', 0.1)
        maya.cmds.setAttr(shp + '.localScaleZ', 0.1)

        self.set_node(tfm)

        # Set Colour (default is green)
        if colour is not None:
            self.set_colour_rgb(colour)
        else:
            green = (0.0, 1.0, 0.0)
            self.set_colour_rgb(green)

        event_utils.trigger_event(const.EVENT_NAME_BUNDLE_CREATED, bnd=self)
        return self
Exemple #26
0
    def create_node(self,
                    name='marker1',
                    colour=None,
                    cam=None,
                    mkr_grp=None,
                    bnd=None):
        """
        Create a marker node network from scratch.

        :param name: Name of the marker to create.
        :type name: str

        :param colour: Colour of marker as R, G and B.
                       'None' will leave as default.
        :type colour: (float, float, float) or None

        :param cam: The camera to create the marker underneath.
        :type cam: Camera

        :param mkr_grp: The marker group to create the marker underneath.
        :type mkr_grp: MarkerGroup

        :param bnd: The bundle to attach to the newly created marker.
        :type bnd: Bundle

        :return: Marker object with newly created node.
        :rtype: Marker
        """
        assert isinstance(name, (str, unicode))
        if cam is not None:
            if mkr_grp is not None:
                msg = 'Cannot specify both camera and marker group, '
                msg += 'please choose only one.'
                raise excep.NotValid(msg)
            assert isinstance(cam, camera.Camera)
        if mkr_grp is not None:
            if cam is not None:
                msg = 'Cannot specify both camera and marker group, '
                msg += 'please choose only one.'
                raise excep.NotValid(msg)
            assert isinstance(mkr_grp, markergroup.MarkerGroup)
        if bnd is not None:
            assert isinstance(bnd, mmSolver._api.bundle.Bundle)
        if colour is not None:
            assert isinstance(colour, (tuple, list))
            assert len(colour) == 3

        # Transform
        tfm = maya.cmds.createNode(const.MARKER_TRANSFORM_NODE_TYPE, name=name)
        tfm = node_utils.get_long_name(tfm)
        maya.cmds.setAttr(tfm + '.tz', -1.0)
        maya.cmds.setAttr(tfm + '.tz', lock=True)
        maya.cmds.setAttr(tfm + '.rx', lock=True)
        maya.cmds.setAttr(tfm + '.ry', lock=True)
        maya.cmds.setAttr(tfm + '.rz', lock=True)
        maya.cmds.setAttr(tfm + '.sx', lock=True)
        maya.cmds.setAttr(tfm + '.sy', lock=True)
        maya.cmds.setAttr(tfm + '.sz', lock=True)
        maya.cmds.setAttr(tfm + '.shxy', lock=True)
        maya.cmds.setAttr(tfm + '.shxz', lock=True)
        maya.cmds.setAttr(tfm + '.shyz', lock=True)
        maya.cmds.setAttr(tfm + '.tz', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.rx', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.ry', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.rz', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.sx', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.sy', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.sz', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.shxy', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.shxz', keyable=False, channelBox=False)
        maya.cmds.setAttr(tfm + '.shyz', keyable=False, channelBox=False)

        # Shape Node
        shp_name = tfm.rpartition('|')[-1] + 'Shape'
        shp = maya.cmds.createNode(const.MARKER_SHAPE_NODE_TYPE,
                                   name=shp_name,
                                   parent=tfm)
        maya.cmds.setAttr(shp + '.localScaleX', 0.01)
        maya.cmds.setAttr(shp + '.localScaleY', 0.01)
        maya.cmds.setAttr(shp + '.localScaleZ', 0.0)
        maya.cmds.setAttr(shp + '.localScaleZ', lock=True)

        # Add attrs
        _create_marker_attributes(tfm)

        src = '{0}.{1}'.format(tfm, const.MARKER_ATTR_LONG_NAME_ENABLE)
        dst = '{0}.{1}'.format(tfm, 'lodVisibility')
        maya.cmds.connectAttr(src, dst)

        self.set_node(tfm)

        # Set Colour (default is red)
        if colour is not None:
            self.set_colour_rgb(colour)
        else:
            red = (1.0, 0.0, 0.0)
            self.set_colour_rgb(red)

        # Link to Camera
        if cam is not None:
            self.set_camera(cam)

        # Link to MarkerGroup
        if mkr_grp is not None:
            self.set_marker_group(mkr_grp)

        # Link to Bundle
        if bnd is not None:
            self.set_bundle(bnd)

        return self
Exemple #27
0
    def create_node(self, name='markerGroup1', cam=None):
        """
        Create a MarkerGroup node network.

        :param name: The newly created node's name.
        :type name: str

        :param cam: The camera to attach to.
        :type cam: Camera

        :return: MarkerGroup object with node set as newly created node.
        """
        assert isinstance(name, (str, unicode))
        assert isinstance(cam, camera.Camera)

        cam_tfm = cam.get_transform_node()
        cam_shp = cam.get_shape_node()

        mkr_grp = maya.cmds.createNode('mmMarkerGroupTransform',
                                       name=name,
                                       parent=cam_tfm)
        mkr_grp = node_utils.get_long_name(mkr_grp)
        mkr_scl = maya.cmds.createNode('mmMarkerScale')
        self.set_node(mkr_grp)

        # Add attr and connect depth
        maya.cmds.addAttr(mkr_grp,
                          longName='depth',
                          at='double',
                          minValue=0.0,
                          defaultValue=1.0)
        maya.cmds.setAttr(mkr_grp + '.depth', keyable=True)
        maya.cmds.setAttr(mkr_grp + '.depth', 10.0)
        maya.cmds.connectAttr(mkr_grp + '.depth', mkr_scl + '.depth')

        # Add attr and connect overscan
        maya.cmds.addAttr(mkr_grp,
                          longName='overscan',
                          at='double',
                          minValue=0.0,
                          defaultValue=1.0)
        maya.cmds.setAttr(mkr_grp + '.overscan', keyable=True)
        maya.cmds.connectAttr(mkr_grp + '.overscan',
                              mkr_scl + '.overscanInverse')

        # Connect camera attributes
        maya.cmds.connectAttr(cam_shp + '.focalLength',
                              mkr_scl + '.focalLength')
        maya.cmds.connectAttr(cam_shp + '.cameraAperture',
                              mkr_scl + '.cameraAperture')
        maya.cmds.connectAttr(cam_shp + '.filmOffset', mkr_scl + '.filmOffset')

        # Connect marker scale to marker group
        maya.cmds.connectAttr(mkr_scl + '.outScale', mkr_grp + '.scale')
        maya.cmds.connectAttr(mkr_scl + '.outTranslate',
                              mkr_grp + '.translate')

        # Lock and hide all the attributes
        attrs = [
            'tx', 'ty', 'tz', 'rx', 'ry', 'rz', 'sx', 'sy', 'sz', 'shearXY',
            'shearXZ', 'shearYZ', 'rotateOrder', 'rotateAxisX', 'rotateAxisY',
            'rotateAxisZ'
        ]
        for attr in attrs:
            maya.cmds.setAttr(mkr_grp + '.' + attr, lock=True)
            maya.cmds.setAttr(mkr_grp + '.' + attr,
                              keyable=False,
                              channelBox=False)

        return self