[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [10872] trunk/blender: OBJ smooting groups exported enabled for by default for the first smooth group (should have been disabled)

Campbell Barton cbarton at metavr.com
Mon Jun 4 10:15:27 CEST 2007


Revision: 10872
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=10872
Author:   campbellbarton
Date:     2007-06-04 10:15:27 +0200 (Mon, 04 Jun 2007)

Log Message:
-----------
OBJ smooting groups exported enabled for by default for the first smooth group (should have been disabled)
gen_library.c - Own error, hashing did not decref the tuple it created.
Draw.c - callback also missed a decref

Modified Paths:
--------------
    trunk/blender/release/scripts/export_obj.py
    trunk/blender/source/blender/python/api2_2x/Draw.c
    trunk/blender/source/blender/python/api2_2x/gen_library.c

Modified: trunk/blender/release/scripts/export_obj.py
===================================================================
--- trunk/blender/release/scripts/export_obj.py	2007-06-04 08:03:37 UTC (rev 10871)
+++ trunk/blender/release/scripts/export_obj.py	2007-06-04 08:15:27 UTC (rev 10872)
@@ -68,15 +68,11 @@
 	else:
 		return name.replace(' ', '_')
 
-# Used to add the scene name into the filename without using odd chars
-global MTL_DICT
-
 # A Dict of Materials
 # (material.name, image.name):matname_imagename # matname_imagename has gaps removed.
 MTL_DICT = {} 
 
 def write_mtl(filename):
-	global MTL_DICT
 	
 	world = Blender.World.GetCurrent()
 	if world:
@@ -88,23 +84,14 @@
 	file.write('# Blender3D MTL File: %s\n' % Blender.Get('filename').split('\\')[-1].split('/')[-1])
 	file.write('# Material Count: %i\n' % len(MTL_DICT))
 	# Write material/image combinations we have used.
-	for key, mtl_mat_name in MTL_DICT.iteritems():
+	for key, (mtl_mat_name, mat, img) in MTL_DICT.iteritems():
 		
 		# Get the Blender data for the material and the image.
 		# Having an image named None will make a bug, dont do it :)
 		
 		file.write('newmtl %s\n' % mtl_mat_name) # Define a new material: matname_imgname
 		
-		if key[0] == None:
-			#write a dummy material here?
-			file.write('Ns 0\n')
-			file.write('Ka %.6f %.6f %.6f\n' %  tuple([c for c in worldAmb])  ) # Ambient, uses mirror colour,
-			file.write('Kd 0.8 0.8 0.8\n')
-			file.write('Ks 0.8 0.8 0.8\n')
-			file.write('d 1\n') # No alpha
-			file.write('illum 2\n') # light normaly	
-		else:
-			mat = Blender.Material.Get(key[0])
+		if mat:
 			file.write('Ns %.6f\n' % ((mat.getHardness()-1) * 1.9607843137254901) ) # Hardness, convert blenders 1-511 to MTL's 
 			file.write('Ka %.6f %.6f %.6f\n' %  tuple([c*mat.amb for c in worldAmb])  ) # Ambient, uses mirror colour,
 			file.write('Kd %.6f %.6f %.6f\n' % tuple([c*mat.ref for c in mat.rgbCol]) ) # Diffuse
@@ -120,13 +107,20 @@
 			else:
 				file.write('illum 2\n') # light normaly	
 		
+		else:
+			#write a dummy material here?
+			file.write('Ns 0\n')
+			file.write('Ka %.6f %.6f %.6f\n' %  tuple([c for c in worldAmb])  ) # Ambient, uses mirror colour,
+			file.write('Kd 0.8 0.8 0.8\n')
+			file.write('Ks 0.8 0.8 0.8\n')
+			file.write('d 1\n') # No alpha
+			file.write('illum 2\n') # light normaly
 		
 		# Write images!
-		if key[1] != None:  # We have an image on the face!
-			img = Image.Get(key[1])
+		if img:  # We have an image on the face!
 			file.write('map_Kd %s\n' % img.filename.split('\\')[-1].split('/')[-1]) # Diffuse mapping image			
 		
-		elif key[0] != None: # No face image. if we havea material search for MTex image.
+		elif not mat: # No face image. if we havea material search for MTex image.
 			for mtex in mat.getTextures():
 				if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
 					try:
@@ -157,26 +151,26 @@
 	
 	# Get unique image names
 	uniqueImages = {}
-	for matname, imagename in MTL_DICT.iterkeys(): # Only use image name
+	for matname, mat, image in MTL_DICT.itervalues(): # Only use image name
 		# Get Texface images
-		if imagename != None:
-			uniqueImages[imagename] = None # Should use sets here. wait until Python 2.4 is default.
+		if image:
+			uniqueImages[image] = image # Should use sets here. wait until Python 2.4 is default.
 		
 		# Get MTex images
-		if matname != None:
-			mat= Blender.Material.Get(matname)
+		if mat:
 			for mtex in mat.getTextures():
 				if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
-					try:
-						uniqueImages[mtex.tex.image.name] = None
-					except:
-						pass
+					image_tex = mtex.tex.image
+					if image_tex:
+						try:
+							uniqueImages[image_tex] = image_tex
+						except:
+							pass
 	
 	# Now copy images
 	copyCount = 0
 	
-	for imageName in uniqueImages.iterkeys():
-		bImage = Image.Get(imageName)
+	for bImage in uniqueImages.itervalues():
 		image_path = sys.expandpath(bImage.filename)
 		if sys.exists(image_path):
 			# Make a name for the target path.
@@ -187,12 +181,6 @@
 				copyCount+=1
 	print '\tCopied %d images' % copyCount
 
-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 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,\
@@ -204,8 +192,14 @@
 	eg.
 	write( 'c:\\test\\foobar.obj', Blender.Object.GetSelected() ) # Using default options.
 	'''
+	
+	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)
+	
 	print 'OBJ Export path: "%s"' % filename
-	global MTL_DICT
 	temp_mesh_name = '~tmp-mesh'
 
 	time1 = sys.time()
@@ -301,6 +295,7 @@
 			materials = me.materials
 			
 			materialNames = []
+			materialItems = materials[:]
 			if materials:
 				for mat in materials:
 					if mat: # !=None
@@ -313,8 +308,8 @@
 			# Possible there null materials, will mess up indicies
 			# but at least it will export, wait until Blender gets fixed.
 			materialNames.extend((16-len(materialNames)) * [None])
+			materialItems.extend((16-len(materialItems)) * [None])
 			
-			
 			# Sort by Material, then images
 			# so we dont over context switch in the obj file.
 			if EXPORT_MORPH_TARGET:
@@ -375,19 +370,22 @@
 							globalNormals[noKey] = totno
 							totno +=1
 							file.write('vn %.6f %.6f %.6f\n' % noKey)
+			if not faceuv:
+				f_image = None
 			
 			for f in faces:
 				f_v= f.v
+				f_smooth= f.smooth
+				f_mat = min(f.mat, len(materialNames)-1)
 				if faceuv:
+					f_image = f.image
 					f_uv= f.uv
 				
 				# MAKE KEY
-				if EXPORT_UV and faceuv and f.image: # Object is always true.
-					key = materialNames[min(f.mat,len(materialNames)-1)],  f.image.name
-					#key = materialNames[f.mat],  f.image.name
+				if EXPORT_UV and faceuv and f_image: # Object is always true.
+					key = materialNames[f_mat],  f_image.name
 				else:
-					key = materialNames[min(f.mat,len(materialNames)-1)],  None # No image, use None instead.
-					#key = materialNames[f.mat],  None # No image, use None instead.
+					key = materialNames[f_mat],  None # No image, use None instead.
 				
 				# CHECK FOR CONTEXT SWITCH
 				if key == contextMat:
@@ -395,16 +393,13 @@
 				else:
 					if key[0] == None and key[1] == None:
 						# Write a null material, since we know the context has changed.
-						matstring = '(null)'
+						if EXPORT_GROUP_BY_MAT:
+							file.write('g %s_%s\n' % (fixName(ob.name), fixName(ob.getData(1))) ) # can be mat_image or (null)
 						file.write('usemtl (null)\n') # mat, image
 						
 					else:
-						try: # Faster to try then 2x dict lookups.
-							# We have the material, just need to write the context switch,
-							matstring = MTL_DICT[key]
-							
-							
-						except KeyError:
+						mat_data= MTL_DICT.get(key)
+						if not mat_data:
 							# First add to global dict so we can export to mtl
 							# Then write mtl
 							
@@ -413,28 +408,27 @@
 							
 							# If none image dont bother adding it to the name
 							if key[1] == None:
-								matstring = MTL_DICT[key] ='%s' % fixName(key[0])
+								mat_data = MTL_DICT[key] = ('%s'%fixName(key[0])), materialItems[f_mat], f_image
 							else:
-								matstring = MTL_DICT[key] = '%s_%s' % (fixName(key[0]), fixName(key[1]))
+								mat_data = MTL_DICT[key] = ('%s_%s' % (fixName(key[0]), fixName(key[1]))), materialItems[f_mat], f_image
+						
+						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)
 					
-					if EXPORT_GROUP_BY_MAT:
-						file.write('g %s_%s_%s\n' % (fixName(ob.name), fixName(ob.getData(1)), matstring) ) # can be mat_image or (null)
-					file.write('usemtl %s\n' % matstring) # can be mat_image or (null)
-					
 				contextMat = key
-				
-				if f.smooth != contextSmooth:
-					if contextSmooth: # on now off
-						file.write('s off\n')
-						contextSmooth = True
-					else: # was off now on
+				if f_smooth != contextSmooth:
+					if f_smooth: # on now off
 						file.write('s 1\n')
-						contextSmooth = False
+						contextSmooth = f_smooth
+					else: # was off now on
+						file.write('s off\n')
+						contextSmooth = f_smooth
 				
 				file.write('f')
 				if faceuv and EXPORT_UV:
 					if EXPORT_NORMALS:
-						if f.smooth: # Smoothed, use vertex normals
+						if f_smooth: # Smoothed, use vertex normals
 							for vi, v in enumerate(f_v):
 								file.write( ' %d/%d/%d' % (\
 								  v.index+totverts,\
@@ -459,7 +453,7 @@
 				
 				else: # No UV's
 					if EXPORT_NORMALS:
-						if f.smooth: # Smoothed, use vertex normals
+						if f_smooth: # Smoothed, use vertex normals
 							for v in f_v:
 								file.write( ' %d//%d' % (\
 								  v.index+totverts,\
@@ -515,7 +509,6 @@
 	#if not BPyMessages.Warning_SaveOver(filename):
 	#	return
 	
-	
 	EXPORT_APPLY_MODIFIERS = Draw.Create(1)
 	EXPORT_ROTX90 = Draw.Create(1)
 	EXPORT_TRI = Draw.Create(0)
@@ -535,7 +528,6 @@
 	
 	# removed too many options are bad!
 	
-	
 	# Get USER Options
 	pup_block = [\
 	('Context...'),\

Modified: trunk/blender/source/blender/python/api2_2x/Draw.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Draw.c	2007-06-04 08:03:37 UTC (rev 10871)
+++ trunk/blender/source/blender/python/api2_2x/Draw.c	2007-06-04 08:15:27 UTC (rev 10872)
@@ -740,7 +740,7 @@
 	PyObject *result;
 	PyObject * pyvalue;
 	uiBut *but = (uiBut *)data;
-	PyObject *arg = PyTuple_New( 2 );
+	PyObject *arg;
 	PyObject *callback = (PyObject *)pyobj;
 	
 	double value = ui_get_but_val(but);
@@ -795,6 +795,7 @@
 		printf("Error, no button type matched.");
 	}
 	
+	arg = PyTuple_New( 2 );
 	if (uiblock==NULL)
 		PyTuple_SetItem( arg, 0, PyInt_FromLong(but->retval - EXPP_BUTTON_EVENTS_OFFSET) );
 	else
@@ -803,6 +804,8 @@
 	PyTuple_SetItem( arg, 1, pyvalue );
 	
 	result = PyObject_CallObject( callback, arg );
+	Py_DECREF(arg);
+	
 	if (!result) {
 		Py_DECREF(pyvalue);
 		PyErr_Print(  );

Modified: trunk/blender/source/blender/python/api2_2x/gen_library.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/gen_library.c	2007-06-04 08:03:37 UTC (rev 10871)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list