def mirror_hierarchy_joints(): """ Mirror hierarchy joints """ out_dict = {'success': False, 'result': dict()} all_joints = list() valid_joints = get_valid_joints() if not valid_joints: out_dict['msg'] = 'No joints to mirror found.' return out_dict for joint in valid_joints: all_joints.append(joint) children = dcc.list_children(joint, full_path=False, children_type='transform') found = list() if children: for child in children: if dcc.node_type(child).find('Constraint') > -1: continue found.append(child) all_joints.extend(found) all_joints = list(set(all_joints)) dcc.mirror_transform(transforms=all_joints, create_if_missing=True) out_dict['success'] = True return out_dict
def _get_twist_axis(self): if self._twist_joint: twist_joint = self._twist_joint else: twist_joint = self._twist_driver driver_children = dcc.list_children(twist_joint) if not driver_children: twist_joint = self._twist_driven return xform_utils.get_axis_aimed_at_child(twist_joint)
def _get_twist_axis(self, as_letter=False): if self._twist_joint: twist_joint = self._twist_joint else: twist_joint = self._twist_driver driver_children = dcc.list_children(twist_joint) if not driver_children: twist_joint = self._twist_driven if as_letter: return xform_utils.get_axis_letter_aimed_at_child(twist_joint) else: return xform_utils.get_axis_aimed_at_child(twist_joint)
def manual_orient_joints(self, data, reply): orient_type = data.get('orient_type', 'add') x_axis = data.get('x_axis', 0.0) y_axis = data.get('y_axis', 0.0) z_axis = data.get('z_axis', 0.0) affect_children = data.get('affect_children', False) if orient_type == 'add': tweak = 1.0 else: tweak = -1.0 tweak_rot = [x_axis * tweak, y_axis * tweak, z_axis * tweak] joints = dcc.selected_nodes_of_type(node_type='joint') if not joints: return for jnt in joints: dcc.set_node_rotation_axis_in_object_space(jnt, tweak_rot[0], tweak_rot[1], tweak_rot[2]) dcc.zero_scale_joint(jnt) dcc.freeze_transforms(jnt, preserve_pivot_transforms=True) if affect_children: childs = dcc.list_children( jnt, children_type=['transform', 'joint'], full_path=False, all_hierarchy=True) or list() for child in childs: parent = dcc.node_parent(child) dcc.set_parent_to_world(child) dcc.set_node_rotation_axis_in_object_space( child, tweak_rot[0], tweak_rot[1], tweak_rot[2]) dcc.zero_scale_joint(child) dcc.freeze_transforms(child, preserve_pivot_transforms=True) dcc.set_parent(child, parent) dcc.select_node(joints, replace_selection=True) reply['success'] = True
def reset_joints_orient_to_world(self, data, reply): apply_to_hierarchy = data.get('apply_to_hierarchy', False) if apply_to_hierarchy: dcc.select_hierarchy() joints = dcc.selected_nodes_of_type(node_type='joint', full_path=False) if not joints: reply['msg'] = 'No joints selected' reply['success'] = False return for jnt in reversed(joints): childs = dcc.list_children(jnt, all_hierarchy=False, children_type=['transform', 'joint']) # If the joints has direct childs, unparent that childs and store names if childs: if len(childs) > 0: childs = dcc.set_parent_to_world(childs) # Get parent of this joints for later use parent = dcc.node_parent(jnt, full_path=False) or '' if parent: dcc.set_parent_to_world(jnt) # Clear joint axis dcc.zero_scale_joint(jnt) dcc.freeze_transforms(jnt, preserve_pivot_transforms=True) dcc.zero_orient_joint(jnt) # Reparent if parent: dcc.set_parent(jnt, parent) # Reparent child if childs: if len(childs) > 0: dcc.set_parent(childs, jnt) dcc.select_node(joints, replace_selection=True) reply['success'] = True
def set_rotation_axis(self, data, reply): rotation_axis = data.get('rotation_axis', '') affect_children = data.get('affect_children', False) sel = dcc.selected_nodes_of_type( node_type=['joint', 'transform']) or list() for obj in sel: dcc.set_rotation_axis(obj, rotation_axis) if affect_children: childs = dcc.list_children( obj, children_type=['transform', 'joint'], full_path=True, all_hierarchy=True) or list() for child in childs: dcc.set_rotation_axis(child, rotation_axis) reply['success'] = True
def get_objects_to_rename(hierarchy_check, selection_type, uuid=False): search_hierarchy = hierarchy_check search_selection = True if selection_type == 0 else False if not search_selection: objs_to_rename = dcc.all_scene_nodes(full_path=True) else: objs_to_rename = dcc.selected_nodes(full_path=True) if not objs_to_rename: LOGGER.warning('No objects to rename!') return if search_hierarchy: children_list = list() for obj in objs_to_rename: children = dcc.list_children(obj, all_hierarchy=True, full_path=True) if children: children_list.extend(children) children_list = list(set(children_list)) objs_to_rename.extend(children_list) if uuid and dcc.is_maya(): import tpDcc.dccs.maya as maya handles_list = list() # objs_to_rename = [obj for obj in objs_to_rename if dcc.node_type(obj) == 'transform'] for obj in objs_to_rename: mobj = maya.OpenMaya.MObject() sel = maya.OpenMaya.MSelectionList() sel.add(obj) sel.getDependNode(0, mobj) handle = maya.OpenMaya.MObjectHandle(mobj) handles_list.append(handle) return handles_list else: # We reverse the list so we update first children and later parents, otherwise we will have # problems during renaming if we use full paths objs_to_rename.reverse() return objs_to_rename
def set_manual_orient_joints(self, data, reply): x_axis = data.get('x_axis', 0.0) y_axis = data.get('y_axis', 0.0) z_axis = data.get('z_axis', 0.0) affect_children = data.get('affect_children', False) childs = list() tweak_rot = [x_axis, y_axis, z_axis] joints = dcc.selected_nodes_of_type(node_type='joint', full_path=False) if not joints: return for jnt in joints: if not affect_children: childs = dcc.list_children( jnt, children_type=['transform', 'joint'], full_path=False, all_hierarchy=False) or list() for child in childs: dcc.set_parent_to_world(child) # Set the rotation axis for i, axis in enumerate(['x', 'y', 'z']): dcc.set_attribute_value(jnt, 'jointOrient{}'.format(axis.upper()), tweak_rot[i]) # Clear joint axis dcc.zero_scale_joint(jnt) dcc.freeze_transforms(jnt, preserve_pivot_transforms=True) if childs: for child in childs: dcc.set_parent(child, jnt) dcc.select_node(joints, replace_selection=True) reply['success'] = True
def refresh(self, selected_objects=False, hierarchy=False): self._names_list.clear() self._names_list.setSortingEnabled(True) try: objs_names = list() if not selected_objects: objs_names.extend(dcc.all_scene_nodes(full_path=True)) else: objs_names.extend(dcc.selected_nodes(full_path=True)) if objs_names and hierarchy: children_list = list() for obj in objs_names: children = dcc.list_children(obj, all_hierarchy=True, full_path=True) if children: children_list.extend(children) children_list = list(set(children_list)) objs_names.extend(children_list) self._update_names_list(objs_names) self._on_filter_names_changed(self._names_filter.get_text()) finally: self._names_list.setSortingEnabled(False)
def auto_rename(self, tokens_dict, unique_id=True, last_joint_end=True): import maya.cmds active_rule = self._model.active_rule if not active_rule: LOGGER.warning('Impossible to auto rename because no active rule defined.') return False rule_name = active_rule.name hierarchy_check = self._model.hierarchy_check selection_type = self._model.selection_type rename_shape = self._model.rename_shape objs_to_rename = utils.get_objects_to_rename( hierarchy_check=hierarchy_check, selection_type=selection_type, uuid=False) or list() if not objs_to_rename: LOGGER.warning('No objects to rename. Please select at least one object!') return False # generated_names = self.generate_names(items=objs_to_rename, **kwargs) if not self._naming_lib.has_rule(rule_name): return False current_rule = self._naming_lib.active_rule() self._naming_lib.set_active_rule(rule_name) # TODO: Naming config should be define the name of the rule to use when using auto renaming solved_names = dict() if rule_name == 'node' and self._model.config: auto_suffix = self._model.naming_config.get('auto_suffixes', default=dict()) if auto_suffix: solved_names = dict() for i, obj_name in enumerate(reversed(objs_to_rename)): obj_uuid = maya.cmds.ls(obj_name, uuid=True)[0] if obj_uuid in solved_names: LOGGER.warning( 'Node with name: "{} and UUID "{}" already renamed to "{}"! Skipping ...'.format( obj_name, obj_uuid, solved_names[obj_name])) continue # TODO: This code is a duplicated version of the one in # tpDcc.dccs.maya.core.name.auto_suffix_object function. Move this code to a DCC specific function obj_type = maya.cmds.objectType(obj_name) if obj_type == 'transform': shape_nodes = maya.cmds.listRelatives(obj_name, shapes=True, fullPath=True) if not shape_nodes: obj_type = 'group' else: obj_type = maya.cmds.objectType(shape_nodes[0]) elif obj_type == 'joint': shape_nodes = maya.cmds.listRelatives(obj_name, shapes=True, fullPath=True) if shape_nodes and maya.cmds.objectType(shape_nodes[0]) == 'nurbsCurve': obj_type = 'controller' else: children = dcc.list_children(obj_name) if not children and last_joint_end: obj_type = 'jointEnd' if obj_type == 'nurbsCurve': connections = maya.cmds.listConnections('{}.message'.format(obj_name)) if connections: for node in connections: if maya.cmds.nodeType(node) == 'controller': obj_type = 'controller' break if obj_type not in auto_suffix: rule_name = 'node' node_type = obj_type else: rule_name = auto_suffix[obj_type] node_type = auto_suffix[obj_type] if 'node_type' in tokens_dict and tokens_dict['node_type']: node_type = tokens_dict.pop('node_type') node_name = dcc.node_short_name(obj_name) if 'description' in tokens_dict and tokens_dict['description']: description = tokens_dict['description'] else: description = node_name side = tokens_dict.get('side', None) if unique_id: solved_name = self._naming_lib.solve( description, side=side, node_type=node_type, id=i) else: solved_name = self._naming_lib.solve( description, side=side, node_type=node_type) if not solved_name: continue solved_name = dcc.find_unique_name(solved_name) solved_names[obj_uuid] = solved_name if solved_names: for obj_id, solved_name in solved_names.items(): obj_name = maya.cmds.ls(obj_id, long=True)[0] dcc.rename_node(obj_name, solved_name, uuid=obj_id, rename_shape=rename_shape) else: for obj_name in objs_to_rename: solve_name = self._naming_lib.solve(**tokens_dict) if not solve_name: LOGGER.warning( 'Impossible to rename "{}" with rule "{}" | "{}"'.format(obj_name, rule_name, tokens_dict)) continue try: dcc.rename_node(obj_name, solve_name, rename_shape=rename_shape) except Exception as exc: LOGGER.error('Impossible to rename "{}" to "{}" | {}'.format(obj_name, solve_name, exc)) continue if current_rule: self._naming_lib.set_active_rule(current_rule.name)
def orient_joints(self, data, reply): aim_axis_index = data.get('aim_axis_index', 0.0) aim_axis_reverse = data.get('aim_axis_reverse', False) up_axis_index = data.get('up_axis_index', 0.0) up_axis_reverse = data.get('up_axis_reverse', False) up_world_axis_x = data.get('up_world_axis_x', 0.0) up_world_axis_y = data.get('up_world_axis_y', 0.0) up_world_axis_z = data.get('up_world_axis_z', 0.0) apply_to_hierarchy = data.get('apply_to_hierarchy', False) reset_joints = list() # Get up and aim axis aim_axis = [0, 0, 0] up_axis = [0, 0, 0] world_up_axis = [up_world_axis_x, up_world_axis_y, up_world_axis_z] if aim_axis_index == up_axis_index: LOGGER.warning( 'aim and up axis are the same, maybe orientation wont work correctly!' ) aim_axis_reverse_value = 1.0 if not aim_axis_reverse else -1.0 up_axis_reverse_value = 1.0 if not up_axis_reverse else -1.0 aim_axis[aim_axis_index] = aim_axis_reverse_value up_axis[up_axis_index] = up_axis_reverse_value # Get selected joints if apply_to_hierarchy: dcc.select_hierarchy() joints = dcc.selected_nodes_of_type(node_type='joint', full_path=False) if not joints: reply['msg'] = 'No joints selected' reply['success'] = False return for jnt in reversed(joints): childs = dcc.list_children(jnt, all_hierarchy=False, children_type=['transform', 'joint']) # If the joints has direct childs, unparent that childs and store names if childs: if len(childs) > 0: childs = dcc.set_parent_to_world(childs) childs = python.force_list(childs) # Get parent of this joints for later use parent = '' parents = dcc.node_parent(jnt) if parents: parent = parents[0] # Aim to the child aim_target = '' if childs: for child in childs: if dcc.node_type(child) == 'joint': aim_target = child break if aim_target != '': # Apply an aim constraint from the joint to its child (target) dcc.delete_node( dcc.create_aim_constraint(jnt, aim_target, aim_axis=aim_axis, up_axis=up_axis, world_up_axis=world_up_axis, world_up_type='vector', weight=1.0)) # Clear joint axis dcc.zero_scale_joint(jnt) dcc.freeze_transforms(jnt, preserve_pivot_transforms=True) elif parent != '': reset_joints.append(jnt) # Reparent child if childs: if len(childs) > 0: dcc.set_parent(childs, jnt) for jnt in reset_joints: # If there is no target, the joint will take its parent orientation for axis in ['x', 'y', 'z']: dcc.set_attribute_value( jnt, 'jointOrient{}'.format(axis.upper()), dcc.get_attribute_value(jnt, 'r{}'.format(axis))) dcc.set_attribute_value(jnt, 'r{}'.format(axis), 0) dcc.select_node(joints, replace_selection=True) reply['success'] = True