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
def link_marker_bundle(mkr_node, bnd_node): """ Try to connect the two marker and bundle nodes. """ mkr = mmapi.Marker(node=mkr_node) bnd = mmapi.Bundle(node=bnd_node) cam_from_mkr = mkr.get_camera() cam_from_mkr_uid = cam_from_mkr.get_shape_uid() # Check the bundle doesn't already have a marker attached to the # same camera as 'mkr' bad_mkr = None connected_mkr_list = bnd.get_marker_list() for conn_mkr in connected_mkr_list: conn_cam = conn_mkr.get_camera() conn_cam_uid = conn_cam.get_shape_uid() if conn_cam_uid == cam_from_mkr_uid: bad_mkr = conn_mkr break valid = bad_mkr is None if valid: # No problem, set the bundle. mkr.set_bundle(bnd) else: msg = 'Cannot link {mkr} to {bnd}; ' msg += 'bundle is already connected to Marker {mkr2} under camera {cam}.' msg = msg.format( mkr=repr(mkr_node), bnd=repr(bnd_node), mkr2=repr(bad_mkr.get_node()), cam=repr(cam_from_mkr.get_shape_node()), ) LOG.warning(msg) return valid
def __create_node(mkr_data, cam, mkr_grp, with_bundles): """ Create a Marker object from a MarkerData object. """ if isinstance(mkr_data, interface.MarkerData) is False: msg = 'mkr_data must be of type: %r' raise TypeError(msg % interface.MarkerData.__name__) if isinstance(with_bundles, bool) is False: msg = 'with_bundles must be of type: %r' raise TypeError(msg % bool.__name__) name = mkr_data.get_name() mkr_name = mmapi.get_new_marker_name(name) bnd_name = mmapi.get_new_bundle_name(name) bnd = None mmapi.load_plugin() if with_bundles is True: bnd = mmapi.Bundle().create_node(bnd_name) if cam and mkr_grp: cam = None mkr = mmapi.Marker().create_node(name=mkr_name, cam=cam, mkr_grp=mkr_grp, bnd=bnd) return mkr, bnd
def _get_markers(mkr_node_list, bnd_node_list, active_cam_shp): use_camera = False mkr_list = [] mkr_uid_list = set() if len(mkr_node_list) > 0: for mkr_node in mkr_node_list: mkr = mmapi.Marker(node=mkr_node) _add_unique_markers_to_list(mkr, mkr_list, mkr_uid_list) if len(bnd_node_list) > 0: for bnd_node in bnd_node_list: bnd = mmapi.Bundle(node=bnd_node) bnd_mkr_list = bnd.get_marker_list() if len(bnd_mkr_list) == 1: # There can only be one possible marker to project from mkr = bnd_mkr_list[0] cam = mkr.get_camera() if cam is None: continue _add_unique_markers_to_list(mkr, mkr_list, mkr_uid_list) else: for mkr in bnd_mkr_list: cam = mkr.get_camera() if cam is None: continue mkr_cam_shp = cam.get_shape_node() if active_cam_shp != mkr_cam_shp: continue use_camera = True _add_unique_markers_to_list(mkr, mkr_list, mkr_uid_list) return mkr_list, use_camera
def create_example_solve_scene(): """ Very basic single frame solver set up. This function does not execute the solve, execution must be done manually. :return: API Collection object. """ # Camera cam_tfm = maya.cmds.createNode('transform', name='cam_tfm') cam_shp = maya.cmds.createNode('camera', name='cam_shp', parent=cam_tfm) maya.cmds.setAttr(cam_tfm + '.tx', -1.0) maya.cmds.setAttr(cam_tfm + '.ty', 1.0) maya.cmds.setAttr(cam_tfm + '.tz', -5.0) cam = mmapi.Camera(shape=cam_shp) # Bundle bnd = mmapi.Bundle().create_node() bundle_tfm = bnd.get_node() maya.cmds.setAttr(bundle_tfm + '.tx', 5.5) maya.cmds.setAttr(bundle_tfm + '.ty', 6.4) maya.cmds.setAttr(bundle_tfm + '.tz', -25.0) assert mmapi.get_object_type(bundle_tfm) == 'bundle' # Marker mkr = mmapi.Marker().create_node(cam=cam, bnd=bnd) marker_tfm = mkr.get_node() assert mmapi.get_object_type(marker_tfm) == 'marker' maya.cmds.setAttr(marker_tfm + '.tx', 0.0) maya.cmds.setAttr(marker_tfm + '.ty', 0.0) # Attributes attr_tx = mmapi.Attribute(bundle_tfm + '.tx') attr_ty = mmapi.Attribute(bundle_tfm + '.ty') # Frames frm_list = [ mmapi.Frame(1, primary=True) ] # Solver sol = mmapi.Solver() sol.set_max_iterations(10) sol.set_verbose(True) sol.set_frame_list(frm_list) # Collection col = mmapi.Collection() col.create_node('mySolveCollection') col.add_solver(sol) col.add_marker(mkr) col.add_attribute(attr_tx) col.add_attribute(attr_ty) return col
def get_markers_from_bundles(nodes): bnd_nodes = filternodes.get_bundle_nodes(nodes) mkr_nodes = [] for bnd_node in bnd_nodes: bnd = mmapi.Bundle(bnd_node) mkr_list = bnd.get_marker_list() for mkr in mkr_list: mkr_node = mkr.get_node() if mkr_node not in mkr_nodes: mkr_nodes.append(mkr_node) return mkr_nodes
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
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 __create_node(mkr_data, cam, mkr_grp, with_bundles): """ Create a Marker object from a MarkerData object. :param mkr_data: The data to create the Marker with. :type mkr_data: MarkerData :param cam: Camera to create marker node underneath. :type cam: Camera :param mkr_grp: MarkerGroup to create marker underneath :type mkr_grp: MarkerGroup :param with_bundles: Create the Marker with Bundle attached? :type with_bundles: bool :returns: Created Marker and Bundle objects. If with_bundles is False, the Bundle object will be None. :rtype: (Marker, Bundle or None) """ if isinstance(mkr_data, interface.MarkerData) is False: msg = 'mkr_data must be of type: %r' raise TypeError(msg % interface.MarkerData.__name__) if isinstance(with_bundles, bool) is False: msg = 'with_bundles must be of type: %r' raise TypeError(msg % bool.__name__) name = mkr_data.get_name() mkr_name = mmapi.get_new_marker_name(name) bnd_name = mmapi.get_new_bundle_name(name) bnd = None mmapi.load_plugin() if with_bundles is True: bnd = mmapi.Bundle().create_node(bnd_name) if cam and mkr_grp: cam = None mkr = mmapi.Marker().create_node( name=mkr_name, cam=cam, mkr_grp=mkr_grp, bnd=bnd) return mkr, bnd
def test_marker_enable(self): start = 1 end = 5 # Set Time Range maya.cmds.playbackOptions( animationStartTime=start, minTime=start, animationEndTime=end, maxTime=end ) # Camera cam_tfm = maya.cmds.createNode('transform', name='cam_tfm') cam_shp = maya.cmds.createNode('camera', name='cam_shp', parent=cam_tfm) maya.cmds.setAttr(cam_tfm + '.tx', -1.0) maya.cmds.setAttr(cam_tfm + '.ty', 1.0) maya.cmds.setAttr(cam_tfm + '.tz', -5.0) cam = mmapi.Camera(shape=cam_shp) # Bundle bnd = mmapi.Bundle().create_node() bundle_tfm = bnd.get_node() maya.cmds.setAttr(bundle_tfm + '.tx', 5.5) maya.cmds.setAttr(bundle_tfm + '.ty', 6.4) maya.cmds.setAttr(bundle_tfm + '.tz', -25.0) assert mmapi.get_object_type(bundle_tfm) == 'bundle' # calculate angle of view (AOV) f = maya.cmds.getAttr(cam_shp + '.focalLength') fbw = maya.cmds.getAttr(cam_shp + '.horizontalFilmAperture') * 25.4 fbh = maya.cmds.getAttr(cam_shp + '.verticalFilmAperture') * 25.4 aov = math.degrees(2.0 * math.atan(fbw * (0.5 / f))) # Set Camera Anim maya.cmds.setKeyframe(cam_tfm, attribute='rotateY', time=start, value=-(aov/2), inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(cam_tfm, attribute='rotateY', time=end, value=(aov/2), inTangentType='linear', outTangentType='linear') # Marker mkr = mmapi.Marker().create_node(cam=cam, bnd=bnd) marker_tfm = mkr.get_node() assert mmapi.get_object_type(marker_tfm) == 'marker' mid_value = 0.23534346 maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=start, value=-0.5, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=start+1, value=-mid_value, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=end-1, value=mid_value, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=end, value=0.5, inTangentType='linear', outTangentType='linear') maya.cmds.setAttr(marker_tfm + '.ty', 0.0) maya.cmds.setKeyframe(marker_tfm, attribute='enable', time=1, value=1, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='enable', time=2, value=1, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='enable', time=3, value=0, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='enable', time=4, value=1, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='enable', time=5, value=1, inTangentType='linear', outTangentType='linear') # Create Sphere sph_tfm, shp_node = maya.cmds.polySphere() maya.cmds.setAttr(sph_tfm + '.tx', -1.0) maya.cmds.setAttr(sph_tfm + '.ty', 1.0) maya.cmds.setAttr(sph_tfm + '.tz', -25.0) # Attributes attr_tx = mmapi.Attribute(bundle_tfm + '.tx') attr_ty = mmapi.Attribute(bundle_tfm + '.ty') # Frames frm_list = [ mmapi.Frame(1, primary=True), mmapi.Frame(2, primary=True), mmapi.Frame(3, primary=True), mmapi.Frame(4, primary=True), mmapi.Frame(5, primary=True) ] # Solver sol = mmapi.Solver() sol.set_max_iterations(1000) sol.set_solver_type(mmapi.SOLVER_TYPE_DEFAULT) sol.set_verbose(True) sol.set_frame_list(frm_list) # Collection col = mmapi.Collection() col.create_node('mySolveCollection') col.add_solver(sol) col.add_marker(mkr) col.add_attribute(attr_tx) col.add_attribute(attr_ty) # save the output path = self.get_data_path('test_solve_marker_enabled_before.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) # Run solver! results = col.execute() # Ensure the values are correct for res in results: success = res.get_success() err = res.get_final_error() print('error stats: ' + pprint.pformat(res.get_error_stats())) print('timer stats: ' + pprint.pformat(res.get_timer_stats())) print('solver stats: ' + pprint.pformat(res.get_solver_stats())) print('frame error list: ' + pprint.pformat(dict(res.get_frame_error_list()))) print('marker error list: ' + pprint.pformat(dict(res.get_marker_error_list()))) self.assertTrue(success) # self.assertGreater(0.001, err) # assert self.approx_equal(maya.cmds.getAttr(bundle_tfm+'.tx'), -6.0) # assert self.approx_equal(maya.cmds.getAttr(bundle_tfm+'.ty'), 3.6) # Set Deviation mmapi.update_deviation_on_markers([mkr], results) mmapi.update_deviation_on_collection(col, results) # save the output path = self.get_data_path('test_solve_marker_enabled_after.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) self.checkSolveResults(results) return
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
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
def rename_markers_and_bundles_with_metadata(mkr_nodes, bnd_nodes, mkr_prefix, bnd_prefix, mkr_suffix, bnd_suffix): """ Rename the given marker and bundle nodes. :param mkr_nodes: Marker nodes to rename. :type mkr_nodes: [str, ..] :param bnd_nodes: Bundle nodes to rename. :type bnd_nodes: [str, ..] :param mkr_prefix: Rename the markers to this name. :type mkr_prefix: str :param bnd_prefix: Rename the bundle to this name. :type bnd_prefix: str :param mkr_suffix: Set the marker suffix name. :type mkr_suffix: str :param bnd_suffix: Set the bundle suffix name. :type bnd_suffix: str :returns: Nodes that have been renamed. :rtype: [str, ..] """ # Convert markers to bundles. mkr_nodes = list(mkr_nodes) bnd_nodes = list(bnd_nodes) for node in mkr_nodes: mkr = mmapi.Marker(node=node) bnd = mkr.get_bundle() if bnd is None: continue bnd_node = bnd.get_node() if not bnd_node: continue if bnd_node not in bnd_nodes: bnd_nodes.append(bnd_node) # Rename the bundles. renamed_nodes = [] for bnd_node in bnd_nodes: bnd = mmapi.Bundle(node=bnd_node) base_name = None mkr_list = bnd.get_marker_list() for mkr in mkr_list: mkr_name = _get_marker_internal_name(mkr) if mkr_name is None or len(mkr_name) == 0: continue base_name = mkr_name if base_name is None: LOG.warn( 'Cannot rename Marker/Bundle with metadata: ' 'bnd=%r mkr_list=%r', bnd, mkr_list) continue new_bnd_name = '{prefix}_{base}{suffix}' new_bnd_name = new_bnd_name.format(prefix=bnd_prefix, base=base_name, suffix=bnd_suffix) bnd_node = bnd.get_node() maya.cmds.rename(bnd_node, new_bnd_name) renamed_nodes.append(bnd.get_node()) new_mkr_name = new_bnd_name.replace(bnd_prefix, mkr_prefix) new_mkr_name = new_mkr_name.replace(bnd_suffix, mkr_suffix) mkr_list = bnd.get_marker_list() for mkr in mkr_list: mkr_node = mkr.get_node() maya.cmds.rename(mkr_node, new_mkr_name) renamed_nodes.append(mkr.get_node()) return renamed_nodes
def create_marker(): """ 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 = filter_nodes.get_nodes(sel) cams = node_filtered['camera'] cams = filter(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; ' msg += 'both node types are selected.' LOG.error(msg) elif len(cams) == 0 and len(mkr_grps) == 0: node = __get_camera() if node is None: msg = 'Please activate a viewport to get a camera.' LOG.error(msg) return if 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: 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: node = mkr_grps[0] mkr_grp = mmapi.MarkerGroup(name=node) else: LOG.error('Should not get here.') bnd_name = mmapi.get_bundle_name('bundle1') bnd = mmapi.Bundle().create_node( name=bnd_name ) mkr_name = mmapi.get_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
def test_solveAllFramesCausesStaticAnimCurves(self): """ Solving with the scene file 'mmSolverBasicSolveB_before.ma', was reported to solve as static values, the same as the initial values. The DG did not evaluate some how and the solve was therefore useless. GitHub Issue #53. """ s = time.time() # Open the Maya file file_name = 'mmSolverBasicSolveB_before.ma' path = self.get_data_path('scenes', file_name) maya.cmds.file(path, open=True, force=True, ignoreVersion=True) # NOTE: We leave these nodes alone, since these are already in # the 'correct' position, we are treating these as surveyed. # When we have less than 3 points as survey the solve goes # crazy. dont_touch_these_nodes = [ '|bundle_12_BND', '|bundle_13_BND', '|bundle_14_BND'] # Triangulate all 3D points. nodes = maya.cmds.ls(type='transform') or [] bnd_nodes = mmapi.filter_bundle_nodes(nodes) bnd_list = [mmapi.Bundle(node=n) for n in bnd_nodes] for bnd in bnd_list: bnd_node = bnd.get_node() if bnd_node in dont_touch_these_nodes: continue attrs = ['translateX', 'translateY', 'translateZ'] for attr_name in attrs: plug = bnd_node + '.' + attr_name maya.cmds.setAttr(plug, lock=False) maya.cmds.setAttr(plug, 0.0) # Get Bundle attributes to compute. bnd_attr_list = [] for bnd in bnd_list: node = bnd.get_node() attrs = ['translateX', 'translateY', 'translateZ'] for attr_name in attrs: attr = mmapi.Attribute(node=node, attr=attr_name) bnd_attr_list.append(attr) # Camera attributes cam_tfm = 'stA_1_1' cam = mmapi.Camera(cam_tfm) cam_shp = cam.get_shape_node() cam_attr_list = [] attrs = ['translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ'] for attr_name in attrs: attr = mmapi.Attribute(node=cam_tfm, attr=attr_name) cam_attr_list.append(attr) attr = mmapi.Attribute(node=cam_shp, attr='focalLength') cam_attr_list.append(attr) # Frame List root_frm_list = [] not_root_frm_list = [] f_list = [14, 35, 50, 85] for f in f_list: frm = mmapi.Frame(f) root_frm_list.append(frm) for f in range(0, 94): frm = mmapi.Frame(f) not_root_frm_list.append(frm) # Run solver! sol_list = [] sol = mmapi.SolverStandard() sol.set_root_frame_list(root_frm_list) sol.set_frame_list(not_root_frm_list) sol.set_only_root_frames(False) sol.set_global_solve(False) sol._auto_attr_blocks = True sol._triangulate_bundles = False sol.set_single_frame(False) sol.set_root_frame_strategy(mmapi.ROOT_FRAME_STRATEGY_GLOBAL_VALUE) # sol._robust_loss_type = mmapi.ROBUST_LOSS_TYPE_TRIVIAL_VALUE # sol._robust_loss_scale = 1.0 sol_list.append(sol) col = mmapi.Collection(node='collection1') col.set_attribute_list(cam_attr_list + bnd_attr_list) col.set_solver_list(sol_list) e = time.time() print 'pre=solve time:', e - s s = time.time() solres_list = col.execute() e = time.time() print 'total time:', e - s # Set Deviation mkr_list = col.get_marker_list() mmapi.update_deviation_on_markers(mkr_list, solres_list) mmapi.update_deviation_on_collection(col, solres_list) # save the output name = 'test_solve_solveAllFramesCausesStaticAnimCurves_after.ma' path = self.get_data_path(name) maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) self.checkSolveResults(solres_list) return
def test_solveAllFramesCausesStaticAnimCurves(self): """ Solving with the scene file 'mmSolverBasicSolveB_before.ma', was reported to solve as static values, the same as the initial values. The DG did not evaluate some how and the solve was therefore useless. GitHub Issue #53. """ # Open the Maya file file_name = 'mmSolverBasicSolveB_before.ma' path = self.get_data_path('scenes', file_name) maya.cmds.file(path, open=True, force=True, ignoreVersion=True) # NOTE: We leave these nodes along, since these are already in # the 'correct' position, we are treating these as surveyed. # When we have less than 3 points as survey the solve goes # crazy. dont_touch_these_nodes = [ '|bundle_12_BND', '|bundle_13_BND', '|bundle_14_BND' ] # Triangulate all 3D points. nodes = maya.cmds.ls(type='transform') or [] bnd_nodes = mmapi.filter_bundle_nodes(nodes) bnd_list = [mmapi.Bundle(node=n) for n in bnd_nodes] for bnd in bnd_list: bnd_node = bnd.get_node() if bnd_node in dont_touch_these_nodes: continue attrs = ['translateX', 'translateY', 'translateZ'] for attr_name in attrs: plug = bnd_node + '.' + attr_name maya.cmds.setAttr(plug, lock=False) lib_triangulate.triangulate_bundle(bnd) # Get Bundle attributes to compute. bnd_attr_list = [] for bnd in bnd_list: node = bnd.get_node() attrs = ['translateX', 'translateY', 'translateZ'] for attr_name in attrs: attr = mmapi.Attribute(node=node, attr=attr_name) bnd_attr_list.append(attr) # Camera attributes cam_tfm = 'stA_1_1' cam_attr_list = [] attrs = [ 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ' ] for attr_name in attrs: attr = mmapi.Attribute(node=cam_tfm, attr=attr_name) cam_attr_list.append(attr) # Run solver! s = time.time() # Solve camera transform based on triangulated bundle # positions. col = mmapi.Collection(node='collection1') col.set_attribute_list(cam_attr_list) lib_col.compile_collection(col) solres_list = col.execute() print 'time (solve #1):', time.time() - s # Refine the bundle positions only s2 = time.time() col.set_attribute_list(bnd_attr_list) lib_col.compile_collection(col) solres_list = col.execute() print 'time (solve #2):', time.time() - s2 # # Solve both camera transform and bundle positions together. # s3 = time.time() # col.set_attribute_list(cam_attr_list + bnd_attr_list) # lib_col.compile_collection(col) # solres_list = col.execute() e = time.time() # print 'time (solve #3):', e - s3 print 'total time:', e - s # save the output name = 'test_solve_solveAllFramesCausesStaticAnimCurves_after.ma' path = self.get_data_path(name) maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) self.checkSolveResults(solres_list) return
def test_init(self): """ Single Frame solve. """ # Camera cam_tfm = maya.cmds.createNode('transform', name='cam_tfm') cam_shp = maya.cmds.createNode('camera', name='cam_shp', parent=cam_tfm) maya.cmds.setAttr(cam_tfm + '.tx', -1.0) maya.cmds.setAttr(cam_tfm + '.ty', 1.0) maya.cmds.setAttr(cam_tfm + '.tz', -5.0) cam = mmapi.Camera(shape=cam_shp) # Bundle bnd = mmapi.Bundle().create_node() bundle_tfm = bnd.get_node() maya.cmds.setAttr(bundle_tfm + '.tx', 5.5) maya.cmds.setAttr(bundle_tfm + '.ty', 6.4) maya.cmds.setAttr(bundle_tfm + '.tz', -25.0) assert mmapi.get_object_type(bundle_tfm) == 'bundle' # Marker mkr = mmapi.Marker().create_node(cam=cam, bnd=bnd) marker_tfm = mkr.get_node() assert mmapi.get_object_type(marker_tfm) == 'marker' maya.cmds.setAttr(marker_tfm + '.tx', 0.0) maya.cmds.setAttr(marker_tfm + '.ty', 0.0) # Attributes attr_tx = mmapi.Attribute(bundle_tfm + '.tx') attr_ty = mmapi.Attribute(bundle_tfm + '.ty') # Frames frm_list = [ mmapi.Frame(1, primary=True) ] # Solver sol = mmapi.Solver() sol.set_max_iterations(10) sol.set_solver_type(mmapi.SOLVER_TYPE_DEFAULT) sol.set_verbose(True) sol.set_frame_list(frm_list) # Collection col = mmapi.Collection() col.create_node('mySolveCollection') col.add_solver(sol) col.add_marker(mkr) col.add_attribute(attr_tx) col.add_attribute(attr_ty) # save the output path = self.get_data_path('test_solve_init_before.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) # Run solver! results = col.execute() # Set Deviation mmapi.update_deviation_on_markers([mkr], results) mmapi.update_deviation_on_collection(col, results) # save the output path = self.get_data_path('test_solve_init_after.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) # Ensure the values are correct self.checkSolveResults(results) # assert self.approx_equal(maya.cmds.getAttr(bundle_tfm+'.tx'), -6.0) # assert self.approx_equal(maya.cmds.getAttr(bundle_tfm+'.ty'), 3.6) return
def test_per_frame(self): """ Solve animated values, per-frame. """ # Camera cam_tfm = maya.cmds.createNode('transform', name='cam_tfm') cam_shp = maya.cmds.createNode('camera', name='cam_shp', parent=cam_tfm) maya.cmds.setAttr(cam_tfm + '.tx', -1.0) maya.cmds.setAttr(cam_tfm + '.ty', 1.0) maya.cmds.setAttr(cam_tfm + '.tz', -5.0) cam = mmapi.Camera(shape=cam_shp) # Bundle bnd = mmapi.Bundle().create_node() bundle_tfm = bnd.get_node() maya.cmds.setAttr(bundle_tfm + '.tx', 5.5) maya.cmds.setAttr(bundle_tfm + '.ty', 6.4) maya.cmds.setAttr(bundle_tfm + '.tz', -25.0) assert mmapi.get_object_type(bundle_tfm) == 'bundle' maya.cmds.setKeyframe(bundle_tfm, attribute='translateX', time=1, value=5.5, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(bundle_tfm, attribute='translateY', time=1, value=6.4, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(bundle_tfm, attribute='translateZ', time=1, value=-25.0, inTangentType='linear', outTangentType='linear') # Marker mkr = mmapi.Marker().create_node(cam=cam, bnd=bnd) marker_tfm = mkr.get_node() assert mmapi.get_object_type(marker_tfm) == 'marker' # maya.cmds.setAttr(marker_tfm + '.tx', 0.0) # maya.cmds.setAttr(marker_tfm + '.ty', 0.0) maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=1, value=-0.5, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateX', time=5, value=0.5, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateY', time=1, value=-0.5, inTangentType='linear', outTangentType='linear') maya.cmds.setKeyframe(marker_tfm, attribute='translateY', time=5, value=0.5, inTangentType='linear', outTangentType='linear') # Attributes attr_tx = mmapi.Attribute(bundle_tfm + '.tx') attr_ty = mmapi.Attribute(bundle_tfm + '.ty') # Frames frm_list = [ mmapi.Frame(1), mmapi.Frame(2), mmapi.Frame(3), mmapi.Frame(4), mmapi.Frame(5), ] # Solver sol_list = [] for frm in frm_list: sol = mmapi.Solver() sol.set_max_iterations(10) sol.set_verbose(True) sol.set_frame_list([frm]) sol_list.append(sol) # Collection col = mmapi.Collection() col.create_node('mySolveCollection') col.add_solver_list(sol_list) col.add_marker(mkr) col.add_attribute(attr_tx) col.add_attribute(attr_ty) # save the output path = self.get_data_path('test_solve_per_frame_before.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) # Run solver! results = col.execute() # Set Deviation mmapi.update_deviation_on_markers([mkr], results) mmapi.update_deviation_on_collection(col, results) # save the output path = self.get_data_path('test_solve_per_frame_after.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) self.checkSolveResults(results) return
def test_find_marker_attr_mapping(self): # top level transform root = maya.cmds.createNode('transform', name='top1') # Camera A, don't parent under the root. cam_tfm_a = maya.cmds.createNode('transform', name='camA_tfm') cam_shp_a = maya.cmds.createNode('camera', name='camA_shp', parent=cam_tfm_a) maya.cmds.setAttr(cam_tfm_a + '.tx', -1.0) maya.cmds.setAttr(cam_tfm_a + '.ty', 1.0) maya.cmds.setAttr(cam_tfm_a + '.tz', -5.0) cam_a = mmapi.Camera(shape=cam_shp_a) # Camera B, parent under the root cam_tfm_b = maya.cmds.createNode('transform', name='camB_tfm', parent=root) cam_shp_b = maya.cmds.createNode('camera', name='camB_shp', parent=cam_tfm_b) maya.cmds.setAttr(cam_tfm_b + '.tx', 1.0) maya.cmds.setAttr(cam_tfm_b + '.ty', 1.0) maya.cmds.setAttr(cam_tfm_b + '.tz', -5.0) cam_b = mmapi.Camera(shape=cam_shp_b) # Hierarchy structure dummy = maya.cmds.createNode('transform', name='dummy') dummy = maya.cmds.ls(dummy, long=True)[0] nothing = maya.cmds.createNode('transform', name='nothing') nothing = maya.cmds.ls(nothing, long=True)[0] top = maya.cmds.createNode('transform', name='top2') top = maya.cmds.ls(top, long=True)[0] multDivide = maya.cmds.createNode('multiplyDivide', name='multDiv') multDivide2 = maya.cmds.createNode('multiplyDivide', name='multDivNotValid') child1 = maya.cmds.createNode('transform', name='child1', parent=top) child2 = maya.cmds.createNode('transform', name='child2', parent=child1) child1 = maya.cmds.ls(child1, long=True)[0] child2 = maya.cmds.ls(child2, long=True)[0] # Set up animation and connections maya.cmds.setKeyframe(dummy, at='tx', t=1, v=1) maya.cmds.setKeyframe(dummy, at='tx', t=10, v=10) maya.cmds.connectAttr(dummy + '.tx', child2 + '.ty') maya.cmds.connectAttr(dummy + '.tx', child1 + '.tz') maya.cmds.connectAttr(dummy + '.ty', top + '.tx') maya.cmds.connectAttr(dummy + '.ry', child2 + '.ry') maya.cmds.connectAttr(multDivide + '.outputX', dummy + '.ty') maya.cmds.connectAttr(multDivide2 + '.outputX', dummy + '.tz') maya.cmds.setKeyframe(child2, at='tz', t=1, v=1) maya.cmds.setKeyframe(child2, at='tz', t=10, v=10) maya.cmds.setKeyframe(top, at='ty', t=1, v=1) maya.cmds.setKeyframe(top, at='ty', t=10, v=10) maya.cmds.setKeyframe(top, at='tz', t=1, v=1) maya.cmds.setKeyframe(top, at='tz', t=10, v=10) maya.cmds.setKeyframe(top, at='ry', t=1, v=-90) maya.cmds.setKeyframe(top, at='ry', t=10, v=45) # Bundle bnd = mmapi.Bundle().create_node() bundle_tfm = bnd.get_node() maya.cmds.setKeyframe(bundle_tfm, at='tx', t=1, v=-1) maya.cmds.setKeyframe(bundle_tfm, at='tx', t=10, v=1) maya.cmds.parent(bundle_tfm, child2) bundle_tfm = bnd.get_node() maya.cmds.setAttr(bundle_tfm + '.tx', 5.5) maya.cmds.setAttr(bundle_tfm + '.ty', 6.4) maya.cmds.setAttr(bundle_tfm + '.tz', -25.0) # Marker A mkr_a = mmapi.Marker().create_node(cam=cam_a, bnd=bnd) marker_tfm = mkr_a.get_node() maya.cmds.setAttr(marker_tfm + '.tx', 0.0) maya.cmds.setAttr(marker_tfm + '.ty', 0.0) # Marker B mkr_b = mmapi.Marker().create_node(cam=cam_b, bnd=bnd) marker_tfm = mkr_b.get_node() maya.cmds.setAttr(marker_tfm + '.tx', 0.0) maya.cmds.setAttr(marker_tfm + '.ty', 0.0) mkr_list = [mkr_a, mkr_b] # Attributes attr_tx = mmapi.Attribute(bundle_tfm + '.tx') attr_ty = mmapi.Attribute(bundle_tfm + '.ty') attr_top_tz = mmapi.Attribute(top + '.tz') attr_top_rx = mmapi.Attribute(top + '.rx') attr_child1_ty = mmapi.Attribute(child1 + '.ty') attr_child1_rx = mmapi.Attribute(child1 + '.rx') attr_root_ty = mmapi.Attribute(root + '.ty') attr_cam_a_ty = mmapi.Attribute(cam_tfm_a + '.ty') attr_cam_a_focal = mmapi.Attribute(cam_shp_a + '.focalLength') attr_nothing_tx = mmapi.Attribute(nothing + '.tx') attr_list = [ attr_tx, attr_ty, attr_top_tz, attr_top_rx, attr_child1_ty, attr_child1_rx, attr_root_ty, attr_cam_a_ty, attr_cam_a_focal, attr_nothing_tx, ] # Test getting affected plugs from a base transform node. bnd_node = bnd.get_node() ret = affects_utils.find_plugs_affecting_transform(bnd_node, None) assert (multDivide + '.input1X') in ret assert (dummy + '.translateX') in ret assert (dummy + '.rotateY') in ret assert (dummy + '.translateY') not in ret for r in ret: assert multDivide2 not in r print 'len(ret):', len(ret) assert len(ret) == 103 # Test getting the affect mapping between markers and attrs. ret = mmapi.find_marker_attr_mapping(mkr_list, attr_list) expected = [[ True, True, True, True, True, True, False, True, True, False ], [True, True, True, True, True, True, True, False, False, False]] assert ret == expected # Save the output path = self.get_data_path('find_marker_attr_mapping_after.ma') maya.cmds.file(rename=path) maya.cmds.file(save=True, type='mayaAscii', force=True) return
def rename_markers_and_bundles(mkr_nodes, bnd_nodes, mkr_name, bnd_name, number_format, mkr_suffix, bnd_suffix): """ Rename the given marker and bundle nodes. :param mkr_nodes: Marker nodes to rename. :type mkr_nodes: [str, ..] :param bnd_nodes: Bundle nodes to rename. :type bnd_nodes: [str, ..] :param mkr_name: Rename the markers to this name. :type mkr_name: str :param bnd_name: Rename the bundle to this name. :type bnd_name: str :param number_format: The name format string for numbering. :type number_format: str :param mkr_suffix: Set the marker suffix name. :type mkr_suffix: str :param bnd_suffix: Set the bundle suffix name. :type bnd_suffix: str :returns: Nodes that have been renamed. :rtype: [str, ..] """ # Convert markers to bundles. mkr_nodes = list(mkr_nodes) bnd_nodes = list(bnd_nodes) for node in mkr_nodes: mkr = mmapi.Marker(node=node) bnd = mkr.get_bundle() if bnd is None: continue bnd_node = bnd.get_node() if not bnd_node: continue if bnd_node not in bnd_nodes: bnd_nodes.append(bnd_node) # Rename the bundles. renamed_nodes = [] for i, bnd_node in enumerate(bnd_nodes): num_str = number_format % (i + 1) bnd = mmapi.Bundle(node=bnd_node) new_bnd_name = mmapi.get_new_bundle_name( num_str, prefix=bnd_name, suffix=bnd_suffix ) bnd_node = bnd.get_node() maya.cmds.rename(bnd_node, new_bnd_name) renamed_nodes.append(bnd.get_node()) new_mkr_name = new_bnd_name.replace(bnd_name, mkr_name) new_mkr_name = new_mkr_name.replace(bnd_suffix, mkr_suffix) mkr_list = bnd.get_marker_list() for mkr in mkr_list: mkr_node = mkr.get_node() maya.cmds.rename(mkr_node, new_mkr_name) renamed_nodes.append(mkr.get_node()) return renamed_nodes
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