05d1ec06aa5482a1ff35a40ac5cd5771dfca55a0
[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 ("uv",c_float*2),
24 ("colour",c_uint8*4),
25 ("weights",c_uint16*4),
26 ("groups",c_uint8*4)]
27
28 class mdl_submesh(Structure):
29 _pack_ = 1
30 _fields_ = [("indice_start",c_uint32),
31 ("indice_count",c_uint32),
32 ("vertex_start",c_uint32),
33 ("vertex_count",c_uint32),
34 ("bbx",(c_float*3)*2),
35 ("material_id",c_uint32)] # index into the material array
36
37 class mdl_material(Structure):
38 _pack_ = 1
39 _fields_ = [("pstr_name",c_uint32)]
40
41 class mdl_node(Structure):
42 _pack_ = 1
43 _fields_ = [("co",c_float*3),
44 ( "q",c_float*4),
45 ( "s",c_float*3),
46 ("submesh_start",c_uint32),
47 ("submesh_count",c_uint32),
48 ("classtype",c_uint32),
49 ("offset",c_uint32),
50 ("parent",c_uint32),
51 ("pstr_name",c_uint32)]
52
53 class mdl_header(Structure):
54 _pack_ = 1
55 _fields_ = [("identifier",c_uint32),
56 ("version",c_uint32),
57 ("file_length",c_uint32),
58 ("vertex_count",c_uint32),
59 ("vertex_offset",c_uint32),
60
61 ("indice_count",c_uint32),
62 ("indice_offset",c_uint32),
63
64 ("submesh_count",c_uint32),
65 ("submesh_offset",c_uint32),
66
67 ("material_count",c_uint32),
68 ("material_offset",c_uint32),
69
70 ("node_count",c_uint32),
71 ("node_offset",c_uint32),
72
73 ("anim_count",c_uint32),
74 ("anim_offset",c_uint32),
75
76 ("strings_offset",c_uint32),
77 ("entdata_offset",c_uint32),
78 ("animdata_offset",c_uint32)
79 ]
80
81 class mdl_animation(Structure):
82 _pack_ = 1
83 _fields_ = [("pstr_name",c_uint32),
84 ("length",c_uint32),
85 ("rate",c_float),
86 ("offset",c_uint32)]
87
88 class mdl_keyframe(Structure):
89 _pack_ = 1
90 _fields_ = [("co",c_float*3),
91 ("q",c_float*4),
92 ("s",c_float*3)]
93
94 # Entity types
95 # ==========================================
96
97 class classtype_gate(Structure):
98 _pack_ = 1
99 _fields_ = [("target",c_uint32),
100 ("dims",c_float*3)]
101
102 class classtype_block(Structure):
103 _pack_ = 1
104 _fields_ = [("bbx",(c_float*3)*2)]
105
106 class classtype_spawn(Structure):
107 _pack_ = 1
108 _fields_ = [("temp",c_uint32)]
109
110 class classtype_water(Structure):
111 _pack_ = 1
112 _fields_ = [("temp",c_uint32)]
113
114 class classtype_car_path(Structure):
115 _pack_ = 1
116 _fields_ = [("target",c_uint32),
117 ("target1",c_uint32)]
118
119 class classtype_instance(Structure):
120 _pack_ = 1
121 _fields_ = [("pstr_file",c_uint32)]
122
123 class classtype_capsule(Structure):
124 _pack_ = 1
125 _fields_ = [("height",c_float),
126 ("radius",c_float)]
127
128 class classtype_route_node(Structure):
129 _pack_ = 1
130 _fields_ = [("target",c_uint32),
131 ("target1",c_uint32)]
132
133 class classtype_route(Structure):
134 _pack_ = 1
135 _fields_ = [("pstr_name",c_uint32),
136 ("id_start",c_uint32),
137 ("colour",c_float*3)]
138
139 class classtype_skin(Structure):
140 _pack_ = 1
141 _fields_ = [("skeleton",c_uint32)]
142
143 class classtype_skeleton(Structure):
144 _pack_ = 1
145 _fields_ = [("channels",c_uint32),
146 ("ik_count",c_uint32),
147 ("collider_count",c_uint32),
148 ("anim_start",c_uint32),
149 ("anim_count",c_uint32)]
150
151 class classtype_bone(Structure):
152 _pack_ = 1
153 _fields_ = [("deform",c_uint32),
154 ("ik_target",c_uint32),
155 ("ik_pole",c_uint32),
156 ("collider",c_uint32),
157 ("use_limits",c_uint32),
158 ("angle_limits",(c_float*3)*2),
159 ("hitbox",(c_float*3)*2)]
160
161 class subclass_audio_channel(Structure):
162 _pack_ = 1
163 _fields_ = [("sound",c_uint32),
164 ("target",c_uint32),
165 ("")]
166
167 class classtype_audio_system(Structure):
168 _pack_ = 1
169 _fields_ = [("")]
170
171 class classtype_audio_zone(Structure):
172 _pack_ = 1
173 _fields_ = [("system",c_uint32)]
174
175 # Exporter
176 # ==============================================================================
177
178 def write_model(collection_name):
179 print( F"Model graph | Create mode '{collection_name}'" )
180
181 header = mdl_header()
182 header.identifier = 0xABCD0000
183 header.version = 0
184 header.vertex_count = 0
185 header.indice_count = 0
186 header.submesh_count = 0
187 header.node_count = 0
188 header.material_count = 0
189 header.file_length = 0
190
191 mesh_cache = {}
192 string_cache = {}
193 material_cache = {}
194
195 strings_buffer = b''
196
197 material_buffer = []
198 submesh_buffer = []
199 vertex_buffer = []
200 indice_buffer = []
201 node_buffer = []
202 entdata_buffer = []
203 entdata_length = 0
204
205 anim_buffer = []
206 animdata_length = 0
207 animdata_buffer = []
208
209 def emplace_string( s ):
210 nonlocal string_cache, strings_buffer
211
212 if s in string_cache:
213 return string_cache[s]
214
215 string_cache[s] = len( strings_buffer )
216 strings_buffer += (s+'\0').encode('utf-8')
217 return string_cache[s]
218
219 def emplace_material( mat ):
220 nonlocal material_cache, material_buffer
221
222 if mat == None:
223 return 0
224
225 if mat.name in material_cache:
226 return material_cache[mat.name]
227
228 material_cache[mat.name] = header.material_count
229 dest = mdl_material()
230 dest.pstr_name = emplace_string( mat.name )
231 material_buffer += [dest]
232
233 header.material_count += 1
234 return material_cache[mat.name]
235
236 # Create root or empty node and materials
237 # this is to designate id 0 as 'NULL'
238 #
239 none_material = c_uint32(69)
240 none_material.name = ""
241 emplace_material( none_material )
242
243 root = mdl_node()
244 root.co[0] = 0
245 root.co[1] = 0
246 root.co[2] = 0
247 root.q[0] = 0
248 root.q[1] = 0
249 root.q[2] = 0
250 root.q[3] = 1
251 root.s[0] = 1
252 root.s[1] = 1
253 root.s[2] = 1
254 root.pstr_name = emplace_string('')
255 root.submesh_start = 0
256 root.submesh_count = 0
257 root.offset = 0
258 root.classtype = 0
259 node_buffer += [root]
260
261 # Do exporting
262 #
263 print( " assigning ids" )
264 collection = bpy.data.collections[collection_name]
265
266 # Scene graph
267 # ==========================================
268
269 header.node_count = 0
270 def _uid():
271 nonlocal header
272 uid = header.node_count
273 header.node_count += 1
274 return uid
275
276 print( " creating scene graph" )
277 graph = {}
278 graph["obj"] = None
279 graph["depth"] = 0
280 graph["children"] = []
281 graph["uid"] = _uid()
282 graph["parent"] = None
283
284 graph_lookup = {} # object can lookup its graph def here
285
286 for obj in collection.all_objects:
287 if not obj.parent:
288
289 def _extend( p, n, d ):
290 uid = _uid()
291 tree = {}
292 tree["uid"] = uid
293 tree["children"] = []
294 tree["depth"] = d
295 tree["obj"] = n
296 tree["parent"] = p
297 n.cv_data.uid = uid
298
299 if n.type == 'ARMATURE':
300 tree["bones"] = [None] # None is the root transform
301 tree["ik_count"] = 0
302 tree["collider_count"] = 0
303
304 def _extendb( p, n, d ):
305 nonlocal tree
306
307 btree = {}
308 btree["bone"] = n
309 btree["uid"] = _uid()
310 btree["children"] = []
311 btree["depth"] = d
312 btree["parent"] = p
313 tree["bones"] += [n.name]
314
315 for c in n.children:
316 _extendb( btree, c, d+1 )
317
318 for c in tree['obj'].pose.bones[n.name].constraints:
319 if c.type == 'IK':
320 btree["target"] = c.subtarget
321 btree["pole"] = c.pole_subtarget
322 tree["ik_count"] += 1
323
324 if n.cv_data.collider:
325 tree['collider_count'] += 1
326
327 btree['deform'] = n.use_deform
328 p['children'] += [btree]
329
330 for b in n.data.bones:
331 if not b.parent:
332 _extendb( tree, b, d+1 )
333
334 for obj1 in n.children:
335 _extend( tree, obj1, d+1 )
336
337 p["children"] += [tree]
338 graph_lookup[n] = tree
339
340 _extend( graph, obj, 1 )
341
342
343 def _graph_iter(p):
344 for c in p['children']:
345 yield c
346 yield from _graph_iter(c)
347
348 it = _graph_iter(graph)
349
350 root.parent = 0xffffffff
351
352 # Compile
353 # ==============================================
354 it = _graph_iter(graph)
355 print( " compiling data" )
356 for node_def in it:
357 if 'obj' in node_def:
358 obj = node_def['obj']
359 objt = obj.type
360 objco = obj.location
361 elif 'bone' in node_def:
362 obj = node_def['bone']
363 objt = 'BONE'
364 objco = obj.head_local
365
366 depth = node_def['depth']
367 uid = node_def['uid']
368
369 node = mdl_node()
370 node.co[0] = objco[0]
371 node.co[1] = objco[2]
372 node.co[2] = -objco[1]
373
374 # Convert rotation quat to our space type
375 quat = obj.matrix_local.to_quaternion()
376 node.q[0] = quat[1]
377 node.q[1] = quat[3]
378 node.q[2] = -quat[2]
379 node.q[3] = quat[0]
380
381 if objt == 'BONE':
382 node.s[0] = obj.tail_local[0] - node.co[0]
383 node.s[1] = obj.tail_local[2] - node.co[1]
384 node.s[2] = -obj.tail_local[1] - node.co[2]
385 else:
386 node.s[0] = obj.scale[0]
387 node.s[1] = obj.scale[2]
388 node.s[2] = obj.scale[1]
389
390 node.pstr_name = emplace_string( obj.name )
391
392 if node_def["parent"]:
393 node.parent = node_def["parent"]["uid"]
394
395 if objt == 'BONE':
396 classtype = 'k_classtype_bone'
397 elif objt == 'ARMATURE':
398 classtype = 'k_classtype_skeleton'
399 else:
400 classtype = obj.cv_data.classtype
401
402 # Process type: MESH
403 # =================================================================
404 #
405
406 # Dont use the cache if we have modifiers that affect the normals
407 #
408 compile_mesh = False
409 if objt == 'MESH':
410 armature_def = None
411 compile_mesh = True
412 can_use_cache = True
413
414 for mod in obj.modifiers:
415 if mod.type == 'DATA_TRANSFER' or mod.type == 'SHRINKWRAP' or \
416 mod.type == 'BOOLEAN' or mod.type == 'CURVE' or \
417 mod.type == 'ARRAY':
418 can_use_cache = False
419
420 if mod.type == 'ARMATURE':
421 classtype = 'k_classtype_skin'
422 armature_def = graph_lookup[mod.object]
423 POSE_OR_REST_CACHE = armature_def['obj'].data.pose_position
424
425 armature_def['obj'].data.pose_position = 'REST'
426
427 if can_use_cache and obj.data.name in mesh_cache:
428 ref = mesh_cache[obj.data.name]
429 node.submesh_start = ref.submesh_start
430 node.submesh_count = ref.submesh_count
431 compile_mesh = False
432
433 if compile_mesh:
434 node.submesh_start = header.submesh_count
435 node.submesh_count = 0
436
437 default_mat = c_uint32(69)
438 default_mat.name = ""
439
440 dgraph = bpy.context.evaluated_depsgraph_get()
441 data = obj.evaluated_get(dgraph).data
442 data.calc_loop_triangles()
443 data.calc_normals_split()
444
445 mat_list = data.materials if len(data.materials) > 0 else [default_mat]
446 for material_id, mat in enumerate(mat_list):
447 mref = {}
448
449 sm = mdl_submesh()
450 sm.indice_start = header.indice_count
451 sm.vertex_start = header.vertex_count
452 sm.vertex_count = 0
453 sm.indice_count = 0
454 sm.material_id = emplace_material( mat )
455
456 for i in range(3):
457 sm.bbx[0][i] = 999999
458 sm.bbx[1][i] = -999999
459
460 boffa = {}
461
462 # Write the vertex / indice data
463 #
464 for tri_index, tri in enumerate(data.loop_triangles):
465 if tri.material_index != material_id:
466 continue
467
468 for j in range(3):
469 vert = data.vertices[tri.vertices[j]]
470 li = tri.loops[j]
471 vi = data.loops[li].vertex_index
472
473 co = vert.co
474 norm = data.loops[li].normal
475 uv = (0,0)
476 colour = (255,255,255,255)
477 groups = [0,0,0,0]
478 weights = [0,0,0,0]
479
480 if data.uv_layers:
481 uv = data.uv_layers.active.data[li].uv
482
483 if data.vertex_colors:
484 colour = data.vertex_colors.active.data[li].color
485 colour = (int(colour[0]*255.0),\
486 int(colour[1]*255.0),\
487 int(colour[2]*255.0),\
488 int(colour[3]*255.0))
489
490 # WEight groups
491 #
492 if armature_def:
493 src_groups = [_ for _ in data.vertices[vi].groups \
494 if obj.vertex_groups[_.group].name in \
495 armature_def['bones']]
496
497 weight_groups = sorted( src_groups, key = \
498 lambda a: a.weight, reverse=True )
499 tot = 0.0
500 for ml in range(3):
501 if len(weight_groups) > ml:
502 g = weight_groups[ml]
503 name = obj.vertex_groups[g.group].name
504 weight = g.weight
505
506 weights[ml] = weight
507 groups[ml] = armature_def['bones'].index(name)
508 tot += weight
509
510 if len(weight_groups) > 0:
511 inv_norm = (1.0/tot) * 65535.0
512 for ml in range(3):
513 weights[ml] = int( weights[ml] * inv_norm )
514 weights[ml] = min( weights[ml], 65535 )
515 weights[ml] = max( weights[ml], 0 )
516
517 TOLERENCE = 4
518 m = float(10**TOLERENCE)
519
520 key = (int(co[0]*m+0.5),\
521 int(co[1]*m+0.5),\
522 int(co[2]*m+0.5),\
523 int(norm[0]*m+0.5),\
524 int(norm[1]*m+0.5),\
525 int(norm[2]*m+0.5),\
526 int(uv[0]*m+0.5),\
527 int(uv[1]*m+0.5),\
528 colour[0],\
529 colour[1],\
530 colour[2],\
531 colour[3],\
532 weights[0],\
533 weights[1],\
534 weights[2],\
535 weights[3],\
536 groups[0],\
537 groups[1],\
538 groups[2],\
539 groups[3])
540
541 if key in boffa:
542 indice_buffer += [boffa[key]]
543 else:
544 index = c_uint32(sm.vertex_count)
545 sm.vertex_count += 1
546
547 boffa[key] = index
548 indice_buffer += [index]
549
550 v = mdl_vert()
551 v.co[0] = co[0]
552 v.co[1] = co[2]
553 v.co[2] = -co[1]
554 v.norm[0] = norm[0]
555 v.norm[1] = norm[2]
556 v.norm[2] = -norm[1]
557 v.uv[0] = uv[0]
558 v.uv[1] = uv[1]
559 v.colour[0] = colour[0]
560 v.colour[1] = colour[1]
561 v.colour[2] = colour[2]
562 v.colour[3] = colour[3]
563 v.weights[0] = weights[0]
564 v.weights[1] = weights[1]
565 v.weights[2] = weights[2]
566 v.weights[3] = weights[3]
567 v.groups[0] = groups[0]
568 v.groups[1] = groups[1]
569 v.groups[2] = groups[2]
570 v.groups[3] = groups[3]
571
572 vertex_buffer += [v]
573
574 for i in range(3):
575 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
576 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
577
578 sm.indice_count += 1
579
580 if sm.vertex_count == 0:
581 for j in range(2):
582 for i in range(3):
583 sm.bbx[j][i] = 0
584
585 submesh_buffer += [sm]
586 node.submesh_count += 1
587 header.submesh_count += 1
588 header.vertex_count += sm.vertex_count
589 header.indice_count += sm.indice_count
590
591 mesh_cache[obj.data.name] = node
592
593 # Process entity data
594 # ==================================================================
595 node.offset = entdata_length
596
597 if classtype != 'k_classtype_none':
598 disptype = classtype
599 else:
600 disptype = objt
601
602 s000 = F" [{uid: 3}/{header.node_count-1}]" + " |"*(depth-1)
603 s001 = F" L {obj.name}"
604 s002 = s000+s001
605 s003 = F"{disptype}"
606 s004 = F"{node.parent: 3}"
607 s005 = ""
608
609 if classtype == 'k_classtype_skin':
610 armature_def['obj'].data.pose_position = POSE_OR_REST_CACHE
611 s005 = F" [armature -> {armature_def['obj'].cv_data.uid}]"
612
613 scmp = F"{s002:<32} {s003:<22} {s004} {s005}"
614 print( scmp )
615
616 if classtype == 'k_classtype_INSTANCE' or \
617 classtype == 'k_classtype_BONE' or \
618 classtype == 'k_classtype_SKELETON' or \
619 classtype == 'k_classtype_SKIN':
620 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
621 node.classtype = 0
622 node.offset = 0
623
624 elif classtype == 'k_classtype_skin':
625 node.classtype = 12
626
627 armature = armature_def['obj']
628 entdata_length += sizeof( classtype_skin )
629
630 skin = classtype_skin()
631 skin.skeleton = armature.cv_data.uid
632 entdata_buffer += [skin]
633
634 elif classtype == 'k_classtype_skeleton':
635 node.classtype = 11
636 entdata_length += sizeof( classtype_skeleton )
637 skeleton = classtype_skeleton()
638
639 armature_def = graph_lookup[obj]
640 armature = obj
641 bones = armature_def['bones']
642 skeleton.channels = len(bones)
643 skeleton.ik_count = armature_def["ik_count"]
644 skeleton.collider_count = armature_def["collider_count"]
645
646 if armature.animation_data:
647 previous_frame = bpy.context.scene.frame_current
648 previous_action = armature.animation_data.action
649
650 skeleton.anim_start = len(anim_buffer)
651 skeleton.anim_count = 0
652
653 for NLALayer in obj.animation_data.nla_tracks:
654 for NLAStrip in NLALayer.strips:
655 # Use action
656 for a in bpy.data.actions:
657 if a.name == NLAStrip.name:
658 armature.animation_data.action = a
659 break
660
661 anim_start = int(NLAStrip.action_frame_start)
662 anim_end = int(NLAStrip.action_frame_end)
663
664 # export strips
665 anim = mdl_animation()
666 anim.pstr_name = emplace_string( NLAStrip.action.name )
667 anim.rate = 30.0
668 anim.offset = animdata_length
669 anim.length = anim_end-anim_start
670
671 # Export the fucking keyframes
672 for frame in range(anim_start,anim_end):
673 bpy.context.scene.frame_set(frame)
674
675 for bone_name in bones:
676 for pb in armature.pose.bones:
677 if pb.name == bone_name:
678 rb = armature.data.bones[ bone_name ]
679
680 # relative bone matrix
681 if rb.parent is not None:
682 offset_mtx = rb.parent.matrix_local
683 offset_mtx = offset_mtx.inverted_safe() @ \
684 rb.matrix_local
685
686 inv_parent = pb.parent.matrix @ offset_mtx
687 inv_parent.invert_safe()
688 fpm = inv_parent @ pb.matrix
689 else:
690 bone_mtx = rb.matrix.to_4x4()
691 local_inv = rb.matrix_local.inverted_safe()
692 fpm = bone_mtx @ local_inv @ pb.matrix
693
694 loc, rot, sca = fpm.decompose()
695
696 # local position
697 final_pos = Vector(( loc[0], loc[2], -loc[1] ))
698
699 # rotation
700 lc_m = pb.matrix_channel.to_3x3()
701 if pb.parent is not None:
702 smtx = pb.parent.matrix_channel.to_3x3()
703 lc_m = smtx.inverted() @ lc_m
704 rq = lc_m.to_quaternion()
705
706 kf = mdl_keyframe()
707 kf.co[0] = final_pos[0]
708 kf.co[1] = final_pos[1]
709 kf.co[2] = final_pos[2]
710
711 kf.q[0] = rq[1]
712 kf.q[1] = rq[3]
713 kf.q[2] = -rq[2]
714 kf.q[3] = rq[0]
715
716 # scale
717 kf.s[0] = sca[0]
718 kf.s[1] = sca[2]
719 kf.s[2] = sca[1]
720
721 animdata_buffer += [kf]
722 animdata_length += sizeof(mdl_keyframe)
723 break
724
725 anim_buffer += [anim]
726 skeleton.anim_count += 1
727
728 s000 = F" [{uid: 3}/{header.node_count-1}]" + " |"*(depth-1)
729 print( F"{s000} | *anim: {NLAStrip.action.name}" )
730
731 bpy.context.scene.frame_set( previous_frame )
732 armature.animation_data.action = previous_action
733
734 entdata_buffer += [skeleton]
735
736 elif classtype == 'k_classtype_bone':
737 node.classtype = 10
738 entdata_length += sizeof( classtype_bone )
739
740 bone = classtype_bone()
741 bone.deform = node_def['deform']
742
743 if 'target' in node_def:
744 bone.ik_target = armature_def['bones'].index( node_def['target'] )
745 bone.ik_pole = armature_def['bones'].index( node_def['pole'] )
746 else:
747 bone.ik_target = 0
748 bone.ik_pole = 0
749
750 bone.collider = 1 if obj.cv_data.collider else 0
751 if obj.cv_data.collider:
752 bone.hitbox[0][0] = obj.cv_data.v0[0]
753 bone.hitbox[0][1] = obj.cv_data.v0[2]
754 bone.hitbox[0][2] = -obj.cv_data.v1[1]
755 bone.hitbox[1][0] = obj.cv_data.v1[0]
756 bone.hitbox[1][1] = obj.cv_data.v1[2]
757 bone.hitbox[1][2] = -obj.cv_data.v0[1]
758 else:
759 bone.hitbox[0][0] = 0.0
760 bone.hitbox[0][1] = 0.0
761 bone.hitbox[0][2] = 0.0
762 bone.hitbox[1][0] = 0.0
763 bone.hitbox[1][1] = 0.0
764 bone.hitbox[1][2] = 0.0
765
766 if obj.cv_data.con0:
767 bone.use_limits = 1
768 bone.angle_limits[0][0] = obj.cv_data.mins[0]
769 bone.angle_limits[0][1] = obj.cv_data.mins[2]
770 bone.angle_limits[0][2] = -obj.cv_data.maxs[1]
771 bone.angle_limits[1][0] = obj.cv_data.maxs[0]
772 bone.angle_limits[1][1] = obj.cv_data.maxs[2]
773 bone.angle_limits[1][2] = -obj.cv_data.mins[1]
774 else:
775 bone.use_limits = 0
776 bone.angle_limits[0][0] = 0.0
777 bone.angle_limits[0][1] = 0.0
778 bone.angle_limits[0][2] = 0.0
779 bone.angle_limits[1][0] = 0.0
780 bone.angle_limits[1][1] = 0.0
781 bone.angle_limits[1][2] = 0.0
782
783 bone.deform = node_def['deform']
784 entdata_buffer += [bone]
785
786 elif classtype == 'k_classtype_gate':
787 node.classtype = 1
788 entdata_length += sizeof( classtype_gate )
789
790 gate = classtype_gate()
791 gate.target = 0
792 if obj.cv_data.target != None:
793 gate.target = obj.cv_data.target.cv_data.uid
794
795 if obj.type == 'MESH':
796 gate.dims[0] = obj.data.cv_data.v0[0]
797 gate.dims[1] = obj.data.cv_data.v0[1]
798 gate.dims[2] = obj.data.cv_data.v0[2]
799 else:
800 gate.dims[0] = obj.cv_data.v0[0]
801 gate.dims[1] = obj.cv_data.v0[1]
802 gate.dims[2] = obj.cv_data.v0[2]
803
804 entdata_buffer += [gate]
805
806 elif classtype == 'k_classtype_block':
807 node.classtype = 2
808 entdata_length += sizeof( classtype_block )
809
810 source = obj.data.cv_data
811
812 block = classtype_block()
813 block.bbx[0][0] = source.v0[0]
814 block.bbx[0][1] = source.v0[2]
815 block.bbx[0][2] = -source.v1[1]
816
817 block.bbx[1][0] = source.v1[0]
818 block.bbx[1][1] = source.v1[2]
819 block.bbx[1][2] = -source.v0[1]
820 entdata_buffer += [block]
821
822 elif classtype == 'k_classtype_spawn':
823 node.classtype = 3
824
825 elif classtype == 'k_classtype_water':
826 node.classtype = 4
827
828 elif classtype == 'k_classtype_car_path':
829 node.classtype = 5
830 entdata_length += sizeof( classtype_car_path )
831
832 pn = classtype_car_path()
833 pn.target = 0
834 pn.target1 = 0
835
836 if obj.cv_data.target != None:
837 pn.target = obj.cv_data.target.cv_data.uid
838 if obj.cv_data.target1 != None:
839 pn.target1 = obj.cv_data.target1.cv_data.uid
840
841 entdata_buffer += [pn]
842
843 elif obj.is_instancer:
844 target = obj.instance_collection
845
846 node.classtype = 6
847 entdata_length += sizeof( classtype_instance )
848
849 inst = classtype_instance()
850 inst.pstr_file = emplace_string( F"models/{target.name}.mdl" )
851 entdata_buffer += [inst]
852
853 elif classtype == 'k_classtype_capsule':
854 node.classtype = 7
855
856 elif classtype == 'k_classtype_route_node':
857 node.classtype = 8
858 entdata_length += sizeof( classtype_route_node )
859
860 rn = classtype_route_node()
861 if obj.cv_data.target != None:
862 rn.target = obj.cv_data.target.cv_data.uid
863 if obj.cv_data.target1 != None:
864 rn.target1 = obj.cv_data.target1.cv_data.uid
865
866 entdata_buffer += [rn]
867
868 elif classtype == 'k_classtype_route':
869 node.classtype = 9
870 entdata_length += sizeof( classtype_route )
871 r = classtype_route()
872 r.pstr_name = emplace_string("not-implemented")
873 r.colour[0] = obj.cv_data.colour[0]
874 r.colour[1] = obj.cv_data.colour[1]
875 r.colour[2] = obj.cv_data.colour[2]
876
877 if obj.cv_data.target != None:
878 r.id_start = obj.cv_data.target.cv_data.uid
879
880 entdata_buffer += [r]
881
882 # classtype == 'k_classtype_none':
883 else:
884 node.classtype = 0
885 node.offset = 0
886
887 node_buffer += [node]
888
889 # Write data arrays
890 #
891 header.anim_count = len(anim_buffer)
892
893 print( "Writing data" )
894 fpos = sizeof(header)
895
896 print( F"Nodes: {header.node_count}" )
897 header.node_offset = fpos
898 fpos += sizeof(mdl_node)*header.node_count
899
900 print( F"Submeshes: {header.submesh_count}" )
901 header.submesh_offset = fpos
902 fpos += sizeof(mdl_submesh)*header.submesh_count
903
904 print( F"Materials: {header.material_count}" )
905 header.material_offset = fpos
906 fpos += sizeof(mdl_material)*header.material_count
907
908 print( F"Animation count: {header.anim_count}" )
909 header.anim_offset = fpos
910 fpos += sizeof(mdl_animation)*header.anim_count
911
912 print( F"Entdata length: {entdata_length}" )
913 header.entdata_offset = fpos
914 fpos += entdata_length
915
916 print( F"Vertex count: {header.vertex_count}" )
917 header.vertex_offset = fpos
918 fpos += sizeof(mdl_vert)*header.vertex_count
919
920 print( F"Indice count: {header.indice_count}" )
921 header.indice_offset = fpos
922 fpos += sizeof(c_uint32)*header.indice_count
923
924 print( F"Keyframe count: {animdata_length}" )
925 header.animdata_offset = fpos
926 fpos += animdata_length
927
928 print( F"Strings length: {len(strings_buffer)}" )
929 header.strings_offset = fpos
930 fpos += len(strings_buffer)
931
932 header.file_length = fpos
933
934 path = F"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
935 fp = open( path, "wb" )
936
937 fp.write( bytearray( header ) )
938
939 for node in node_buffer:
940 fp.write( bytearray(node) )
941 for sm in submesh_buffer:
942 fp.write( bytearray(sm) )
943 for mat in material_buffer:
944 fp.write( bytearray(mat) )
945 for a in anim_buffer:
946 fp.write( bytearray(a) )
947 for ed in entdata_buffer:
948 fp.write( bytearray(ed) )
949 for v in vertex_buffer:
950 fp.write( bytearray(v) )
951 for i in indice_buffer:
952 fp.write( bytearray(i) )
953 for kf in animdata_buffer:
954 fp.write( bytearray(kf) )
955
956 fp.write( strings_buffer )
957 fp.close()
958
959 print( F"Completed {collection_name}.mdl" )
960
961 # Clicky clicky GUI
962 # ------------------------------------------------------------------------------
963
964 cv_view_draw_handler = None
965 cv_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
966
967 def cv_draw():
968 global cv_view_shader
969 cv_view_shader.bind()
970 gpu.state.depth_mask_set(False)
971 gpu.state.line_width_set(2.0)
972 gpu.state.face_culling_set('BACK')
973 gpu.state.depth_test_set('LESS')
974 gpu.state.blend_set('NONE')
975
976 verts = []
977 colours = []
978
979 #def drawbezier(p0,h0,p1,h1,c0,c1):
980 # nonlocal verts, colours
981
982 # verts += [p0]
983 # verts += [h0]
984 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
985 # verts += [p1]
986 # verts += [h1]
987 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
988 #
989 # last = p0
990 # for i in range(10):
991 # t = (i+1)/10
992 # a0 = 1-t
993
994 # tt = t*t
995 # ttt = tt*t
996 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
997 # verts += [(last[0],last[1],last[2])]
998 # verts += [(p[0],p[1],p[2])]
999 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
1000 # last = p
1001
1002 course_count = 0
1003
1004 def drawbhandle(obj, direction, colour):
1005 nonlocal verts, colours
1006 p0 = obj.location
1007 h0 = obj.matrix_world @ Vector((0,direction,0))
1008 verts += [p0]
1009 verts += [h0]
1010 colours += [colour,colour]
1011
1012 def drawbezier(p0,h0,p1,h1,c0,c1):
1013 nonlocal verts, colours
1014
1015 last = p0
1016 for i in range(10):
1017 t = (i+1)/10
1018 a0 = 1-t
1019
1020 tt = t*t
1021 ttt = tt*t
1022 p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
1023 verts += [(last[0],last[1],last[2])]
1024 verts += [(p[0],p[1],p[2])]
1025 colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
1026 last = p
1027
1028 def drawsbpath(o0,o1,c0,c1,s0,s1):
1029 nonlocal course_count
1030
1031 offs = ((course_count % 2)*2-1) * course_count * 0.02
1032
1033 p0 = o0.matrix_world @ Vector((offs, 0,0))
1034 h0 = o0.matrix_world @ Vector((offs, s0,0))
1035 p1 = o1.matrix_world @ Vector((offs, 0,0))
1036 h1 = o1.matrix_world @ Vector((offs,-s1,0))
1037 drawbezier(p0,h0,p1,h1,c0,c1)
1038
1039 def drawbpath(o0,o1,c0,c1):
1040 drawsbpath(o0,o1,c0,c1,1.0,1.0)
1041
1042 def drawbline(p0,p1,c0,c1):
1043 nonlocal verts, colours
1044 verts += [p0,p1]
1045 colours += [c0,c1]
1046
1047 for obj in bpy.context.collection.objects:
1048 if obj.type == 'ARMATURE':
1049 for bone in obj.data.bones:
1050 if bone.cv_data.collider and obj.data.pose_position == 'REST':
1051 c = bone.head_local
1052 a = bone.cv_data.v0
1053 b = bone.cv_data.v1
1054
1055 vs = [None]*8
1056 vs[0]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+a[2]))
1057 vs[1]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+a[2]))
1058 vs[2]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+a[2]))
1059 vs[3]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+a[2]))
1060 vs[4]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+b[2]))
1061 vs[5]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+b[2]))
1062 vs[6]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+b[2]))
1063 vs[7]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+b[2]))
1064
1065 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1066 (0,4),(1,5),(2,6),(3,7)]
1067
1068 for l in indices:
1069 v0 = vs[l[0]]
1070 v1 = vs[l[1]]
1071 verts += [(v0[0],v0[1],v0[2])]
1072 verts += [(v1[0],v1[1],v1[2])]
1073 colours += [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
1074
1075 center=obj.matrix_world@c
1076
1077 def _angle_lim( major, minor, amin, amax, colour ):
1078 nonlocal verts, colours
1079 f = 0.05
1080 ay = major*f
1081 ax = minor*f
1082
1083 for x in range(16):
1084 t0 = x/16
1085 t1 = (x+1)/16
1086 a0 = amin*(1.0-t0)+amax*t0
1087 a1 = amin*(1.0-t1)+amax*t1
1088
1089 p0 = c + major*f*math.cos(a0) + minor*f*math.sin(a0)
1090 p1 = c + major*f*math.cos(a1) + minor*f*math.sin(a1)
1091
1092 p0=obj.matrix_world @ p0
1093 p1=obj.matrix_world @ p1
1094 verts += [p0,p1]
1095 colours += [colour,colour]
1096
1097 if x == 0:
1098 verts += [p0,c]
1099 colours += [colour,colour]
1100 if x == 15:
1101 verts += [p1,c]
1102 colours += [colour,colour]
1103
1104 verts += [c+major*1.2*f,c+major*f*0.8]
1105 colours += [colour,colour]
1106
1107 if bone.cv_data.con0:
1108 _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
1109 bone.cv_data.mins[0], bone.cv_data.maxs[0], \
1110 (1,0,0,1))
1111 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1112 bone.cv_data.mins[1], bone.cv_data.maxs[1], \
1113 (0,1,0,1))
1114 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1115 bone.cv_data.mins[2], bone.cv_data.maxs[2], \
1116 (0,0,1,1))
1117
1118
1119 if obj.cv_data.classtype == 'k_classtype_gate':
1120 if obj.type == 'MESH':
1121 dims = obj.data.cv_data.v0
1122 else:
1123 dims = obj.cv_data.v0
1124
1125 vs = [None]*9
1126 c = Vector((0,0,dims[2]))
1127
1128 vs[0] = obj.matrix_world @ Vector((-dims[0],0.0,-dims[1]+dims[2]))
1129 vs[1] = obj.matrix_world @ Vector((-dims[0],0.0, dims[1]+dims[2]))
1130 vs[2] = obj.matrix_world @ Vector(( dims[0],0.0, dims[1]+dims[2]))
1131 vs[3] = obj.matrix_world @ Vector(( dims[0],0.0,-dims[1]+dims[2]))
1132 vs[4] = obj.matrix_world @ (c+Vector((-1,0,-2)))
1133 vs[5] = obj.matrix_world @ (c+Vector((-1,0, 2)))
1134 vs[6] = obj.matrix_world @ (c+Vector(( 1,0, 2)))
1135 vs[7] = obj.matrix_world @ (c+Vector((-1,0, 0)))
1136 vs[8] = obj.matrix_world @ (c+Vector(( 1,0, 0)))
1137
1138 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
1139
1140 for l in indices:
1141 v0 = vs[l[0]]
1142 v1 = vs[l[1]]
1143 verts += [(v0[0],v0[1],v0[2])]
1144 verts += [(v1[0],v1[1],v1[2])]
1145 colours += [(1,1,0,1),(1,1,0,1)]
1146
1147 sw = (0.4,0.4,0.4,0.2)
1148 if obj.cv_data.target != None:
1149 drawbline( obj.location, obj.cv_data.target.location, sw,sw )
1150
1151 elif obj.cv_data.classtype == 'k_classtype_route_node':
1152 sw = Vector((0.4,0.4,0.4,0.2))
1153 sw2 = Vector((1.5,0.2,0.2,0.0))
1154 if obj.cv_data.target != None:
1155 drawbpath( obj, obj.cv_data.target, sw, sw )
1156 if obj.cv_data.target1 != None:
1157 drawbpath( obj, obj.cv_data.target1, sw, sw )
1158
1159 drawbhandle( obj, 1.0, (0.8,0.8,0.8,1.0) )
1160 drawbhandle( obj, -1.0, (0.4,0.4,0.4,1.0) )
1161
1162 p1 = obj.location+ \
1163 obj.matrix_world.to_quaternion() @ Vector((0,0,-6+1.5))
1164 drawbline( obj.location, p1, sw,sw2 )
1165
1166
1167 elif obj.cv_data.classtype == 'k_classtype_block':
1168 a = obj.data.cv_data.v0
1169 b = obj.data.cv_data.v1
1170
1171 vs = [None]*8
1172 vs[0] = obj.matrix_world @ Vector((a[0], a[1], a[2]))
1173 vs[1] = obj.matrix_world @ Vector((a[0], b[1], a[2]))
1174 vs[2] = obj.matrix_world @ Vector((b[0], b[1], a[2]))
1175 vs[3] = obj.matrix_world @ Vector((b[0], a[1], a[2]))
1176 vs[4] = obj.matrix_world @ Vector((a[0], a[1], b[2]))
1177 vs[5] = obj.matrix_world @ Vector((a[0], b[1], b[2]))
1178 vs[6] = obj.matrix_world @ Vector((b[0], b[1], b[2]))
1179 vs[7] = obj.matrix_world @ Vector((b[0], a[1], b[2]))
1180
1181 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1182 (0,4),(1,5),(2,6),(3,7)]
1183
1184 for l in indices:
1185 v0 = vs[l[0]]
1186 v1 = vs[l[1]]
1187 verts += [(v0[0],v0[1],v0[2])]
1188 verts += [(v1[0],v1[1],v1[2])]
1189 colours += [(1,1,0,1),(1,1,0,1)]
1190
1191 elif obj.cv_data.classtype == 'k_classtype_capsule':
1192 h = obj.data.cv_data.v0[0]
1193 r = obj.data.cv_data.v0[1]
1194
1195 vs = [None]*10
1196 vs[0] = obj.matrix_world @ Vector((0.0,0.0, h*0.5 ))
1197 vs[1] = obj.matrix_world @ Vector((0.0,0.0,-h*0.5 ))
1198 vs[2] = obj.matrix_world @ Vector(( r,0.0, h*0.5-r))
1199 vs[3] = obj.matrix_world @ Vector(( -r,0.0, h*0.5-r))
1200 vs[4] = obj.matrix_world @ Vector(( r,0.0,-h*0.5+r))
1201 vs[5] = obj.matrix_world @ Vector(( -r,0.0,-h*0.5+r))
1202 vs[6] = obj.matrix_world @ Vector((0.0, r , h*0.5-r))
1203 vs[7] = obj.matrix_world @ Vector((0.0,-r , h*0.5-r))
1204 vs[8] = obj.matrix_world @ Vector((0.0, r ,-h*0.5+r))
1205 vs[9] = obj.matrix_world @ Vector((0.0,-r ,-h*0.5+r))
1206
1207 indices = [(0,1),(2,3),(4,5),(6,7),(8,9)]
1208
1209 for l in indices:
1210 v0 = vs[l[0]]
1211 v1 = vs[l[1]]
1212 verts += [(v0[0],v0[1],v0[2])]
1213 verts += [(v1[0],v1[1],v1[2])]
1214 colours += [(0.5,1,0,1),(0.5,1,0,1)]
1215
1216 elif obj.cv_data.classtype == 'k_classtype_spawn':
1217 vs = [None]*4
1218 vs[0] = obj.matrix_world @ Vector((0,0,0))
1219 vs[1] = obj.matrix_world @ Vector((0,2,0))
1220 vs[2] = obj.matrix_world @ Vector((0.5,1,0))
1221 vs[3] = obj.matrix_world @ Vector((-0.5,1,0))
1222 indices = [(0,1),(1,2),(1,3)]
1223 for l in indices:
1224 v0 = vs[l[0]]
1225 v1 = vs[l[1]]
1226 verts += [(v0[0],v0[1],v0[2])]
1227 verts += [(v1[0],v1[1],v1[2])]
1228 colours += [(0,1,1,1),(0,1,1,1)]
1229
1230 elif obj.cv_data.classtype == 'k_classtype_route':
1231 vs = [None]*2
1232 vs[0] = obj.location
1233 vs[1] = obj.cv_data.target.location
1234 indices = [(0,1)]
1235 for l in indices:
1236 v0 = vs[l[0]]
1237 v1 = vs[l[1]]
1238 verts += [(v0[0],v0[1],v0[2])]
1239 verts += [(v1[0],v1[1],v1[2])]
1240 colours += [(0,1,1,1),(0,1,1,1)]
1241
1242 stack = [None]*64
1243 stack_i = [0]*64
1244 stack[0] = obj.cv_data.target
1245 si = 1
1246 loop_complete = False
1247
1248 while si > 0:
1249 if stack_i[si-1] == 2:
1250 si -= 1
1251 continue
1252
1253 if si == 0: # Loop failed to complete
1254 break
1255
1256 node = stack[si-1]
1257
1258 targets = [None,None]
1259 targets[0] = node.cv_data.target
1260
1261 if node.cv_data.classtype == 'k_classtype_route_node':
1262 targets[1] = node.cv_data.target1
1263
1264 nextnode = targets[stack_i[si-1]]
1265 stack_i[si-1] += 1
1266
1267 if nextnode != None: # branch
1268 if nextnode == stack[0]: # Loop completed
1269 loop_complete = True
1270 break
1271
1272 valid=True
1273 for sj in range(si):
1274 if stack[sj] == nextnode: # invalidated path
1275 valid=False
1276 break
1277
1278 if valid:
1279 stack_i[si] = 0
1280 stack[si] = nextnode
1281 si += 1
1282 continue
1283
1284 if loop_complete:
1285 cc = Vector((obj.cv_data.colour[0],\
1286 obj.cv_data.colour[1],\
1287 obj.cv_data.colour[2],\
1288 1.0))
1289
1290 for sj in range(si):
1291 sk = (sj+1)%si
1292
1293 if stack[sj].cv_data.classtype == 'k_classtype_gate' and \
1294 stack[sk].cv_data.classtype == 'k_classtype_gate':
1295 dist = (stack[sj].location-stack[sk].location).magnitude
1296 drawsbpath( stack[sj], stack[sk], cc*0.4, cc, dist, dist )
1297
1298 else:
1299 drawbpath( stack[sj], stack[sk], cc, cc )
1300
1301 course_count += 1
1302
1303 elif obj.cv_data.classtype == 'k_classtype_car_path':
1304 v0 = obj.matrix_world.to_quaternion() @ Vector((0,1,0))
1305 c0 = Vector((v0.x*0.5+0.5, v0.y*0.5+0.5, 0.0, 1.0))
1306 drawbhandle( obj, 1.0, (0.9,0.9,0.9,1.0) )
1307
1308 if obj.cv_data.target != None:
1309 v1 = obj.cv_data.target.matrix_world.to_quaternion()@Vector((0,1,0))
1310 c1 = Vector((v1.x*0.5+0.5, v1.y*0.5+0.5, 0.0, 1.0))
1311
1312 drawbhandle( obj.cv_data.target, -1.0, (0.5,0.5,0.5,1.0) )
1313 drawbpath( obj, obj.cv_data.target, c0, c1 )
1314
1315 if obj.cv_data.target1 != None:
1316 v1 = obj.cv_data.target1.matrix_world.to_quaternion()@Vector((0,1,0))
1317 c1 = Vector((v1.x*0.5+0.5, v1.y*0.5+0.5, 0.0, 1.0))
1318
1319 drawbhandle( obj.cv_data.target1, -1.0, (0.5,0.5,0.5,1.0) )
1320 drawbpath( obj, obj.cv_data.target1, c0, c1 )
1321
1322 lines = batch_for_shader(\
1323 cv_view_shader, 'LINES', \
1324 { "pos":verts, "color":colours })
1325
1326 lines.draw( cv_view_shader )
1327
1328 def cv_poll_target(scene, obj):
1329 if obj == bpy.context.active_object:
1330 return False
1331 if obj.cv_data.classtype == 'k_classtype_none':
1332 return False
1333 return True
1334
1335 class CV_MESH_SETTINGS(bpy.types.PropertyGroup):
1336 v0: bpy.props.FloatVectorProperty(name="v0",size=3)
1337 v1: bpy.props.FloatVectorProperty(name="v1",size=3)
1338 v2: bpy.props.FloatVectorProperty(name="v2",size=3)
1339 v3: bpy.props.FloatVectorProperty(name="v3",size=3)
1340
1341 class CV_OBJ_SETTINGS(bpy.types.PropertyGroup):
1342 uid: bpy.props.IntProperty( name="" )
1343
1344 target: bpy.props.PointerProperty( type=bpy.types.Object, name="target", \
1345 poll=cv_poll_target )
1346 target1: bpy.props.PointerProperty( type=bpy.types.Object, name="target1", \
1347 poll=cv_poll_target )
1348
1349 colour: bpy.props.FloatVectorProperty(name="colour",subtype='COLOR',\
1350 min=0.0,max=1.0)
1351
1352 classtype: bpy.props.EnumProperty(
1353 name="Format",
1354 items = [
1355 ('k_classtype_none', "k_classtype_none", "", 0),
1356 ('k_classtype_gate', "k_classtype_gate", "", 1),
1357 ('k_classtype_block', "k_classtype_block", "", 2),
1358 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1359 ('k_classtype_water', "k_classtype_water", "", 4),
1360 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1361 ('k_classtype_INSTANCE', "","", 6 ),
1362 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1363 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1364 ('k_classtype_route', "k_classtype_route", "", 9 ),
1365 ('k_classtype_bone',"k_classtype_bone","",10),
1366 ('k_classtype_SKELETON', "","", 11 ),
1367 ('k_classtype_SKIN',"","",12)
1368 ])
1369
1370 class CV_BONE_SETTINGS(bpy.types.PropertyGroup):
1371 collider: bpy.props.BoolProperty(name="Collider",default=False)
1372 v0: bpy.props.FloatVectorProperty(name="v0",size=3)
1373 v1: bpy.props.FloatVectorProperty(name="v1",size=3)
1374
1375 con0: bpy.props.BoolProperty(name="Constriant 0",default=False)
1376 mins: bpy.props.FloatVectorProperty(name="mins",size=3)
1377 maxs: bpy.props.FloatVectorProperty(name="maxs",size=3)
1378
1379 class CV_BONE_PANEL(bpy.types.Panel):
1380 bl_label="Bone Config"
1381 bl_idname="SCENE_PT_cv_bone"
1382 bl_space_type='PROPERTIES'
1383 bl_region_type='WINDOW'
1384 bl_context='bone'
1385
1386 def draw(_,context):
1387 active_object = context.active_object
1388 if active_object == None: return
1389
1390 bone = active_object.data.bones.active
1391 if bone == None: return
1392
1393 _.layout.prop( bone.cv_data, "collider" )
1394 _.layout.prop( bone.cv_data, "v0" )
1395 _.layout.prop( bone.cv_data, "v1" )
1396
1397 _.layout.label( text="Angle Limits" )
1398 _.layout.prop( bone.cv_data, "con0" )
1399 _.layout.prop( bone.cv_data, "mins" )
1400 _.layout.prop( bone.cv_data, "maxs" )
1401
1402 class CV_SCENE_SETTINGS(bpy.types.PropertyGroup):
1403 use_hidden: bpy.props.BoolProperty( name="use hidden", default=False )
1404
1405 class CV_OBJ_PANEL(bpy.types.Panel):
1406 bl_label="Entity Config"
1407 bl_idname="SCENE_PT_cv_entity"
1408 bl_space_type='PROPERTIES'
1409 bl_region_type='WINDOW'
1410 bl_context="object"
1411
1412 def draw(_,context):
1413 active_object = bpy.context.active_object
1414 if active_object == None: return
1415 _.layout.prop( active_object.cv_data, "classtype" )
1416
1417 if active_object.cv_data.classtype == 'k_classtype_gate':
1418 _.layout.prop( active_object.cv_data, "target" )
1419
1420 mesh = active_object.data
1421 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
1422 _.layout.prop( mesh.cv_data, "v0" )
1423
1424 elif active_object.cv_data.classtype == 'k_classtype_car_path' or \
1425 active_object.cv_data.classtype == 'k_classtype_route_node':
1426 _.layout.prop( active_object.cv_data, "target" )
1427 _.layout.prop( active_object.cv_data, "target1" )
1428
1429 elif active_object.cv_data.classtype == 'k_classtype_route':
1430 _.layout.prop( active_object.cv_data, "target" )
1431 _.layout.prop( active_object.cv_data, "colour" )
1432
1433 elif active_object.cv_data.classtype == 'k_classtype_block':
1434 mesh = active_object.data
1435
1436 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
1437 _.layout.prop( mesh.cv_data, "v0" )
1438 _.layout.prop( mesh.cv_data, "v1" )
1439 _.layout.prop( mesh.cv_data, "v2" )
1440 _.layout.prop( mesh.cv_data, "v3" )
1441 elif active_object.cv_data.classtype == 'k_classtype_capsule':
1442 mesh = active_object.data
1443 _.layout.label( text=F"(i) Data is stored in {mesh.name}" )
1444 _.layout.prop( mesh.cv_data, "v0" )
1445
1446 class CV_INTERFACE(bpy.types.Panel):
1447 bl_idname = "VIEW3D_PT_carve"
1448 bl_label = "Carve"
1449 bl_space_type = 'VIEW_3D'
1450 bl_region_type = 'UI'
1451 bl_category = "Carve"
1452
1453 def draw(_, context):
1454 layout = _.layout
1455 layout.prop( context.scene.cv_data, "use_hidden")
1456 layout.operator( "carve.compile_all" )
1457
1458 def test_compile():
1459 view_layer = bpy.context.view_layer
1460 for col in view_layer.layer_collection.children["export"].children:
1461 if not col.hide_viewport or bpy.context.scene.cv_data.use_hidden:
1462 write_model( col.name )
1463
1464 class CV_COMPILE(bpy.types.Operator):
1465 bl_idname="carve.compile_all"
1466 bl_label="Compile All"
1467
1468 def execute(_,context):
1469 test_compile()
1470 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1471 #for col in bpy.data.collections["export"].children:
1472 # write_model( col.name )
1473
1474 return {'FINISHED'}
1475
1476 classes = [CV_OBJ_SETTINGS,CV_OBJ_PANEL,CV_COMPILE,CV_INTERFACE,\
1477 CV_MESH_SETTINGS, CV_SCENE_SETTINGS, CV_BONE_SETTINGS,\
1478 CV_BONE_PANEL]
1479
1480 def register():
1481 global cv_view_draw_handler
1482
1483 for c in classes:
1484 bpy.utils.register_class(c)
1485
1486 bpy.types.Object.cv_data = bpy.props.PointerProperty(type=CV_OBJ_SETTINGS)
1487 bpy.types.Mesh.cv_data = bpy.props.PointerProperty(type=CV_MESH_SETTINGS)
1488 bpy.types.Scene.cv_data = bpy.props.PointerProperty(type=CV_SCENE_SETTINGS)
1489 bpy.types.Bone.cv_data = bpy.props.PointerProperty(type=CV_BONE_SETTINGS)
1490
1491 cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
1492 cv_draw,(),'WINDOW','POST_VIEW')
1493
1494 def unregister():
1495 global cv_view_draw_handler
1496
1497 for c in classes:
1498 bpy.utils.unregister_class(c)
1499
1500 bpy.types.SpaceView3D.draw_handler_remove(cv_view_draw_handler,'WINDOW')