|
| 1 | +import bpy |
| 2 | + |
| 3 | +def write_gpmesh(context, filepath, vertFormat): |
| 4 | + # Get current object |
| 5 | + obj = bpy.context.scene.objects.active |
| 6 | + if obj.type == 'MESH': |
| 7 | + mesh = obj.data |
| 8 | + f = open(filepath, 'w', encoding='utf-8') |
| 9 | + f.write("{\n") |
| 10 | + f.write("\t\"version\":1,\n") |
| 11 | + f.write("\t\"vertexformat\":\"" + vertFormat + "\",\n") |
| 12 | + f.write("\t\"shader\":\"BasicMesh\",\n") |
| 13 | + |
| 14 | + # For now, only one texture |
| 15 | + # figure out the file name... |
| 16 | + texname = filepath.split("\\")[-1].split("/")[-1].split(".")[0] |
| 17 | + # Make it a png |
| 18 | + texname += ".png" |
| 19 | + f.write("\t\"textures\":[\n") |
| 20 | + f.write("\t\t\"" + texname + "\"\n") |
| 21 | + f.write("\t],\n") |
| 22 | + |
| 23 | + # specular power |
| 24 | + f.write("\t\"specularPower\":100,\n") |
| 25 | + |
| 26 | + # vertices |
| 27 | + # We have to create our own storage for because uvs are stored separately |
| 28 | + verts = [dict() for x in range(len(mesh.vertices))] |
| 29 | + for v in mesh.vertices: |
| 30 | + verts[v.index]["pos"] = v.co |
| 31 | + verts[v.index]["norm"] = v.normal |
| 32 | + |
| 33 | + for l in mesh.loops: |
| 34 | + verts[l.vertex_index]["uv"] = mesh.uv_layers.active.data[l.index].uv |
| 35 | + |
| 36 | + f.write("\t\"vertices\":[\n") |
| 37 | + first = True |
| 38 | + for v in verts: |
| 39 | + if first: |
| 40 | + f.write("\t\t[") |
| 41 | + first = False |
| 42 | + else: |
| 43 | + f.write(",\n\t\t[") |
| 44 | + f.write("%f,%f,%f," % (v["pos"].x, v["pos"].y, v["pos"].z)) |
| 45 | + f.write("%f,%f,%f," % (v["norm"].x, v["norm"].y, v["norm"].z)) |
| 46 | + f.write("%f,%f" % (v["uv"].x, v["uv"].y)) |
| 47 | + f.write("]") |
| 48 | + f.write("\n\t],\n") |
| 49 | + |
| 50 | + # indices |
| 51 | + f.write("\t\"indices\":[\n") |
| 52 | + first = True |
| 53 | + for p in mesh.polygons: |
| 54 | + if first: |
| 55 | + f.write("\t\t") |
| 56 | + first = False |
| 57 | + else: |
| 58 | + f.write(",\n\t\t") |
| 59 | + f.write("[%d,%d,%d]" % (p.vertices[0], p.vertices[1], p.vertices[2])) |
| 60 | + f.write("\n\t]\n") |
| 61 | + |
| 62 | + f.write("}\n") |
| 63 | + f.close() |
| 64 | + else: |
| 65 | + raise ValueError("No mesh selected") |
| 66 | + |
| 67 | + return {'FINISHED'} |
| 68 | + |
| 69 | + |
| 70 | +# ExportHelper is a helper class, defines filename and |
| 71 | +# invoke() function which calls the file selector. |
| 72 | +from bpy_extras.io_utils import ExportHelper |
| 73 | +from bpy.props import StringProperty, BoolProperty, EnumProperty |
| 74 | +from bpy.types import Operator |
| 75 | + |
| 76 | + |
| 77 | +class ExportGPMesh(Operator, ExportHelper): |
| 78 | + """Export to Game Programming in C++ mesh format""" |
| 79 | + bl_idname = "export_test.gpmesh" # important since its how bpy.ops.import_test.some_data is constructed |
| 80 | + bl_label = "Export Mesh" |
| 81 | + |
| 82 | + # ExportHelper mixin class uses this |
| 83 | + filename_ext = ".gpmesh" |
| 84 | + |
| 85 | + filter_glob = StringProperty( |
| 86 | + default="*.gpmesh", |
| 87 | + options={'HIDDEN'}, |
| 88 | + maxlen=255, # Max internal buffer length, longer would be clamped. |
| 89 | + ) |
| 90 | + |
| 91 | + vertFormat = EnumProperty( |
| 92 | + name="Vertex Format", |
| 93 | + description="Choose the vertex format", |
| 94 | + items=(('PosNormTex', "PosNormTex", "Position, normal, tex coord"), |
| 95 | + ('PosTex', "PosTex", "Position, tex coord")), |
| 96 | + default='PosNormTex', |
| 97 | + ) |
| 98 | + |
| 99 | + def execute(self, context): |
| 100 | + return write_gpmesh(context, self.filepath, self.vertFormat) |
| 101 | + |
| 102 | + |
| 103 | +# Only needed if you want to add into a dynamic menu |
| 104 | +def menu_func_export(self, context): |
| 105 | + self.layout.operator(ExportGPMesh.bl_idname, text="GPMesh Exporter (.gpmesh)") |
| 106 | + |
| 107 | + |
| 108 | +def register(): |
| 109 | + bpy.utils.register_class(ExportGPMesh) |
| 110 | + bpy.types.INFO_MT_file_export.append(menu_func_export) |
| 111 | + |
| 112 | + |
| 113 | +def unregister(): |
| 114 | + bpy.utils.unregister_class(ExportGPMesh) |
| 115 | + bpy.types.INFO_MT_file_export.remove(menu_func_export) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + register() |
| 120 | + |
| 121 | + # test call |
| 122 | + bpy.ops.export_test.gpmesh('INVOKE_DEFAULT') |
0 commit comments