[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21026] branches/soc-2009-kazanbas: - tweak Object.create_render_mesh params

Arystanbek Dyussenov arystan.d at gmail.com
Sat Jun 20 07:52:20 CEST 2009


Revision: 21026
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21026
Author:   kazanbas
Date:     2009-06-20 07:52:19 +0200 (Sat, 20 Jun 2009)

Log Message:
-----------
- tweak Object.create_render_mesh params
- svn merge -r 20124:20998 \
https://svn.blender.org/svnroot/bf-blender/branches/blender2.5/blender/release/scripts/export_obj.py \
export_obj-2.5.py to make sure I don't miss new OBJ exporter features ;)

Modified Paths:
--------------
    branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py
    branches/soc-2009-kazanbas/source/blender/makesrna/intern/rna_object_api.c

Modified: branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py
===================================================================
--- branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py	2009-06-20 05:16:09 UTC (rev 21025)
+++ branches/soc-2009-kazanbas/release/scripts/export_obj-2.5.py	2009-06-20 05:52:19 UTC (rev 21026)
@@ -7,9 +7,9 @@
 Tooltip: 'Save a Wavefront OBJ File'
 """
 
-__author__ = "Campbell Barton, Jiri Hnidek"
+__author__ = "Campbell Barton, Jiri Hnidek, Paolo Ciccone"
 __url__ = ['http://wiki.blender.org/index.php/Scripts/Manual/Export/wavefront_obj', 'www.blender.org', 'blenderartists.org']
-__version__ = "1.2"
+__version__ = "1.21"
 
 __bpydoc__ = """\
 This script is an exporter to OBJ file format.
@@ -184,12 +184,89 @@
 				copyCount+=1
 	print '\tCopied %d images' % copyCount
 
