コード例 #1
0
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
コード例 #2
0
def main():
    """
    Toggles selected marker lock state.
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if len(selected_markers) == 0:
        LOG.warning("Please select marker's to lock or unlock")
        return

    attrs = const.ATTRS
    marker_attrs = []
    for marker in selected_markers:
        for attr in attrs:
            marker_attrs.append('%s.%s' % (marker, attr))

    is_locked = False
    for attr in marker_attrs:
        if maya.cmds.getAttr(attr, lock=True):
            is_locked = True

    for attr in marker_attrs:
        lock_value = not is_locked
        maya.cmds.setAttr(attr, lock=lock_value)
    return
コード例 #3
0
def main():
    """
    Averages marker position from selected markers.
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if len(selected_markers) < 2:
        LOG.warning('Please select more than 1 marker')
        return

    mkr_selection = selected_markers[0]
    mkr = mmapi.Marker(node=mkr_selection)
    # getting camera from the selected marker
    cam_from_mkr = mkr.get_camera()
    mkr_name = mmapi.get_new_marker_name('avgMarker1')
    new_mkr = mmapi.Marker().create_node(cam=cam_from_mkr, name=mkr_name)

    new_mkr_node = new_mkr.get_node()
    bnd_name = mmapi.get_new_bundle_name('avgBundle1')
    new_bnd = mmapi.Bundle().create_node(name=bnd_name)
    # connecting bundle to the marker
    new_mkr.set_bundle(new_bnd)

    # getting first frame and last frame from the selected markers
    start_frame, end_frame = mmapi.get_markers_start_end_frames(
        selected_markers)

    # Running average from selected markers for giving frame range
    lib.__set_average_marker_position(selected_markers, start_frame, end_frame,
                                      new_mkr_node)

    maya.cmds.select(new_mkr_node)
    # dgdirty for Channel box value update
    maya.cmds.dgdirty(new_mkr_node)
    return None
コード例 #4
0
def main():
    """
    Renames selected markers and bundles (and the connected nodes).
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    sel_mkr_nodes = mmapi.filter_marker_nodes(selection)
    sel_bnd_nodes = mmapi.filter_bundle_nodes(selection)
    if len(sel_mkr_nodes) == 0 and len(sel_bnd_nodes) == 0:
        LOG.warning('Please select markers or bundles to rename.')
        return

    title = const.TITLE
    message = const.MESSAGE
    text = const.MARKER_NAME
    mkr_name = lib.prompt_for_new_node_name(title, message, text)
    if mkr_name is None:
        # If user clicks cancel on prompt window it returns None.
        LOG.warning('User canceled rename.')
        return

    number_format = const.NUMBER_FORMAT
    mkr_suffix = const.MARKER_SUFFIX
    bnd_suffix = const.BUNDLE_SUFFIX
    if mkr_name == text:
        bnd_name = const.BUNDLE_NAME
    else:
        bnd_name = mkr_name
    nodes = lib.rename_markers_and_bundles(sel_mkr_nodes, sel_bnd_nodes,
                                           mkr_name, bnd_name, number_format,
                                           mkr_suffix, bnd_suffix)
    maya.cmds.select(nodes, replace=True)
    return
コード例 #5
0
ファイル: tool.py プロジェクト: whigg/mayaMatchMoveSolver
def main():
    """
    This tool renames selected markers and bundles with the internal
    metadata names stored on the markers.
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    sel_mkr_nodes = mmapi.filter_marker_nodes(selection)
    sel_bnd_nodes = mmapi.filter_bundle_nodes(selection)
    if len(sel_mkr_nodes) == 0 and len(sel_bnd_nodes) == 0:
        LOG.warning('Please select markers or bundles to rename.')
        return

    number_format = const.NUMBER_FORMAT
    mkr_prefix = const.MARKER_PREFIX
    mkr_suffix = const.MARKER_SUFFIX
    bnd_prefix = const.BUNDLE_PREFIX
    bnd_suffix = const.BUNDLE_SUFFIX
    nodes = lib.rename_markers_and_bundles_with_metadata(
        sel_mkr_nodes, sel_bnd_nodes, mkr_prefix, bnd_prefix, mkr_suffix,
        bnd_suffix)
    if len(nodes) == 0:
        maya.cmds.select(selection, replace=True)
    else:
        maya.cmds.select(nodes, replace=True)
    return
