def hidden_objects(): """ Removes all invisible objects from a scene """ # TODO: Deletes/unconnects shading groups from instanced instances # eg, Screws shared by Rims and shared by transforms hidden_paths = Om.MDagPathArray() last_hidden_node = '' search = '.*?({})' transforms = Om.MDagPathArray() for dag_path in dag_path_iterator(traversal_type=Om.MItDag.kDepthFirst): if dag_path.hasFn(Om.MFn.kTransform) and dag_path.isInstanced( ) and not dag_path.hasFn(Om.MFn.kCamera): if not pm.getAttr(dag_path.partialPathName() + '.visibility'): transforms.append(dag_path) if transforms: LOGGER.info( 'Deleting %s hidden instances. (Instanced transform nodes that have been hidden' 'by the user or importer.', len(transforms)) delete(transforms) for dag_path in dag_path_iterator(traversal_type=Om.MItDag.kDepthFirst): if dag_path.hasFn(Om.MFn.kCamera) or dag_path.isVisible( ) or dag_path.isInstanced(): # Skip cameras, instances, visible objects continue # Check if we are inside a child path of an already added path # !IMPORTANT! works only with iterator set to depthFirst if last_hidden_node: # Returns a match object if we are inside an already known path if re.search(search.format(last_hidden_node), dag_path.fullPathName()): # Skip paths we already know of continue # Unique path to this hidden node(in allmighty unicode encoded as utf-8) last_hidden_node = unicode(dag_path.partialPathName()).encode('utf-8') LOGGER.info('Deleting hidden scene object: %s', dag_path.fullPathName()) hidden_paths.append(dag_path) if hidden_paths: delete(hidden_paths)
def all_lights(): """ Removes all Om.MFn.kLight objects and their transform parents """ light_transform_paths = Om.MDagPathArray() for dag_path in dag_path_iterator(filter_type=Om.MFn.kLight): # Get parent transform node and delete it transform_obj = dag_path.transform() if not transform_obj.isNull(): light_transform_paths.append( Om.MFnDagNode(transform_obj).getPath()) delete(light_transform_paths)
def empty_groups(): """ Brute force delete all empty groups, waiting for inspiration to only delete highest empty hierarchy path """ while True: empty_paths = Om.MDagPathArray() for m_path in dag_path_iterator(): if not m_path.childCount(): empty_paths.append(m_path) if not empty_paths: break delete(empty_paths)
def get_sel_vtces_idcs(): """ Get the indices of the selected vertices. :return: DagPath of the current mesh, indices of the selected vertices :rtype: MDagPathArray, MIntArray """ # Get current selection selection_list = om2.MGlobal.getActiveSelectionList() # Get the dag path and components of the first item in the list obj_dag_path, components = selection_list.getComponent(0) # Initialize MDagPathArray dag_path_list = om2.MDagPathArray() # If no vertices selected if components.isNull(): # Empty list of vertices selected_vertices_indices = om2.MIntArray() # Create iterator sel_iter = om2.MItSelectionList(selection_list) # Create list of dagPath of selected objects while not sel_iter.isDone(): dag_path_list.append(sel_iter.getDagPath()) sel_iter.next() # If vertices are selected else: dag_path_list.append(selection_list.getDagPath(0)) # Query vertex indices fn_components = om2.MFnSingleIndexedComponent(components) # Create an MIntArray with the vertex indices selected_vertices_indices = fn_components.getElements() return { 'obj_path': dag_path_list, 'indices': selected_vertices_indices }