Exemple #1
0
def select_both_markers_and_bundles():
    """
    Get the connected Markers and bundles, and select them.
    """
    sel = maya.cmds.ls(sl=True, long=True) or []
    if len(sel) == 0:
        LOG.warning('Select a node.')
        return

    node_filtered = mmapi.filter_nodes_into_categories(sel)
    num_marker = len(node_filtered['marker'])
    num_bundle = len(node_filtered['bundle'])

    mkr_nodes = []
    bnd_nodes = []
    if num_marker >= num_bundle:
        nodes = node_filtered['marker']
        bnd_nodes = mmapi.get_bundle_nodes_from_marker_nodes(nodes)
        mkr_nodes = mmapi.get_marker_nodes_from_bundle_nodes(bnd_nodes)
    else:
        nodes = node_filtered['bundle']
        mkr_nodes = mmapi.get_marker_nodes_from_bundle_nodes(nodes)
        bnd_nodes = mmapi.get_bundle_nodes_from_marker_nodes(mkr_nodes)

    new_sel = mkr_nodes + bnd_nodes
    maya.cmds.select(new_sel, replace=True)
    return
def get_markers_from_selection():
    """
    Given a selection of nodes, find the associated markers.

    :return: list of Marker objects.
    :rtype: [Marker, ..]
    """
    nodes = maya.cmds.ls(long=True, selection=True) or []
    node_categories = mmapi.filter_nodes_into_categories(nodes)
    marker_nodes = node_categories.get('marker', [])

    camera_nodes = node_categories.get('camera', [])
    for node in camera_nodes:
        node_type = maya.cmds.nodeType(node)
        cam = None
        if node_type == 'transform':
            cam = mmapi.Camera(transform=node)
        if node_type == 'camera':
            cam = mmapi.Camera(shape=node)
        tfm_node = cam.get_transform_node()
        below_nodes = maya.cmds.ls(tfm_node, dag=True, long=True)
        marker_nodes += mmapi.filter_marker_nodes(below_nodes)

    marker_group_nodes = list(node_categories['markergroup'])
    for node in marker_group_nodes:
        below_nodes = maya.cmds.ls(node, dag=True, long=True)
        marker_nodes += mmapi.filter_marker_nodes(below_nodes)

    # Convert nodes into Marker objects.
    marker_nodes = list(set(marker_nodes))
    marker_list = []
    for node in marker_nodes:
        mkr = mmapi.Marker(node=node)
        marker_list.append(mkr)
    return marker_list
Exemple #3
0
def swap_between_selected_markers_and_bundles():
    """
    Toggles the selection of Markers and Bundles.

    If a marker is selected, the attached bundle will be selected and
    vice versa.
    """
    sel = maya.cmds.ls(sl=True, long=True) or []
    if len(sel) == 0:
        LOG.warning('Select a node.')
        return

    node_filtered = mmapi.filter_nodes_into_categories(sel)
    num_marker = len(node_filtered['marker'])
    num_bundle = len(node_filtered['bundle'])
    new_sel = []

    if num_marker >= num_bundle:
        bnd_nodes = mmapi.get_bundle_nodes_from_marker_nodes(
            node_filtered['marker'])
        new_sel = bnd_nodes
    else:
        mkr_nodes = mmapi.get_marker_nodes_from_bundle_nodes(
            node_filtered['bundle'])
        new_sel = mkr_nodes

    maya.cmds.select(new_sel, replace=True)
    return
Exemple #4
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
Exemple #5
0
def get_collections():
    """
    Get all Collection objects defined in the scene.

    :returns: A list of Collection objects.
    :rtype: [Collection, ..]
    """
    nodes = maya.cmds.ls(type='objectSet', long=True) or []
    node_categories = mmapi.filter_nodes_into_categories(nodes)
    cols = []
    for col_node in node_categories['collection']:
        col = mmapi.Collection(node=col_node)
        cols.append(col)
    return cols