-def write(filename, objects,
-		  EXPORT_TRI=False,	 EXPORT_EDGES=False,
-		  EXPORT_NORMALS=False,	 EXPORT_NORMALS_HQ=False,
-		  EXPORT_UV=True,  EXPORT_MTL=True,	 EXPORT_COPY_IMAGES=False,
-		  EXPORT_APPLY_MODIFIERS=True, EXPORT_ROTX90=True, EXPORT_BLEN_OBS=True,
-		  EXPORT_GROUP_BY_OB=False,	 EXPORT_GROUP_BY_MAT=False, EXPORT_KEEP_VERT_ORDER=False):
+
+def test_nurbs_compat(ob):
+	if ob.type != 'Curve':
+		return False
+	
+	for nu in ob.data:
+		if (not nu.knotsV) and nu.type != 1: # not a surface and not bezier
+			return True
+	
+	return False
+
+def write_nurb(file, ob, ob_mat):
+	tot_verts = 0
+	cu = ob.data
+	
+	# use negative indices
+	Vector = Blender.Mathutils.Vector
+	for nu in cu:
+		
+		if nu.type==0:		DEG_ORDER_U = 1
+		else:				DEG_ORDER_U = nu.orderU-1  # Tested to be correct
+		
+		if nu.type==1:
+			print "\tWarning, bezier curve:", ob.name, "only poly and nurbs curves supported"
+			continue
+		
+		if nu.knotsV:
+			print "\tWarning, surface:", ob.name, "only poly and nurbs curves supported"
+			continue
+		
+		if len(nu) <= DEG_ORDER_U:
+			print "\tWarning, orderU is lower then vert count, skipping:", ob.name
+			continue
+		
+		pt_num = 0
+		do_closed = (nu.flagU & 1)
+		do_endpoints = (do_closed==0) and (nu.flagU & 2)
+		
+		for pt in nu:
+			pt = Vector(pt[0], pt[1], pt[2]) * ob_mat
+			file.write('v %.6f %.6f %.6f\n' % (pt[0], pt[1], pt[2]))
+			pt_num += 1
+		tot_verts += pt_num
+		
+		file.write('g %s\n' % (fixName(ob.name))) # fixName(ob.getData(1)) could use the data name too
+		file.write('cstype bspline\n') # not ideal, hard coded
+		file.write('deg %d\n' % DEG_ORDER_U) # not used for curves but most files have it still
+		
+		curve_ls = [-(i+1) for i in xrange(pt_num)]
+		
+		# 'curv' keyword
+		if do_closed:
+			if DEG_ORDER_U == 1:
+				pt_num += 1
+				curve_ls.append(-1)
+			else:
+				pt_num += DEG_ORDER_U
+				curve_ls = curve_ls + curve_ls[0:DEG_ORDER_U]
+		
+		file.write('curv 0.0 1.0 %s\n' % (' '.join( [str(i) for i in curve_ls] ))) # Blender has no U and V values for the curve
+		
+		# 'parm' keyword
+		tot_parm = (DEG_ORDER_U + 1) + pt_num
+		tot_parm_div = float(tot_parm-1)
+		parm_ls = [(i/tot_parm_div) for i in xrange(tot_parm)]
+		
+		if do_endpoints: # end points, force param
+			for i in xrange(DEG_ORDER_U+1):
+				parm_ls[i] = 0.0
+				parm_ls[-(1+i)] = 1.0
+		
+		file.write('parm u %s\n' % ' '.join( [str(i) for i in parm_ls] ))
+
+		file.write('end\n')
+	
+	return tot_verts
+
+def write(filename, objects,\
+EXPORT_TRI=False,  EXPORT_EDGES=False,  EXPORT_NORMALS=False,  EXPORT_NORMALS_HQ=False,\
+EXPORT_UV=True,  EXPORT_MTL=True,  EXPORT_COPY_IMAGES=False,\
+EXPORT_APPLY_MODIFIERS=True, EXPORT_ROTX90=True, EXPORT_BLEN_OBS=True,\
+EXPORT_GROUP_BY_OB=False,  EXPORT_GROUP_BY_MAT=False, EXPORT_KEEP_VERT_ORDER=False,\
+EXPORT_POLYGROUPS=False, EXPORT_CURVE_AS_NURBS=True):
 	'''
 	Basic write function. The context and options must be alredy set
 	This can be accessed externaly
@@ -203,6 +280,29 @@
 	def veckey2d(v):
 		return round(v.x, 6), round(v.y, 6)
 	
+	def findVertexGroupName(face, vWeightMap):
+		"""
+		Searches the vertexDict to see what groups is assigned to a given face.
+		We use a frequency system in order to sort out the name because a given vetex can
+		belong to two or more groups at the same time. To find the right name for the face
+		we list all the possible vertex group names with their frequency and then sort by
+		frequency in descend order. The top element is the one shared by the highest number
+		of vertices is the face's group 
+		"""
+		weightDict = {}
+		for vert in face:
+			vWeights = vWeightMap[vert.index]
+			for vGroupName, weight in vWeights:
+				weightDict[vGroupName] = weightDict.get(vGroupName, 0) + weight
+		
+		if weightDict:
+			alist = [(weight,vGroupName) for vGroupName, weight in weightDict.iteritems()] # sort least to greatest amount of weight
+			alist.sort()
+			return(alist[-1][1]) # highest value last
+		else:
+			return '(null)'
+
+
 	print 'OBJ Export path: "%s"' % filename
 	temp_mesh_name = '~tmp-mesh'
 
@@ -248,12 +348,23 @@
 	
 	globalNormals = {}
 	
-	# Get all meshs
+	# Get all meshes
 	for ob_main in objects:
 		for ob, ob_mat in BPyObject.getDerivedObjects(ob_main):
+			
+			# Nurbs curve support
+			if EXPORT_CURVE_AS_NURBS and test_nurbs_compat(ob):
+				if EXPORT_ROTX90:
+					ob_mat = ob_mat * mat_xrot90
+				
+				totverts += write_nurb(file, ob, ob_mat)
+				
+				continue
+			# end nurbs
+			
 			# Will work for non meshes now! :)
 			# getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=True, scn=None)
-			me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, False, scn)
+			me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, EXPORT_POLYGROUPS, scn)
 			if not me:
 				continue
 			
@@ -406,6 +517,17 @@
 			if not faceuv:
 				f_image = None
 			
+			if EXPORT_POLYGROUPS:
+				# Retrieve the list of vertex groups
+				vertGroupNames = me.getVertGroupNames()
+
+				currentVGroup = ''
+				# Create a dictionary keyed by face id and listing, for each vertex, the vertex groups it belongs to
+				vgroupsMap = [[] for _i in xrange(len(me.verts))]
+				for vertexGroupName in vertGroupNames:
+					for vIdx, vWeight in me.getVertsFromGroup(vertexGroupName, 1):
+						vgroupsMap[vIdx].append((vertexGroupName, vWeight))
+
 			for f_index, f in enumerate(faces):
 				f_v= f.v
 				f_smooth= f.smooth
@@ -420,9 +542,18 @@
 				else:
 					key = materialNames[f_mat],	 None # No image, use None instead.
 				
+				# Write the vertex group
+				if EXPORT_POLYGROUPS:
+					if vertGroupNames:
+						# find what vertext group the face belongs to
+						theVGroup = findVertexGroupName(f,vgroupsMap)
+						if	theVGroup != currentVGroup:
+							currentVGroup = theVGroup
+							file.write('g %s\n' % theVGroup)
+
 				# CHECK FOR CONTEXT SWITCH
 				if key == contextMat:
-					pass # Context alredy switched, dont do anythoing
+					pass # Context alredy switched, dont do anything
 				else:
 					if key[0] == None and key[1] == None:
 						# Write a null material, since we know the context has changed.
@@ -447,6 +578,7 @@
 						
 						if EXPORT_GROUP_BY_MAT:
 							file.write('g %s_%s_%s\n' % (fixName(ob.name), fixName(ob.getData(1)), mat_data[0]) ) # can be mat_image or (null)
+
 						file.write('usemtl %s\n' % mat_data[0]) # can be mat_image or (null)
 					
 				contextMat = key
@@ -548,7 +680,8 @@
 		EXPORT_NORMALS, EXPORT_NORMALS_HQ, EXPORT_UV,\
 		EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
 		EXPORT_ANIMATION, EXPORT_COPY_IMAGES, EXPORT_BLEN_OBS,\
-		EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER
+		EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER,\
+		EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS
 	
 	EXPORT_APPLY_MODIFIERS = Draw.Create(0)
 	EXPORT_ROTX90 = Draw.Create(1)
@@ -566,7 +699,10 @@
 	EXPORT_GROUP_BY_OB = Draw.Create(0)
 	EXPORT_GROUP_BY_MAT = Draw.Create(0)
 	EXPORT_KEEP_VERT_ORDER = Draw.Create(1)
+	EXPORT_POLYGROUPS = Draw.Create(0)
+	EXPORT_CURVE_AS_NURBS = Draw.Create(1)
 	
+	
 	# Old UI
 	'''
 	# removed too many options are bad!
