init
[carveJwlIkooP6JGAAIwe30JlM.git] / blender_export.py
1 import bpy, math
2 from ctypes import *
3
4 class model(Structure):
5 _pack_ = 1
6 _fields_ = [("identifier",c_uint32),
7 ("vertex_count",c_uint32),
8 ("indice_count",c_uint32),
9 ("layer_count",c_uint32)]
10
11 class sdf_primative(Structure):
12 _pack_ = 1
13 _fields_ = [("origin",c_float*4),
14 ("info",c_float*4)]
15
16 class submodel(Structure):
17 _pack_ = 1
18 _fields_ = [("indice_start",c_uint32),
19 ("indice_count",c_uint32),
20 ("vertex_start",c_uint32),
21 ("vertex_count",c_uint32),
22 ("bbx",(c_float*3)*2),
23 ("sdf",sdf_primative),
24 ("sdf_type",c_int32),
25 ("name",c_char*32)]
26
27 class model_vert(Structure):
28 _pack_ = 1
29 _fields_ = [("co",c_float*3),
30 ("norm",c_float*3),
31 ("colour",c_float*4),
32 ("uv",c_float*2)]
33
34 def fixed_string(dest,string):
35 return
36 for i in range(len(string)):
37 dest[i] = string[i]
38
39 def write_model(name):
40 fp = open(F"/home/harry/Documents/carve/models/{name}.mdl", "wb")
41 collection = bpy.data.collections[name]
42
43 header = model()
44 header.identifier = 0xABCD0000
45 header.vertex_count = 0
46 header.indice_count = 0
47 header.layer_count = 0
48
49 layers = []
50 vertex_buffer = []
51 indice_buffer = []
52
53 for obj in collection.objects:
54 if obj.type == 'MESH':
55 dgraph = bpy.context.evaluated_depsgraph_get()
56 data = obj.evaluated_get(dgraph).data
57 data.calc_loop_triangles()
58 data.calc_normals_split()
59
60 sm = submodel()
61 sm.indice_start = header.indice_count
62 sm.vertex_start = header.vertex_count
63 sm.vertex_count = len(data.vertices)
64 sm.indice_count = len(data.loop_triangles)*3
65 sm.sdf_type = 0
66 for i in range(3):
67 sm.bbx[0][i] = 999999
68 sm.bbx[1][i] = -999999
69
70 if F"{obj.name}.sdf_cone" in bpy.data.objects:
71 cone = bpy.data.objects[F"{obj.name}.sdf_cone"]
72 sm.sdf.origin[0] = cone.location[0]
73 sm.sdf.origin[1] = cone.location[2] + cone.scale[1]*2.0
74 sm.sdf.origin[2] = -cone.location[1]
75 sm.sdf.origin[3] = 0.0
76
77 lo = cone.scale[0]
78 la = cone.scale[1]*2.0
79 lh = math.sqrt(lo*lo+la*la)
80
81 sm.sdf.info[0] = lo
82 sm.sdf.info[1] = la
83 sm.sdf.info[2] = lo/lh
84 sm.sdf.info[3] = la/lh
85
86 sm.sdf_type = 1
87
88 sm.name = obj.name.encode('utf-8')
89
90 for vert in data.vertices:
91 v = model_vert()
92 v.co[0] = vert.co[0]
93 v.co[1] = vert.co[2]
94 v.co[2] = -vert.co[1]
95 v.colour[0] = 0.0
96 v.colour[1] = 0.0
97 v.colour[2] = 0.0
98 v.colour[3] = 1.0
99 vertex_buffer += [v]
100
101 for i in range(3):
102 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
103 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
104
105 for l in data.loops:
106 pvert = vertex_buffer[l.vertex_index + sm.vertex_start]
107 norm = l.normal
108 pvert.norm[0] = norm[0]
109 pvert.norm[1] = norm[2]
110 pvert.norm[2] = -norm[1]
111
112 #if data.vertex_colors:
113 # colour = data.vertex_colors.active.data[ l.index ].color
114 # pvert.colour[0] = colour[0]
115
116 if data.uv_layers:
117 uv = data.uv_layers.active.data[ l.index ].uv
118 pvert.uv[0] = uv[0]
119 pvert.uv[1] = uv[1]
120
121 for tri in data.loop_triangles:
122 indice_buffer += [c_uint32(tri.vertices[_]) for _ in range(3)]
123
124 layers += [sm]
125 header.layer_count += 1
126 header.vertex_count += sm.vertex_count
127 header.indice_count += sm.indice_count
128
129 fp.write( bytearray( header ) )
130 for l in layers:
131 fp.write( bytearray(l) )
132 for v in vertex_buffer:
133 fp.write( bytearray(v) )
134 for i in indice_buffer:
135 fp.write( bytearray(i) )
136
137 fp.close()
138
139 write_model( "test" )
140 write_model( "free_dev" )
141 write_model( "char_dev" )