Skip to content
Snippets Groups Projects
io_import_scene_mhx.py 61.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Luca Bonavita's avatar
    Luca Bonavita committed
        return matrix
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    parseDefault(data, tokens, exclude):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    def parseDefault(data, tokens):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        for (key, val, sub) in tokens:    
            defaultKey(key, val, sub, "data", exclude, globals(), locals())
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    Utilities    
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    extractBpyType(data):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    def extractBpyType(data):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        typeSplit = str(type(data)).split("'")
        if typeSplit[0] != '<class ':
            return None
        classSplit = typeSplit[1].split(".")
        if classSplit[0] == 'bpy' and classSplit[1] == 'types':
            return classSplit[2]
        elif classSplit[0] == 'bpy_types':
            return classSplit[1]
        else:
            return None
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    Bool(string):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    def Bool(string):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        if string == 'True':
            return True
        elif string == 'False':
            return False
        else:
            raise NameError("Bool %s?" % string)
            
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    invalid(condition):
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    def invalid(condition):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        global rigLeg, rigArm, toggle
        res = eval(condition, globals())
        try:
            res = eval(condition, globals())
            #print("%s = %s" % (condition, res))
            return not res
        except:
            #print("%s invalid!" % condition)
            return True
        
    #
    #    clearScene(context):
    #    hideLayers():
    #
        
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    def clearScene():
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        global toggle
        scn = bpy.context.scene
        for n in range(len(scn.layers)):
            scn.layers[n] = True
        print("clearScene %s %s" % (toggle & T_Replace, scn))
        if not toggle & T_Replace:
            return scn
    
        for ob in scn.objects:
            if ob.type == "MESH" or ob.type == "ARMATURE":
                scn.objects.active = ob
                bpy.ops.object.mode_set(mode='OBJECT')
                scn.objects.unlink(ob)
                del ob
        #print(scn.objects)
        return scn
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    def hideLayers():
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        scn = bpy.context.scene
        for n in range(len(scn.layers)):
            if n < 3:
                scn.layers[n] = True
            else:
                scn.layers[n] = False
        return
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    User interface
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    
    DEBUG= False
    from bpy.props import *
    
    class IMPORT_OT_makehuman_mhx(bpy.types.Operator):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        '''Import from MHX file format (.mhx)'''
        bl_idname = "import_scene.makehuman_mhx"
        bl_description = 'Import from MHX file format (.mhx)'
        bl_label = "Import MHX"
        bl_space_type = "PROPERTIES"
        bl_region_type = "WINDOW"
    
        filepath = StringProperty(name="File Path", description="Filepath used for importing the MHX file", maxlen= 1024, default= "")
    
        #preset = BoolProperty(name="Use rig preset", description="Use rig preset (Classic/Gobo)?", default=True)
        #presetRig = EnumProperty(name="Rig", description="Choose preset rig", 
        #    items = [('Classic','Classic','Classic'), ('Gobo','Gobo','Gobo')], default = '1')
        footRig = EnumProperty(name="Foot rig", description="Foot rig", 
            items = [('Reverse foot','Reverse foot','Reverse foot'), ('Gobo','Gobo','Gobo')], default = '1')
        fingerRig = EnumProperty(name="Finger rig", description="Finger rig", 
            items = [('Panel','Panel','Panel'), ('Curl','Curl','Curl'), ('IK','IK','IK')], default = '1')
    
        mesh = BoolProperty(name="Mesh", description="Use main mesh", default=toggle&T_Mesh)
        armature = BoolProperty(name="Armature", description="Use armature", default=toggle&T_Armature)
        proxy = BoolProperty(name="Proxy", description="Use proxy object", default=toggle&T_Proxy)
        replace = BoolProperty(name="Replace scene", description="Replace scene", default=toggle&T_Replace)
        face = BoolProperty(name="Face shapes", description="Include facial shapekeys", default=toggle&T_Face)
        shape = BoolProperty(name="Body shapes", description="Include body shapekeys", default=toggle&T_Shape)
        symm = BoolProperty(name="Symmetric shapes", description="Keep shapekeys symmetric", default=toggle&T_Symm)
            
        def execute(self, context):
            global toggle
    
            O_Mesh = T_Mesh if self.mesh else 0
            O_Armature = T_Armature if self.armature else 0
            O_Proxy = T_Proxy if self.proxy else 0
            O_Replace = T_Replace if self.replace else 0
            O_Face = T_Face if self.face else 0
            O_Shape = T_Shape if self.shape else 0
            O_Symm = T_Symm if self.symm else 0
            #O_Preset = T_Preset if self.preset else 0
    
    Luca Bonavita's avatar
    Luca Bonavita committed
            toggle =  O_Mesh | O_Armature | O_Proxy | T_ArmIK | T_LegIK | O_Replace | O_Face | O_Shape | O_Symm | T_MHX 
    
            
    
    Luca Bonavita's avatar
    Luca Bonavita committed
            return {'FINISHED'}
    
        def invoke(self, context, event):
    
    Campbell Barton's avatar
    Campbell Barton committed
            wm = context.window_manager
    
    Luca Bonavita's avatar
    Luca Bonavita committed
            wm.add_fileselect(self)
            return {'RUNNING_MODAL'}
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    '''
    class MakeHumanFKIKPanel(bpy.types.Panel):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        bl_label = "MakeHuman FK/IK"
        bl_space_type = "VIEW_3D"
        bl_region_type = "UI"
        
        def draw(self, context):
            layout = self.layout
            ob = bpy.context.active_object
            if ob.type == 'ARMATURE':
                layout.row().prop(ob, "PArmIK_L")
                layout.row().prop(ob, "PArmIK_R")
                layout.row().prop(ob, "PLegIK_L")
                layout.row().prop(ob, "PLegIK_R")
    
                layout.row().prop(ob, "PHandLocal_L")
                layout.row().prop(ob, "PHandLocal_R")
                layout.row().prop(ob, "PFootLocal_L")
                layout.row().prop(ob, "PFootLocal_R")
            return
              
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    class MakeHumanFingerPanel(bpy.types.Panel):
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        bl_label = "MakeHuman Fingers"
        bl_space_type = "VIEW_3D"
        bl_region_type = "UI"
        
        def draw(self, context):
            layout = self.layout
            pb = bpy.context.active_pose_bone
            layout.row().prop(pb, "MHRelax")
            layout.row().prop(pb, "MHCurl")
            layout.row().prop(pb, "MHCone")
            layout.row().prop(pb, "MHSpread")
            layout.row().prop(pb, "MHScrunch")
            layout.row().prop(pb, "MHLean")
            return
              
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    def registerPanels():
    
        bpy.types.Object.PArmIK_L = FloatProperty(name="L arm - IK", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PArmIK_R = FloatProperty(name="R arm - IK", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PLegIK_L = FloatProperty(name="L leg - IK", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PLegIK_R = FloatProperty(name="R leg - IK", default = 0, min = 0.0, max = 1.0)
    
        bpy.types.Object.PHandLocal_L = FloatProperty(name="L hand - Loc", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PHandLocal_R = FloatProperty(name="R hand - Loc", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PFootLocal_L = FloatProperty(name="L foot - Loc", default = 0, min = 0.0, max = 1.0)
        bpy.types.Object.PFootLocal_R = FloatProperty(name="R foot - Loc", default = 0, min = 0.0, max = 1.0)
    
        bpy.types.PoseBone.MHCone = FloatProperty(name="Cone", default = 0, min = -0.5, max = 1.0)
        bpy.types.PoseBone.MHRelax = FloatProperty(name="Relax", default = 0, min = -0.5, max = 1.0)
        bpy.types.PoseBone.MHCurl = FloatProperty(name="Curl", default = 0, min = -0.5, max = 1.0)
        bpy.types.PoseBone.MHLean = FloatProperty(name="Lean", default = 0, min = -1.0, max = 1.0)
        bpy.types.PoseBone.MHScrunch = FloatProperty(name="Scrunch", default = 0, min = -0.5, max = 1.0)
        bpy.types.PoseBone.MHSpread = FloatProperty(name="Spread", default = 0, min = -0.5, max = 1.0)
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    
        bpy.types.register(MakeHumanFKIKPanel)
        bpy.types.register(MakeHumanFingerPanel)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    def unregisterPanels():
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        bpy.types.unregister(MakeHumanFKIKPanel)
        bpy.types.unregister(MakeHumanFingerPanel)
        '''
    
    
    def menu_func(self, context):
        self.layout.operator(IMPORT_OT_makehuman_mhx.bl_idname, text="MakeHuman (.mhx)...")
    
    
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    def register():
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        bpy.types.INFO_MT_file_import.append(menu_func)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    def unregister():
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        bpy.types.INFO_MT_file_import.remove(menu_func)
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    
    if __name__ == "__main__":
    
    Luca Bonavita's avatar
    Luca Bonavita committed
        register()
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    #    Testing
    
    Brendon Murphy's avatar
    Brendon Murphy committed
    #
    """
    theScale = 1.0
    
    toggle = T_Replace + T_Mesh + T_Armature + T_MHX + T_ArmIK + T_LegIK
    #rigLeg = T_Toes + T_GoboFoot
    #rigArm = T_ElbowPT + T_FingerCurl
    
    #readMhxFile("/home/thomas/makehuman/exports/foo-25.mhx")
    
    #toggle = T_Replace + T_Armature 
    #readMhxFile("/home/thomas/makehuman/exports/foo-sintel-25.mhx")
    
    readMhxFile("C:/Documents and Settings/xxxxxxxxxxxxxxxxxxxx/Mina dokument/makehuman/exports/foo-25.mhx", 'Classic')
    #readMhxFile("/home/thomas/mhx5/test1.mhx")
    #readMhxFile("/home/thomas/myblends/gobo/gobo.mhx")
    #readMhxFile("/home/thomas/myblends/sintel/simple.mhx")
    """