diff --git a/release/scripts/obj_export.py b/release/scripts/obj_export.py index 2fb84274c98..c260e619b97 100644 --- a/release/scripts/obj_export.py +++ b/release/scripts/obj_export.py @@ -1,9 +1,9 @@ #!BPY """ -Name: 'OBJ Wavefront' +Name: 'Wavefront (*.obj)' Blender: 232 -Group: 'Export' +Group: 'Export' Tooltip: 'Save a Wavefront OBJ File' """ @@ -45,27 +45,9 @@ def getWorldMat(ob): #==================# # Apply Transform # #==================# -def apply_transform(verts, matrix): - - x, y, z = verts - xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2] - xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc - ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc - zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc - - return [xcomponent, ycomponent, zcomponent] - - -#=====================================# -# Apply Transform for vertex normals # -# ignore the translation # -#=====================================# -def apply_normal_transform(verts, matrix): - x, y, z = verts - xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] - ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] - zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] - return Mathutils.Vector([xcomponent, ycomponent, zcomponent]) +def apply_transform(verts, matrix): + verts.resize4D() + return Mathutils.VecMultMat(verts, matrix) #====================================================# # Return a 6 deciaml point floating point value # @@ -129,8 +111,8 @@ def save_obj(filename): for f in m.faces: for v in f.v: # Transform the normal - noTx = apply_normal_transform(v.no, matrix) - noTx.normalize() + noTx = apply_transform(v.no, matrix) + noTx.normalize() file.write('vn ') file.write(saneFloat(noTx[0])) file.write(saneFloat(noTx[1])) diff --git a/release/scripts/obj_import.py b/release/scripts/obj_import.py index 856b156deb5..1b015db5089 100644 --- a/release/scripts/obj_import.py +++ b/release/scripts/obj_import.py @@ -1,7 +1,7 @@ #!BPY """ -Name: 'OBJ Wavefront' +Name: 'Wavefront (*.obj)' Blender: 232 Group: 'Import' Tooltip: 'Load a Wavefront OBJ File' @@ -150,12 +150,12 @@ def load_obj(file): # This is a new vert, make a new mesh mesh.verts.append( NMesh.Vert(eval(l[1]), eval(l[2]), eval(l[3]) ) ) - elif l[0] == 'vn': - pass + elif l[0] == 'vn': + pass - elif l[0] == 'vt': - # This is a new vert, make a new mesh - uvMapList.append( (eval(l[1]), eval(l[2])) ) + elif l[0] == 'vt': + # This is a new vert, make a new mesh + uvMapList.append( (eval(l[1]), eval(l[2])) ) elif l[0] == 'f': @@ -165,75 +165,75 @@ def load_obj(file): # Set up vIdxLs : Verts # Set up vtIdxLs : UV - vIdxLs = [] - vtIdxLs = [] + vIdxLs = [] + vtIdxLs = [] for v in l[1:]: - objVert = split( v, ['/'] ) + objVert = split( v, ['/'] ) - # VERT INDEX - vIdxLs.append(eval(objVert[0]) -1) - # UV - if len(objVert) == 1: - vtIdxLs.append(eval(objVert[0]) -1) # Sticky UV coords - else: - vtIdxLs.append(eval(objVert[1]) -1) # Seperate UV coords + # VERT INDEX + vIdxLs.append(eval(objVert[0]) -1) + # UV + if len(objVert) == 1: + vtIdxLs.append(eval(objVert[0]) -1) # Sticky UV coords + else: + vtIdxLs.append(eval(objVert[1]) -1) # Seperate UV coords # Quads only, we could import quads using the method below but it polite to import a quad as a quad.f - if len(vIdxLs) == 4: - f.v.append(mesh.verts[vIdxLs[0]]) - f.v.append(mesh.verts[vIdxLs[1]]) - f.v.append(mesh.verts[vIdxLs[2]]) - f.v.append(mesh.verts[vIdxLs[3]]) - # UV MAPPING - if uvMapList: - if vtIdxLs[0] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[0] ] ) - if vtIdxLs[1] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[1] ] ) - if vtIdxLs[2] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[2] ] ) - if vtIdxLs[3] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[3] ] ) - mesh.faces.append(f) # move the face onto the mesh + if len(vIdxLs) == 4: + f.v.append(mesh.verts[vIdxLs[0]]) + f.v.append(mesh.verts[vIdxLs[1]]) + f.v.append(mesh.verts[vIdxLs[2]]) + f.v.append(mesh.verts[vIdxLs[3]]) + # UV MAPPING + if uvMapList: + if vtIdxLs[0] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[0] ] ) + if vtIdxLs[1] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[1] ] ) + if vtIdxLs[2] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[2] ] ) + if vtIdxLs[3] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[3] ] ) + mesh.faces.append(f) # move the face onto the mesh - elif len(vIdxLs) >= 3: # This handles tri's and fans - for i in range(len(vIdxLs)-2): - f = NMesh.Face() - mesh, f = applyMat(mesh, f, currentMat) - f.v.append(mesh.verts[vIdxLs[0]]) - f.v.append(mesh.verts[vIdxLs[i+1]]) - f.v.append(mesh.verts[vIdxLs[i+2]]) - # UV MAPPING - if uvMapList: - if vtIdxLs[0] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[0] ] ) - if vtIdxLs[1] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[i+1] ] ) - if vtIdxLs[2] < len(uvMapList): - f.uv.append( uvMapList[ vtIdxLs[i+2] ] ) + elif len(vIdxLs) >= 3: # This handles tri's and fans + for i in range(len(vIdxLs)-2): + f = NMesh.Face() + mesh, f = applyMat(mesh, f, currentMat) + f.v.append(mesh.verts[vIdxLs[0]]) + f.v.append(mesh.verts[vIdxLs[i+1]]) + f.v.append(mesh.verts[vIdxLs[i+2]]) + # UV MAPPING + if uvMapList: + if vtIdxLs[0] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[0] ] ) + if vtIdxLs[1] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[i+1] ] ) + if vtIdxLs[2] < len(uvMapList): + f.uv.append( uvMapList[ vtIdxLs[i+2] ] ) - mesh.faces.append(f) # move the face onto the mesh + mesh.faces.append(f) # move the face onto the mesh - # is o the only vert/face delimeter? - # if not we could be screwed. - elif l[0] == 'o': - # Make sure the objects is worth puttong - if len(mesh.verts) > 0: - NMesh.PutRaw(mesh, fileName + '_' + objectName) - # Make new mesh - mesh = NMesh.GetRaw() + # is o the only vert/face delimeter? + # if not we could be screwed. + elif l[0] == 'o': + # Make sure the objects is worth puttong + if len(mesh.verts) > 0: + NMesh.PutRaw(mesh, fileName + '_' + objectName) + # Make new mesh + mesh = NMesh.GetRaw() - # New mesh name - objectName = join(l[1:]) # Use join in case of spaces + # New mesh name + objectName = join(l[1:]) # Use join in case of spaces # New texture list - uvMapList = [] + uvMapList = [] - elif l[0] == 'usemtl': - if l[1] == '(null)': - currentMat = NULL_MAT - else: - currentMat = getMat(join(l[1:])) # Use join in case of spaces + elif l[0] == 'usemtl': + if l[1] == '(null)': + currentMat = getMat(NULL_MAT) + else: + currentMat = getMat(join(l[1:])) # Use join in case of spaces lIdx+=1