def switch( self, space, keyframe = False ): ''' @param space: Space you want to switch on. @type space: *int* @param keyframe: Set keyframe into spaceAttrNode @type keyframe: *bool* ''' # get current frame currentFrame = cmds.currentTime(q=True) currentSpace = cmds.getAttr( '%s.%s' % (self.spaceAttrNode,'space') ) # ---------------------------------------------------------------------- # get transformation ( script ) position = cmds.xform( self.spaceAttrNode, q=True,ws=True, rp=True) rotation = cmds.xform( self.spaceAttrNode, q=True,ws=True, ro=True) localPosition = cmds.getAttr('%s.%s' % (self.spaceAttrNode,'translate'))[0] localRotation = cmds.getAttr('%s.%s' % (self.spaceAttrNode,'rotate'))[0] # ---------------------------------------------------------------------- # key previous if keyframe: cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'space') , time=currentFrame-1, value=currentSpace ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateX'), time=currentFrame-1, value=localPosition[0] ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateY'), time=currentFrame-1, value=localPosition[1] ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateZ'), time=currentFrame-1, value=localPosition[2] ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateX'), time=currentFrame-1, value=localRotation[0] ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateY'), time=currentFrame-1, value=localRotation[1] ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateZ'), time=currentFrame-1, value=localRotation[2] ) # switch to new space cmds.setAttr( '%s.%s' % (self.spaceAttrNode,'space'), space) # set position if not attribute.isLocked('tx',self.spaceAttrNode) and not attribute.isLocked('ty',self.spaceAttrNode) and not attribute.isLocked('tz',self.spaceAttrNode): cmds.xform(self.spaceAttrNode,ws=True,t=position) # set rotation if not attribute.isLocked('rx',self.spaceAttrNode) and not attribute.isLocked('ry',self.spaceAttrNode) and not attribute.isLocked('rz',self.spaceAttrNode): cmds.xform(self.spaceAttrNode,ws=True,ro=rotation) # set keyframes if keyframe: cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'space') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateX') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateY') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'translateZ') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateX') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateY') ) cmds.setKeyframe( '%s.%s' % (self.spaceAttrNode,'rotateZ') )
def load( filepath, world=False ): '''Import joints exported with the *save* function. **python format** If the joint already exists it will set the transformation, if it doesn't it will create it. .. python :: # This command will only import the exported values into persp node, if any. load( '/var/tmp/attributes.pyson', 'persp' ) :param filepath: File exported with the save function. :type filepath: **str** :param world: Import world position :type world: **bool** ''' if not fileIO.isFile(filepath): raise RuntimeError, 'No valid filepath "%s"' % filepath # Get Data data = pyon.load( filepath ) # Create Joints for joint in data.keys(): parameters = data[joint]['parameters'] parent = data[joint]['parent'] # Create Joint if not common.isValid( joint ): joint = create( name=joint ) # Create Parent Node if not common.isValid( parent ) and parent in data.keys(): parent = create( name=parent ) # Parent joint if common.isValid( parent ) and common.getParent( joint ) != parent: cmds.parent( joint, parent ) # Set values for attr in parameters.keys(): # Check if valid if not common.isValid('%s.%s' % (joint, attr)): continue # ------------------------------------------------------------------ # Check if connected or locked locked = False if attribute.isConnected( attr, joint, incoming=True, outgoing=False ) or attribute.isLocked( attr, joint ): locked = True if attribute.isCompound( attr, joint ): attrChildren = cmds.attributeQuery( attr, node = joint, listChildren = True) for attrChild in attrChildren: if attribute.isConnected( attrChild, joint, incoming=True, outgoing=False ) or attribute.isLocked( attrChild, joint ): locked = True if locked: continue # ------------------------------------------------------------------ # Set values value = parameters[attr] attribute.setValue( attr, joint, value ) # Set world position if world: for joint in data.keys(): worldPosition = data[joint]['worldPosition'] cmds.move( worldPosition[0], worldPosition[1], worldPosition[2], joint, worldSpace=True, a=True, pcp=True ) #logger.info( 'Joints loaded from: "%s"' % filepath)