# -*- coding: utf-8 -*- """ :author: Jeremy Ernst :description: This module contains a class that represents a joint mover object. A joint mover object is meant to be instantiated in the __init__ of the base_component class. Nothing should inherit from the JointMover class. The main methods a JointMover handles are: adding the joint mover file, renaming movers, setting up the hierarchy, locking or unlocking movers, retrieving mover-related data, and other joint mover specific operations. """ import os import pymel.core as pm import artv2.utilities.general_utilities as utils import artv2.utilities.joint_mover_utilities as dev_utils import artv2.utilities.component_utilities as component_utils import artv2.utilities.error_utilities as errors import artv2.tools.system.logger.output_logger as logger from artv2.components.base_components.aim_helper import AimHelper class JointMover(object): """ Class for creating and working with a joint mover rig, which is a simplified rig used to aid in the creation and placement of a skeleton, keeping it clean and consistent. The JointMover is instantiated by a component and is passed that component's network_node. """ def __init__(self, path, metanode): """ :param path: Path to joint mover file. :param metanode: This is the network node from a component, like a leg or torso. It contains all of the metadata """ self.logger = logger.OutputLogger(self.__class__.__name__) self._path = path self._metanode = pm.PyNode(metanode) self.aim_helper = AimHelper(self._metanode) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def add_joint_mover(self): """ Adds the joint mover maya ascii file to the maya scene. """ tools_directory = utils.return_settings()[0] joint_mover_path = utils.path_join(tools_directory, self._path) self.logger.info("{0}.add_joint_mover: Importing joint mover file for {1}" "".format(self.__class__.__name__, self.metanode.componentName.get())) if not os.path.exists(joint_mover_path): self.logger.warning("{0}.add_joint_mover: The specified file does not exist: {1}" "".format(self.__class__.__name__, joint_mover_path)) errors.raise_error("The specified joint mover file does not exist: {0}".format(joint_mover_path)) return else: nodes = utils.import_file(joint_mover_path, return_nodes=True) joints = utils.filter_nodes(nodes, filter_by="joint") root_joint = utils.get_root_joint_of_component(joints) for joint in utils.convert_to_pynodes(joints): joint.overrideEnabled.set(1) joint.overrideDisplayType.set(2) self.create_joint_mover_controls(root_joint) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def create_joint_mover_controls(self, root_joint): """ Creates the joint mover controls on the joints that come in from the ascii file. :param root_joint: Root joint of the hierarchy from the joint mover ascii file. """ self.logger.info("{0}.create_joint_mover_controls: Creating controls for the guide joints on {1}." "".format(self.__class__.__name__, self.metanode.componentName.get())) created_movers = dev_utils.create_movers_from_joints(self.metanode.componentName.get(), root_joint) dev_utils.mark_joint_movers(created_movers, self.metanode.componentName.get(), self.metanode.prefix.get(), self.metanode.suffix.get()) dev_utils.drive_joints_with_movers(created_movers, self.metanode.componentName.get()) self._connect_movers_to_metanode(created_movers) movers = self.get_movers() for each in [movers.get("global"), movers.get("offset")]: for mover in each: component_utils.setup_global_scale(mover) self._lock_joint_movers(lock_children=True) containers = self.get_containers() pm.lockNode(containers[2], lock=True, lockUnpublished=True) pm.lockNode(containers[1], lock=True, lockUnpublished=False) pm.lockNode(containers[0], lock=True, lockUnpublished=False) self.select_top_mover() # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def get_containers(self): """ Get and return the containers that our joint mover controls and utility nodes are contained in. :return: PyNodes of containers that our joint mover controls and utility nodes reside in. """ guide_container = pm.container(q=True, findContainer=[self.return_top_mover_grp()]) top_container = pm.container(q=True, findContainer=[guide_container]) utils_container = top_container.utils_container.connections()[0] return [top_container, guide_container, utils_container] # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def get_created_joints(self): """ Finds and returns the joints created by this JointMover. :return: string array of joint names """ movers = self.get_movers() offset_movers = movers.get("offset") joints = [] for offset in offset_movers: if offset.build_joint.get() is True: joints.append(offset.created_joint.get()) return joints # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def get_driven_joints(self): """ Return the joint mover joints currently driven by the movers (these are not the same as the created joints). :return: List of PyNodes representing the joint mover joints. """ movers = self.get_movers() offset_movers = movers.get("offset") joints = [] for offset in offset_movers: joints.append(offset.connected_joint.connections()[0]) return joints # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def get_movers(self): """ Finds and returns the joint mover control objects created by the JointMover. :return: Dictionary of joint movers with the keys: global and offset. """ global_movers, offset_movers = [], [] for attr in [["global_movers", global_movers], ["offset_movers", offset_movers]]: if self.metanode.hasAttr(attr[0]): connections = self.metanode.attr(attr[0]).connections() if connections is not None: attr[1].extend(connections) movers = {"global": global_movers, "offset": offset_movers} return movers # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _lock_joint_movers(self, lock=True, lock_children=False): """ Method for locking down the joint mover nodes so they cannot be deleted (or unlocking them). :param lock: Whether to lock or unlock the nodes. :param lock_children: Whether to lock all children of the nodes, whether they are movers or not. :type lock: bool :type lock_children: bool """ joint_movers = self.get_movers() for mover_type in joint_movers: movers = joint_movers.get(mover_type) container = pm.container(q=True, findContainer=[mover for mover in movers]) if container is not None: container.unlock() for mover in movers: pm.lockNode(mover, lock=lock) if lock_children: pm.select(mover, hi=True) children = pm.ls(sl=True) for child in children: pm.lockNode(child, lock=lock) if container is not None: container.lock() # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def remove_movers(self): """ Remove the generated joint mover controls from the driven joints (as well as any associated containers, nodes). """ self.logger.info("{0}.remove_movers: Removing movers for {1}".format(self.__class__.__name__, self.metanode.componentName.get())) current_state = self.metanode.pinned.get() if not current_state: self.toggle_pin_component() self._lock_joint_movers(lock=False, lock_children=True) mover_data = self.get_movers() containers = self.get_containers() for container in containers: container.unlock() utility_nodes = containers[2].getNodeList() for node in utility_nodes: node.unlock() pm.delete(node) for mover_type in mover_data: movers = mover_data.get(mover_type) for mover in movers: if pm.objExists(mover): pm.delete(mover) top_grp = self.return_top_mover_grp() children = top_grp.getChildren() top_grp.unlock() for child in children: child.unlock() pm.delete(top_grp) joints = containers[1].getNodeList() containers[1].removeNode(joints) for joint in joints: if joint.hasAttr("otherType"): original_name = joint.attr("otherType").get() utils.rename_object(joint, original_name) for container in containers: if pm.objExists(container): pm.delete(container) if current_state is False: utils.set_attribute(self.metanode, "pinned", False) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def rename_movers(self, old_prefix, old_suffix): """ Renames joint movers with the new component prefix and/or suffix. :param str old_prefix: The old prefix to search/replace :param str old_suffix: The old suffix to search/replace """ self.logger.info("{0}.rename_movers: Renaming movers for {1}".format(self.__class__.__name__, self.metanode.componentName.get())) prefix = self.metanode.prefix.get() suffix = self.metanode.suffix.get() mover_grp = self.metanode.mover_grp.connections()[0] pm.select(mover_grp, hi=True) selection = pm.ls(sl=True, long=True) rename_nodes = self._find_nodes_to_rename(selection, old_prefix, old_suffix) rename_data = [] for each in rename_nodes: if each[0].hasAttr("created_joint"): original_name = each[0].created_joint.get() current_name = utils.strip_name(original_name, old_prefix, None, old_suffix) utils.set_attribute(each[0], "created_joint", prefix + current_name + suffix, "string") self.logger.debug("original name: {0}, prefix: {1}, current_name: {2}, suffix: {3}".format( original_name, prefix, current_name, suffix)) rename_data.append([original_name, prefix + current_name + suffix]) guide_joint = each[0].connected_joint.connections()[0] utils.rename_object(guide_joint, self.metanode.componentName.get() + "_" + current_name) utils.rename_object(each[0], each[1]) self._rename_dependent_attrs(rename_data) pm.select(clear=True) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def return_top_mover_grp(self): """ Finds the top-most global mover group and returns it. :return: string name of the top-most global mover group. """ top_mover_grp = self.metanode.mover_grp.connections()[0] if not pm.objExists(top_mover_grp): self.logger.error("{0}.return_top_mover_grp: {1} does not exist. This should never be the case!" "".format(self.__class__.__name__, top_mover_grp)) errors.raise_error("{0} does not exist! This should not be the case!".format(top_mover_grp)) return top_mover_grp # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def set_mover_parent(self, parent): """ Parents the top-most group in the joint mover file under the given parent joint's global mover. :param str parent: Name of parent joint. """ self.logger.info("{0}.set_mover_parent: Setting the mover parent to {1} for {2}" "".format(self.__class__.__name__, parent, self.metanode.componentName.get())) parent_mover = component_utils.find_associated_mover_from_joint(parent) top_mover = self.return_top_mover_grp() parent_constraint = top_mover.translateX.connections(type="parentConstraint", exactType=True) scale_constraint = top_mover.scaleX.connections(type="scaleConstraint", exactType=True) if parent_constraint: if pm.objExists(parent_constraint[0]): parent_constraint[0].unlock() pm.delete(parent_constraint[0]) if scale_constraint: if pm.objExists(scale_constraint[0]): scale_constraint[0].unlock() pm.delete(scale_constraint[0]) if pm.objExists(parent_mover): new_parent_constraint = pm.parentConstraint(parent_mover, top_mover, mo=True) new_scale_constraint = pm.scaleConstraint(parent_mover, top_mover, mo=True) new_parent_constraint.lock() new_scale_constraint.lock() else: self.logger.warning("{0}.set_mover_parent: Could not parent {1} to {2}. {2} does not exist." "".format(self.__class__.__name__, top_mover, parent_mover)) errors.raise_error("Could not parent {0} to {1}. {1} does not exist.".format(top_mover, parent_mover)) return # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def toggle_build_joint(self, joint, mover, build=True): """ Toggles the visibility of the given joint and associated movers, as well as setting the build state. :param joint: The guide joint (PyNode) to toggle visiblity on. :param mover: The offset mover (PyNode) to set the build state on and toggle visibility on. :param build: The boolean value of whether to build the joint or not. """ self.logger.info("{0}.toggle_build_joint: Toggling {1} to build={2}.".format(self.__class__.__name__, joint, build)) alert_user = None created_joint = mover.created_joint.get() issues = component_utils.check_for_children(created_joint) if issues: alert_user = component_utils.fix_dependencies(issues) self._lock_joint_movers(False, True) mover.build_joint.set(build) if build: utils.set_attribute(joint, "drawStyle", 0) if not build: utils.set_attribute(joint, "drawStyle", 2) global_mover = mover.nodeName().rpartition("_offset")[0] asset = self.metanode.asset.connections()[0] if pm.objExists(global_mover): mover_node = pm.PyNode(global_mover) if asset.hasAttr("global_mover_visibility"): if asset.global_mover_visibility.get(): mover_node.getShape().visibility.set(build) else: mover_node.getShape().visibility.set(build) if mover: if asset.hasAttr("offset_mover_visibility"): if asset.offset_mover_visibility.get(): mover.getShape().visibility.set(build) else: mover.getShape().visibility.set(build) self._lock_joint_movers(lock_children=True) return alert_user # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def toggle_pin_component(self): """ Toggles whether the component is pinned, meaning if pinned, the component is unaffected by any transforms of its parent. """ current_state = self.metanode.pinned.get() self.logger.info("{0}.toggle_pin_component: Setting component pinning to {1} for {2}" "".format(self.__class__.__name__, not current_state, self.metanode.componentName.get())) if current_state: utils.set_attribute(self.metanode, "pinned", False) parent = self.metanode.parentComponentBone.get() if parent: self.set_mover_parent(parent) else: utils.set_attribute(self.metanode, "pinned", True) top_mover = self.return_top_mover_grp() parent_constraint = top_mover.translateX.connections(type="parentConstraint", exactType=True) scale_constraint = top_mover.scaleX.connections(type="scaleConstraint", exactType=True) if parent_constraint: if pm.objExists(parent_constraint[0]): parent_constraint[0].unlock() pm.delete(parent_constraint[0]) if scale_constraint: if pm.objExists(scale_constraint[0]): scale_constraint[0].unlock() pm.delete(scale_constraint[0]) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def bake_offsets(self): """ Ensures that the global movers are aligned with the offset movers, so that the offsets are once again zeroed out. This function runs twice to account for aim mode, which changes the orientation of an offset mover (on a group above) when a global mover is moved. The second run cleans up the orientation offsets that were added due to this. """ self.logger.info("{0}.bake_offsets: Baking offsets on {1}.".format(self.__class__.__name__, self.metanode.componentName.get())) pm.undoInfo(openChunk=True) for _ in range(2): movers = self.get_movers() offset_movers = movers.get("offset") position_data, locators = [], [] for offset in offset_movers: connected_joint = offset.connected_joint.connections()[0] if not connected_joint.twistJoint.get(): locator = pm.spaceLocator() locators.append(locator) pm.delete(pm.parentConstraint(offset, locator)) global_mover = str(offset.rpartition("_offset")[0]) position_data.append([global_mover, locator]) self._reset_movers(offset_movers) constraints = [] for data in position_data: global_mover = data[0] locator = data[1] if pm.objExists(global_mover): mover = pm.PyNode(global_mover) constraints.append(pm.parentConstraint(locator, mover)) pm.delete(constraints) pm.delete(locators) pm.undoInfo(closeChunk=True) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def reset_movers(self, mover_type): """ Resets the joint movers to their default transform values. :param str mover_type: Valid options are "all", "offset", "global". """ self.logger.info("{0}.reset_movers: Resetting movers for {1}".format(self.__class__.__name__, self.metanode.componentName.get())) movers = self.get_movers() pm.undoInfo(openChunk=True) if mover_type == "all": self._reset_movers(movers.get("offset")) self._reset_movers(movers.get("global")) if mover_type == "offset": self._reset_movers(movers.get("offset")) if mover_type == "global": self._reset_movers(movers.get("global")) pm.undoInfo(closeChunk=True) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def copy_transforms(self, template=False): """ Copies the translate, rotate, and scale values of the joint mover control and stores them to a file on disk. """ self.logger.info("{0}.copy_transforms: Copying {1}'s mover transforms." "".format(self.__class__.__name__, self.metanode.componentName.get())) movers = self.get_movers() all_movers = movers.get("offset") all_movers.extend(movers.get("global")) all_movers.append(self.metanode.mover_grp.connections()[0]) transform_data = {} attributes = ["translateX", "translateY", "translateZ", "rotateX", "rotateY", "rotateZ", "scaleX", "scaleY", "scaleZ"] for mover in all_movers: self.logger.debug("{0}.copy_transforms: mover = {1}".format(self.__class__.__name__, mover)) attribute_data = {} for each in attributes: self.logger.debug("{0}.copy_transforms: attribute = {1}".format(self.__class__.__name__, each)) if mover.attr(each).isKeyable(): attribute_data[each] = mover.attr(each).get() inst = component_utils.get_component_instance(self.metanode) basename = utils.strip_name(mover.nodeName(), inst.prefix, inst.network_node.baseName.get(), inst.suffix) self.logger.debug("{0}.copy_transforms: dict entry= {1}, {2}".format(self.__class__.__name__, basename, attribute_data)) if not template: transform_data[basename] = attribute_data utils.create_clipboard("artv2JMxForms", transform_data) else: transform_data[mover.nodeName()] = attribute_data return transform_data # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def paste_transforms(self, mirror=False, template=False, data=None): """ Opens the file on disk that has clipboard data for copied transforms, and sets any applicable data on the joint mover controls. :param mirror: Whether to mirror the values when pasting. :param template: Whether this is being called from loading a template. :param data: (optional) data of transforms to paste rather than using clipboard. """ self.logger.info("{0}.paste_transforms: Pasting mover transforms from clipboard onto {1}'s movers." "".format(self.__class__.__name__, self.metanode.componentName.get())) if not template: clipboard = utils.retrieve_clipboard("artv2JMxForms") data = utils.read_clipboard(clipboard) pm.undoInfo(openChunk=True) if data is not None: inst = component_utils.get_component_instance(self.metanode) for key in data: self.logger.debug("{0}.paste_transforms: {1}".format(self.__class__.__name__, key)) if not template: mover_name = inst.prefix + inst.network_node.baseName.get() + inst.suffix + key else: mover_name = key self.logger.debug("{0}.paste_transforms: constructed mover name = {1}".format(self.__class__.__name__, mover_name)) if pm.objExists(mover_name): self._paste_transforms(mover_name, data, key, mirror) del inst pm.undoInfo(closeChunk=True) else: self.logger.warning("{0}.paste_transforms: No clipboard exists.".format(self.__class__.__name__)) errors.raise_error("No clipboard exists!") # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _paste_transforms(self, mover_name, data, key, mirror): inst = component_utils.get_component_instance(self.metanode) self.logger.debug("{0}.paste_transforms: Constructed mover ({1}) exists." "".format(self.__class__.__name__, mover_name)) mover_node = pm.PyNode(mover_name) attribute_data = data.get(key) self.logger.debug("{0}.paste_transforms: attribute data for mover = {1}" "".format(self.__class__.__name__, attribute_data)) for each in attribute_data: attr_value = attribute_data.get(each) self.logger.debug("{0}.paste_transforms: {1} value is {2}".format(self.__class__.__name__, each, attr_value)) if mover_node.attr(each).isKeyable(): if mirror: multiplier = inst.mirror_table.get(each) if not multiplier: multiplier = 1 self.logger.debug("{0}.paste_transforms: Setting {1}.{2} to {3}" "".format(self.__class__.__name__, mover_name, each, attr_value * multiplier)) mover_node.attr(each).set(attr_value * multiplier) else: self.logger.debug("{0}.paste_transforms: Setting {1}.{2} to {3}" "".format(self.__class__.__name__, mover_name, each, attr_value)) mover_node.attr(each).set(attr_value) del inst # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def mirror_transforms(self): """ Mirrors joint mover transforms to the owning component's mirror (if it has one). example usage: .. code-block:: python asset = rig_asset.ART_RigAsset() leg = leg.BipedLeg(prefix="l") mirror = leg.create_mirror(prefix="r", suffix="", parent="root") leg.joint_mover.mirror_transforms() """ pm.undoInfo(openChunk=True) if self.metanode.has_mirror.get(): mirror = self.metanode.mirror_component.connections()[0] self.logger.info("{0}.mirror_transforms: Mirroring {1}'s transforms to {2}." "".format(self.__class__.__name__, self.metanode.componentName.get(), mirror)) mirror_inst = component_utils.get_component_instance(mirror) source_inst = component_utils.get_component_instance(self.metanode) self.copy_transforms() current_state = mirror_inst.joint_mover.metanode.pinned.get() if not current_state: mirror_inst.joint_mover.toggle_pin_component() mirror_inst.joint_mover.paste_transforms(mirror=True) component_utils.mirror_top_mover_grp(source_inst, mirror_inst) mirror_inst.joint_mover.toggle_pin_component() pm.undoInfo(closeChunk=True) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def toggle_visibility(self, state): """ Toggles visibility of joint mover nodes. """ guide_container = self.get_containers()[1] nodes = guide_container.getNodeList() self.metanode.movers_visible.set(state) for node in nodes: if node.getParent() is None: utils.set_attribute(node, "visibility", state) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def toggle_global_visibility(self, state): """ Toggles the visibility of the global movers' shape nodes whose joints are set to be built. :param state: The visibility value to set on the global mover shapes. """ movers = self.get_movers() global_movers = movers.get("global") for mover in global_movers: offset_name = "{0}_offset".format(mover.nodeName()) if pm.objExists(offset_name): offset_node = pm.PyNode(offset_name) if offset_node.build_joint.get(): shape = mover.getShape() shape.visibility.set(state) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def toggle_offset_visibility(self, state): """ Toggles the visibility of the offset movers' shape nodes whose joints are set to be built. :param state: The visibility value to set on the offset mover shapes. """ movers = self.get_movers() offset_movers = movers.get("offset") for mover in offset_movers: if mover.build_joint.get(): shape = mover.getShape() shape.visibility.set(state) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def select_top_mover(self): """ Selects the top-most joint mover control for this component. """ pynode = pm.PyNode(self.metanode.mover_grp.connections()[0]) mover = component_utils.get_top_level_mover(pynode) pm.select(mover.fullPath()) pm.setToolTo("moveSuperContext") # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _find_nodes_to_rename(self, nodes, old_prefix, old_suffix): """ Given a list of nodes, find only the transforms and rename them. :param nodes: A list of PyNodes :param old_prefix: The old prefix. :param old_suffix: The old suffix. :return: Return a list of nodes(PyNodes) to rename, with their new name (str): [[node1, newName], [node2, newName]] """ unique_name = self.metanode.componentName.get() base_name = self.metanode.baseName.get() rename_nodes = [] for node in nodes: types = pm.nodeType(node.name(), inherited=True) if "transform" in types: if base_name in node.name(): tail = utils.strip_name(node.name(), old_prefix, base_name, old_suffix) new_name = unique_name + tail self.logger.debug("{0}._find_nodes_to_rename: tail: {1}, new_name: {2}, node: {3}".format( self.__class__.__name__, tail, new_name, node.name())) rename_nodes.append([node, new_name]) return rename_nodes # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _connect_movers_to_metanode(self, nodes): """ Connect the joint mover controls to the owning component's metanode. :param nodes: a list of joint mover controls (PyNodes) """ for key in nodes: mover_nodes = nodes.get(key) for node in mover_nodes: if node.hasAttr("isMoverGrp"): self.metanode.mover_grp.connect(node.owning_component) if node.hasAttr("isMover"): mover_type = node.moverType.get() mover_types = {"global": "global_movers", "offset": "offset_movers"} for each in mover_types: if mover_type == each: self.metanode.attr(mover_types.get(each)).connect(node.owning_component) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @staticmethod def set_mover_values(value_data): """ Sets values on the joint mover controls. :param value_data: The values to set on the controls. It should be formatted like the dictionary generated by the get_mover_values method. """ for mover in value_data: mover_node = pm.PyNode(mover) attribute_data = value_data.get(mover) for attribute in attribute_data: attribute_value = attribute_data.get(attribute) if pm.objExists(mover_node): if mover_node.hasAttr(attribute): mover_node.attr(attribute).set(attribute_value) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @staticmethod def _rename_dependent_attrs(renamed_nodes): """ Renames the parentComponentBone attribute on any components that had a bone that was renamed. :param renamed_nodes: List of PyNodes and their new names. Used to check if the parentComponentBone attribute value is in that list, and if so, will then rename the attribute value for parentComponentBone. """ components = component_utils.return_all_components() for component in components: parent_joint_name = component.parentComponentBone.get() for each in renamed_nodes: if each[0] == parent_joint_name: utils.set_attribute(component, "parentComponentBone", each[1], "string") # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @staticmethod def _reset_movers(movers): attributes = ["translateX", "translateY", "translateZ", "rotateX", "rotateY", "rotateZ", "scaleX", "scaleY", "scaleZ"] default_values = {"translateX": 0, "translateY": 0, "translateZ": 0, "rotateX": 0, "rotateY": 0, "rotateZ": 0, "scaleX": 1, "scaleY": 1, "scaleZ": 1} for mover in movers: for each in attributes: if mover.attr(each).isKeyable(): default_value = default_values.get(each) mover.attr(each).set(default_value) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @property def metanode(self): """ This property holds the metanode that is passed in from our component using the joint mover. :return: Returns the metanode from our component as a pynode. """ return self._metanode @metanode.setter def metanode(self, new_value): self._metanode = pm.PyNode(new_value)