-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportbcfdata_singleNormalPerFace.py
More file actions
86 lines (64 loc) · 2.72 KB
/
exportbcfdata_singleNormalPerFace.py
File metadata and controls
86 lines (64 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import bpy
import bmesh
def write_bcf_data(filepath):
# get selected object mesh
print("Starting to write data-----------------")
me = bpy.context.active_object.data
bm = bmesh.new()
bm.from_mesh(me)
bmesh.ops.triangulate(bm, faces=bm.faces[:])
# face count after triangulation
triCount = len(bm.faces)
bm.to_mesh(me)
bm.free()
me.calc_loop_triangles()
str_vertices = []
str_normals = []
print(f"Found {triCount} triangles")
print(f"num triangles from loop_triangles: {len(me.loop_triangles)}")
for tri in me.polygons:
for vert_index in tri.vertices:
v = me.vertices[vert_index]
str_vertices.append(f"{v.co[0]} {v.co[1]} {v.co[2]}")
# same normal for all three verts
str_normals.append(' '.join([f"{tri.normal[0]} {tri.normal[1]} {tri.normal[2]}"] * 3))
#str_vertices = [f"{v.co[0]} {v.co[1]} {v.co[2]}" for v in me.vertices]
#str_normals = [f"{v.normal[0]} {v.normal[1]} {v.normal[2]}" for v in me.vertices]
print(f"Num vertices: {len(str_vertices)}")
f = open(filepath, "w", encoding='utf-8')
f.write(f"{len(str_vertices)} ")
f.write(f"{' '.join(str_vertices)} {' '.join(str_normals)}")
f.close()
return {'FINISHED'}
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class ExportBCFData(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "export_test.some_data" # Important since its how bpy.ops.import_test.some_data is constructed.
bl_label = "Export mesh in BCF format"
# ExportHelper mix-in class uses this.
filename_ext = ".bcf"
filter_glob: StringProperty(
default="*.bcf",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
return write_bcf_data(self.filepath)
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(ExportBCFData.bl_idname, text="BCF Export Operator")
# Register and add to the "file selector" menu (required to use F3 search "Text Export Operator" for quick access).
def register():
bpy.utils.register_class(ExportBCFData)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportBCFData)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
# Test call.
bpy.ops.export_test.some_data('INVOKE_DEFAULT')