From f368b60bafcf9643afa7a51447ceca813b656abc Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 4 Jun 2007 08:15:27 +0000 Subject: [PATCH] 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 --- release/scripts/export_obj.py | 124 +++++++++----------- source/blender/python/api2_2x/Draw.c | 5 +- source/blender/python/api2_2x/gen_library.c | 9 +- 3 files changed, 68 insertions(+), 70 deletions(-) diff --git a/release/scripts/export_obj.py b/release/scripts/export_obj.py index 4c7ced2d200..c8a5fc55430 100644 --- a/release/scripts/export_obj.py +++ b/release/scripts/export_obj.py @@ -68,15 +68,11 @@ def fixName(name): 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 @@ def write_mtl(filename): 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 @@ def write_mtl(filename): 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 @@ def copy_images(dest_dir): # 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 @@ def copy_images(dest_dir): 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 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) 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 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) materials = me.materials materialNames = [] + materialItems = materials[:] if materials: for mat in materials: if mat: # !=None @@ -313,7 +308,7 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) # 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. @@ -375,19 +370,22 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) 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 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) 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 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) # 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])) - - 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) + 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) 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 @@ EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_MORPH_TARGET=False) 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 @@ def write_ui(filename): #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 @@ def write_ui(filename): # removed too many options are bad! - # Get USER Options pup_block = [\ ('Context...'),\ diff --git a/source/blender/python/api2_2x/Draw.c b/source/blender/python/api2_2x/Draw.c index ede780294ef..d0d2810bfae 100644 --- a/source/blender/python/api2_2x/Draw.c +++ b/source/blender/python/api2_2x/Draw.c @@ -740,7 +740,7 @@ static void exec_but_callback(void *pyobj, void *data) 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 @@ static void exec_but_callback(void *pyobj, void *data) 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 @@ static void exec_but_callback(void *pyobj, void *data) PyTuple_SetItem( arg, 1, pyvalue ); result = PyObject_CallObject( callback, arg ); + Py_DECREF(arg); + if (!result) { Py_DECREF(pyvalue); PyErr_Print( ); diff --git a/source/blender/python/api2_2x/gen_library.c b/source/blender/python/api2_2x/gen_library.c index 1e9470f8dcf..315a86ff607 100644 --- a/source/blender/python/api2_2x/gen_library.c +++ b/source/blender/python/api2_2x/gen_library.c @@ -330,9 +330,12 @@ long GenericLib_hash(PyObject * pydata) { ID *id = ((BPy_GenericLib *)pydata)->id; PyObject *pyhash = PyTuple_New( 2 ); + long hash; PyTuple_SetItem( pyhash, 0, PyString_FromString(id->name) ); - if (id->lib) PyTuple_SetItem( pyhash, 0, PyString_FromString(id->lib->name) ); - else PyTuple_SetItem( pyhash, 1, Py_None ); - return PyObject_Hash(pyhash); + if (id->lib) PyTuple_SetItem( pyhash, 1, PyString_FromString(id->lib->name) ); + else PyTuple_SetItem( pyhash, 1, EXPP_incr_ret(Py_None) ); + hash = PyObject_Hash(pyhash); + Py_DECREF(pyhash); + return hash; }