[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11882] branches/2-44-stable/blender/ release/scripts/export_fbx.py: sync with trunk

Campbell Barton cbarton at metavr.com
Wed Aug 29 16:20:56 CEST 2007


Revision: 11882
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11882
Author:   campbellbarton
Date:     2007-08-29 16:20:56 +0200 (Wed, 29 Aug 2007)

Log Message:
-----------
sync with trunk

Modified Paths:
--------------
    branches/2-44-stable/blender/release/scripts/export_fbx.py

Modified: branches/2-44-stable/blender/release/scripts/export_fbx.py
===================================================================
--- branches/2-44-stable/blender/release/scripts/export_fbx.py	2007-08-29 09:50:08 UTC (rev 11881)
+++ branches/2-44-stable/blender/release/scripts/export_fbx.py	2007-08-29 14:20:56 UTC (rev 11882)
@@ -72,6 +72,18 @@
 
 import sys
 
+## This was used to make V, but faster not to do all that
+##valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_,.()[]{}'
+##v = range(255)
+##for c in valid: v.remove(ord(c))
+v = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,42,43,47,58,59,60,61,62,63,64,92,94,96,124,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254]
+invalid = ''.join([chr(i) for i in v])
+def cleanName(name):
+	for ch in invalid:	name = name.replace(ch, '_')
+	return name
+del v, i
+
+
 def copy_file(source, dest):
 	file = open(source, 'rb')
 	data = file.read()
@@ -106,6 +118,8 @@
 	
 	print '\tCopied %d images' % copyCount
 
+mtx4_identity = Matrix()
+
 mtx_z90 = RotationMatrix(90, 3, 'z')
 mtx_x90 = RotationMatrix(90, 3, 'x')
 
@@ -171,7 +185,8 @@
 	if not name:
 		name = 'unnamed' # blank string, ASKING FOR TROUBLE!
 	else:
-		name = BPySys.cleanName(name)
+		#name = BPySys.cleanName(name)
+		name = cleanName(name) # use our own
 	
 	while name in dct.itervalues():	name = increment_string(name)
 	
@@ -284,6 +299,7 @@
 		for data in data_seq: # scene or group
 			newname = BATCH_FILE_PREFIX + BPySys.cleanName(data.name)
 			
+			
 			if BATCH_OWN_DIR:
 				new_fbxpath = fbxpath + newname + Blender.sys.sep
 				# path may alredy exist
@@ -345,9 +361,6 @@
 	# end batch support
 	
 	
-	
-	
-	
 	# ----------------------------------------------
 	# storage classes
 	class my_bone_class:
@@ -428,6 +441,10 @@
 			else:
 				return (mtx4_z90 * ((self.getPoseMatrix(frame) * arm_mat)))  *  (mtx4_z90 * (self.parent.getPoseMatrix(frame) * arm_mat)).invert()
 		
+		# we need thes because cameras and lights modified rotations
+		def getAnimMatrixRot(self, frame):
+			return self.getAnimMatrix(frame)
+		
 		def flushAnimData(self):
 			self.__anim_poselist.clear()
 
@@ -438,15 +455,33 @@
 			self.fbxName = sane_obname(ob)
 			self.blenObject = ob
 			self.matrixWorld = ob.matrixWorld * GLOBAL_MATRIX
+			self.__anim_poselist = {}
+		
+		def setPoseFrame(self, f):
+			self.__anim_poselist[f] =  self.blenObject.matrixWorld.copy()
+		
+		def getAnimMatrix(self, frame):
+			return self.__anim_poselist[frame] * GLOBAL_MATRIX
+		
+		def getAnimMatrixRot(self, frame):
+			type = self.blenObject.type
+			matrix_rot = (self.__anim_poselist[frame] * GLOBAL_MATRIX).rotationPart()
+			
+			# Lamps need to be rotated
+			if type =='Lamp':
+				matrix_rot = mtx_x90 * matrix_rot
+			elif ob and type =='Camera':
+				y = Vector(0,1,0) * matrix_rot
+				matrix_rot = matrix_rot * RotationMatrix(90, 3, 'r', y)
+			
+			return matrix_rot
+			
 	# ----------------------------------------------
 	
 	
 	
 	
 	
-	
-	
-	
 	print '\nFBX export starting...', filename
 	start_time = Blender.sys.time()
 	file = open(filename, 'w')
