def paste_animation(self, animation_data, paste_attributes): target_start = cmds.currentTime(query=True) source_start = self.animation_source_frames[0] time_offset = abs(int(target_start - source_start)) target_rig = animMod.get_target("rigs", selected=True)[0] target_namespace = animMod.get_target("namespace", node=target_rig) for i in range(len(self.animation_source_frames)): target_frame = target_start + i source_frame = source_start + i for control in animMod.get_target("controls", selected=True, node=target_rig): control_name = control.rpartition(":")[2] target_control = "{0}:{1}".format(target_namespace,control_name) for control_attribute in paste_attributes: attribute = control_attribute.rpartition(".")[2] if control_name in control_attribute: attribute_value = self.animation_data[source_frame][control_attribute] target_attribute = "{0}:{1}".format(target_namespace,control_attribute) try: cmds.setAttr(target_attribute, attribute_value) cmds.setKeyframe(target_control, attribute=attribute, time=target_frame) except RuntimeError: pass
def delete_redundant_keys(self): controls = animMod.get_target("controls", selected=True) print controls for control in controls: print control attributes = animMod.get_target("attributes", attribute_options = ["keyable"], node=control) for attribute in attributes: anim_layers = animMod.get_target("anim_layers", selected=False) for anim_layer in anim_layers: anim_curves = animMod.get_target("anim_curves", anim_layer=anim_layer, attribute=attribute, node=control) for anim_curve in anim_curves: print anim_curve redundant_keys = [] keyframes = cmds.keyframe(anim_curve, query=True, timeChange=True) for previous_frame, current_frame, next_frame in animMod.get_prev_current_next(keyframes): if previous_frame is None: previous_frame = current_frame if next_frame is None: next_frame = current_frame break previous_value = cmds.keyframe(anim_curve, query=True, time=(previous_frame, previous_frame), valueChange=True) current_value = cmds.keyframe(anim_curve, query=True, time=(current_frame, current_frame), valueChange=True) next_value = cmds.keyframe(anim_curve, query=True, time=(next_frame, next_frame), valueChange=True) if previous_value == current_value == next_value: redundant_keys.append(current_frame) for frame in redundant_keys: cmds.selectKey(anim_curve, time=(frame,frame), keyframe=True, remove=True) cmds.cutKey(animation="keys", clear=True)
def select_control(self, control_name): selected_rig = animMod.get_target(target="rigs", selected=True) controls = [] for rig in animMod.get_target(target="rigs", selected=True): control = animMod.get_target(target="control_name", control_name=control_name, node=rig) controls.append(control) cmds.select(controls)
def select_all_controls(self): selected_rigs = animMod.get_target(target="rigs", selected=True) print selected_rigs all_rig_controls = [] for rig in selected_rigs: all_controls = animMod.get_target(target="controls", selected=False, node=rig) or [] all_rig_controls = all_rig_controls + all_controls cmds.select(all_rig_controls)
def average(self): cmds.undoInfo(openChunk = True) anim_curves = cmds.keyframe(q = True, sl = True, n = True) for anim_curve in anim_curves: key_times = animMod.get_target("key_times", anim_curve = anim_curve, selected = True, get_from = "graphEditor") key_values = animMod.get_target("key_values", anim_curve = anim_curve, selected = True, get_from = "graphEditor") average_value = sum(key_values)/len(key_values) for key_time in key_times: cmds.keyframe(anim_curve, edit = True, time = (key_time, key_time), valueChange = average_value) cmds.undoInfo(closeChunk = True)
def copy_anim_layer(self): controls = animMod.get_target("controls", selected=True) source_anim_layers = animMod.get_target("anim_layers", selected=True) for anim_layer in source_anim_layers: anim_layer_name = str(anim_layer) anim_layer_override = cmds.animLayer(anim_layer, query=True, override=True) #Create new anim layer new_anim_layer_name = "{0}_copy".format(anim_layer_name) new_anim_layer = cmds.animLayer(new_anim_layer_name, override=anim_layer_override) for control in controls: print control attributes = animMod.get_target( "attributes", attribute_options=["unlocked", "c", "keyable"], node=control) or [] print attributes for attribute in attributes: #Is it in the source layer? if not animMod.is_control_in_anim_layer( control, anim_layer): pass else: #Are there curves on the attribute? anim_curves = cmds.copyKey(control, time=[], option="curve", animLayer=anim_layer) if anim_curves > 0: #Add the control to the new layer cmds.select(control) cmds.animLayer(new_anim_layer, edit=True, at=control + "." + attribute) cmds.setKeyframe(control, at=attribute, al=new_anim_layer) cmds.pasteKey(control, option="replaceCompletely", al=new_anim_layer)
def scale_anim_layer(self): controls = animMod.get_target("controls", selected=True) source_anim_layers = animMod.get_target("anim_layers", selected=True) for anim_layer in source_anim_layers: anim_layer_name = str(anim_layer) anim_layer_override = cmds.animLayer(anim_layer, query=True, override=True) #Create new anim layer new_anim_layer_name = "{0}_scale".format(anim_layer_name) new_anim_layer = cmds.animLayer(new_anim_layer_name, override=anim_layer_override) for control in controls: attributes = animMod.get_target("attributes", attribute_options=["unlocked", "c", "keyable"], node=control) or [] for attribute in attributes: #Is it in the source layer? if not animMod.is_control_in_anim_layer(control, anim_layer): pass else: #Are there curves on the attribute? anim_curves = cmds.copyKey(control, time = [], at=attribute, option="curve", animLayer=anim_layer) if anim_curves > 0: #Add the control to the new layer cmds.animLayer(new_anim_layer, edit=True, at=control + "." + attribute) cmds.setKeyframe(control, at=attribute, al=new_anim_layer) cmds.pasteKey(control, option="replaceCompletely", al=new_anim_layer) new_anim_curve = animMod.get_target("anim_curve", node=control, attribute=attribute, anim_layer=new_anim_layer) current_time = cmds.playbackOptions(query=True, minTime=True) print current_time current_value = cmds.getAttr(new_anim_curve, time=current_time) print current_value cmds.keyframe(new_anim_curve, edit=True, relative=True, valueChange=-current_value) cmds.animLayer(new_anim_layer_name, edit=True, override=False)
def paste_pose(self, anim_data, paste_attributes): selected_frames = animMod.get_target("frames", selected=True) print selected_frames target_rig = animMod.get_target("rigs", selected=True)[0] target_namespace = animMod.get_target("namespace", node=target_rig) #Loop through selected controls on target rig and apply data for frame in selected_frames: print frame for control in animMod.get_target("controls", selected=True, node=target_rig): control_name = control.rpartition(":")[2] target_control = "{0}:{1}".format(target_namespace,control_name) for control_attribute in paste_attributes: attribute = control_attribute.rpartition(".")[2] if control_name in control_attribute: attribute_value = self.pose_data[self.source_time][control_attribute] target_attribute = "{0}:{1}".format(target_namespace,control_attribute) try: cmds.setAttr(target_attribute, attribute_value) cmds.setKeyframe(target_control, attribute=attribute, time=frame) except RuntimeError: pass
def snap_rig(self): print "snap rig" source_rig = cmds.ls(sl=1)[0] target_rig = cmds.ls(sl=1)[1] source_namespace = source_rig.rpartition(":")[0] target_namespace = target_rig.rpartition(":")[0] source_controls = animMod.get_target("controls", selected=False, node=source_rig) print source_controls for source_control in source_controls: control = source_control.rpartition(":")[2] target_control = target_namespace + ":" + control if cmds.objExists(target_control): self.snap(source_control, target_control) else: print target_control, "doesn't exist"
def shift(self, all_keys=True, forward=True): cmds.undoInfo(openChunk=True) controls = cmds.ls(sl=True) current_frame = int(animMod.get_timeline_frame()) frame = int(self.frame_number) if self.relative == True: time_change = frame if forward == False: time_change = -1 * time_change target_frame = current_frame + time_change else: time_change = frame - current_frame target_frame = frame for control in controls: if all_keys == True: cmds.keyframe(control, edit=True, animation="objects", includeUpperBound=True, relative=True, timeChange=time_change, option="over") else: selected_keys = animMod.get_target("frames", node=control, selected=True) cmds.keyframe(control, edit=True, time=(selected_keys[0], selected_keys[-1]), relative=True, timeChange=time_change, option="over") cmds.currentTime(target_frame) cmds.undoInfo(closeChunk=True)
def copy_animation(self, rig): self.animation_source_frames = animMod.get_target("frames", selected = True) self.animation_data = animMod.store_animation_data(time_range=(self.animation_source_frames[0], self.animation_source_frames[-1]), rig=rig)
def on_copy_animation_clicked(self): self.source_rig = animMod.get_target("rigs", selected=True)[0] self.anim_data = self.copyPose.copy_animation(rig=self.source_rig)
def on_copy_pose_clicked(self): print "copy" self.source_rig = animMod.get_target("rigs", selected=True)[0] self.anim_data = self.copyPose.copy_pose(rig=self.source_rig)
def select_all_rigs(self): all_rigs = animMod.get_target(target="rigs", selected=False) cmds.select(all_rigs)
def set_rig_speed(self, speed): for rig in animMod.get_target(target="rigs", selected=True): global_control = animMod.get_target(target="control_name", node=rig, control_name="global", selected=True) cmds.setAttr(global_control + ".modelDisplayLevel", speed)