[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [659] trunk/py/scripts/addons/ import_scene_mhx.py: make human import.

Brendon Murphy meta.androcto1 at gmail.com
Wed May 12 15:33:54 CEST 2010


Revision: 659
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=659
Author:   meta-androcto
Date:     2010-05-12 15:33:53 +0200 (Wed, 12 May 2010)

Log Message:
-----------
make human import.
imports fully rigged, textured characters from make human alpha 5.
(current MH release)

Added Paths:
-----------
    trunk/py/scripts/addons/import_scene_mhx.py

Added: trunk/py/scripts/addons/import_scene_mhx.py
===================================================================
--- trunk/py/scripts/addons/import_scene_mhx.py	                        (rev 0)
+++ trunk/py/scripts/addons/import_scene_mhx.py	2010-05-12 13:33:53 UTC (rev 659)
@@ -0,0 +1,2243 @@
+""" 
+**Project Name:**	  MakeHuman
+
+**Product Home Page:** http://www.makehuman.org/
+
+**Code Home Page:**	http://code.google.com/p/makehuman/
+
+**Authors:**		   Thomas Larsson
+
+**Copyright(c):**	  MakeHuman Team 2001-2010
+
+**Licensing:**		 GPL3 (see also http://sites.google.com/site/makehumandocs/licensing)
+
+**Coding Standards:**  See http://sites.google.com/site/makehumandocs/developers-guide
+
+Abstract
+MHX (MakeHuman eXchange format) importer for Blender 2.5x.
+Version 0.9
+
+"""
+
+bl_addon_info = {
+	'name': 'Import MakeHuman (.mhx)',
+	'author': 'Thomas Larsson',
+	'version': '0.9, Make Human Alpha 5',
+	'blender': (2, 5, 3),
+	'location': 'File > Import',
+	'description': 'Import files in the MakeHuman eXchange format (.mhx)',
+	'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/File_I-O/Make_Human',
+	'category': 'Import/Export'}
+
+"""
+Place this file in the .blender/scripts/addons dir
+You have to activated the script in the "Add-Ons" tab (user preferences).
+Access from the File > Import menu.
+"""
+
+#
+#
+#
+
+import bpy
+import os
+import time
+import mathutils
+from mathutils import *
+import geometry
+import string
+
+MAJOR_VERSION = 0
+MINOR_VERSION = 9
+MHX249 = False
+Blender24 = False
+Blender25 = True
+TexDir = "~/makehuman/exports"
+
+#
+#
+#
+
+theScale = 1.0
+useMesh = 1
+doSmash = 1
+verbosity = 2
+warnedTextureDir = False
+warnedVersion = False
+
+true = True
+false = False
+Epsilon = 1e-6
+nErrors = 0
+theTempDatum = None
+
+todo = []
+
+#
+#	toggle flags
+#
+
+T_ArmIK = 0x01
+T_LegIK = 0x02
+T_Replace = 0x20
+T_Face = 0x40
+T_Shape = 0x80
+T_Mesh = 0x100
+T_Armature = 0x200
+T_Proxy = 0x400
+T_Panel = 0x800
+
+T_Rigify = 0x1000
+T_Preset = 0x2000
+T_Symm = 0x4000
+T_MHX = 0x8000
+
+toggle = T_Replace + T_ArmIK + T_LegIK + T_Mesh + T_Armature + T_Face
+
+#
+#	setFlagsAndFloats(rigFlags):
+#
+#	Global floats
+fLegIK = 0.0
+fArmIK = 0.0
+fFingerPanel = 0.0
+fFingerIK = 0.0
+fFingerCurl = 0.0
+
+#	rigLeg and rigArm flags
+T_Toes = 0x0001
+T_GoboFoot = 0x0002
+T_InvFoot = 0x0004
+
+T_FingerPanel = 0x100
+T_FingerCurl = 0x0200
+T_FingerIK = 0x0400
+
+
+T_LocalFKIK = 0x8000
+
+rigLeg = 0
+rigArm = 0
+
+def setFlagsAndFloats(rigFlags):
+	global toggle, rigLeg, rigArm
+
+	(footRig, fingerRig) = rigFlags
+	rigLeg = 0
+	rigArm = 0
+	if footRig == 'Reverse foot': rigLeg |= T_InvFoot
+	elif footRig == 'Gobo': rigLeg |= T_GoboFoot
+
+	if fingerRig == 'Panel': rigArm |= T_FingerPanel
+	elif fingerRig == 'IK': rigArm |= T_FingerIK
+	elif fingerRig == 'Curl': rigArm |= T_FingerCurl
+
+	toggle |= T_Panel
+
+	# Global floats, used as influences
+	global fFingerCurl, fLegIK, fArmIK, fFingerIK
+
+	fFingerCurl = 1.0 if rigArm&T_FingerCurl else 0.0
+	fLegIK = 1.0 if toggle&T_LegIK else 0.0
+	fArmIK = 1.0 if toggle&T_ArmIK else 0.0
+	fFingerIK = 1.0 if rigArm&T_FingerIK else 0.0
+
+	return
+
+
+#
+#	Dictionaries
+#
+
+loadedData = {
+	'NONE' : {},
+
+	'Object' : {},
+	'Mesh' : {},
+	'Armature' : {},
+	'Lamp' : {},
+	'Camera' : {},
+	'Lattice' : {},
+	'Curve' : {},
+
+	'Material' : {},
+	'Image' : {},
+	'MaterialTextureSlot' : {},
+	'Texture' : {},
+	
+	'Bone' : {},
+	'BoneGroup' : {},
+	'Rigify' : {},
+
+	'Action' : {},
+	'Group' : {},
+
+	'MeshTextureFaceLayer' : {},
+	'MeshColorLayer' : {},
+	'VertexGroup' : {},
+	'ShapeKey' : {},
+	'ParticleSystem' : {},
+
+	'ObjectConstraints' : {},
+	'ObjectModifiers' : {},
+	'MaterialSlot' : {},
+}
+
+Plural = {
+	'Object' : 'objects',
+	'Mesh' : 'meshes',
+	'Lattice' : 'lattices',
+	'Curve' : 'curves',
+	'Group' : 'groups',
+	'Empty' : 'empties',
+	'Armature' : 'armatures',
+	'Bone' : 'bones',
+	'BoneGroup' : 'bone_groups',
+	'Pose' : 'poses',
+	'PoseBone' : 'pose_bones',
+	'Material' : 'materials',
+	'Texture' : 'textures',
+	'Image' : 'images',
+	'Camera' : 'cameras',
+	'Lamp' : 'lamps',
+	'World' : 'worlds',
+}
+
+#
+#	Creators
+#
+
+def uvtexCreator(me, name):
+	print("uvtexCreator", me, name)
+	me.add_uv_texture()
+	uvtex = me.uv_textures[-1]
+	uvtex.name = name
+	return uvtex
+
+
+def vertcolCreator(me, name):
+	print("vertcolCreator", me, name)
+	me.add_vertex_color()
+	vcol = me.vertex_colors[-1]
+	vcol.name = name
+	return vcol
+		
+
+#
+#	loadMhx(filePath, context, flags):
+#
+
+def loadMhx(filePath, context, flags):
+	global toggle
+	toggle = flags
+	readMhxFile(filePath)
+	return
+
+#
+#	readMhxFile(filePath, rigFlags):
+#
+
+def readMhxFile(filePath, rigFlags):
+	global todo, nErrors
+	
+	fileName = os.path.expanduser(filePath)
+	(shortName, ext) = os.path.splitext(fileName)
+	if ext != ".mhx":
+		print("Error: Not a mhx file: " + fileName)
+		return
+	print( "Opening MHX file "+ fileName )
+	time1 = time.clock()
+
+	ignore = False
+	stack = []
+	tokens = []
+	key = "toplevel"
+	level = 0
+	nErrors = 0
+
+	setFlagsAndFloats(rigFlags)
+
+	file= open(fileName, "rU")
+	print( "Tokenizing" )
+	lineNo = 0
+	for line in file: 
+		# print(line)
+		lineSplit= line.split()
+		lineNo += 1
+		if len(lineSplit) == 0:
+			pass
+		elif lineSplit[0] == '#':
+			pass
+		elif lineSplit[0] == 'end':
+			try:
+				sub = tokens
+				tokens = stack.pop()
+				if tokens:
+					tokens[-1][2] = sub
+				level -= 1
+			except:
+				print( "Tokenizer error at or before line %d" % lineNo )
+				print( line )
+				dummy = stack.pop()
+		elif lineSplit[-1] == ';':
+			if lineSplit[0] == '\\':
+				key = lineSplit[1]
+				tokens.append([key,lineSplit[2:-1],[]])
+			else:
+				key = lineSplit[0]
+				tokens.append([key,lineSplit[1:-1],[]])
+		else:
+			key = lineSplit[0]
+			tokens.append([key,lineSplit[1:],[]])
+			stack.append(tokens)
+			level += 1
+			tokens = []
+	file.close()
+
+	if level != 0:
+		raise NameError("Tokenizer out of kilter %d" % level)	
+	clearScene()
+	print( "Parsing" )
+	parse(tokens)
+	
+	for (expr, glbals, lcals) in todo:
+		try:
+			# print("Doing %s" % expr)
+			exec(expr, glbals, lcals)
+		except:
+			msg = "Failed: "+expr
+			print( msg )
+			nErrors += 1
+			#raise NameError(msg)
+
+	print("Postprocess")
+	postProcess()
+	print("HideLayers")
+	hideLayers()
+	time2 = time.clock()
+	print("toggle = %x" % toggle)
+	msg = "File %s loaded in %g s" % (fileName, time2-time1)
+	if nErrors:
+		msg += " but there where %d errors. " % (nErrors)
+	print(msg)
+	return	# loadMhx
+
+#
+#	getObject(name, var, glbals, lcals):
+#
+
+def getObject(name, var, glbals, lcals):
+	try:
+		ob = loadedData['Object'][name]
+	except:
+		if name != "None":
+			expr = "%s = loadedData['Object'][name]" % var
+			print("Todo ", expr)
+			todo.append((expr, glbals, lcals))
+		ob = None
+	return ob
+
+#
+#	parse(tokens):
+#
+
+ifResult = False
+
+def parse(tokens):
+	global warnedVersion, MHX249, ifResult
+	
+	for (key, val, sub) in tokens:	
+		# print("Parse %s" % key)
+		data = None
+		if key == 'MHX':
+			if int(val[0]) != MAJOR_VERSION and int(val[1]) != MINOR_VERSION and not warnedVersion:
+				print("Warning: \nThis file was created with another version of MHX\n")
+				warnedVersion = True
+
+		elif key == 'MHX249':
+			MHX249 = eval(val[0])
+			print("Blender 2.49 compatibility mode is %s\n" % MHX249)
+
+		elif key == 'if':
+			try:
+				ifResult = eval(val[0])
+			except:
+				ifResult = False
+			if ifResult:
+				parse(sub)
+				
+		elif key == 'elif':
+			if not ifResult:
+				try:
+					ifResult = eval(val[0])
+				except:
+					ifResult = False
+				if ifResult:
+					parse(sub)
+		
+		elif key == 'else':
+			if not ifResult:
+				parse(sub)
+		
+
+		elif MHX249:
+			pass
+
+		elif key == 'print':
+			msg = concatList(val)
+			print(msg)
+		elif key == 'warn':
+			msg = concatList(val)
+			print(msg)
+		elif key == 'error':
+			msg = concatList(val)
+			raise NameError(msg)			
+		elif key == "Object":
+			parseObject(val, sub)
+		elif key == "Mesh":
+			data = parseMesh(val, sub)
+		elif key == "Curve":
+			data = parseCurve(val, sub)
+		elif key == "Lattice":
+			data = parseLattice(val, sub)
+		elif key == "Group":
+			data = parseGroup(val, sub)
+		elif key == "Armature":
+			data = parseArmature(val, sub)
+		elif key == "Pose":
+			data = parsePose(val, sub)
+		elif key == "Action":
+			data = parseAction(val, sub)
+		elif key == "Material":
+			data = parseMaterial(val, sub)
+		elif key == "Texture":
+			data = parseTexture(val, sub)
+		elif key == "Image":
+			data = parseImage(val, sub)
+		elif key == "Process":
+			parseProcess(val, sub)
+		elif key == 'AnimationData':
+			try:
+				ob = loadedData['Object'][val[0]]
+			except:
+				ob = None
+			if ob:
+				bpy.context.scene.objects.active = ob
+				parseAnimationData(ob, sub)
+		elif key == 'ShapeKeys':
+			try:
+				ob = loadedData['Object'][val[0]]
+			except:
+				ob = None
+			if ob:
+				bpy.context.scene.objects.active = ob
+				parseShapeKeys(ob, ob.data, val, sub)
+		else:
+			data = parseDefaultType(key, val, sub)				
+
+		if data and key != 'Mesh':
+			print( data )
+	return
+
+#
+#	parseDefaultType(typ, args, tokens):
+#
+
+def parseDefaultType(typ, args, tokens):
+	global todo
+
+	name = args[0]
+	data = None
+	expr = "bpy.data.%s.new('%s')" % (Plural[typ], name)
+	print(expr)
+	data = eval(expr)
+	print("  ok", data)
+
+	bpyType = typ.capitalize()
+	print(bpyType, name, data)
+	loadedData[bpyType][name] = data
+	if data == None:
+		return None
+
+	for (key, val, sub) in tokens:
+		#print("%s %s" % (key, val))
+		defaultKey(key, val, sub, 'data', [], globals(), locals())
+	print("Done ", data)
+	return data
+	
+#
+#	concatList(elts)
+#
+
+def concatList(elts):
+	string = ""
+	for elt in elts:
+		string += " %s" % elt
+	return string
+
+#
+#	parseAction(args, tokens):
+#	parseFCurve(fcu, args, tokens):
+#	parseKeyFramePoint(pt, args, tokens):
+#
+
+def parseAction(args, tokens):
+	name = args[0]
+	if invalid(args[1]):
+		return
+
+	ob = bpy.context.object
+	bpy.ops.object.mode_set(mode='POSE')
+	if ob.animation_data:
+		ob.animation_data.action = None
+	created = {}
+	for (key, val, sub) in tokens:
+		if key == 'FCurve':
+			prepareActionFCurve(ob, created, val, sub)
+		
+	act = ob.animation_data.action
+	loadedData['Action'][name] = act
+	if act == None:
+		print("Ignoring action %s" % name)
+		return act
+	act.name = name
+	print("Action", name, act, ob)
+	
+	for (key, val, sub) in tokens:
+		if key == 'FCurve':
+			fcu = parseActionFCurve(act, ob, val, sub)
+		else:
+			defaultKey(key, val, sub, 'act', [], globals(), locals())
+	ob.animation_data.action = None
+	bpy.ops.object.mode_set(mode='OBJECT')

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list