Ejemplo n.º 1
0
def get_frame_range_from_file_pattern(file_path_pattern, fallback_range=None):
    """
    Given a file pattern with '#' characters representing the frame
    number, get the full frame range on disk.

    :returns: FrameRange
    :rtype: FrameRange or (int, int)
    """
    assert isinstance(file_path_pattern, (str, unicode, basestring))
    assert len(file_path_pattern) > 0
    assert isinstance(fallback_range, (time_utils.FrameRange))
    start = 99999999
    end = -99999999
    glob_pattern = file_path_pattern.replace('#', '?')
    for path in glob.iglob(glob_pattern):
        token_data = get_image_path_tokens(path)
        if token_data is None:
            continue
        frame = int(token_data.get('frame'))
        start = min(start, frame)
        end = max(end, frame)
    frame_range = time_utils.FrameRange(start, end)
    if start > end:
        # frame_range = time_utils.FrameRange(None, None)
        frame_range = fallback_range
    return frame_range
Ejemplo n.º 2
0
def get_bake_frame_range(frame_range_mode, custom_start_frame,
                         custom_end_frame):
    assert isinstance(frame_range_mode, basestring)
    assert frame_range_mode in const.FRAME_RANGE_MODE_VALUES
    assert isinstance(custom_start_frame, (int, long))
    assert isinstance(custom_end_frame, (int, long))
    frame_range = None
    if frame_range_mode == const.FRAME_RANGE_MODE_TIMELINE_INNER_VALUE:
        frame_range = time_utils.get_maya_timeline_range_inner()
    elif frame_range_mode == const.FRAME_RANGE_MODE_TIMELINE_OUTER_VALUE:
        frame_range = time_utils.get_maya_timeline_range_outer()
    elif frame_range_mode == const.FRAME_RANGE_MODE_CUSTOM_VALUE:
        frame_range = time_utils.FrameRange(custom_start_frame,
                                            custom_end_frame)
    else:
        LOG.error("Invalid frame range mode: %r", frame_range_mode)
    return frame_range
Ejemplo n.º 3
0
def _do_raycast(node_list, mesh_nodes, frame_range, max_dist, use_smooth_mesh):
    bnd_nodes = set()
    cur_frame = maya.cmds.currentTime(query=True)
    if frame_range is None:
        frame_range = time_utils.FrameRange(int(cur_frame), int(cur_frame))
    frames = range(frame_range.start, frame_range.end + 1)
    is_multi_frame = len(frames) > 1
    for frame in frames:
        maya.cmds.currentTime(frame, edit=True, update=True)
        for mkr_node, bnd_node, cam_tfm in node_list:
            assert bnd_node is not None
            assert cam_tfm is not None

            direction = reproject_utils.get_camera_direction_to_point(
                cam_tfm, mkr_node)
            origin_point = maya.cmds.xform(mkr_node,
                                           query=True,
                                           translation=True,
                                           worldSpace=True)
            hit_point = raytrace_utils.closest_intersect(
                origin_point,
                direction,
                mesh_nodes,
                test_both_directions=False,
                max_dist=max_dist,
                use_smooth_mesh=use_smooth_mesh)
            if hit_point is None:
                if is_multi_frame is False:
                    LOG.warn("%s didn't hit the mesh.", mkr_node)
                continue

            hit_xyz = (hit_point.x, hit_point.y, hit_point.z)
            maya.cmds.xform(
                bnd_node,
                translation=hit_xyz,
                worldSpace=True,
            )
            if is_multi_frame is True:
                maya.cmds.setKeyframe(bnd_node, attribute=BND_ATTRS)
            bnd_nodes.add(bnd_node)
    maya.cmds.currentTime(cur_frame, edit=True, update=True)
    return bnd_nodes
