from .geo import Geo from .geomesh import * import bpy.path import bpy from bpy_extras.io_utils import axis_conversion def convert_mesh(geo_model, mesh, obj): # Be sure tessface & co are available! if not mesh.tessfaces and mesh.polygons: mesh.calc_tessface() mesh_verts = mesh.vertices # save a lookup has_uv = bool(mesh.tessface_uv_textures) if has_uv: active_uv_layer = mesh.tessface_uv_textures.active if not active_uv_layer: has_uv = False else: active_uv_layer = active_uv_layer.data geomesh = GeoMesh() texture_name = "white" print("len(mesh.tessfaces): %s" % len(mesh.tessfaces)) print("mesh.tessfaces: %s" % repr(mesh.tessfaces)) for i, f in enumerate(mesh.tessfaces): if has_uv: uv = active_uv_layer[i] texture_image = uv.image uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4] if texture_image is None: texture_name = "white" else: texture_name = texture_image.name if texture_name == "": texture_name = bpy.path.display_name_from_filepath(texture_image.filepath) if texture_name == "": texture_name = "white" else: uv = [(0, 0)] * 4 texture_name = "white" f_verts = f.vertices verts = [] norms = [] groups = [] geoverts = [] for i, v_index in enumerate(f_verts): v = mesh_verts[v_index] verts.append(v.co) norms.append(v.normal) weights = [] for weight in v.groups: group = obj.vertex_groups[weight.group] w = [group.name, weight.weight] weights.append(w) gv = GeoVertex(v.co, v.normal, uv[i], weights) geoverts.append(gv) geomesh.addFace(geoverts, texture_name) print("face: vertices: %s uvs: %s norms: %s groups: %s" % (verts, uv, norms, weights)) geomesh.dump() geo_model.loadFromGeoMesh(geomesh) #todo: pass def save(operator, context, filepath = "", global_matrix = None, use_mesh_modifiers = True): print("export_geo.save(): %s" % (filepath, )) geo = Geo() geo.getTextureIndex("white.tga") geo.getTextureIndex("white") body_name = bpy.path.display_name_from_filepath(filepath) geo.setName(body_name) axis_rotation = axis_conversion('-Y', 'Z', 'Z', 'Y') axis_rotation.resize_4x4() for ob in context.selected_objects: print("Object: %s (%s)" % (ob.name, ob.type)) if ob.type != "MESH": continue ob.update_from_editmode() if global_matrix is None: from mathutils import Matrix global_matrix = Matrix() # get the modifiers mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW") #translate_matrix = Matrix.Translation(-ob.location) translate_matrix = Matrix() # * ob.matrix_world mesh.transform(global_matrix * translate_matrix * axis_rotation) mesh.calc_normals() geo_model = geo.addModel(ob.name) convert_mesh(geo_model, mesh, ob) data = geo.saveToData() fh = open(filepath, "wb") fh.write(data) fh.close() return {'FINISHED'}