Exemple #6
0
def get_selected_cameras():
    """
    Return the (associated) Camera objects from the selection.

    :returns: Camera objects.
    :rtype: mmSolver.api.Camera
    """
    cams = []
    nodes = maya.cmds.ls(sl=True, long=True) or []

    added_cameras = []
    objects = mmapi.filter_nodes_into_categories(nodes)
    for node in objects['camera']:
        cam = None
        if maya.cmds.nodeType(node) == 'camera':
            cam = mmapi.Camera(shape=node)
        else:
            cam = mmapi.Camera(transform=node)
        if cam is None:
            continue
        shp_node = cam.get_shape_node()
        if shp_node not in added_cameras:
            cams.append(cam)
            added_cameras.append(shp_node)

    for node in objects['marker']:
        mkr = mmapi.Marker(node=node)
        cam = mkr.get_camera()
        if cam is None:
            continue
        shp_node = cam.get_shape_node()
        if shp_node not in added_cameras:
            cams.append(cam)
            added_cameras.append(shp_node)

    for node in objects['markergroup']:
        mkr_grp = mmapi.MarkerGroup(node=node)
        cam = mkr_grp.get_camera()
        if cam is None:
            continue
        shp_node = cam.get_shape_node()
        if shp_node not in added_cameras:
            cams.append(cam)
            added_cameras.append(shp_node)

    return cams
Exemple #7
0
def main():
    """
    Triangulate Bundle using camera and Marker.

    Usage:

    1) Select markers or bundles (or both).

    2) Run tool.

    3) Bundle is triangulated in TX, TY and TZ.
    """
    # Get Markers and Bundles
    sel = maya.cmds.ls(selection=True, long=True) or []
    filter_nodes = mmapi.filter_nodes_into_categories(sel)
    mkr_nodes = filter_nodes.get('marker', [])
    bnd_nodes = filter_nodes.get('bundle', [])
    if len(mkr_nodes) == 0 and len(bnd_nodes) == 0:
        msg = 'Please select at least one marker / bundle!'
        LOG.warning(msg)
        return

    # Get Bundles from Markers
    for mkr_node in mkr_nodes:
        mkr = mmapi.Marker(node=mkr_node)
        bnd = mkr.get_bundle()
        bnd_node = bnd.get_node()
        if bnd_node not in bnd_nodes:
            bnd_nodes.append(bnd_node)
    bnd_list = [mmapi.Bundle(node=node) for node in bnd_nodes]

    # Triangulate
    adjusted_bnd_node_list = []
    for bnd in bnd_list:
        lib.triangulate_bundle(bnd)
        adjusted_bnd_node_list.append(bnd.get_node())

    # Select all bundle nodes.
    if len(adjusted_bnd_node_list) > 0:
        maya.cmds.select(adjusted_bnd_node_list, replace=True)
    else:
        msg = 'No Bundle nodes found, see Script Editor for details.'
        LOG.warning(msg)
    return
def filter_nodes(what_to_delete_dict):
    nodes = cmds.ls(long=True) or []
    cmds.select([])
    node_categories = mmapi.filter_nodes_into_categories(nodes)
    unknown_node_found = False
    LOG.info('removesolvernodes: filter_nodes run')
    all_list_to_delete = list()
    if what_to_delete_dict.get('markers') is True:
        all_list_to_delete += collect_nodes(node_categories, mode='marker')
    if what_to_delete_dict.get('other_nodes') is True:
        all_list_to_delete += collect_misc_nodes()
    if what_to_delete_dict.get('bundles') is True:
        delete_list, unknown_node_found = collect_bundles(node_categories)
        all_list_to_delete += delete_list
    if what_to_delete_dict.get('marker_groups') is True:
        all_list_to_delete += collect_nodes(node_categories,
                                            mode='markergroup')
    if what_to_delete_dict.get('collections') is True:
        all_list_to_delete += collect_nodes(node_categories, mode='collection')
    return all_list_to_delete, unknown_node_found
Exemple #9
0
def filter_nodes(what_to_delete_dict):
    nodes = cmds.ls(long=True) or []
    cmds.select([])
    node_categories = mmapi.filter_nodes_into_categories(nodes)
    unknown_node_found = False
    found_nodes_map = dict()
    if what_to_delete_dict.get('markers') is True:
        found_nodes_map['markers'] = collect_nodes(node_categories,
                                                   mode='marker')
    if what_to_delete_dict.get('other_nodes') is True:
        found_nodes_map['other_nodes'] = collect_misc_nodes()
    if what_to_delete_dict.get('bundles') is True:
        delete_list, unknown_node_found = collect_bundles(node_categories)
        found_nodes_map['bundles'] = delete_list
    if what_to_delete_dict.get('marker_groups') is True:
        found_nodes = collect_nodes(node_categories, mode='markergroup')
        found_nodes_map['marker_groups'] = found_nodes
    if what_to_delete_dict.get('collections') is True:
        found_nodes = collect_nodes(node_categories, mode='collection')
        found_nodes_map['collections'] = found_nodes
    return found_nodes_map, unknown_node_found
