def test_get_camera_non_standard_transform_node_type(self):
        """
        Test getting a camera from a single half, transform or shape node.

        This function also tests non-standard transform node types.
        In this example we use 'mmMarkerGroupTransform' for the test.

        GitHub issue #123.
        """
        # Custom transform node type. GitHub issue #123.
        custom_node_type = 'mmMarkerGroupTransform'
        cam_tfm, cam_shp = self.create_camera(
            'myCamera', tfm_node_type=custom_node_type)

        # Must be a transform type.
        inherited_types = maya.cmds.nodeType(cam_tfm, inherited=True) or []
        self.assertIn('transform', inherited_types)

        # Get from transform node.
        x, y = camera_utils.get_camera(cam_tfm)
        self.assertEqual(cam_tfm, x)
        self.assertEqual(cam_shp, y)

        # Get from shape node.
        x, y = camera_utils.get_camera(cam_shp)
        self.assertEqual(cam_tfm, x)
        self.assertEqual(cam_shp, y)
        return
Exemple #2
0
def _find_camera_from_selection(sel):
    cam = None
    filtered_nodes = mmapi.filter_nodes_into_categories(sel)
    cam_nodes = filtered_nodes['camera']
    mkr_grp_nodes = filtered_nodes['markergroup']
    mkr_nodes = filtered_nodes['marker']

    # Check selected camera.
    if len(cam_nodes) > 0:
        cam_node = cam_nodes[0]
        cam_tfm, cam_shp = camera_utils.get_camera(cam_node)
        if cam_tfm is not None and cam_shp is not None:
            cam = mmapi.Camera(transform=cam_tfm, shape=cam_shp)
    if cam is not None and cam.is_valid():
        return cam

    # Check selected marker group.
    if len(mkr_grp_nodes) > 0:
        mkr_grp_node = mkr_grp_nodes[0]
        mkr_grp = mmapi.MarkerGroup(node=mkr_grp_node)
        cam = mkr_grp.get_camera()
    if cam is not None and cam.is_valid():
        return cam

    # Check selected marker.
    if len(mkr_nodes) > 0:
        mkr_node = mkr_nodes[0]
        mkr = mmapi.Marker(node=mkr_node)
        cam = mkr.get_camera()
    if cam is not None and cam.is_valid():
        return cam
    return None
    def test_get_camera(self):
        """
        Test getting a camera from a single half, transform or shape node.
        """
        cam_tfm, cam_shp = self.create_camera('myCamera')

        # Get from transform node.
        x, y = camera_utils.get_camera(cam_tfm)
        self.assertEqual(cam_tfm, x)
        self.assertEqual(cam_shp, y)

        # Get from shape node.
        x, y = camera_utils.get_camera(cam_shp)
        self.assertEqual(cam_tfm, x)
        self.assertEqual(cam_shp, y)
        return
def _get_active_or_selected_camera(cam_node_list):
    active_cam_tfm = None
    active_cam_shp = None
    if len(cam_node_list) > 0:
        cam_node = cam_node_list[0]
        active_cam_tfm, active_cam_shp = camera_utils.get_camera(cam_node)
    else:
        model_editor = viewport_utils.get_active_model_editor()
        if model_editor is not None:
            active_cam_tfm, active_cam_shp = viewport_utils.get_viewport_camera(
                model_editor)
    return active_cam_tfm, active_cam_shp
Exemple #5
0
def get_viewport_camera(model_editor):
    """
    Get the camera from the model editor.

    :param model_editor: The viewport model editor to get the camera from.
    :type model_editor: str

    :returns: Camera transform and camera shape nodes.
    :rtype: (str, str) or (None, None)
    """
    if model_editor is None:
        msg = 'Cannot get camera, Model Editor is invalid! model_editor=%r'
        LOG.warning(msg, model_editor)
    cam = maya.cmds.modelEditor(model_editor, query=True, camera=True)
    cam_tfm, cam_shp = camera_utils.get_camera(cam)
    return cam_tfm, cam_shp
def main():
    # Get selected camera node.
    sel = maya.cmds.ls(selection=True, long=True, type='transform') or []
    if len(sel) == 0:
        sel += maya.cmds.ls(selection=True, long=True, type='camera') or []
    if len(sel) != 1:
        LOG.warn('Please select one camera: selection=%r', sel)
        return
    cam_tfm, cam_shp = camera_utils.get_camera(sel[0])
    if cam_tfm is None or cam_shp is None:
        LOG.warn('Selected node is not a camera, please select one camera.')
        return

    # Get the connected image planes
    # If more than one image plane exists, print a warning.
    img_pl_shps = camera_utils.get_image_plane_shapes_from_camera(
        cam_tfm, cam_shp)
    img_pl_shp = None
    if len(img_pl_shps) == 0:
        msg = 'No image plane nodes found.'
        LOG.warn(msg)
    elif len(img_pl_shps) > 1:
        img_pl_shp = img_pl_shps[0]
        msg = 'More than one image plane was found, using first image plane.'
        LOG.warn(msg)
    else:
        # Exactly one image plane node.
        img_pl_shp = img_pl_shps[0]

    # Query plate data from the image plane
    test_disk = const.TEST_DISK
    frame_range = time_utils.get_maya_timeline_range_inner()
    plate_data = {}
    if img_pl_shp is not None:
        plate_data = lib.query_plate_data(cam_tfm, cam_shp, img_pl_shp,
                                          test_disk)
        # Calculate the frame range.
        image_file_path = plate_data.get('file_path')
        if image_file_path is not None:
            frame_range = lib.get_frame_range_from_file_pattern(
                image_file_path, fallback_range=frame_range)
    assert isinstance(frame_range, (time_utils.FrameRange, tuple))
    frames = list(range(frame_range.start, frame_range.end + 1))
    assert len(frames) > 0

    # Node must be transform and have a camera shape node to be valid.
    rotate_order = const.ROTATE_ORDER

    with tools_utils.tool_context(use_undo_chunk=False,
                                  use_dg_evaluation_mode=True,
                                  disable_viewport=True):
        cam_data = lib.query_camera_data(cam_tfm, cam_shp, frames,
                                         rotate_order, test_disk)

    # Generate file contents.
    data_str = lib.generate(cam_data, plate_data, frame_range)

    # Write out file.
    file_path = lib.write_temp_file(data_str)
    if not os.path.isfile(file_path):
        msg = 'Failed to write temp file. path=%r'
        LOG.error(msg, file_path)
        return
    msg = 'Successfully wrote to temporary file. path=%r'
    LOG.info(msg, file_path)

    # Set Copy/Paste Clipboard buffer.
    try:
        clippy = QtGui.QClipboard()
        clippy.setText(file_path)
    except Exception as e:
        msg = 'Could not set file path on clipboard. path=%r'
        LOG.warn(msg, file_path)
        LOG.info(str(e))
    return file_path
