From dab46067bf3c3b808a7ba5071e0d51d753651674 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Sun, 19 Sep 2004 10:41:04 +0000 Subject: - Cam (ideasman) improved speed of exporting - I fixed indentation --- release/scripts/obj_export.py | 228 ++++++++++++++++++++---------------------- 1 file changed, 108 insertions(+), 120 deletions(-) (limited to 'release') diff --git a/release/scripts/obj_export.py b/release/scripts/obj_export.py index 12e09828169..3650409e6e7 100644 --- a/release/scripts/obj_export.py +++ b/release/scripts/obj_export.py @@ -43,10 +43,9 @@ def stripPath(path): for CH in range(len(path), 0, -1): if path[CH-1] == "/" or path[CH-1] == "\\": path = path[CH:] - break + break return path - #==================# # Apply Transform # #==================# @@ -55,15 +54,6 @@ def apply_transform(vert, matrix): vertCopy.resize4D() return Mathutils.VecMultMat(vertCopy, matrix) -#====================================================# -# Return a 6 deciaml point floating point value # -# as a string that dosent have any python chars # -#====================================================# -def saneFloat(float): - #return '%(float)b' % vars() # 6 fp as house.hqx - return str('%f' % float) + ' ' - - from Blender import * NULL_MAT = '(null)' @@ -73,140 +63,138 @@ def save_mtl(filename): file = open(filename, "w") for mat in Material.Get(): - file.write('newmtl ' + mat.getName() + '\n') # Define a new material - - file.write('Ns ' + saneFloat((mat.getHardness()-1) * 1.9607843137254901 ) + '\n') # Hardness, convert blenders 1-511 to MTL's + file.write('newmtl %s\n' % (mat.getName())) # Define a new material + + # Hardness, convert blenders 1-511 to MTL's + file.write('Ns %s\n' % ((mat.getHardness()-1) * 1.9607843137254901 ) ) col = mat.getRGBCol() - file.write('Kd ' + saneFloat(col[0]) + saneFloat(col[1]) + saneFloat(col[2]) +'\n') # Diffuse + # Diffuse + file.write('Kd %s %s %s\n' % (col[0], col[1], col[2])) col = mat.getMirCol() - file.write('Ka ' + saneFloat(col[0]) + saneFloat(col[1]) + saneFloat(col[2]) + '\n') # Ambient, uses mirror colour, + # Ambient, uses mirror colour, + file.write('Ka %s %s %s\n' % (col[0], col[1], col[2])) col = mat.getSpecCol() - file.write('Ks ' + saneFloat(col[0]) + saneFloat(col[1]) + saneFloat(col[2]) +'\n') # Specular - - file.write('d ' + saneFloat(mat.getAlpha()) +'\n') # Alpha (dissolve) + # Specular + file.write('Ks %s %s %s\n' % (col[0],col[1], col[2])) + + # Alpha (dissolve) + file.write('d %s\n' % (mat.getAlpha())) # illum, 0 to disable lightng, 2 is normal. - file.write('illum ') if mat.getMode() & Material.Modes['SHADELESS']: - file.write('0\n') # ignore lighting + file.write('illum 0\n') # ignore lighting else: - file.write('2\n') # light normaly + file.write('illum 2\n') # light normaly # End OF Mat file.write('\n') # new line file.close() +def save_obj(filename): + time1 = sys.time() + # First output all material + mtlfilename = filename[:-4] + '.mtl' + save_mtl(mtlfilename) + file = open(filename, "w") -def save_obj(filename): + # Write Header + file.write('# Blender OBJ File: %s\n' % (Get('filename'))) + file.write('# www.blender.org\n') + + # Tell the obj file what material file to use. + file.write('mtllib %s\n' % (stripPath(mtlfilename))) + + # Initialize totals, these are updated each object + totverts = totuvco = 0 + + # Get all meshs + for ob in Object.Get(): + if ob.getType() != 'Mesh': + continue + m = NMesh.GetRawFromObject(ob.name) + + # remove any edges, is not written back to the mesh so its not going to + # modify the open file. + for f in m.faces: + if len(f.v) < 3: + mesh.faces.remove(f) - # First output all material - mtlfilename = filename[:-4] + '.mtl' - save_mtl(mtlfilename) + if len(m.faces) == 0: # Make sure there is somthing to write. + continue #dont bother with this mesh. - file = open(filename, "w") + # Set the default mat + currentMatName = NULL_MAT + currentImgName = NULL_IMG - # Write Header - file.write('# Blender OBJ File: ' + Get('filename') + ' \n') - file.write('# www.blender.org\n') + #file.write('o ' + ob.getName() + '_' + m.name + '\n') # Write Object name + file.write('o %s_%s\n' % (ob.getName(), m.name)) # Write Object name - # Tell the obj file what file to use. - file.write('mtllib ' + stripPath(mtlfilename) + ' \n') + # Works 100% Yay + matrix = ob.getMatrix('worldspace') - # Initialize totals, these are updated each object - totverts = totuvco = 0 + # Vert + for v in m.verts: + # Transform the vert + vTx = apply_transform(v.co, matrix) + file.write('v %s %s %s\n' % (vTx[0], vTx[1], vTx[2])) + # UV + for f in m.faces: + for uvIdx in range(len(f.v)): + if f.uv: + file.write('vt %s %s 0.0\n' % (f.uv[uvIdx][0], f.uv[uvIdx][1])) + else: + file.write('vt 0.0 0.0 0.0\n') - # Get all meshs - for ob in Object.Get(): - if ob.getType() == 'Mesh': - m = ob.getData() - if len(m.verts) > 0: # Make sure there is somthing to write. - - # Set the default mat - currentMatName = NULL_MAT - currentImgName = NULL_IMG - - file.write('o ' + ob.getName() + '_' + m.name + '\n') # Write Object name - - # Works 100% Yay - matrix = ob.getMatrix('worldspace') - - # Vert - for v in m.verts: - # Transform the vert - vTx = apply_transform(v.co, matrix) - - file.write('v ') - file.write(saneFloat(vTx[0])) - file.write(saneFloat(vTx[1])) - file.write(saneFloat(vTx[2]) + '\n') - - # UV - for f in m.faces: - if len(f.v) > 2: - for uvIdx in range(len(f.v)): - file.write('vt ') - if f.uv: - file.write(saneFloat(f.uv[uvIdx][0])) - file.write(saneFloat(f.uv[uvIdx][1])) - else: - file.write('0.0 ') - file.write('0.0 ') - - file.write('0.0' + '\n') - - # NORMAL - for f1 in m.faces: - if len(f1.v) > 2: - for v in f1.v: - # Transform the normal - noTx = apply_transform(v.no, matrix) - noTx.normalize() - file.write('vn ') - file.write(saneFloat(noTx[0])) - file.write(saneFloat(noTx[1])) - file.write(saneFloat(noTx[2]) + '\n') - - uvIdx = 0 - for f in m.faces: - if len(f.v) > 2: - # Check material and change if needed. - if len(m.materials) > f.mat: - if currentMatName != m.materials[f.mat].getName(): - currentMatName = m.materials[f.mat].getName() - file.write('usemtl ' + currentMatName + '\n') - - elif currentMatName != NULL_MAT: - currentMatName = NULL_MAT - file.write('usemtl ' + currentMatName + '\n') - - # UV IMAGE - # If the face uses a different image from the one last set then add a usemap line. - if f.image: - if f.image.filename != currentImgName: - currentImgName = f.image.filename - file.write( 'usemat ' + stripPath(currentImgName) +'\n') # Set a new image for all following faces - - elif currentImgName != NULL_IMG: # Not using an image so set to NULL_IMG - currentImgName = NULL_IMG - file.write( 'usemat ' + stripPath(currentImgName) +'\n') # Set a new image for all following faces - - file.write('f ') - for v in f.v: - file.write( str(m.verts.index(v) + totverts +1) + '/') # Vert IDX - file.write( str(uvIdx + totuvco +1) + '/') # UV IDX - file.write( str(uvIdx + totuvco +1) + ' ') # NORMAL IDX - uvIdx+=1 - file.write('\n') + # NORMAL + for f1 in m.faces: + for v in f1.v: + # Transform the normal + noTx = apply_transform(v.no, matrix) + noTx.normalize() + file.write('vn %s %s %s\n' % (noTx[0], noTx[1], noTx[2])) + + uvIdx = 0 + for f in m.faces: + # Check material and change if needed. + if len(m.materials) > f.mat: + if currentMatName != m.materials[f.mat].getName(): + currentMatName = m.materials[f.mat].getName() + file.write('usemtl %s\n' % (currentMatName)) + + elif currentMatName != NULL_MAT: + currentMatName = NULL_MAT + file.write('usemtl %s\n' % (currentMatName)) + + # UV IMAGE + # If the face uses a different image from the one last set then add a usemap line. + if f.image: + if f.image.filename != currentImgName: + currentImgName = f.image.filename + # Set a new image for all following faces + file.write( 'usemat %s\n' % (stripPath(currentImgName))) - # Make the indicies global rather then per mesh - totverts += len(m.verts) - totuvco += uvIdx - file.close() + elif currentImgName != NULL_IMG: # Not using an image so set to NULL_IMG + currentImgName = NULL_IMG + # Set a new image for all following faces + file.write( 'usemat %s\n' % (stripPath(currentImgName))) + + file.write('f ') + for v in f.v: + file.write( '%s/%s/%s ' % (m.verts.index(v) + totverts+1, uvIdx+totuvco+1, uvIdx+totuvco+1)) + + uvIdx+=1 + file.write('\n') + + # Make the indicies global rather then per mesh + totverts += len(m.verts) + totuvco += uvIdx + file.close() + print "obj export time: ", sys.time() - time1 Window.FileSelector(save_obj, 'Export Wavefront OBJ', newFName('obj')) -- cgit v1.2.3