コード例 #6
0
def remove_layer_override():
    """
    Removed markers layer override.

    :return: None
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if len(selected_markers) == 0:
        text = 'Please select a marker to remove anim layer override'
        LOG.warning(text)
        return None

    marker_nodes = []
    for marker in selected_markers:
        marker = mmapi.Marker(node=marker)
        marker_node = marker.get_node()
        marker_nodes.append(marker_node)

    if not marker_nodes:
        text = 'Please select a marker to remove anim layer override'
        LOG.warning(text)
        return

    attrs = lib.get_attrs_for_offset(marker_nodes)
    anim_layer = const.ANIM_LAYER
    for attr in attrs:
        maya.cmds.animLayer(anim_layer, removeAttribute=attr, edit=True)

    if maya.cmds.animLayer(anim_layer, attribute=True, query=True) is None:
        maya.cmds.delete(anim_layer)
    return
コード例 #7
0
def get_selected_markers():
    """
    Get selected Marker object.

    :rtype: list of mmSolver.api.Marker
    """
    nodes = maya.cmds.ls(selection=True, type='transform', long=True) or []
    mkr_nodes = mmapi.filter_marker_nodes(nodes)
    mkr_list = [mmapi.Marker(node=n) for n in mkr_nodes]
    return mkr_list
コード例 #8
0
def main():
    """
    Toggles selected marker lock state.
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_marker_nodes = mmapi.filter_marker_nodes(selection)
    if len(selected_marker_nodes) == 0:
        LOG.warning("Please select markers to lock or unlock")
        return

    lib.markers_lock_toggle(selected_marker_nodes)
    return
コード例 #9
0
def bake_offset():
    """
    Bakes offset for the selected markers.

    :return: None
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if len(selected_markers) == 0:
        LOG.warning('Please select a marker to bake offset')
        return None

    marker_nodes = []
    for marker in selected_markers:
        marker = mmapi.Marker(node=marker)
        marker_node = marker.get_node()
        marker_nodes.append(marker_node)
    if not marker_nodes:
        LOG.warning('Please select a marker to bake offset')
        return None

    attrs = lib.get_attrs_for_offset(marker_nodes)
    anim_layer = const.ANIM_LAYER
    for attr in attrs:
        if not lib.is_in_layer(attr, anim_layer):
            text = 'Selected marker is not having a layer override'
            LOG.warning(text)
            return None
    if not lib.is_key_framed(attrs):
        maya.cmds.setKeyframe(attrs)

    for attr in attrs:
        first_frame, last_frame = lib.__get_first_last_frame(attr, anim_layer)
        input_a, input_b = lib.get_attr_blend_plugs(attr, anim_layer)
        input_a_value = lib.__get_attr_value_array(input_a, first_frame,
                                                   last_frame)
        input_b_value = lib.__get_attr_value_array(input_b, first_frame,
                                                   last_frame)
        new_array = list(map(operator.add, input_a_value, input_b_value))

        maya.cmds.animLayer(anim_layer, removeAttribute=attr, edit=True)
        lib.set_attr_value_array(attr, new_array, first_frame, last_frame)

    if maya.cmds.animLayer(anim_layer, attribute=True, query=True) is None:
        maya.cmds.delete(anim_layer)

    maya.cmds.select(selected_markers)
    maya.cmds.dgdirty(selected_markers)
    return
コード例 #10
0
def unlink_marker_bundle():
    """
    All selected markers are disconnected from their respective bundle.
    """
    sel = maya.cmds.ls(selection=True, long=True) or []
    mkr_nodes = mmapi.filter_marker_nodes(sel)

    if len(mkr_nodes) == 0:
        msg = 'Please select one or more Markers.'
        LOG.warning(msg)
        return

    for mkr_node in mkr_nodes:
        mkr = mmapi.Marker(node=mkr_node)
        mkr.set_bundle(None)
    return
コード例 #11
0
def main():
    """
    Create a new Bundle, attached to the selected Marker (if a Marker
    is selected)
    """
    sel = maya.cmds.ls(sl=True, long=True)
    mkr_nodes = mmapi.filter_marker_nodes(sel)

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

    bnd_node = bnd.get_node()
    for mkr_node in mkr_nodes:
        linkmarkerbundle_lib.link_marker_bundle(mkr_node, bnd_node)

    maya.cmds.select(bnd.get_node(), replace=True)
    return
コード例 #12
0
def link_marker_bundle():
    """
    Select a marker node, and a bundle node, run to link both nodes.
    """
    sel = maya.cmds.ls(selection=True, long=True) or []
    mkr_nodes = mmapi.filter_marker_nodes(sel)
    bnd_nodes = mmapi.filter_bundle_nodes(sel)

    if len(mkr_nodes) != 1 and len(bnd_nodes) != 1:
        msg = 'Please select only one Marker and one Bundle.'
        LOG.warning(msg)
        return
    if len(mkr_nodes) != 1:
        msg = 'Please select only one Marker.'
        LOG.warning(msg)
    if len(bnd_nodes) != 1:
        msg = 'Please select only one Bundle.'
        LOG.warning(msg)
    if len(mkr_nodes) != 1 or len(bnd_nodes) != 1:
        return

    lib.link_marker_bundle(mkr_nodes[0], bnd_nodes[0])
    return
コード例 #13
0
def create_offset_layer():
    """
    Creates anim layer for the selected markers.

    :return: None
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if len(selected_markers) == 0:
        LOG.warning('Please select a marker to create offset')
        return None

    marker_nodes = []
    for marker in selected_markers:
        marker = mmapi.Marker(node=marker)
        marker_node = marker.get_node()
        marker_nodes.append(marker_node)

    if not marker_nodes:
        LOG.warning('Please select a marker to create offset')
        return None

    attrs = lib.get_attrs_for_offset(marker_nodes)
    if attrs is None:
        LOG.warning('Please select marker to create override')
        return None

    anim_layer_name = const.ANIM_LAYER
    anim_layer = lib.find_animlayer(anim_layer_name)

    for attr in attrs:
        if lib.is_in_layer(attr, anim_layer):
            LOG.warning('Selected marker is already having a override')
            return None

    maya.cmds.animLayer(anim_layer, attribute=attrs, edit=True)
    return None