@@ -484,6 +519,8 @@
 	file.write('\nCreationTime: "%.4i-%.2i-%.2i %.2i:%.2i:%.2i:000"' % curtime)
 	file.write('\nCreator: "Blender3D version %.2f"' % Blender.Get('version'))
 	
+	pose_items = [] # list of (fbxName, matrix) to write pose data for, easier to collect allong the way
+	
 	# --------------- funcs for exporting
 	def object_tx(ob, loc, matrix, matrix_mod = None):
 		'''
@@ -518,7 +555,7 @@
 				matrix_rot = matrix.rotationPart()
 				# Lamps need to be rotated
 				if ob and ob.type =='Lamp':
-					matrix_rot = mtx_x90 * matrix.rotationPart()
+					matrix_rot = mtx_x90 * matrix_rot
 					rot = tuple(matrix_rot.toEuler())
 				elif ob and ob.type =='Camera':
 					y = Vector(0,1,0) * matrix_rot
@@ -645,8 +682,10 @@
 		file.write('\n\tModel: "Model::%s", "Limb" {' % my_bone.fbxName)
 		file.write('\n\t\tVersion: 232')
 		
-		write_object_props(my_bone.blenBone, None, None, my_bone.fbxArm.matrixWorld)
+		poseMatrix = write_object_props(my_bone.blenBone, None, None, my_bone.fbxArm.matrixWorld)[3]
+		pose_items.append( (my_bone.fbxName, poseMatrix) )
 		
+		
 		# file.write('\n\t\t\tProperty: "Size", "double", "",%.6f' % ((my_bone.blenData.head['ARMATURESPACE'] - my_bone.blenData.tail['ARMATURESPACE']) * my_bone.fbxArm.matrixWorld).length)
 		file.write('\n\t\t\tProperty: "Size", "double", "",1')
 		
@@ -941,11 +980,11 @@
 		file.write('\n\t\t\tProperty: "DrawFrontFacingVolumetricLight", "bool", "",0')
 		file.write('\n\t\t\tProperty: "GoboProperty", "object", ""')
 		file.write('\n\t\t\tProperty: "Color", "Color", "A+",1,1,1')
-		file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (light.energy*100))
+		file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (min(light.energy*100, 200))) # clamp below 200
 		file.write('\n\t\t\tProperty: "Cone angle", "Cone angle", "A+",%.2f' % light.spotSize)
 		file.write('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
 		file.write('\n\t\t\tProperty: "Color", "Color", "A",%.2f,%.2f,%.2f' % tuple(light.col))
-		file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (light.energy*100))
+		file.write('\n\t\t\tProperty: "Intensity", "Intensity", "A+",%.2f' % (min(light.energy*100, 200))) # clamp below 200
 		file.write('\n\t\t\tProperty: "Cone angle", "Cone angle", "A+",%.2f' % light.spotSize)
 		file.write('\n\t\t\tProperty: "Fog", "Fog", "A+",50')
 		file.write('\n\t\t\tProperty: "LightType", "enum", "",%i' % light_type)
@@ -973,15 +1012,24 @@
 		file.write('\n\t\tGeometryVersion: 124')
 		file.write('\n\t}')
 	
-	def write_null(my_null, fbxName = None):
+	# matrixOnly is not used at the moment
+	def write_null(my_null = None, fbxName = None, matrixOnly = None):
 		# ob can be null
 		if not fbxName: fbxName = my_null.fbxName
 		
 		file.write('\n\tModel: "Model::%s", "Null" {' % fbxName)
 		file.write('\n\t\tVersion: 232')
-		if my_null:		write_object_props(my_null.blenObject)
-		else:			write_object_props()
-			
+		
+		# only use this for the root matrix at the moment
+		if matrixOnly:
+			poseMatrix = write_object_props(None, None, matrixOnly)[3]
+		
+		else: # all other Null's
+			if my_null:		poseMatrix = write_object_props(my_null.blenObject)[3]
+			else:			poseMatrix = write_object_props()[3]
+		
+		pose_items.append((fbxName, poseMatrix))
+		
 		file.write('''
 		}
 		MultiLayer: 0
@@ -1010,6 +1058,7 @@
 			mat_hard = (float(mat.hard)-1)/5.10
 			mat_spec = mat.spec/2.0
 			mat_alpha = mat.alpha