Ejemplo n.º 4
0
def main():
    """Ray-casts each bundle connected to the selected markers on to the
    mesh from the associated camera.

    Select markers and mesh objects to ray-cast on to, if no mesh
    objects are selected the tool will ray-cast on to all visible mesh
    objects.

    If a bundle translate attribute is locked, it will be
    unlocked, then projected, and then the lock state will
    be reverted to the original value.

    .. note::

        The Marker node is the origin point of the ray-cast, *not* the
        camera's pivot position. This is intentional. If the user has a single
        dense (LIDAR) model node it can be helpful to project from a distance
        away from the camera origin. With a single dense mesh it is difficult
        to split the model up to use different mesh selections.

    Example::

        >>> import mmSolver.tools.raycastmarker.tool as tool
        >>> tool.main()

    """
    selection = maya.cmds.ls(selection=True) or []
    if not selection:
        LOG.warning('Please select a marker to rayCast.')
        return

    node_categories = mmapi.filter_nodes_into_categories(selection)
    mkr_node_list = node_categories['marker']
    bnd_node_list = node_categories['bundle']
    cam_node_list = node_categories['camera']
    if len(mkr_node_list) == 0 and len(bnd_node_list) == 0:
        LOG.warn('Please select markers or bundles to ray-cast.')
        return

    # The camera used to determine where bundles will be projected from.
    active_cam_tfm, active_cam_shp = _get_active_or_selected_camera(
        cam_node_list)

    # Get Markers
    mkr_list, use_camera = _get_markers(mkr_node_list, bnd_node_list,
                                        active_cam_shp)
    if use_camera and active_cam_shp is None:
        LOG.warn('Please activate a viewport to ray-cast Bundles from.')

    frame_range_mode = configmaya.get_scene_option(
        const.CONFIG_FRAME_RANGE_MODE_KEY,
        default=const.DEFAULT_FRAME_RANGE_MODE)

    frame_start = configmaya.get_scene_option(
        const.CONFIG_FRAME_START_KEY, default=const.DEFAULT_FRAME_START)
    frame_end = configmaya.get_scene_option(const.CONFIG_FRAME_END_KEY,
                                            default=const.DEFAULT_FRAME_END)
    if frame_range_mode == const.FRAME_RANGE_MODE_CURRENT_FRAME_VALUE:
        frame_start = int(maya.cmds.currentTime(query=True))
        frame_end = frame_start
    elif frame_range_mode == const.FRAME_RANGE_MODE_TIMELINE_INNER_VALUE:
        frame_start, frame_end = time_utils.get_maya_timeline_range_inner()
    elif frame_range_mode == const.FRAME_RANGE_MODE_TIMELINE_OUTER_VALUE:
        frame_start, frame_end = time_utils.get_maya_timeline_range_outer()
    frame_range = time_utils.FrameRange(frame_start, frame_end)

    use_smooth_mesh = True
    bundle_unlock_relock = configmaya.get_scene_option(
        const.CONFIG_BUNDLE_UNLOCK_RELOCK_KEY,
        default=const.DEFAULT_BUNDLE_UNLOCK_RELOCK)

    # Do not disable the viewport if we're only baking a single frame.
    disable_viewport = True
    if frame_range.start == frame_range.end:
        disable_viewport = False

    mesh_nodes = _get_selected_meshes()
    with tools_utils.tool_context(use_undo_chunk=True,
                                  restore_current_frame=True,
                                  use_dg_evaluation_mode=True,
                                  disable_viewport=disable_viewport):
        bnd_nodes = lib.raycast_markers_onto_meshes(
            mkr_list,
            mesh_nodes,
            frame_range=frame_range,
            unlock_bnd_attrs=bundle_unlock_relock,
            relock_bnd_attrs=bundle_unlock_relock,
            use_smooth_mesh=use_smooth_mesh)
        if len(bnd_nodes) > 0:
            maya.cmds.select(bnd_nodes)
        else:
            maya.cmds.select(selection)
    return
Ejemplo n.º 5
0
def _do_raycast(node_list, mesh_nodes, frame_range, max_dist, use_smooth_mesh,
                bundle_rotate_mode):
    bnd_nodes = set()
    cur_frame = maya.cmds.currentTime(query=True)
    if frame_range is None:
        frame_range = time_utils.FrameRange(int(cur_frame), int(cur_frame))
    frames = range(frame_range.start, frame_range.end + 1)
    is_multi_frame = len(frames) > 1
    keyable_attrs = _get_bundle_modify_attributes(bundle_rotate_mode)
    for frame in frames:
        maya.cmds.currentTime(frame, edit=True, update=True)
        for mkr_node, bnd_node, cam_tfm in node_list:
            assert bnd_node is not None
            assert cam_tfm is not None

            direction = reproject_utils.get_camera_direction_to_point(
                cam_tfm, mkr_node)
            origin_point = maya.cmds.xform(mkr_node,
                                           query=True,
                                           translation=True,
                                           worldSpace=True)
            hit_point, hit_normal = raytrace_utils.closest_intersect_with_normal(
                origin_point,
                direction,
                mesh_nodes,
                test_both_directions=False,
                max_dist=max_dist,
                use_smooth_mesh=use_smooth_mesh,
            )
            if hit_point is None:
                if is_multi_frame is False:
                    LOG.warn("%s didn't hit the mesh.", mkr_node)
                continue
            hit_xyz = (hit_point.x, hit_point.y, hit_point.z)
            hit_normal = (hit_normal.x, hit_normal.y, hit_normal.z)

            maya.cmds.xform(
                bnd_node,
                translation=hit_xyz,
                worldSpace=True,
            )

            # Set rotations.
            if bundle_rotate_mode == const.BUNDLE_ROTATE_MODE_NO_CHANGE_VALUE:
                pass
            elif bundle_rotate_mode in [
                    const.BUNDLE_ROTATE_MODE_AIM_AT_CAMERA_VALUE,
                    const.BUNDLE_ROTATE_MODE_MESH_NORMAL_VALUE
            ]:
                mat = None
                if bundle_rotate_mode == const.BUNDLE_ROTATE_MODE_AIM_AT_CAMERA_VALUE:
                    mat = _create_look_at_matrix(-direction[0], -direction[1],
                                                 -direction[2])
                elif bundle_rotate_mode == const.BUNDLE_ROTATE_MODE_MESH_NORMAL_VALUE:
                    mat = _create_look_at_matrix(hit_normal[0], hit_normal[1],
                                                 hit_normal[2])

                rotate_order = maya.cmds.xform(bnd_node,
                                               query=True,
                                               rotateOrder=True)
                rotate_order_api = tfm_utils.ROTATE_ORDER_STR_TO_APITWO_CONSTANT[
                    rotate_order]

                tfm_mat = OpenMaya2.MTransformationMatrix(mat)
                tfm_mat.reorderRotation(rotate_order_api)
                prev_rot = (0.0, 0.0, 0.0)
                components = tfm_utils.decompose_matrix(tfm_mat, prev_rot)
                _, _, _, rx, ry, rz, _, _, _ = components
                maya.cmds.xform(
                    bnd_node,
                    rotation=(rx, ry, rz),
                    worldSpace=True,
                )

            else:
                msg = 'Invalid bundle rotate mode: ' % bundle_rotate_mode
                raise NotImplementedError(msg)

            if is_multi_frame is True:
                maya.cmds.setKeyframe(bnd_node, attribute=keyable_attrs)
            bnd_nodes.add(bnd_node)
    maya.cmds.currentTime(cur_frame, edit=True, update=True)
    return bnd_nodes