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