Exemple #10
0
def filter_nodes(what_to_delete_dict):
    nodes = cmds.ls(long=True) or []
    cmds.select([])
    node_categories = mmapi.filter_nodes_into_categories(nodes)
    unknown_node_found = False
    print 'filter_nodes run'
    all_list_to_delete = list()
    for key in what_to_delete_dict.keys():
        if key == 'markers' and what_to_delete_dict[key]:
            all_list_to_delete += \
                collect_nodes(node_categories, mode='marker')
        elif key == 'other_nodes' and what_to_delete_dict[key]:
            all_list_to_delete += collect_misc_nodes()
        elif key == 'bundles' and what_to_delete_dict[key]:
            delete_list, unknown_node_found = collect_bundles(node_categories)
            all_list_to_delete += delete_list
        elif key == 'marker_groups' and what_to_delete_dict[key]:
            all_list_to_delete += \
                collect_nodes(node_categories, mode='markergroup')
        elif key == 'collections' and what_to_delete_dict[key]:
            all_list_to_delete += \
                collect_nodes(node_categories, mode='collection')
    return all_list_to_delete, unknown_node_found
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
def main():
    """
    Move the Bundle to the Marker, on the current-frame.

    Perform a reprojection of the selected bundle (or bundle connected
    to the selected marker), at the current frame.

    Usage:

    1) Select markers or connected bundles (or both).

    2) Run tool.

    3) Bundle is triangulated in TX, TY and TZ.

    .. note::

        If a Bundle has locked attributes, they will be unlocked and
        then relocked.

    """
    # If a bundle has locked attributes, they will be unlocked and
    # then relocked.
    relock = True

    # Get Markers and Bundles
    sel = maya.cmds.ls(sl=True) or []
    filter_nodes = mmapi.filter_nodes_into_categories(sel)
    mkr_nodes = filter_nodes.get('marker', [])
    bnd_nodes = filter_nodes.get('bundle', [])
    if len(mkr_nodes) == 0 and len(bnd_nodes) == 0:
        msg = 'Please select at least one marker / bundle!'
        LOG.warning(msg)
        return

    # Get Markers from Bundles
    for bnd_node in bnd_nodes:
        bnd = mmapi.Bundle(node=bnd_node)
        bnd_node_full = bnd.get_node()
        bnd_mkr_list = bnd.get_marker_list()
        mkr_count = len(bnd_mkr_list)
        if mkr_count == 0:
            msg = ('Cannot find Marker from Bundle, '
                   'Bundle doesn\'t have any Markers connected. '
                   'bnd=%r mkr_count=%r')
            LOG.warning(msg, bnd_node_full, mkr_count)
            continue
        elif mkr_count > 1:
            msg = ('Cannot find Marker from Bundle, '
                   'Bundle has more than 1 Marker. '
                   'bnd=%r mkr_count=%r')
            LOG.warning(msg, bnd_node_full, mkr_count)
            continue
        assert mkr_count == 1
        mkr = bnd_mkr_list[0]
        mkr_node_full = mkr.get_node()
        mkr_nodes.append(mkr_node_full)

    # Get list of markers to operate on.
    mkr_list = []
    have_mkr_nodes = []
    attrs = ['translateX', 'translateY', 'translateZ']
    for mkr_node in mkr_nodes:
        mkr = mmapi.Marker(node=mkr_node)
        mkr_node_full = mkr.get_node()
        if mkr_node_full in have_mkr_nodes:
            msg = 'Skipping Marker, already have it; mkr=%r'
            LOG.debug(msg, mkr_node_full)
            continue

        # Get Bundle
        bnd = mkr.get_bundle()
        if bnd is None:
            msg = 'Marker does not have a connected Bundle; mkr=%r'
            LOG.warning(msg, mkr_node_full)
            continue
        bnd_node_full = bnd.get_node()

        # Check we can handle locked attributes.
        locked_num = 0
        for attr in attrs:
            plug = bnd_node_full + '.' + attr
            locked = maya.cmds.getAttr(plug, lock=True)
            locked_num += int(locked)
        if relock is False:
            if locked_num > 0:
                msg = ('Bundle must have unlocked translate attributes: '
                       'bnd=%r')
                LOG.warning(msg, bnd_node_full)
                continue
        elif relock is True:
            # Check the bundle isn't referenced and has locked attrs.
            referenced = maya.cmds.referenceQuery(bnd_node_full,
                                                  isNodeReferenced=True)
            if referenced is True and locked_num > 0:
                msg = (
                    'Bundle has locked translate attributes and is referenced '
                    '(cannot be unlocked): '
                    'bnd=%r')
                LOG.warning(msg, bnd_node_full)
                continue

        mkr_list.append(mkr)
        have_mkr_nodes.append(mkr_node_full)

    # Do projection
    modified_bnds = lib.reproject_bundle_current_frame(mkr_list, relock=relock)

    # Select all moved bundle nodes.
    modified_bnd_nodes = [bnd.get_node() for bnd in modified_bnds]
    if len(modified_bnd_nodes) > 0:
        maya.cmds.select(modified_bnd_nodes, replace=True)
    else:
        msg = 'No Bundle nodes modified, see Script Editor for details.'
        LOG.warning(msg)
    return
