Exemple #1
0
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)