routes
[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 ("dims",c_float*3)]
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 class classtype_instance(Structure):
100 _pack_ = 1
101 _fields_ = [("pstr_file",c_uint32)]
102
103 class classtype_capsule(Structure):
104 _pack_ = 1
105 _fields_ = [("height",c_float),
106 ("radius",c_float)]
107
108 class classtype_route_node(Structure):
109 _pack_ = 1
110 _fields_ = [("target",c_uint32),
111 ("target1",c_uint32)]
112
113 class classtype_route(Structure):
114 _pack_ = 1
115 _fields_ = [("pstr_name",c_uint32),
116 ("id_start",c_uint32)]
117
118 # Exporter
119 # ==============================================================================
120
121 def write_model(name):
122 print( F"Create mode {name}" )
123
124 header = mdl_header()
125 header.identifier = 0xABCD0000
126 header.version = 0
127 header.vertex_count = 0
128 header.indice_count = 0
129 header.submesh_count = 0
130 header.node_count = 0
131 header.material_count = 0
132 header.file_length = 0
133
134 mesh_cache = {}
135 string_cache = {}
136 material_cache = {}
137
138 strings_buffer = b''
139
140 material_buffer = []
141 submesh_buffer = []
142 vertex_buffer = []
143 indice_buffer = []
144 node_buffer = []
145 entdata_buffer = []
146 entdata_length = 0
147
148 def emplace_string( s ):
149 nonlocal string_cache, strings_buffer
150
151 if s in string_cache:
152 return string_cache[s]
153
154 string_cache[s] = len( strings_buffer )
155 strings_buffer += (s+'\0').encode('utf-8')
156 return string_cache[s]
157
158 def emplace_material( mat ):
159 nonlocal material_cache, material_buffer
160
161 if mat.name in material_cache:
162 return material_cache[mat.name]
163
164 material_cache[mat.name] = header.material_count
165 dest = mdl_material()
166 dest.pstr_name = emplace_string( mat.name )
167 material_buffer += [dest]
168
169 header.material_count += 1
170 return material_cache[mat.name]
171
172 # Create root or empty node and materials
173 #
174 none_material = c_uint32(69)
175 none_material.name = ""
176 emplace_material( none_material )
177
178 root = mdl_node()
179 root.co[0] = 0
180 root.co[1] = 0
181 root.co[2] = 0
182 root.q[0] = 0
183 root.q[1] = 0
184 root.q[2] = 0
185 root.q[3] = 1
186 root.s[0] = 1
187 root.s[1] = 1
188 root.s[2] = 1
189 root.pstr_name = emplace_string('')
190 root.submesh_start = 0
191 root.submesh_count = 0
192 root.offset = 0
193 root.classtype = 0
194 node_buffer += [root]
195
196 # Do exporting
197 #
198 print( " assigning ids" )
199 collection = bpy.data.collections[name]
200
201 header.node_count = 1
202 for obj in collection.all_objects:
203 obj.cv_data.uid = header.node_count
204 header.node_count += 1
205
206 print( " compiling data" )
207 for obj in collection.all_objects:
208 print( F" [{obj.cv_data.uid}/{header.node_count-1}] {obj.name}" )
209
210 node = mdl_node()
211 node.co[0] = obj.location[0]
212 node.co[1] = obj.location[2]
213 node.co[2] = -obj.location[1]
214
215 # Convert rotation quat to our space type
216 quat = obj.matrix_world.to_quaternion()
217 node.q[0] = quat[1]
218 node.q[1] = quat[3]
219 node.q[2] = -quat[2]
220 node.q[3] = quat[0]
221
222 node.s[0] = obj.scale[0]
223 node.s[1] = obj.scale[2]
224 node.s[2] = obj.scale[1]
225 node.pstr_name = emplace_string( obj.name )
226
227 # Process entity data
228 #
229 node.offset = entdata_length
230 classtype = obj.cv_data.classtype
231
232 if classtype == 'k_classtype_gate':
233 node.classtype = 1
234 entdata_length += sizeof( classtype_gate )
235
236 gate = classtype_gate()
237 gate.target = 0
238 if obj.cv_data.target != None:
239 gate.target = obj.cv_data.target.cv_data.uid
240
241 if obj.type == 'MESH':
242 gate.dims[0] = obj.data.cv_data.v0[0]
243 gate.dims[1] = obj.data.cv_data.v0[1]
244 gate.dims[2] = obj.data.cv_data.v0[2]
245 else:
246 gate.dims[0] = obj.cv_data.v0[0]
247 gate.dims[1] = obj.cv_data.v0[1]
248 gate.dims[2] = obj.cv_data.v0[2]
249
250 entdata_buffer += [gate]
251
252 elif classtype == 'k_classtype_block':
253 node.classtype = 2
254 entdata_length += sizeof( classtype_block )
255
256 source = obj.data.cv_data
257
258 block = classtype_block()
259 block.bbx[0][0] = source.v0[0]
260 block.bbx[0][1] = source.v0[2]
261 block.bbx[0][2] = -source.v1[1]
262
263 block.bbx[1][0] = source.v1[0]
264 block.bbx[1][1] = source.v1[2]
265 block.bbx[1][2] = -source.v0[1]
266 entdata_buffer += [block]
267
268 elif classtype == 'k_classtype_spawn':
269 node.classtype = 3
270
271 elif classtype == 'k_classtype_water':
272 node.classtype = 4
273 elif classtype == 'k_classtype_car_path':
274 node.classtype = 5
275 entdata_length += sizeof( classtype_car_path )
276
277 pn = classtype_car_path()
278 pn.target = 0
279 pn.target1 = 0
280
281 if obj.cv_data.target != None:
282 pn.target = obj.cv_data.target.cv_data.uid
283 if obj.cv_data.target1 != None:
284 pn.target1 = obj.cv_data.target1.cv_data.uid
285
286 entdata_buffer += [pn]
287 elif obj.is_instancer:
288 target = obj.instance_collection
289
290 node.classtype = 6
291 entdata_length += sizeof( classtype_instance )
292
293 inst = classtype_instance()
294 inst.pstr_file = emplace_string( F"models/{target.name}.mdl" )
295 entdata_buffer += [inst]
296 elif classtype == 'k_classtype_capsule':
297 node.classtype = 7
298 elif classtype == 'k_classtype_route_node':
299 node.classtype = 8
300 entdata_length += sizeof( classtype_route_node )
301
302 rn = classtype_route_node()
303 if obj.cv_data.target != None:
304 rn.target = obj.cv_data.target.cv_data.uid
305 if obj.cv_data.target1 != None:
306 rn.target1 = obj.cv_data.target1.cv_data.uid
307
308 entdata_buffer += [rn]
309 elif classtype == 'k_classtype_route':
310 node.classtype = 9
311 entdata_length += sizeof( classtype_route )
312 r = classtype_route()
313 r.pstr_name = emplace_string("not-implemented")
314 if obj.cv_data.target != None:
315 r.target = obj.cv_data.target.cv_data.uid
316
317 entdata_buffer += [r]
318
319 # classtype == 'k_classtype_none':
320 else:
321 node.classtype = 0
322 node.offset = 0
323
324 # Process meshes
325 #
326 node.submesh_start = header.submesh_count
327 node.submesh_count = 0
328
329 if obj.type == 'MESH':
330 default_mat = c_uint32(69)
331 default_mat.name = ""
332
333 # Dont use the cache if we have modifiers that affect the normals
334 #
335 use_cache = True
336 for mod in obj.modifiers:
337 if mod.type == 'DATA_TRANSFER':
338 use_cache = False
339
340 if use_cache and obj.data.name in mesh_cache:
341 ref = mesh_cache[obj.data.name]
342 node.submesh_start = ref.submesh_start
343 node.submesh_count = ref.submesh_count
344 node_buffer += [node]
345 continue
346
347 dgraph = bpy.context.evaluated_depsgraph_get()
348 data = obj.evaluated_get(dgraph).data
349 data.calc_loop_triangles()
350 data.calc_normals_split()
351
352 mat_list = data.materials if len(data.materials) > 0 else [default_mat]
353 for material_id, mat in enumerate(mat_list):
354 mref = {}
355
356 sm = mdl_submesh()
357 sm.indice_start = header.indice_count
358 sm.vertex_start = header.vertex_count
359 sm.vertex_count = 0
360 sm.indice_count = 0
361 sm.material_id = emplace_material( mat )
362
363 for i in range(3):
364 sm.bbx[0][i] = 999999
365 sm.bbx[1][i] = -999999
366
367 boffa = {}
368
369 # Write the vertex / indice data
370 #
371 for tri_index, tri in enumerate(data.loop_triangles):
372 if tri.material_index != material_id:
373 continue
374
375 for j in range(3):
376 vert = data.vertices[tri.vertices[j]]
377 li = tri.loops[j]
378
379 co = vert.co
380 norm = data.loops[li].normal
381 uv = (0,0)
382 colour = (1,1,1,1)
383 if data.uv_layers:
384 uv = data.uv_layers.active.data[li].uv
385 if data.vertex_colors:
386 colour = data.vertex_colors.active.data[li].color
387
388 TOLERENCE = 4
389 m = float(10**TOLERENCE)
390
391 key = (int(co[0]*m+0.5),\
392 int(co[1]*m+0.5),\
393 int(co[2]*m+0.5),\
394 int(norm[0]*m+0.5),\
395 int(norm[1]*m+0.5),\
396 int(norm[2]*m+0.5),\
397 int(uv[0]*m+0.5),\
398 int(uv[1]*m+0.5),\
399 int(colour[0]*m+0.5),\
400 int(colour[1]*m+0.5),\
401 int(colour[2]*m+0.5),\
402 int(colour[3]*m+0.5))
403
404 if key in boffa:
405 indice_buffer += [boffa[key]]
406 else:
407 index = c_uint32(sm.vertex_count)
408 sm.vertex_count += 1
409
410 boffa[key] = index
411 indice_buffer += [index]
412
413 v = mdl_vert()
414 v.co[0] = co[0]
415 v.co[1] = co[2]
416 v.co[2] = -co[1]
417 v.norm[0] = norm[0]
418 v.norm[1] = norm[2]
419 v.norm[2] = -norm[1]
420 v.uv[0] = uv[0]
421 v.uv[1] = uv[1]
422 v.colour[0] = colour[0]
423 v.colour[1] = colour[1]
424 v.colour[2] = colour[2]
425 v.colour[3] = colour[3]
426 vertex_buffer += [v]
427
428 for i in range(3):
429 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
430 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
431
432 sm.indice_count += 1
433
434 if sm.vertex_count == 0:
435 for j in range(2):
436 for i in range(3):
437 sm.bbx[j][i] = 0
438
439 submesh_buffer += [sm]
440 node.submesh_count += 1
441 header.submesh_count += 1
442 header.vertex_count += sm.vertex_count
443 header.indice_count += sm.indice_count
444
445 mesh_cache[obj.data.name] = node
446 node_buffer += [node]
447
448 # Write data arrays
449 #
450 print( "Writing data" )
451 fpos = sizeof(header)
452
453 header.node_offset = fpos
454 fpos += sizeof(mdl_node)*header.node_count
455
456 header.submesh_offset = fpos
457 fpos += sizeof(mdl_submesh)*header.submesh_count
458
459 header.material_offset = fpos
460 fpos += sizeof(mdl_material)*header.material_count
461
462 header.entdata_offset = fpos
463 fpos += entdata_length
464
465 header.vertex_offset = fpos
466 fpos += sizeof(mdl_vert)*header.vertex_count
467
468 header.indice_offset = fpos
469 fpos += sizeof(c_uint32)*header.indice_count
470
471 header.strings_offset = fpos
472 fpos += len(strings_buffer)
473
474 header.file_length = fpos
475
476 fp = open(F"/home/harry/Documents/carve/models/{name}.mdl", "wb")
477 fp.write( bytearray( header ) )
478
479 for node in node_buffer:
480 fp.write( bytearray(node) )
481 for sm in submesh_buffer:
482 fp.write( bytearray(sm) )
483 for mat in material_buffer:
484 fp.write( bytearray(mat) )
485 for ed in entdata_buffer:
486 fp.write( bytearray(ed) )
487 for v in vertex_buffer:
488 fp.write( bytearray(v) )
489 for i in indice_buffer:
490 fp.write( bytearray(i) )
491 fp.write( strings_buffer )
492 fp.close()
493
494 print( F"Completed {name}.mdl" )
495
496 # Clicky clicky GUI
497 # ------------------------------------------------------------------------------
498
499 cv_view_draw_handler = None
500 cv_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
501
502 def cv_draw():
503 global cv_view_shader
504 cv_view_shader.bind()
505 gpu.state.depth_mask_set(False)
506 gpu.state.line_width_set(2.0)
507 gpu.state.face_culling_set('BACK')
508 gpu.state.depth_test_set('LESS')
509 gpu.state.blend_set('NONE')
510
511 verts = []
512 colours = []
513
514 #def drawbezier(p0,h0,p1,h1,c0,c1):
515 # nonlocal verts, colours
516
517 # verts += [p0]
518 # verts += [h0]
519 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
520 # verts += [p1]
521 # verts += [h1]
522 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
523 #
524 # last = p0
525 # for i in range(10):
526 # t = (i+1)/10
527 # a0 = 1-t
528
529 # tt = t*t
530 # ttt = tt*t
531 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
532 # verts += [(last[0],last[1],last[2])]
533 # verts += [(p[0],p[1],p[2])]
534 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
535 # last = p
536
537 course_count = 0
538
539 def drawbhandle(obj, direction, colour):
540 nonlocal verts, colours
541 p0 = obj.location
542 h0 = obj.matrix_world @ Vector((0,direction,0))
543 verts += [p0]
544 verts += [h0]
545 colours += [colour,colour]
546
547 def drawbezier(p0,h0,p1,h1,c0,c1):
548 nonlocal verts, colours
549
550 last = p0
551 for i in range(10):
552 t = (i+1)/10
553 a0 = 1-t
554
555 tt = t*t
556 ttt = tt*t
557 p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
558 verts += [(last[0],last[1],last[2])]
559 verts += [(p[0],p[1],p[2])]
560 colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
561 last = p
562
563 def drawsbpath(o0,o1,c0,c1,s0,s1):
564 nonlocal course_count
565
566 offs = ((course_count % 2)*2-1) * course_count * 0.02
567
568 p0 = o0.matrix_world @ Vector((offs, 0,0))
569 h0 = o0.matrix_world @ Vector((offs, s0,0))
570 p1 = o1.matrix_world @ Vector((offs, 0,0))
571 h1 = o1.matrix_world @ Vector((offs,-s1,0))
572 drawbezier(p0,h0,p1,h1,c0,c1)
573
574 def drawbpath(o0,o1,c0,c1):
575 drawsbpath(o0,o1,c0,c1,1.0,1.0)
576
577 def drawbline(o0,o1,c0,c1):
578 nonlocal verts, colours
579 verts += [o0.location]
580 verts += [o1.location]
581 colours += [c0,c1]
582
583 for obj in bpy.context.collection.objects:
584 if obj.cv_data.classtype == 'k_classtype_gate' and False:
585 if obj.cv_data.target != None:
586 p0 = obj.location
587 p1 = obj.cv_data.target.location
588
589 for i in range(20):
590 t = i/20.0
591 t1 = (i+0.5)/20.0
592
593 pa = p0*t+p1*(1.0-t)
594 pb = p0*t1+p1*(1.0-t1)
595
596 verts += [(pa[0],pa[1],pa[2])]
597 verts += [(pb[0],pb[1],pb[2])]
598 colours += [(0,1,0,1.0),(1,0,0,1.0)]
599
600 if obj.type == 'MESH':
601 dims = obj.data.cv_data.v0
602 else:
603 dims = obj.cv_data.v0
604
605 vs = [None]*9
606 c = Vector((0,0,dims[2]))
607
608 vs[0] = obj.matrix_world @ Vector((-dims[0],0.0,-dims[1]+dims[2]))
609 vs[1] = obj.matrix_world @ Vector((-dims[0],0.0, dims[1]+dims[2]))
610 vs[2] = obj.matrix_world @ Vector(( dims[0],0.0, dims[1]+dims[2]))
611 vs[3] = obj.matrix_world @ Vector(( dims[0],0.0,-dims[1]+dims[2]))
612 vs[4] = obj.matrix_world @ (c+Vector((-1,0,-2)))
613 vs[5] = obj.matrix_world @ (c+Vector((-1,0, 2)))
614 vs[6] = obj.matrix_world @ (c+Vector(( 1,0, 2)))
615 vs[7] = obj.matrix_world @ (c+Vector((-1,0, 0)))
616 vs[8] = obj.matrix_world @ (c+Vector(( 1,0, 0)))
617
618 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
619
620 for l in indices:
621 v0 = vs[l[0]]
622 v1 = vs[l[1]]
623 verts += [(v0[0],v0[1],v0[2])]
624 verts += [(v1[0],v1[1],v1[2])]
625 colours += [(1,1,0,1),(1,1,0,1)]
626
627 elif obj.cv_data.classtype == 'k_classtype_block':
628 a = obj.data.cv_data.v0
629 b = obj.data.cv_data.v1
630
631 vs = [None]*8
632 vs[0] = obj.matrix_world @ Vector((a[0], a[1], a[2]))
633 vs[1] = obj.matrix_world @ Vector((a[0], b[1], a[2]))
634 vs[2] = obj.matrix_world @ Vector((b[0], b[1], a[2]))
635 vs[3] = obj.matrix_world @ Vector((b[0], a[1], a[2]))
636 vs[4] = obj.matrix_world @ Vector((a[0], a[1], b[2]))
637 vs[5] = obj.matrix_world @ Vector((a[0], b[1], b[2]))
638 vs[6] = obj.matrix_world @ Vector((b[0], b[1], b[2]))
639 vs[7] = obj.matrix_world @ Vector((b[0], a[1], b[2]))
640
641 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
642 (0,4),(1,5),(2,6),(3,7)]
643
644 for l in indices:
645 v0 = vs[l[0]]
646 v1 = vs[l[1]]
647 verts += [(v0[0],v0[1],v0[2])]
648 verts += [(v1[0],v1[1],v1[2])]
649 colours += [(1,1,0,1),(1,1,0,1)]
650
651 elif obj.cv_data.classtype == 'k_classtype_capsule':
652 h = obj.data.cv_data.v0[0]
653 r = obj.data.cv_data.v0[1]
654
655 vs = [None]*10
656 vs[0] = obj.matrix_world @ Vector((0.0,0.0, h*0.5 ))
657 vs[1] = obj.matrix_world @ Vector((0.0,0.0,-h*0.5 ))
658 vs[2] = obj.matrix_world @ Vector(( r,0.0, h*0.5-r))
659 vs[3] = obj.matrix_world @ Vector(( -r,0.0, h*0.5-r))
660 vs[4] = obj.matrix_world @ Vector(( r,0.0,-h*0.5+r))
661 vs[5] = obj.matrix_world @ Vector(( -r,0.0,-h*0.5+r))
662 vs[6] = obj.matrix_world @ Vector((0.0, r , h*0.5-r))
663 vs[7] = obj.matrix_world @ Vector((0.0,-r , h*0.5-r))
664 vs[8] = obj.matrix_world @ Vector((0.0, r ,-h*0.5+r))
665 vs[9] = obj.matrix_world @ Vector((0.0,-r ,-h*0.5+r))
666
667 indices = [(0,1),(2,3),(4,5),(6,7),(8,9)]
668
669 for l in indices:
670 v0 = vs[l[0]]
671 v1 = vs[l[1]]
672 verts += [(v0[0],v0[1],v0[2])]
673 verts += [(v1[0],v1[1],v1[2])]
674 colours += [(0.5,1,0,1),(0.5,1,0,1)]
675
676 elif obj.cv_data.classtype == 'k_classtype_spawn':
677 vs = [None]*4
678 vs[0] = obj.matrix_world @ Vector((0,0,0))
679 vs[1] = obj.matrix_world @ Vector((0,2,0))
680 vs[2] = obj.matrix_world @ Vector((0.5,1,0))
681 vs[3] = obj.matrix_world @ Vector((-0.5,1,0))
682 indices = [(0,1),(1,2),(1,3)]
683 for l in indices:
684 v0 = vs[l[0]]
685 v1 = vs[l[1]]
686 verts += [(v0[0],v0[1],v0[2])]
687 verts += [(v1[0],v1[1],v1[2])]
688 colours += [(0,1,1,1),(0,1,1,1)]
689
690 elif obj.cv_data.classtype == 'k_classtype_route':
691 vs = [None]*2
692 vs[0] = obj.location
693 vs[1] = obj.cv_data.target.location
694 indices = [(0,1)]
695 for l in indices:
696 v0 = vs[l[0]]
697 v1 = vs[l[1]]
698 verts += [(v0[0],v0[1],v0[2])]
699 verts += [(v1[0],v1[1],v1[2])]
700 colours += [(0,1,1,1),(0,1,1,1)]
701
702 stack = [None]*64
703 stack_i = [0]*64
704 stack[0] = obj.cv_data.target
705 si = 1
706 loop_complete = False
707
708 while si > 0:
709 node = stack[si-1]
710
711 targets = [None,None]
712 targets[0] = node.cv_data.target
713
714 if node.cv_data.classtype == 'k_classtype_route_node':
715 targets[1] = node.cv_data.target1
716
717 nextnode = targets[stack_i[si-1]]
718 stack_i[si-1] += 1
719
720 if nextnode != None: # branch
721 if nextnode == stack[0]: # Loop completed
722 loop_complete = True
723 break
724
725 valid=True
726 for sj in range(si):
727 if stack[sj] == nextnode: # invalidated path
728 valid=False
729 break
730
731 if valid:
732 stack_i[si] = 0
733 stack[si] = nextnode
734 si += 1
735 continue
736
737 if stack_i[si-1] == 2:
738 si -= 1
739
740 if si == 0: # Loop failed to complete
741 break
742
743 if loop_complete:
744 course_colours = [Vector((0,0.8,0.2,1.0)), \
745 Vector((0,0.3,0.9,1.0)), \
746 Vector((0.4,0.0,0.8,1.0)),\
747 Vector((0.5,0.8,0.0,1.0)),\
748 Vector((0.0,0.7,0.6,1.0)),\
749 Vector((0.2,0.9,0.5,1.0)) ]
750
751 cc = course_colours[ course_count % len(course_colours) ]
752
753 for sj in range(si):
754 sk = (sj+1)%si
755
756 if stack[sj].cv_data.classtype == 'k_classtype_gate' and \
757 stack[sk].cv_data.classtype == 'k_classtype_gate':
758 dist = (stack[sj].location-stack[sk].location).magnitude
759 drawsbpath( stack[sj], stack[sk], cc*0.4, cc, dist, dist )
760
761 else:
762 drawbpath( stack[sj], stack[sk], cc, cc )
763
764 course_count += 1
765
766 elif obj.cv_data.classtype == 'k_classtype_car_path':
767 v0 = obj.matrix_world.to_quaternion() @ Vector((0,1,0))
768 c0 = Vector((v0.x*0.5+0.5, v0.y*0.5+0.5, 0.0, 1.0))
769 drawbhandle( obj, 1.0, (0.9,0.9,0.9,1.0) )
770
771 if obj.cv_data.target != None:
772 v1 = obj.cv_data.target.matrix_world.to_quaternion()@Vector((0,1,0))
773 c1 = Vector((v1.x*0.5+0.5, v1.y*0.5+0.5, 0.0, 1.0))
774
775 drawbhandle( obj.cv_data.target, -1.0, (0.5,0.5,0.5,1.0) )
776 drawbpath( obj, obj.cv_data.target, c0, c1 )
777
778 if obj.cv_data.target1 != None:
779 v1 = obj.cv_data.target1.matrix_world.to_quaternion()@Vector((0,1,0))
780 c1 = Vector((v1.x*0.5+0.5, v1.y*0.5+0.5, 0.0, 1.0))
781
782 drawbhandle( obj.cv_data.target1, -1.0, (0.5,0.5,0.5,1.0) )
783 drawbpath( obj, obj.cv_data.target1, c0, c1 )
784
785 lines = batch_for_shader(\
786 cv_view_shader, 'LINES', \
787 { "pos":verts, "color":colours })
788
789 lines.draw( cv_view_shader )
790
791 def cv_poll_target(scene, obj):
792 if obj == bpy.context.active_object:
793 return False
794 if obj.cv_data.classtype == 'k_classtype_none':
795 return False
796 return True
797
798 class CV_MESH_SETTINGS(bpy.types.PropertyGroup):
799 v0: bpy.props.FloatVectorProperty(name="v0",size=3)
800 v1: bpy.props.FloatVectorProperty(name="v1",size=3)
801 v2: bpy.props.FloatVectorProperty(name="v2",size=3)
802 v3: bpy.props.FloatVectorProperty(name="v3",size=3)
803
804 class CV_OBJ_SETTINGS(bpy.types.PropertyGroup):
805 uid: bpy.props.IntProperty( name="" )
806
807 target: bpy.props.PointerProperty( type=bpy.types.Object, name="target", \
808 poll=cv_poll_target )
809 target1: bpy.props.PointerProperty( type=bpy.types.Object, name="target1", \
810 poll=cv_poll_target )
811
812 classtype: bpy.props.EnumProperty(
813 name="Format",
814 items = [
815 ('k_classtype_none', "k_classtype_none", "", 0),
816 ('k_classtype_gate', "k_classtype_gate", "", 1),
817 ('k_classtype_block', "k_classtype_block", "", 2),
818 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
819 ('k_classtype_water', "k_classtype_water", "", 4),
820 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
821 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
822 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
823 ('k_classtype_route', "k_classtype_route", "", 9 )
824 ])
825
826 class CV_OBJ_PANEL(bpy.types.Panel):
827 bl_label="Entity Config"
828 bl_idname="SCENE_PT_cv_entity"
829 bl_space_type='PROPERTIES'
830 bl_region_type='WINDOW'
831 bl_context="object"
832
833 def draw(_,context):
834 active_object = bpy.context.active_object
835 if active_object == None: return
836 _.layout.prop( active_object.cv_data, "classtype" )
837
838 if active_object.cv_data.classtype == 'k_classtype_gate':
839 _.layout.prop( active_object.cv_data, "target" )
840
841 mesh = active_object.data
842 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
843 _.layout.prop( mesh.cv_data, "v0" )
844
845 elif active_object.cv_data.classtype == 'k_classtype_car_path' or \
846 active_object.cv_data.classtype == 'k_classtype_route_node':
847 _.layout.prop( active_object.cv_data, "target" )
848 _.layout.prop( active_object.cv_data, "target1" )
849
850 elif active_object.cv_data.classtype == 'k_classtype_route':
851 _.layout.prop( active_object.cv_data, "target" )
852
853 elif active_object.cv_data.classtype == 'k_classtype_block':
854 mesh = active_object.data
855
856 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
857 _.layout.prop( mesh.cv_data, "v0" )
858 _.layout.prop( mesh.cv_data, "v1" )
859 _.layout.prop( mesh.cv_data, "v2" )
860 _.layout.prop( mesh.cv_data, "v3" )
861 elif active_object.cv_data.classtype == 'k_classtype_capsule':
862 mesh = active_object.data
863 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
864 _.layout.prop( mesh.cv_data, "v0" )
865
866 class CV_INTERFACE(bpy.types.Panel):
867 bl_idname = "VIEW3D_PT_carve"
868 bl_label = "Carve"
869 bl_space_type = 'VIEW_3D'
870 bl_region_type = 'UI'
871 bl_category = "Carve"
872
873 def draw(_, context):
874 layout = _.layout
875 layout.operator( "carve.compile_all" )
876
877 def test_compile():
878 for col in bpy.data.collections["export"].children:
879 write_model( col.name )
880
881 class CV_COMPILE(bpy.types.Operator):
882 bl_idname="carve.compile_all"
883 bl_label="Compile All"
884
885 def execute(_,context):
886 test_compile()
887 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
888 #for col in bpy.data.collections["export"].children:
889 # write_model( col.name )
890
891 return {'FINISHED'}
892
893 classes = [CV_OBJ_SETTINGS,CV_OBJ_PANEL,CV_COMPILE,CV_INTERFACE,\
894 CV_MESH_SETTINGS]
895
896 def register():
897 global cv_view_draw_handler
898
899 for c in classes:
900 bpy.utils.register_class(c)
901
902 bpy.types.Object.cv_data = bpy.props.PointerProperty(type=CV_OBJ_SETTINGS)
903 bpy.types.Mesh.cv_data = bpy.props.PointerProperty(type=CV_MESH_SETTINGS)
904
905 cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
906 cv_draw,(),'WINDOW','POST_VIEW')
907
908 def unregister():
909 global cv_view_draw_handler
910
911 for c in classes:
912 bpy.utils.unregister_class(c)
913
914 bpy.types.SpaceView3D.draw_handler_remove(cv_view_draw_handler,'WINDOW')