@@ -616,7 +752,7 @@
 			GLOBALS['EVENT'] = e
 		
 		def do_split(e,v):
-			global EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_APPLY_MODIFIERS, KEEP_VERT_ORDER
+			global EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_APPLY_MODIFIERS, KEEP_VERT_ORDER, EXPORT_POLYGROUPS
 			if EXPORT_BLEN_OBS.val or EXPORT_GROUP_BY_OB.val or EXPORT_GROUP_BY_MAT.val or EXPORT_APPLY_MODIFIERS.val:
 				EXPORT_KEEP_VERT_ORDER.val = 0
 			else:
@@ -630,6 +766,7 @@
 				if not (EXPORT_BLEN_OBS.val or EXPORT_GROUP_BY_OB.val or EXPORT_GROUP_BY_MAT.val or EXPORT_APPLY_MODIFIERS.val):
 					EXPORT_KEEP_VERT_ORDER.val = 1
 			
+			
 		def do_help(e,v):
 			url = __url__[0]
 			print 'Trying to open web browser with documentation at this address...'
@@ -646,45 +783,49 @@
 			
 			# Center based on overall pup size
 			ui_x -= 165
-			ui_y -= 110
+			ui_y -= 140
 			
 			global EXPORT_APPLY_MODIFIERS, EXPORT_ROTX90, EXPORT_TRI, EXPORT_EDGES,\
 				EXPORT_NORMALS, EXPORT_NORMALS_HQ, EXPORT_UV,\
 				EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
 				EXPORT_ANIMATION, EXPORT_COPY_IMAGES, EXPORT_BLEN_OBS,\
-				EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER
+				EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER,\
+				EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS
 
-			Draw.Label('Context...', ui_x+9, ui_y+209, 220, 20)
+			Draw.Label('Context...', ui_x+9, ui_y+239, 220, 20)
 			Draw.BeginAlign()
-			EXPORT_SEL_ONLY = Draw.Toggle('Selection Only', EVENT_NONE, ui_x+9, ui_y+189, 110, 20, EXPORT_SEL_ONLY.val, 'Only export objects in visible selection. Else export whole scene.')
-			EXPORT_ALL_SCENES = Draw.Toggle('All Scenes', EVENT_NONE, ui_x+119, ui_y+189, 110, 20, EXPORT_ALL_SCENES.val, 'Each scene as a separate OBJ file.')
-			EXPORT_ANIMATION = Draw.Toggle('Animation', EVENT_NONE, ui_x+229, ui_y+189, 110, 20, EXPORT_ANIMATION.val, 'Each frame as a numbered OBJ file.')
+			EXPORT_SEL_ONLY = Draw.Toggle('Selection Only', EVENT_NONE, ui_x+9, ui_y+219, 110, 20, EXPORT_SEL_ONLY.val, 'Only export objects in visible selection. Else export whole scene.')
+			EXPORT_ALL_SCENES = Draw.Toggle('All Scenes', EVENT_NONE, ui_x+119, ui_y+219, 110, 20, EXPORT_ALL_SCENES.val, 'Each scene as a separate OBJ file.')
+			EXPORT_ANIMATION = Draw.Toggle('Animation', EVENT_NONE, ui_x+229, ui_y+219, 110, 20, EXPORT_ANIMATION.val, 'Each frame as a numbered OBJ file.')
 			Draw.EndAlign()
 			
 			
-			Draw.Label('Output Options...', ui_x+9, ui_y+159, 220, 20)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list