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