Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. import bpy
  2. import bmesh
  3. import mathutils
  4.  
  5. class TE_Vertex:
  6.     pos = mathutils.Vector()
  7.     normal = mathutils.Vector()
  8.     uv = mathutils.Vector((0, 0))
  9.    
  10. class TE_Mesh:
  11.     vertices = []
  12.     indices = []
  13.    
  14.    
  15. def prepare_mesh(mesh):
  16.    
  17.     bm = bmesh.new()
  18.     bm.from_mesh(mesh)
  19.     bmesh.ops.triangulate(bm, faces=bm.faces[:])
  20.    
  21.     export_mesh = TE_Mesh()
  22.     export_mesh.vertices = [TE_Vertex() for _ in range(len(bm.verts))]
  23.     export_mesh.indices = [-1 for _ in range(len(bm.faces) * 3)]
  24.  
  25.     uvl = bm.loops.layers.uv[0] if bm.loops.layers.uv else None
  26.    
  27.     for vert in bm.verts:
  28.         export_vert = export_mesh.vertices[vert.index]
  29.        
  30.         export_vert.pos = vert.co
  31.         export_vert.normal = vert.normal
  32.    
  33.     index = 0
  34.     for face in bm.faces:
  35.         vert0 = face.verts[0]
  36.         vert1 = face.verts[1]
  37.         vert2 = face.verts[2]
  38.        
  39.         export_mesh.indices[index] = vert0.index
  40.         export_mesh.indices[index + 1] = vert1.index
  41.         export_mesh.indices[index + 2] = vert2.index
  42.         index += 3
  43.        
  44.         if not face.smooth:
  45.             export_mesh.vertices[vert0.index].normal = face.normal
  46.             export_mesh.vertices[vert1.index].normal = face.normal
  47.             export_mesh.vertices[vert2.index].normal = face.normal
  48.                
  49.         if uvl:
  50.             export_mesh.vertices[vert0.index].uv = mathutils.Vector(face.loops[0][uvl].uv)
  51.             export_mesh.vertices[vert1.index].uv = mathutils.Vector(face.loops[1][uvl].uv)
  52.             export_mesh.vertices[vert2.index].uv = mathutils.Vector(face.loops[2][uvl].uv)
  53.  
  54.     return export_mesh
  55.  
  56.  
  57. def export_mesh_to_cpp(mesh, filepath):
  58.              
  59.     with open(filepath, 'w', encoding='utf-8') as f:
  60.         f.write("unique_ptr<StaticMesh> GeometryFactory::mesh()\n")
  61.         f.write("{\n")
  62.         f.write("   unique_ptr<StaticMesh> result = make_unique<StaticMesh>();\n")
  63.         f.write("\n")
  64.         f.write("   result->vertices = \n")
  65.         f.write("   {\n")
  66.  
  67.         for v in mesh.vertices:
  68.             f.write(str.format('        {{ {{ {0}, {1}, {2} }},  {{ {3}, {4}, {5} }}, {{ {6}, {7} }} }},\n',
  69.                 v.pos.x, v.pos.y, v.pos.z, v.normal.x, v.normal.y, v.normal.z, v.uv.x, v.uv.y));
  70.             print(v.uv)
  71.            
  72.         f.write("   };\n")
  73.         f.write("\n")
  74.         f.write("   result->indices = \n")
  75.         f.write("   {")
  76.  
  77.         cnt = 0
  78.         for i in mesh.indices:
  79.             if cnt % 3 == 0:
  80.                 f.write('\n    ')
  81.            
  82.             f.write(str.format('{0}, ', i))
  83.             cnt += 1
  84.  
  85.         f.write("\n")
  86.         f.write("   };\n")
  87.         f.write("\n")
  88.         f.write("   return result;\n")
  89.         f.write("}\n")
  90.            
  91.  
  92. def export_to_cpp(context, filepath):
  93.     print("Exporting to C++...")
  94.     with open(filepath, 'w', encoding='utf-8') as f:
  95.  
  96.         for obj in bpy.context.selected_objects:
  97.  
  98.             if obj.type != 'MESH':
  99.                 continue
  100.  
  101.             mesh = prepare_mesh(obj.data)
  102.             export_mesh_to_cpp(mesh, filepath)
  103.  
  104.     print("Exporting to C++... done")    
  105.     return {'FINISHED'}
  106.  
  107.  
  108. # ExportHelper is a helper class, defines filename and
  109. # invoke() function which calls the file selector.
  110. from bpy_extras.io_utils import ExportHelper
  111. from bpy.props import StringProperty, BoolProperty, EnumProperty
  112. from bpy.types import Operator
  113.  
  114.  
  115. class TE_CppExporter(Operator, ExportHelper):
  116.     """Export as C++ source"""
  117.     bl_idname = "te.cpp_export"  # important since its how bpy.ops.import_test.some_data is constructed
  118.     bl_label = "TE Export To C++"
  119.  
  120.     # ExportHelper mixin class uses this
  121.     filename_ext = ".txt"
  122.  
  123.     filter_glob: StringProperty(
  124.         default="*.cpp",
  125.         options={'HIDDEN'},
  126.         maxlen=255,  # Max internal buffer length, longer would be clamped.
  127.     )
  128.  
  129.     def execute(self, context):
  130.         return export_to_cpp(context, self.filepath)
  131.  
  132.  
  133. # Only needed if you want to add into a dynamic menu
  134. def menu_func_export(self, context):
  135.     self.layout.operator(TE_CppExporter.bl_idname, text="TE C++ Export")
  136.  
  137.  
  138. def register():
  139.     print("TE_CppExporter register")
  140.     bpy.utils.register_class(TE_CppExporter)
  141.     bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
  142.  
  143.  
  144. def unregister():
  145.     print("TE_CppExporter unregister")
  146.     bpy.utils.unregister_class(TE_CppExporter)
  147.     bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     register()
  152.  
  153.     # test call
  154.     bpy.ops.te.cpp_export('INVOKE_DEFAULT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement