rework scene format
[carveJwlIkooP6JGAAIwe30JlM.git] / blender_export.py
1 import bpy, math, gpu
2 from ctypes import *
3 from mathutils import *
4 from gpu_extras.batch import batch_for_shader
5
6 bl_info = {
7 "name":"Carve exporter",
8 "author": "Harry Godden (hgn)",
9 "version": (0,1),
10 "blender":(3,1,0),
11 "location":"Export",
12 "descriptin":"",
13 "warning":"",
14 "wiki_url":"",
15 "category":"Import/Export",
16 }
17
18 class mdl_vert(Structure):
19 _pack_ = 1
20 _fields_ = [("co",c_float*3),
21 ("norm",c_float*3),
22 ("colour",c_float*4),
23 ("uv",c_float*2)]
24
25 class mdl_submesh(Structure):
26 _pack_ = 1
27 _fields_ = [("indice_start",c_uint32),
28 ("indice_count",c_uint32),
29 ("vertex_start",c_uint32),
30 ("vertex_count",c_uint32),
31 ("bbx",(c_float*3)*2),
32 ("material_id",c_uint32)] # index into the material array
33
34 class mdl_material(Structure):
35 _pack_ = 1
36 _fields_ = [("pstr_name",c_uint32)]
37
38 class mdl_node(Structure):
39 _pack_ = 1
40 _fields_ = [("co",c_float*3),
41 ( "q",c_float*4),
42 ( "s",c_float*3),
43 ("submesh_start",c_uint32),
44 ("submesh_count",c_uint32),
45 ("classtype",c_uint32),
46 ("offset",c_uint32),
47 ("pstr_name",c_uint32)]
48
49 class mdl_header(Structure):
50 _pack_ = 1
51 _fields_ = [("identifier",c_uint32),
52 ("version",c_uint32),
53 ("file_length",c_uint32),
54 ("vertex_count",c_uint32),
55 ("vertex_offset",c_uint32),
56
57 ("indice_count",c_uint32),
58 ("indice_offset",c_uint32),
59
60 ("submesh_count",c_uint32),
61 ("submesh_offset",c_uint32),
62
63 ("material_count",c_uint32),
64 ("material_offset",c_uint32),
65
66 ("node_count",c_uint32),
67 ("node_offset",c_uint32),
68
69 ("strings_offset",c_uint32),
70 ("entdata_offset",c_uint32)
71 ]
72
73 # Entity types
74 # ==========================================
75
76 class classtype_gate(Structure):
77 _pack_ = 1
78 _fields_ = [("target",c_uint32)]
79
80 class classtype_block(Structure):
81 _pack_ = 1
82 _fields_ = [("bbx",(c_float*3)*2)]
83
84 class classtype_spawn(Structure):
85 _pack_ = 1
86 _fields_ = [("temp",c_uint32)]
87
88 class classtype_water(Structure):
89 _pack_ = 1
90 _fields_ = [("temp",c_uint32)]
91
92 # Exporter
93 # ==============================================================================
94
95 def write_model(name):
96 print( F"Create mode {name}" )
97
98 collection = bpy.data.collections[name]
99
100 header = mdl_header()
101 header.identifier = 0xABCD0000
102 header.version = 0
103 header.vertex_count = 0
104 header.indice_count = 0
105 header.submesh_count = 0
106 header.node_count = 0
107 header.material_count = 0
108 header.file_length = 0
109
110 mesh_cache = {}
111 string_cache = {}
112 material_cache = {}
113
114 strings_buffer = b''
115
116 material_buffer = []
117 submesh_buffer = []
118 vertex_buffer = []
119 indice_buffer = []
120 node_buffer = []
121 entdata_buffer = []
122 entdata_length = 0
123
124 def emplace_string( s ):
125 nonlocal string_cache, strings_buffer
126
127 if s in string_cache:
128 return string_cache[s]
129
130 string_cache[s] = len( strings_buffer )
131 strings_buffer += (s+'\0').encode('utf-8')
132 return string_cache[s]
133
134 def emplace_material( mat ):
135 nonlocal material_cache, material_buffer
136
137 if mat.name in material_cache:
138 return material_cache[mat.name]
139
140 material_cache[mat.name] = header.material_count
141 dest = mdl_material()
142 dest.pstr_name = emplace_string( mat.name )
143 material_buffer += [dest]
144
145 header.material_count += 1
146 return material_cache[mat.name]
147
148 # Create root or empty node and materials
149 #
150 none_material = c_uint32(69)
151 none_material.name = ""
152 emplace_material( none_material )
153
154 root = mdl_node()
155 root.co[0] = 0
156 root.co[1] = 0
157 root.co[2] = 0
158 root.q[0] = 0
159 root.q[1] = 0
160 root.q[2] = 0
161 root.q[3] = 1
162 root.s[0] = 1
163 root.s[1] = 1
164 root.s[2] = 1
165 root.pstr_name = emplace_string('')
166 root.submesh_start = 0
167 root.submesh_count = 0
168 root.offset = 0
169 root.classtype = 0
170 node_buffer += [root]
171
172 # Do exporting
173 #
174 print( " assigning ids" )
175 header.node_count = 1
176 for obj in collection.all_objects:
177 obj.cv_data.uid = header.node_count
178 header.node_count += 1
179
180 print( " compiling data" )
181 for obj in collection.all_objects:
182 print( F" [{obj.cv_data.uid}/{header.node_count-1}] {obj.name}" )
183
184 node = mdl_node()
185 node.co[0] = obj.location[0]
186 node.co[1] = obj.location[2]
187 node.co[2] = -obj.location[1]
188
189 # Convert rotation quat to our space type
190 quat = obj.matrix_world.to_quaternion()
191 node.q[0] = quat[1]
192 node.q[1] = quat[3]
193 node.q[2] = -quat[2]
194 node.q[3] = quat[0]
195
196 node.s[0] = obj.scale[0]
197 node.s[1] = obj.scale[2]
198 node.s[2] = obj.scale[1]
199 node.pstr_name = emplace_string( obj.name )
200
201 # Process entity data
202 #
203 node.offset = entdata_length
204 classtype = obj.cv_data.classtype
205
206 if classtype == 'k_classtype_none':
207 node.classtype = 0
208 node.offset = 0
209
210 elif classtype == 'k_classtype_gate':
211 node.classtype = 1
212 entdata_length += sizeof( classtype_gate )
213
214 gate = classtype_gate()
215 gate.target = 0
216 if obj.cv_data.target != None:
217 gate.target = obj.cv_data.target.cv_data.uid
218
219 entdata_buffer += [gate]
220
221 elif classtype == 'k_classtype_block':
222 node.classtype = 2
223 entdata_length += sizeof( classtype_block )
224
225 source = obj.data.cv_data
226
227 block = classtype_block()
228 block.bbx[0][0] = source.v0[0]
229 block.bbx[0][1] = source.v0[2]
230 block.bbx[0][2] = -source.v0[1]
231 block.bbx[1][0] = source.v1[0]
232 block.bbx[1][1] = source.v1[2]
233 block.bbx[1][2] = -source.v1[1]
234 entdata_buffer += [block]
235
236 elif classtype == 'k_classtype_spawn':
237 node.classtype = 3
238
239 elif classtype == 'k_classtype_water':
240 node.classtype = 4
241
242 # Process meshes
243 #
244 node.submesh_start = header.submesh_count
245 node.submesh_count = 0
246
247 if obj.type == 'MESH':
248 default_mat = c_uint32(69)
249 default_mat.name = ""
250
251 if obj.data.name in mesh_cache:
252 ref = mesh_cache[obj.data.name]
253 node.submesh_start = ref.submesh_start
254 node.submesh_count = ref.submesh_count
255 node_buffer += [node]
256 continue
257
258 dgraph = bpy.context.evaluated_depsgraph_get()
259 data = obj.evaluated_get(dgraph).data
260 data.calc_loop_triangles()
261 data.calc_normals_split()
262
263 mat_list = data.materials if len(data.materials) > 0 else [default_mat]
264 for material_id, mat in enumerate(mat_list):
265 mref = {}
266
267 sm = mdl_submesh()
268 sm.indice_start = header.indice_count
269 sm.vertex_start = header.vertex_count
270 sm.vertex_count = 0
271 sm.indice_count = 0
272 sm.material_id = emplace_material( mat )
273
274 for i in range(3):
275 sm.bbx[0][i] = 999999
276 sm.bbx[1][i] = -999999
277
278 boffa = {}
279
280 # Write the vertex / indice data
281 #
282 for tri_index, tri in enumerate(data.loop_triangles):
283 if tri.material_index != material_id:
284 continue
285
286 for j in range(3):
287 vert = data.vertices[tri.vertices[j]]
288 li = tri.loops[j]
289
290 co = vert.co
291 norm = data.loops[li].normal
292 uv = (0,0)
293 colour = (1,1,1,1)
294 if data.uv_layers:
295 uv = data.uv_layers.active.data[li].uv
296 if data.vertex_colors:
297 colour = data.vertex_colors.active.data[li].color
298
299 key = (round(co[0],4),round(co[1],4),round(co[2],4),\
300 round(norm[0],4),round(norm[1],4),round(norm[2],4),\
301 round(uv[0],4),round(uv[1],4),\
302 round(colour[0],4),round(colour[1],4),\
303 round(colour[2],4),round(colour[3],4))
304
305 if key in boffa:
306 indice_buffer += [boffa[key]]
307 else:
308 index = c_uint32(sm.vertex_count)
309 sm.vertex_count += 1
310
311 boffa[key] = index
312 indice_buffer += [index]
313
314 v = mdl_vert()
315 v.co[0] = co[0]
316 v.co[1] = co[2]
317 v.co[2] = -co[1]
318 v.norm[0] = norm[0]
319 v.norm[1] = norm[2]
320 v.norm[2] = -norm[1]
321 v.uv[0] = uv[0]
322 v.uv[1] = uv[1]
323 v.colour[0] = colour[0]
324 v.colour[1] = colour[1]
325 v.colour[2] = colour[2]
326 v.colour[3] = colour[3]
327 vertex_buffer += [v]
328
329 for i in range(3):
330 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
331 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
332
333 sm.indice_count += 1
334
335 if sm.vertex_count == 0:
336 for j in range(2):
337 for i in range(3):
338 sm.bbx[j][i] = 0
339
340 submesh_buffer += [sm]
341 node.submesh_count += 1
342 header.submesh_count += 1
343 header.vertex_count += sm.vertex_count
344 header.indice_count += sm.indice_count
345
346 mesh_cache[obj.data.name] = node
347 node_buffer += [node]
348
349 # Write data arrays
350 #
351 print( "Writing data" )
352 fpos = sizeof(header)
353
354 header.node_offset = fpos
355 fpos += sizeof(mdl_node)*header.node_count
356
357 header.submesh_offset = fpos
358 fpos += sizeof(mdl_submesh)*header.submesh_count
359
360 header.material_offset = fpos
361 fpos += sizeof(mdl_material)*header.material_count
362
363 header.entdata_offset = fpos
364 fpos += entdata_length
365
366 header.vertex_offset = fpos
367 fpos += sizeof(mdl_vert)*header.vertex_count
368
369 header.indice_offset = fpos
370 fpos += sizeof(c_uint32)*header.indice_count
371
372 header.strings_offset = fpos
373 fpos += len(strings_buffer)
374
375 header.file_length = fpos
376
377 fp = open(F"/home/harry/Documents/carve/models/{name}.mdl", "wb")
378 fp.write( bytearray( header ) )
379
380 for node in node_buffer:
381 fp.write( bytearray(node) )
382 for sm in submesh_buffer:
383 fp.write( bytearray(sm) )
384 for mat in material_buffer:
385 fp.write( bytearray(mat) )
386 for ed in entdata_buffer:
387 fp.write( bytearray(ed) )
388 for v in vertex_buffer:
389 fp.write( bytearray(v) )
390 for i in indice_buffer:
391 fp.write( bytearray(i) )
392 fp.write( strings_buffer )
393 fp.close()
394
395 print( F"Completed {name}.mdl" )
396
397 # Clicky clicky GUI
398 # ------------------------------------------------------------------------------
399
400 cv_view_draw_handler = None
401 cv_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
402
403 def cv_draw():
404 global cv_view_shader
405 cv_view_shader.bind()
406 gpu.state.depth_mask_set(False)
407 gpu.state.line_width_set(2.0)
408 gpu.state.face_culling_set('BACK')
409 gpu.state.depth_test_set('NONE')
410 gpu.state.blend_set('ADDITIVE')
411
412 verts = []
413 colours = []
414
415 for obj in bpy.context.collection.objects:
416 if obj.cv_data.classtype == 'k_classtype_gate':
417 if obj.cv_data.target != None:
418 p0 = obj.location
419 p1 = obj.cv_data.target.location
420 verts += [(p0[0],p0[1],p0[2])]
421 verts += [(p1[0],p1[1],p1[2])]
422 colours += [(0,1,0,1.0),(1,0,0,1.0)]
423 elif obj.cv_data.classtype == 'k_classtype_block':
424 a = obj.data.cv_data.v0
425 b = obj.data.cv_data.v1
426
427 vs = [None]*8
428 vs[0] = obj.matrix_world @ Vector((a[0], a[1], a[2]))
429 vs[1] = obj.matrix_world @ Vector((a[0], b[1], a[2]))
430 vs[2] = obj.matrix_world @ Vector((b[0], b[1], a[2]))
431 vs[3] = obj.matrix_world @ Vector((b[0], a[1], a[2]))
432 vs[4] = obj.matrix_world @ Vector((a[0], a[1], b[2]))
433 vs[5] = obj.matrix_world @ Vector((a[0], b[1], b[2]))
434 vs[6] = obj.matrix_world @ Vector((b[0], b[1], b[2]))
435 vs[7] = obj.matrix_world @ Vector((b[0], a[1], b[2]))
436
437 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
438 (0,4),(1,5),(2,6),(3,7)]
439
440 for l in indices:
441 v0 = vs[l[0]]
442 v1 = vs[l[1]]
443 verts += [(v0[0],v0[1],v0[2])]
444 verts += [(v1[0],v1[1],v1[2])]
445 colours += [(1,1,0,1),(1,1,0,1)]
446
447 lines = batch_for_shader(\
448 cv_view_shader, 'LINES', \
449 { "pos":verts, "color":colours })
450
451 lines.draw( cv_view_shader )
452
453 def cv_poll_target(scene, obj):
454 if obj == bpy.context.active_object:
455 return False
456 if obj.cv_data.classtype == 'k_classtype_none':
457 return False
458 return True
459
460 class CV_MESH_SETTINGS(bpy.types.PropertyGroup):
461 v0: bpy.props.FloatVectorProperty(name="v0",size=3)
462 v1: bpy.props.FloatVectorProperty(name="v1",size=3)
463 v2: bpy.props.FloatVectorProperty(name="v2",size=3)
464 v3: bpy.props.FloatVectorProperty(name="v3",size=3)
465
466 class CV_OBJ_SETTINGS(bpy.types.PropertyGroup):
467 uid: bpy.props.IntProperty( name="" )
468
469 target: bpy.props.PointerProperty( type=bpy.types.Object, name="target", \
470 poll=cv_poll_target )
471
472 classtype: bpy.props.EnumProperty(
473 name="Format",
474 items = [
475 ('k_classtype_none', "k_classtype_none", "", 0),
476 ('k_classtype_gate', "k_classtype_gate", "", 1),
477 ('k_classtype_block', "k_classtype_block", "", 2),
478 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
479 ('k_classtype_water', "k_classtype_water", "", 4)
480 ])
481
482 class CV_OBJ_PANEL(bpy.types.Panel):
483 bl_label="Entity Config"
484 bl_idname="SCENE_PT_cv_entity"
485 bl_space_type='PROPERTIES'
486 bl_region_type='WINDOW'
487 bl_context="object"
488
489 def draw(_,context):
490 active_object = bpy.context.active_object
491 if active_object == None: return
492 _.layout.prop( active_object.cv_data, "classtype" )
493
494 if active_object.cv_data.classtype == 'k_classtype_gate':
495 _.layout.prop( active_object.cv_data, "target" )
496 elif active_object.cv_data.classtype == 'k_classtype_block':
497 mesh = active_object.data
498
499 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
500 _.layout.prop( mesh.cv_data, "v0" )
501 _.layout.prop( mesh.cv_data, "v1" )
502 _.layout.prop( mesh.cv_data, "v2" )
503 _.layout.prop( mesh.cv_data, "v3" )
504
505 class CV_INTERFACE(bpy.types.Panel):
506 bl_idname = "VIEW3D_PT_carve"
507 bl_label = "Carve"
508 bl_space_type = 'VIEW_3D'
509 bl_region_type = 'UI'
510 bl_category = "Carve"
511
512 def draw(_, context):
513 layout = _.layout
514 layout.operator( "carve.compile_all" )
515
516 class CV_COMPILE(bpy.types.Operator):
517 bl_idname="carve.compile_all"
518 bl_label="Compile All"
519
520 def execute(_,context):
521 for col in bpy.data.collections["export"].children:
522 write_model( col.name )
523
524 return {'FINISHED'}
525
526 classes = [CV_OBJ_SETTINGS,CV_OBJ_PANEL,CV_COMPILE,CV_INTERFACE,\
527 CV_MESH_SETTINGS]
528
529 def register():
530 global cv_view_draw_handler
531
532 for c in classes:
533 bpy.utils.register_class(c)
534
535 bpy.types.Object.cv_data = bpy.props.PointerProperty(type=CV_OBJ_SETTINGS)
536 bpy.types.Mesh.cv_data = bpy.props.PointerProperty(type=CV_MESH_SETTINGS)
537
538 cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
539 cv_draw,(),'WINDOW','POST_VIEW')
540
541 def unregister():
542 global cv_view_draw_handler
543
544 for c in classes:
545 bpy.utils.unregister_class(c)
546
547 bpy.types.SpaceView3D.draw_handler_remove(cv_view_draw_handler,'WINDOW')