コード例 #14
0
def main():
    """
    Main function runs duplicate marker on all selected markers.
    """
    selection = maya.cmds.ls(selection=True, long=True) or []
    selected_markers = mmapi.filter_marker_nodes(selection)
    if not selected_markers:
        LOG.warning('Please select markers')
        return

    mkr_selection = selected_markers[0]
    mkr = mmapi.Marker(node=mkr_selection)
    # getting camera from the selected marker
    cam_from_mkr = mkr.get_camera()

    new_mkr_nodes = []
    for marker in selected_markers:
        # Get Marker's name
        mkr = mmapi.Marker(node=marker)
        # old_mkr_node is expected to be a long name.
        old_mkr_node = mkr.get_node()
        if old_mkr_node is None:
            LOG.warning('Invalid Marker, skipping duplicate.')
            continue
        mkr_name = old_mkr_node.rpartition('|')[-1]
        mkr_name = mkr_name[0].upper() + mkr_name[1:]
        mkr_name = 'dup' + mkr_name

        # Get Bundles's name
        bnd_name = 'dupBundle1'
        bnd = mkr.get_bundle()
        if bnd is None:
            pass
        else:
            bnd_node = bnd.get_node()
            bnd_name = bnd_node.rpartition('|')[-1]
            bnd_name = bnd_name[0].upper() + bnd_name[1:]
            bnd_name = 'dup' + bnd_name

        # get attrs lock state
        lock_value = lib.__get_lock_state(marker, const.MKR_ATTRS)

        mkr_name = mmapi.get_new_marker_name(mkr_name)
        new_mkr = mmapi.Marker().create_node(cam=cam_from_mkr, name=mkr_name)
        new_mkr_node = new_mkr.get_node()
        bnd_name = mmapi.get_new_bundle_name(bnd_name)
        new_bnd = mmapi.Bundle().create_node(name=bnd_name)
        # connecting bundle to the marker
        new_mkr.set_bundle(new_bnd)

        # running duplicate
        lib.__copy_key_frames(marker, new_mkr_node)

        # set lock state on newly created markers
        lib.__set_lock_state(marker, new_mkr_node, const.MKR_ATTRS, lock_value)
        new_mkr_nodes.append(new_mkr_node)

    if len(new_mkr_nodes) > 0:
        maya.cmds.select(new_mkr_nodes, replace=True)
    else:
        # Restore the original selection.
        maya.cmds.select(selection, replace=True)
    return