+			mat_emit = mat.emit
 			mat_shadeless = mat.mode & Blender.Material.Modes.SHADELESS
 			if mat_shadeless:
 				mat_shader = 'Lambert'
@@ -1027,6 +1076,7 @@
 			mat_hard = 20.0
 			mat_spec = 0.2
 			mat_alpha = 1.0
+			mat_emit = 0.0
 			mat_shadeless = False
 			mat_shader = 'Phong'
 		
@@ -1037,19 +1087,19 @@
 		file.write('\n\t\tProperties60:  {')
 		file.write('\n\t\t\tProperty: "ShadingModel", "KString", "", "%s"' % mat_shader)
 		file.write('\n\t\t\tProperty: "MultiLayer", "bool", "",0')
-		file.write('\n\t\t\tProperty: "EmissiveColor", "ColorRGB", "",0,0,0')
-		file.write('\n\t\t\tProperty: "EmissiveFactor", "double", "",1')
+		file.write('\n\t\t\tProperty: "EmissiveColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold) # emit and diffuse color are he same in blender
+		file.write('\n\t\t\tProperty: "EmissiveFactor", "double", "",%.4f' % mat_dif)
 		
-		file.write('\n\t\t\tProperty: "AmbientColor", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_colamb)
-		file.write('\n\t\t\tProperty: "AmbientFactor", "double", "",%.1f' % mat_amb)
-		file.write('\n\t\t\tProperty: "DiffuseColor", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cold)
-		file.write('\n\t\t\tProperty: "DiffuseFactor", "double", "",%.1f' % mat_dif)
+		file.write('\n\t\t\tProperty: "AmbientColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_colamb)
+		file.write('\n\t\t\tProperty: "AmbientFactor", "double", "",%.4f' % mat_amb)
+		file.write('\n\t\t\tProperty: "DiffuseColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cold)
+		file.write('\n\t\t\tProperty: "DiffuseFactor", "double", "",%.4f' % mat_emit)
 		file.write('\n\t\t\tProperty: "Bump", "Vector3D", "",0,0,0')
 		file.write('\n\t\t\tProperty: "TransparentColor", "ColorRGB", "",1,1,1')
-		file.write('\n\t\t\tProperty: "TransparencyFactor", "double", "",0')
+		file.write('\n\t\t\tProperty: "TransparencyFactor", "double", "",%.4f' % (1.0 - mat_alpha))
 		if not mat_shadeless:
-			file.write('\n\t\t\tProperty: "SpecularColor", "ColorRGB", "",%.1f,%.1f,%.1f' % mat_cols)
-			file.write('\n\t\t\tProperty: "SpecularFactor", "double", "",%.1f' % mat_spec)
+			file.write('\n\t\t\tProperty: "SpecularColor", "ColorRGB", "",%.4f,%.4f,%.4f' % mat_cols)
+			file.write('\n\t\t\tProperty: "SpecularFactor", "double", "",%.4f' % mat_spec)
 			file.write('\n\t\t\tProperty: "ShininessExponent", "double", "",80.0')
 			file.write('\n\t\t\tProperty: "ReflectionColor", "ColorRGB", "",0,0,0')
 			file.write('\n\t\t\tProperty: "ReflectionFactor", "double", "",1')
@@ -1249,38 +1299,72 @@
 		file.write('\n\t\tTransformLink: %s' % matstr)
 		file.write('\n\t}')
 	
-	#def write_mesh(obname, ob, mtx, me, mats, arm, armname):
 	def write_mesh(my_mesh):
+		
+		me = my_mesh.blenData
+		
+		# if there are non NULL materials on this mesh
+		if [mat for mat in my_mesh.blenMaterials if mat]: 	do_materials = True
+		else:												do_materials = False
+		
+		if my_mesh.blenTextures:	do_textures = True
+		else:						do_textures = False			
+		
+		
 		file.write('\n\tModel: "Model::%s", "Mesh" {' % my_mesh.fbxName)
 		file.write('\n\t\tVersion: 232') # newline is added in write_object_props
 		
-		# Apply the mesh matrix because bones arnt applied correctly if we use object transformation
-		# Other then that, object matricies work well on meshes.
-		# if this can be fixd, be sure to remove matrix multiplication on the verts.
-		#write_object_props(ob, None, mtx)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list