[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13898] trunk/blender/release/scripts/ export_obj.py: Fix for [#8382] UV unwrap & export puts seams on all edges.

Campbell Barton ideasman42 at gmail.com
Thu Feb 28 00:10:26 CET 2008


Revision: 13898
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13898
Author:   campbellbarton
Date:     2008-02-28 00:10:25 +0100 (Thu, 28 Feb 2008)

Log Message:
-----------
Fix for [#8382] UV unwrap & export puts seams on all edges.
Maya uses OBJ's UV's to generate seams, some people complained that models imported from blender didnt work well in maya.
It was faster to write every faces UV (without doing a remove doubles), but sharing UV coords makes smaller files and might also be more efficient in other applications.

Modified Paths:
--------------
    trunk/blender/release/scripts/export_obj.py

Modified: trunk/blender/release/scripts/export_obj.py
===================================================================
--- trunk/blender/release/scripts/export_obj.py	2008-02-27 20:17:03 UTC (rev 13897)
+++ trunk/blender/release/scripts/export_obj.py	2008-02-27 23:10:25 UTC (rev 13898)
@@ -195,9 +195,9 @@
 	
 	def veckey3d(v):
 		return round(v.x, 6), round(v.y, 6), round(v.z, 6)
-
-	#def veckey2d(v):
-	#	return round(v.x, 6), round(v.y, 6)
+		
+	def veckey2d(v):
+		return round(v.x, 6), round(v.y, 6)
 	
 	print 'OBJ Export path: "%s"' % filename
 	temp_mesh_name = '~tmp-mesh'
@@ -235,7 +235,7 @@
 	# Initialize totals, these are updated each object
 	totverts = totuvco = totno = 1
 	
-	face_vert_index = 1 # used for uvs now
+	face_vert_index = 1
 	
 	globalNormals = {}
 	
@@ -247,8 +247,12 @@
 			me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, False, scn)
 			if not me:
 				continue
-			faceuv= me.faceUV
 			
+			if EXPORT_UV:
+				faceuv= me.faceUV
+			else:
+				faceuv = False
+			
 			# We have a valid mesh
 			if EXPORT_TRI and me.faces:
 				# Add a dummy object to it.
@@ -320,7 +324,7 @@
 			# so we dont over context switch in the obj file.
 			if EXPORT_MORPH_TARGET:
 				pass
-			elif faceuv and EXPORT_UV:
+			elif faceuv:
 				try:	faces.sort(key = lambda a: (a.mat, a.image, a.smooth))
 				except:	faces.sort(lambda a,b: cmp((a.mat, a.image, a.smooth), (b.mat, b.image, b.smooth)))
 			elif len(materials) > 1:
@@ -354,10 +358,23 @@
 				file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
 			
 			# UV
-			if faceuv and EXPORT_UV:
-				for f in faces:
-					for uv in f.uv:
-						file.write('vt %.6f %.6f 0.0\n' % tuple(uv))
+			if faceuv:
+				uv_face_mapping = [[0,0,0,0] for f in faces] # a bit of a waste for tri's :/
+				
+				uv_dict = {} # could use a set() here
+				for f_index, f in enumerate(faces):
+					
+					for uv_index, uv in enumerate(f.uv):
+						uvkey = veckey2d(uv)
+						try:
+							uv_face_mapping[f_index][uv_index] = uv_dict[uvkey]
+						except:
+							uv_face_mapping[f_index][uv_index] = uv_dict[uvkey] = len(uv_dict)
+							file.write('vt %.6f %.6f\n' % tuple(uv))
+				
+				uv_unique_count = len(uv_dict)
+				del uv, uvkey, uv_dict, f_index, uv_index
+				# Only need uv_unique_count and uv_face_mapping
 			
 			# NORMAL, Smooth/Non smoothed.
 			if EXPORT_NORMALS:
@@ -376,10 +393,11 @@
 							globalNormals[noKey] = totno
 							totno +=1
 							file.write('vn %.6f %.6f %.6f\n' % noKey)
+			
 			if not faceuv:
 				f_image = None
 			
-			for f in faces:
+			for f_index, f in enumerate(faces):
 				f_v= f.v
 				f_smooth= f.smooth
 				f_mat = min(f.mat, len(materialNames)-1)
@@ -388,7 +406,7 @@
 					f_uv= f.uv
 				
 				# MAKE KEY
-				if EXPORT_UV and faceuv and f_image: # Object is always true.
+				if faceuv and f_image: # Object is always true.
 					key = materialNames[f_mat],  f_image.name
 				else:
 					key = materialNames[f_mat],  None # No image, use None instead.
@@ -432,13 +450,13 @@
 						contextSmooth = f_smooth
 				
 				file.write('f')
-				if faceuv and EXPORT_UV:
+				if faceuv:
 					if EXPORT_NORMALS:
 						if f_smooth: # Smoothed, use vertex normals
 							for vi, v in enumerate(f_v):
 								file.write( ' %d/%d/%d' % (\
 								  v.index+totverts,\
-								  face_vert_index + vi,\
+								  totuvco + uv_face_mapping[f_index][vi],\
 								  globalNormals[ veckey3d(v.no) ])) # vert, uv, normal
 							
 						else: # No smoothing, face normals
@@ -446,14 +464,22 @@
 							for vi, v in enumerate(f_v):
 								file.write( ' %d/%d/%d' % (\
 								  v.index+totverts,\
-								  face_vert_index + vi,\
+								  totuvco + uv_face_mapping[f_index][vi],\
 								  no)) # vert, uv, normal
 					
 					else: # No Normals
 						for vi, v in enumerate(f_v):
+						
+							#print _uv_face_mapping == tuple([tuple([0] * len(f)) for f in faces])
+							if len(uv_face_mapping) != len(faces):
+								raise "ass"
+							print f_index
+							print uv_face_mapping[f_index]
+							print vi
+							print uv_face_mapping[f_index][vi]
 							file.write( ' %d/%d' % (\
 							  v.index+totverts,\
-							  face_vert_index + vi)) # vert, uv
+							  totuvco + uv_face_mapping[f_index][vi])) # vert, uv
 					
 					face_vert_index += len(f_v)
 				
@@ -486,6 +512,8 @@
 				
 			# Make the indicies global rather then per mesh
 			totverts += len(me.verts)
+			if faceuv:
+				totuvco += uv_unique_count
 			me.verts= None
 	file.close()
 	




More information about the Bf-blender-cvs mailing list