Exemple #7
0
def create_screen_space_motion_trail(cam,
                                     tfm,
                                     name=None,
                                     use_frame_range=None,
                                     pre_frame=None,
                                     post_frame=None,
                                     start_frame=None,
                                     end_frame=None,
                                     increment=None):
    """
    Create a Screen-Space Maya Locator that may be solved in Screen XYZ.
    """
    assert isinstance(cam, pycompat.TEXT_TYPE)
    assert isinstance(tfm, pycompat.TEXT_TYPE)
    if name is None:
        name = tfm.rpartition('|')[-1]

    frame_range = time_utils.get_maya_timeline_range_inner()
    if use_frame_range is None:
        use_frame_range = const.USE_FRAME_RANGE_DEFAULT
    if pre_frame is None:
        pre_frame = const.PRE_FRAME_DEFAULT
    if post_frame is None:
        post_frame = const.POST_FRAME_DEFAULT
    if start_frame is None:
        start_frame = frame_range.start
    if end_frame is None:
        end_frame = frame_range.end
    if increment is None:
        if use_frame_range:
            increment = const.FRAME_RANGE_INCREMENT_DEFAULT
        else:
            increment = const.PER_FRAME_INCREMENT_DEFAULT

    tfm_attrs = [
        'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY',
        'rotateZ', 'scaleX', 'scaleY', 'scaleZ'
    ]

    maya.cmds.loadPlugin('matrixNodes', quiet=True)
    cam_tfm, cam_shp = camera_utils.get_camera(cam)

    # Create temporary group
    temp_grp_name = const.TEMP_OBJECT_NAME
    temp_grp = temp_grp_name
    if not maya.cmds.objExists(temp_grp):
        temp_grp = maya.cmds.createNode('transform', name=temp_grp_name)
        maya.cmds.setAttr(temp_grp + '.visibility', False)

    # Create Temporary transform node to calculate motion path on.
    temp_tfm_name = name + '_TEMP_NULL'
    temp_tfm_name = mmapi.find_valid_maya_node_name(temp_tfm_name)
    temp_tfm = maya.cmds.createNode('transform',
                                    parent=temp_grp,
                                    name=temp_tfm_name)

    # Create trail group under the camera.
    trail_handle_grp_name = const.MOTION_PATH_GROUP_NAME
    trail_handle_grp = cam_tfm + '|' + trail_handle_grp_name
    if not maya.cmds.objExists(trail_handle_grp):
        trail_handle_grp = maya.cmds.createNode('transform',
                                                name=trail_handle_grp_name,
                                                parent=cam_tfm)
        # Trails are non-selectable by default.
        plug_name = trail_handle_grp + '.template'
        maya.cmds.setAttr(plug_name, True)

        # Lock transform attributes.
        for attr in tfm_attrs:
            plug_name = trail_handle_grp + '.' + attr
            maya.cmds.setAttr(plug_name, lock=True)

    # Matrix Multiply
    mult_mat = maya.cmds.createNode('multMatrix')
    src = tfm + '.worldMatrix[0]'
    dst = mult_mat + '.matrixIn[0]'
    maya.cmds.connectAttr(src, dst)

    src = cam_tfm + '.worldInverseMatrix[0]'
    dst = mult_mat + '.matrixIn[1]'
    maya.cmds.connectAttr(src, dst)

    # Decompose Matrix
    decompose = maya.cmds.createNode('decomposeMatrix')
    src = mult_mat + '.matrixSum'
    dst = decompose + '.inputMatrix'
    maya.cmds.connectAttr(src, dst)

    src = decompose + '.outputTranslate'
    dst = temp_tfm + '.translate'
    maya.cmds.connectAttr(src, dst)

    # Lock the temporary transform node.
    for attr in tfm_attrs:
        plug_name = temp_tfm + '.' + attr
        maya.cmds.setAttr(plug_name, lock=True)

    # Create Motion Trail
    update_mode = 'always'
    handle_tfm, handle_shp, trail_shp = create_motion_trail_setup(
        temp_tfm,
        trail_handle_grp,
        name,
        use_frame_range,
        start_frame,
        end_frame,
        pre_frame,
        post_frame,
        increment,
        update_mode,
    )
    return handle_tfm, handle_shp, trail_shp