Exemple #13
0
def main():
    """
    Create a new marker under the current viewport camera, or under
    the selected camera, if a camera is selected.
    """
    mmapi.load_plugin()

    sel = maya.cmds.ls(sl=True, long=True)
    node_filtered = mmapi.filter_nodes_into_categories(sel)
    cams = node_filtered['camera']
    cams = filter(utils_camera.is_not_startup_cam, cams)
    mkr_grps = node_filtered['markergroup']

    cam = None
    mkr_grp = None
    if len(cams) > 0 and len(mkr_grps) > 0:
        msg = ('Please select a camera or marker group; '
               'both node types are selected.')
        LOG.error(msg)

    elif len(cams) == 0 and len(mkr_grps) == 0:
        # Create a Marker under the active viewport camera.
        model_editor = utils_viewport.get_active_model_editor()
        if model_editor is None:
            msg = 'Please select an active 3D viewport.'
            LOG.warning(msg)
            return
        cam_tfm, cam_shp = utils_viewport.get_viewport_camera(model_editor)
        node = cam_shp
        if node is None:
            msg = 'Please select an active viewport to get a camera.'
            LOG.error(msg)
            return
        if utils_camera.is_startup_cam(node) is True:
            msg = "Cannot create Markers in 'persp' camera."
            LOG.error(msg)
            return
        if maya.cmds.nodeType(node) == 'transform':
            cam = mmapi.Camera(transform=node)
        elif maya.cmds.nodeType(node) == 'camera':
            cam = mmapi.Camera(shape=node)
        else:
            LOG.error('Camera node is invalid; %r', node)
            return

    elif len(cams) > 0 and len(mkr_grps) == 0:
        # Create a Marker under the selected camera.
        node = cams[0]
        if maya.cmds.nodeType(node) == 'transform':
            cam = mmapi.Camera(transform=node)
        elif maya.cmds.nodeType(node) == 'camera':
            cam = mmapi.Camera(shape=node)
        else:
            LOG.error('Camera node is invalid; %r', node)
            return

    elif len(cams) == 0 and len(mkr_grps) > 0:
        # Create a marker under the first selected Marker Group.
        node = mkr_grps[0]
        mkr_grp = mmapi.MarkerGroup(node=node)

    else:
        LOG.error('Should not get here.')

    bnd_name = mmapi.get_new_bundle_name('bundle1')
    bnd = mmapi.Bundle().create_node(name=bnd_name)

    mkr_name = mmapi.get_new_marker_name('marker1')
    mkr = mmapi.Marker().create_node(name=mkr_name,
                                     cam=cam,
                                     mkr_grp=mkr_grp,
                                     bnd=bnd)

    maya.cmds.select(mkr.get_node(), replace=True)
    return