4 class model(Structure
):
6 _fields_
= [("identifier",c_uint32
),
7 ("vertex_count",c_uint32
),
8 ("indice_count",c_uint32
),
9 ("layer_count",c_uint32
),
10 ("marker_count",c_uint32
)]
12 class submodel(Structure
):
14 _fields_
= [("indice_start",c_uint32
),
15 ("indice_count",c_uint32
),
16 ("vertex_start",c_uint32
),
17 ("vertex_count",c_uint32
),
18 ("bbx",(c_float
*3)*2),
22 ("material",c_char
*32)]
24 class marker(Structure
):
26 _fields_
= [("co",c_float
*3),
31 class model_vert(Structure
):
33 _fields_
= [("co",c_float
*3),
39 return a
[0]*b
[0] + a
[1]*b
[1] + a
[2]*b
[2] + a
[3]*a
[3]
42 return math
.sqrt( v4_dot(a
,a
) )
45 if abs(a
[0]-b
[0]) < 0.0001:
46 if abs(a
[1]-b
[1]) < 0.0001:
51 if abs(a
[0]-b
[0]) < 0.0001:
52 if abs(a
[1]-b
[1]) < 0.0001:
53 if abs(a
[2]-b
[2]) < 0.0001:
58 if abs(a
[0]-b
[0]) < 0.0001:
59 if abs(a
[1]-b
[1]) < 0.0001:
60 if abs(a
[2]-b
[2]) < 0.0001:
61 if abs(a
[3]-b
[3]) < 0.0001:
65 def m3x3_mul( a
, b
, d
):
84 d
[0][0] = a00
*b00
+ a10
*b01
+ a20
*b02
85 d
[0][1] = a01
*b00
+ a11
*b01
+ a21
*b02
86 d
[0][2] = a02
*b00
+ a12
*b01
+ a22
*b02
87 d
[1][0] = a00
*b10
+ a10
*b11
+ a20
*b12
88 d
[1][1] = a01
*b10
+ a11
*b11
+ a21
*b12
89 d
[1][2] = a02
*b10
+ a12
*b11
+ a22
*b12
90 d
[2][0] = a00
*b20
+ a10
*b21
+ a20
*b22
91 d
[2][1] = a01
*b20
+ a11
*b21
+ a21
*b22
92 d
[2][2] = a02
*b20
+ a12
*b21
+ a22
*b22
96 s
= 2.0 if l
> 0.0 else 0.0
106 d
[0][0] = 1.0 - yy
- zz
107 d
[1][1] = 1.0 - xx
- zz
108 d
[2][2] = 1.0 - xx
- yy
117 diag
= m
[0][0] + m
[1][1] + m
[2][2]
119 r
= math
.sqrt( 1.0 + diag
)
121 q
[0] = rinv
* (m
[1][2] - m
[2][1])
122 q
[1] = rinv
* (m
[2][0] - m
[0][2])
123 q
[2] = rinv
* (m
[0][1] - m
[1][0])
125 elif m
[0][0] >= m
[1][1] and m
[0][0] >= m
[2][2]:
126 r
= math
.sqrt( 1.0 - m
[1][1] - m
[2][2] + m
[0][0] )
129 q
[1] = rinv
* (m
[0][1] + m
[1][0])
130 q
[2] = rinv
* (m
[0][2] + m
[2][0])
131 q
[3] = rinv
* (m
[1][2] - m
[2][1])
132 elif m
[1][1] >= m
[2][2]:
133 r
= math
.sqrt( 1.0 - m
[0][0] - m
[2][2] + m
[1][1] )
135 q
[0] = rinv
* (m
[0][1] + m
[1][0])
137 q
[2] = rinv
* (m
[1][2] + m
[2][1])
138 q
[3] = rinv
* (m
[2][0] - m
[0][2])
140 r
= math
.sqrt( 1.0 - m
[0][0] - m
[1][1] + m
[2][2] )
142 q
[0] = rinv
* (m
[0][2] + m
[2][0])
143 q
[1] = rinv
* (m
[1][2] + m
[2][1])
145 q
[3] = rinv
* (m
[0][1] - m
[1][0])
147 def write_model(name
):
148 fp
= open(F
"/home/harry/Documents/carve/models/{name}.mdl", "wb")
149 collection
= bpy
.data
.collections
[name
]
152 header
.identifier
= 0xABCD0000
153 header
.vertex_count
= 0
154 header
.indice_count
= 0
155 header
.layer_count
= 0
162 print( F
"Create mode {name}" )
164 for obj
in collection
.objects
:
165 if obj
.type == 'EMPTY':
167 mk
.co
[0] = obj
.location
[0]
168 mk
.co
[1] = obj
.location
[2]
169 mk
.co
[2] = -obj
.location
[1]
171 # Convert rotation quat to our space type
172 quat
= obj
.matrix_world
.to_quaternion()
178 mk
.s
[0] = obj
.scale
[0]
179 mk
.s
[1] = obj
.scale
[2]
180 mk
.s
[2] = obj
.scale
[1]
181 mk
.name
= obj
.name
.encode('utf-8')
184 header
.marker_count
+= 1
186 elif obj
.type == 'MESH':
187 dgraph
= bpy
.context
.evaluated_depsgraph_get()
188 data
= obj
.evaluated_get(dgraph
).data
189 data
.calc_loop_triangles()
190 data
.calc_normals_split()
192 for material_id
, mat
in enumerate(data
.materials
):
194 sm
.indice_start
= header
.indice_count
195 sm
.vertex_start
= header
.vertex_count
198 sm
.pivot
[0] = obj
.matrix_world
.translation
[0]
199 sm
.pivot
[1] = obj
.matrix_world
.translation
[2]
200 sm
.pivot
[2] = -obj
.matrix_world
.translation
[1]
202 quat
= obj
.matrix_world
.to_quaternion()
209 sm
.bbx
[0][i
] = 999999
210 sm
.bbx
[1][i
] = -999999
212 sm
.name
= obj
.name
.encode('utf-8')
213 sm
.material
= mat
.name
.encode('utf-8')
214 print( F
" Creating submesh '{obj.name}:{mat.name}'" )
220 # Write the vertex / indice data
222 for tri_index
, tri
in enumerate(data
.loop_triangles
):
223 if tri
.material_index
!= material_id
:
227 vert
= data
.vertices
[tri
.vertices
[j
]]
230 norm
= data
.loops
[tri
.loops
[j
]].normal
233 uv
= data
.uv_layers
.active
.data
[tri
.loops
[j
]].uv
235 key
= (round(co
[0],4),round(co
[1],4),round(co
[2],4),\
236 round(norm
[0],4),round(norm
[1],4),round(norm
[2],4),\
237 round(uv
[0],4),round(uv
[1],4))
240 indice_buffer
+= [boffa
[key
]]
244 index
= c_uint32(sm
.vertex_count
)
249 indice_buffer
+= [index
]
267 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
268 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
273 header
.layer_count
+= 1
274 header
.vertex_count
+= sm
.vertex_count
275 header
.indice_count
+= sm
.indice_count
277 fp
.write( bytearray( header
) )
279 fp
.write( bytearray(l
) )
281 fp
.write( bytearray(m
) )
282 for v
in vertex_buffer
:
283 fp
.write( bytearray(v
) )
284 for i
in indice_buffer
:
285 fp
.write( bytearray(i
) )
289 for col
in bpy
.data
.collections
["export"].children
:
290 write_model( col
.name
)