Skip to content
Snippets Groups Projects
import_scene_mhx.py 52.1 KiB
Newer Older
Brendon Murphy's avatar
Brendon Murphy committed
def parseDefault(data, tokens):
	for (key, val, sub) in tokens:	
		defaultKey(key, val, sub, "data", exclude, globals(), locals())


#
#	Utilities	
#

#
#	extractBpyType(data):
#

def extractBpyType(data):
	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

#
#	Bool(string):
#

def Bool(string):
	if string == 'True':
		return True
	elif string == 'False':
		return False
	else:
		raise NameError("Bool %s?" % string)
		
#
#	invalid(condition):
#

def invalid(condition):
	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():
#
	
def clearScene():
	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

def hideLayers():
	scn = bpy.context.scene
	for n in range(len(scn.layers)):
		if n < 3:
			scn.layers[n] = True
		else:
			scn.layers[n] = False
	return

#
#	User interface
#

DEBUG= False
from bpy.props import *

class IMPORT_OT_makehuman_mhx(bpy.types.Operator):
	'''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"

	path = StringProperty(name="File Path", description="File path 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.properties.mesh else 0
		O_Armature = T_Armature if self.properties.armature else 0
		O_Proxy = T_Proxy if self.properties.proxy else 0
		O_Replace = T_Replace if self.properties.replace else 0
		O_Face = T_Face if self.properties.face else 0
		O_Shape = T_Shape if self.properties.shape else 0
		O_Symm = T_Symm if self.properties.symm else 0
		#O_Preset = T_Preset if self.properties.preset else 0
		toggle =  O_Mesh | O_Armature | O_Proxy | T_ArmIK | T_LegIK | O_Replace | O_Face | O_Shape | O_Symm | T_MHX 

		
		readMhxFile(self.properties.path, 	
			(self.properties.footRig, 
			self.properties.fingerRig))
		return {'FINISHED'}

	def invoke(self, context, event):
		wm = context.manager
		wm.add_fileselect(self)
		return {'RUNNING_MODAL'}

'''
class MakeHumanFKIKPanel(bpy.types.Panel):
	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
		  
class MakeHumanFingerPanel(bpy.types.Panel):
	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
		  

def registerPanels():
	bpy.types.Object.FloatProperty(attr="PArmIK_L", name="L arm - IK", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PArmIK_R", name="R arm - IK", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PLegIK_L", name="L leg - IK", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PLegIK_R", name="R leg - IK", default = 0, min = 0.0, max = 1.0)

	bpy.types.Object.FloatProperty(attr="PHandLocal_L", name="L hand - Loc", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PHandLocal_R", name="R hand - Loc", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PFootLocal_L", name="L foot - Loc", default = 0, min = 0.0, max = 1.0)
	bpy.types.Object.FloatProperty(attr="PFootLocal_R", name="R foot - Loc", default = 0, min = 0.0, max = 1.0)

	bpy.types.PoseBone.FloatProperty(attr="MHCone", name="Cone", default = 0, min = -0.5, max = 1.0)
	bpy.types.PoseBone.FloatProperty(attr="MHRelax", name="Relax", default = 0, min = -0.5, max = 1.0)
	bpy.types.PoseBone.FloatProperty(attr="MHCurl", name="Curl", default = 0, min = -0.5, max = 1.0)
	bpy.types.PoseBone.FloatProperty(attr="MHLean", name="Lean", default = 0, min = -1.0, max = 1.0)
	bpy.types.PoseBone.FloatProperty(attr="MHScrunch", name="Scrunch", default = 0, min = -0.5, max = 1.0)
	bpy.types.PoseBone.FloatProperty(attr="MHSpread", name="Spread", default = 0, min = -0.5, max = 1.0)

	bpy.types.register(MakeHumanFKIKPanel)
	bpy.types.register(MakeHumanFingerPanel)

def unregisterPanels():
	bpy.types.unregister(MakeHumanFKIKPanel)
	bpy.types.unregister(MakeHumanFingerPanel)
	'''

def register():
	# registerPanels()
	bpy.types.register(IMPORT_OT_makehuman_mhx)
	menu_func = lambda self, context: self.layout.operator(IMPORT_OT_makehuman_mhx.bl_idname, text="MakeHuman (.mhx)...")
	bpy.types.INFO_MT_file_import.append(menu_func)
	return
 
def unregister():
	# unregisterPanels()
	bpy.types.unregister(IMPORT_OT_makehuman_mhx)
	menu_func = lambda self, context: self.layout.operator(IMPORT_OT_makehuman_mhx.bl_idname, text="MakeHuman (.mhx)...")
	bpy.types.INFO_MT_file_import.remove(menu_func)

if __name__ == "__main__":
	register()

#
#	Testing
#
"""
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")
"""