コード例 #15
0
def place_marker():
    """
    Called each time the user left-clicks in the viewport.
    """
    nodes = maya.cmds.ls(
        selection=True,
        long=True,
        type='transform'
    ) or []
    if len(nodes) == 0:
        msg = 'No nodes selected! Please select Marker nodes to place.'
        LOG.warning(msg)
        return
    mkr_nodes = mmapi.filter_marker_nodes(nodes)
    if len(mkr_nodes) == 0:
        msg = 'No Marker nodes selected!'
        LOG.warning(msg)
        return
    mkr_list = [mmapi.Marker(node=n) for n in mkr_nodes]
    if len(mkr_list) == 0:
        msg = 'No Marker nodes!'
        LOG.warning(msg)
        return

    # Get viewport coordinate. Viewport coordinate is relative to the
    # viewport resolution in pixels.
    vpX, vpY, vpZ = maya.cmds.draggerContext(
        const.CTX,
        query=True,
        dragPoint=True)

    view = OpenMayaUI.M3dView().active3dView()

    # Get the camera nodes from 3D viewport.
    camDag = view.getCamera()
    camShp = camDag.fullPathName()
    camDag.pop()
    camTfm = camDag.fullPathName()

    # 'Image resolution' is used to make sure the film back aspect
    # ratio is respected.
    imageWidth = maya.cmds.getAttr(camShp + '.horizontalFilmAperture') * 100.0
    imageHeight = maya.cmds.getAttr(camShp + '.verticalFilmAperture') * 100.0

    # Get the world-space location for the clicked point.
    position = OpenMaya.MPoint()
    direction = OpenMaya.MVector()
    view.viewToWorld(
        int(vpX),
        int(vpY),
        position,
        direction)

    # Compute the Marker coordinates for the given camera.
    frame = maya.cmds.currentTime(query=True)
    coord = maya.cmds.mmReprojection(
        worldPoint=(position.x, position.y, position.z),
        camera=(camTfm, camShp),
        asMarkerCoordinate=True,
        imageResolution=(imageWidth, imageHeight),
        time=frame
    )
    if coord is None:
        msg = 'Could not get Marker coordinate.'
        LOG.warning(msg)
        return
    assert len(coord) == 3

    # Set the marker position
    for mkr in mkr_list:
        mkr_grp = mkr.get_marker_group()
        mkr_grp_node = mkr_grp.get_node()
        plug_overscan_x = mkr_grp_node + '.overscanX'
        plug_overscan_y = mkr_grp_node + '.overscanY'
        overscan_x = maya.cmds.getAttr(plug_overscan_x)
        overscan_y = maya.cmds.getAttr(plug_overscan_y)
        node = mkr.get_node()
        plug_tx = node + '.translateX'
        plug_ty = node + '.translateY'
        value_tx = coord[0] * overscan_x
        value_ty = coord[1] * overscan_y
        lock_tx = maya.cmds.getAttr(plug_tx, lock=True)
        lock_ty = maya.cmds.getAttr(plug_tx, lock=True)
        if lock_tx is False and lock_ty is False:
            maya.cmds.setAttr(plug_tx, value_tx)
            maya.cmds.setAttr(plug_ty, value_ty)
        else:
            msg = 'Did not set Marker position, node is locked; node=%r'
            LOG.warning(msg, node)

    maya.cmds.select(nodes, replace=True)
    maya.cmds.refresh()
    return
コード例 #16
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.

    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

    selected_markers = mmapi.filter_marker_nodes(selection)
    if not selected_markers:
        LOG.warning('No markers found in the selection list.')
        return

    meshes = []
    selected_meshes = maya.cmds.ls(
        sl=True, type='mesh', dagObjects=True, noIntermediate=True) or []
    if len(selected_meshes) > 0:
        meshes = selected_meshes
    else:
        meshes = maya.cmds.ls(type='mesh', visible=True, long=True) or []
        cache = {}
        meshes = [n for n in meshes if _visible(n, cache)]

    max_dist = 9999999999.0
    bnd_nodes = []
    for node in selected_markers:
        mkr = mmapi.Marker(node=node)
        bnd = mkr.get_bundle()
        if bnd is None:
            continue
        mkr_node = mkr.get_node()
        camera = mkr.get_camera()
        cam_tfm = camera.get_transform_node()
        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,
            meshes,
            test_both_directions=False,
            max_dist=max_dist,
        )
        if hit_point is None:
            LOG.warning('%s didn\'t hit the mesh.' % node)
            continue

        bnd_node = bnd.get_node()
        plugs = [
            '%s.translateX' % bnd_node,
            '%s.translateY' % bnd_node,
            '%s.translateZ' % bnd_node
        ]
        plug_lock_state = {}
        for plug_name in plugs:
            value = maya.cmds.getAttr(plug_name, lock=True)
            plug_lock_state[plug_name] = value
            maya.cmds.setAttr(plug_name, lock=False)
        hit_xyz = (hit_point.x, hit_point.y, hit_point.z)
        maya.cmds.xform(
            bnd_node,
            translation=hit_xyz,
            worldSpace=True,
        )
        for plug_name in plugs:
            value = plug_lock_state.get(plug_name)
            maya.cmds.setAttr(plug_name, lock=value)
        bnd_nodes.append(bnd_node)

    if len(bnd_nodes) > 0:
        maya.cmds.select(bnd_nodes)
    else:
        maya.cmds.select(selection)
    return