acd28ec3bcfcadbd96bc17cc539bd1508a78f86d
4 from mathutils
import *
5 from gpu_extras
.batch
import batch_for_shader
8 "name":"Carve exporter",
9 "author": "Harry Godden (hgn)",
16 "category":"Import/Export",
19 class mdl_vert(Structure
):
21 _fields_
= [("co",c_float
*3),
25 ("weights",c_uint16
*4),
28 class mdl_submesh(Structure
):
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
37 class mdl_material(Structure
):
39 _fields_
= [("pstr_name",c_uint32
)]
41 class mdl_node(Structure
):
43 _fields_
= [("co",c_float
*3),
46 ("submesh_start",c_uint32
),
47 ("submesh_count",c_uint32
),
48 ("classtype",c_uint32
),
51 ("pstr_name",c_uint32
)]
53 class mdl_header(Structure
):
55 _fields_
= [("identifier",c_uint32
),
57 ("file_length",c_uint32
),
58 ("vertex_count",c_uint32
),
59 ("vertex_offset",c_uint32
),
61 ("indice_count",c_uint32
),
62 ("indice_offset",c_uint32
),
64 ("submesh_count",c_uint32
),
65 ("submesh_offset",c_uint32
),
67 ("material_count",c_uint32
),
68 ("material_offset",c_uint32
),
70 ("node_count",c_uint32
),
71 ("node_offset",c_uint32
),
73 ("anim_count",c_uint32
),
74 ("anim_offset",c_uint32
),
76 ("strings_offset",c_uint32
),
77 ("entdata_offset",c_uint32
),
78 ("animdata_offset",c_uint32
)
81 class mdl_animation(Structure
):
83 _fields_
= [("pstr_name",c_uint32
),
88 class mdl_keyframe(Structure
):
90 _fields_
= [("co",c_float
*3),
95 # ==========================================
97 class classtype_gate(Structure
):
99 _fields_
= [("target",c_uint32
),
102 class classtype_block(Structure
):
104 _fields_
= [("bbx",(c_float
*3)*2)]
106 class classtype_spawn(Structure
):
108 _fields_
= [("temp",c_uint32
)]
110 class classtype_water(Structure
):
112 _fields_
= [("temp",c_uint32
)]
114 class classtype_car_path(Structure
):
116 _fields_
= [("target",c_uint32
),
117 ("target1",c_uint32
)]
119 class classtype_instance(Structure
):
121 _fields_
= [("pstr_file",c_uint32
)]
123 class classtype_capsule(Structure
):
125 _fields_
= [("height",c_float
),
128 class classtype_route_node(Structure
):
130 _fields_
= [("target",c_uint32
),
131 ("target1",c_uint32
)]
133 class classtype_route(Structure
):
135 _fields_
= [("pstr_name",c_uint32
),
136 ("id_start",c_uint32
),
137 ("colour",c_float
*3)]
139 class classtype_skin(Structure
):
141 _fields_
= [("skeleton",c_uint32
)]
143 class classtype_skeleton(Structure
):
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
)]
151 class classtype_bone(Structure
):
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)]
162 # ==============================================================================
164 def write_model(collection_name
):
165 print( F
"Model graph | Create mode '{collection_name}'" )
167 header
= mdl_header()
168 header
.identifier
= 0xABCD0000
170 header
.vertex_count
= 0
171 header
.indice_count
= 0
172 header
.submesh_count
= 0
173 header
.node_count
= 0
174 header
.material_count
= 0
175 header
.file_length
= 0
195 def emplace_string( s
):
196 nonlocal string_cache
, strings_buffer
198 if s
in string_cache
:
199 return string_cache
[s
]
201 string_cache
[s
] = len( strings_buffer
)
202 strings_buffer
+= (s
+'\0').encode('utf-8')
203 return string_cache
[s
]
205 def emplace_material( mat
):
206 nonlocal material_cache
, material_buffer
211 if mat
.name
in material_cache
:
212 return material_cache
[mat
.name
]
214 material_cache
[mat
.name
] = header
.material_count
215 dest
= mdl_material()
216 dest
.pstr_name
= emplace_string( mat
.name
)
217 material_buffer
+= [dest
]
219 header
.material_count
+= 1
220 return material_cache
[mat
.name
]
222 # Create root or empty node and materials
223 # this is to designate id 0 as 'NULL'
225 none_material
= c_uint32(69)
226 none_material
.name
= ""
227 emplace_material( none_material
)
240 root
.pstr_name
= emplace_string('')
241 root
.submesh_start
= 0
242 root
.submesh_count
= 0
245 node_buffer
+= [root
]
249 print( " assigning ids" )
250 collection
= bpy
.data
.collections
[collection_name
]
253 # ==========================================
255 header
.node_count
= 0
258 uid
= header
.node_count
259 header
.node_count
+= 1
262 print( " creating scene graph" )
266 graph
["children"] = []
267 graph
["uid"] = _uid()
268 graph
["parent"] = None
270 graph_lookup
= {} # object can lookup its graph def here
272 for obj
in collection
.all_objects
:
275 def _extend( p
, n
, d
):
279 tree
["children"] = []
285 if n
.type == 'ARMATURE':
286 tree
["bones"] = [None] # None is the root transform
288 tree
["collider_count"] = 0
290 def _extendb( p
, n
, d
):
295 btree
["uid"] = _uid()
296 btree
["children"] = []
299 tree
["bones"] += [n
.name
]
302 _extendb( btree
, c
, d
+1 )
304 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
306 btree
["target"] = c
.subtarget
307 btree
["pole"] = c
.pole_subtarget
308 tree
["ik_count"] += 1
310 if n
.cv_data
.collider
:
311 tree
['collider_count'] += 1
313 btree
['deform'] = n
.use_deform
314 p
['children'] += [btree
]
316 for b
in n
.data
.bones
:
318 _extendb( tree
, b
, d
+1 )
320 for obj1
in n
.children
:
321 _extend( tree
, obj1
, d
+1 )
323 p
["children"] += [tree
]
324 graph_lookup
[n
] = tree
326 _extend( graph
, obj
, 1 )
330 for c
in p
['children']:
332 yield from _graph_iter(c
)
334 it
= _graph_iter(graph
)
336 root
.parent
= 0xffffffff
339 # ==============================================
340 it
= _graph_iter(graph
)
341 print( " compiling data" )
343 if 'obj' in node_def
:
344 obj
= node_def
['obj']
347 elif 'bone' in node_def
:
348 obj
= node_def
['bone']
350 objco
= obj
.head_local
352 depth
= node_def
['depth']
353 uid
= node_def
['uid']
356 node
.co
[0] = objco
[0]
357 node
.co
[1] = objco
[2]
358 node
.co
[2] = -objco
[1]
360 # Convert rotation quat to our space type
361 quat
= obj
.matrix_local
.to_quaternion()
368 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
369 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
370 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
372 node
.s
[0] = obj
.scale
[0]
373 node
.s
[1] = obj
.scale
[2]
374 node
.s
[2] = obj
.scale
[1]
376 node
.pstr_name
= emplace_string( obj
.name
)
378 if node_def
["parent"]:
379 node
.parent
= node_def
["parent"]["uid"]
382 classtype
= 'k_classtype_bone'
383 elif objt
== 'ARMATURE':
384 classtype
= 'k_classtype_skeleton'
386 classtype
= obj
.cv_data
.classtype
389 # =================================================================
392 # Dont use the cache if we have modifiers that affect the normals
400 for mod
in obj
.modifiers
:
401 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
402 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
404 can_use_cache
= False
406 if mod
.type == 'ARMATURE':
407 classtype
= 'k_classtype_skin'
408 armature_def
= graph_lookup
[mod
.object]
409 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
411 armature_def
['obj'].data
.pose_position
= 'REST'
413 if can_use_cache
and obj
.data
.name
in mesh_cache
:
414 ref
= mesh_cache
[obj
.data
.name
]
415 node
.submesh_start
= ref
.submesh_start
416 node
.submesh_count
= ref
.submesh_count
420 node
.submesh_start
= header
.submesh_count
421 node
.submesh_count
= 0
423 default_mat
= c_uint32(69)
424 default_mat
.name
= ""
426 dgraph
= bpy
.context
.evaluated_depsgraph_get()
427 data
= obj
.evaluated_get(dgraph
).data
428 data
.calc_loop_triangles()
429 data
.calc_normals_split()
431 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
432 for material_id
, mat
in enumerate(mat_list
):
436 sm
.indice_start
= header
.indice_count
437 sm
.vertex_start
= header
.vertex_count
440 sm
.material_id
= emplace_material( mat
)
443 sm
.bbx
[0][i
] = 999999
444 sm
.bbx
[1][i
] = -999999
448 # Write the vertex / indice data
450 for tri_index
, tri
in enumerate(data
.loop_triangles
):
451 if tri
.material_index
!= material_id
:
455 vert
= data
.vertices
[tri
.vertices
[j
]]
457 vi
= data
.loops
[li
].vertex_index
460 norm
= data
.loops
[li
].normal
462 colour
= (255,255,255,255)
467 uv
= data
.uv_layers
.active
.data
[li
].uv
469 if data
.vertex_colors
:
470 colour
= data
.vertex_colors
.active
.data
[li
].color
471 colour
= (int(colour
[0]*255.0),\
472 int(colour
[1]*255.0),\
473 int(colour
[2]*255.0),\
474 int(colour
[3]*255.0))
479 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
480 if obj
.vertex_groups
[_
.group
].name
in \
481 armature_def
['bones']]
483 weight_groups
= sorted( src_groups
, key
= \
484 lambda a
: a
.weight
, reverse
=True )
487 if len(weight_groups
) > ml
:
488 g
= weight_groups
[ml
]
489 name
= obj
.vertex_groups
[g
.group
].name
493 groups
[ml
] = armature_def
['bones'].index(name
)
496 if len(weight_groups
) > 0:
497 inv_norm
= (1.0/tot
) * 65535.0
499 weights
[ml
] = int( weights
[ml
] * inv_norm
)
500 weights
[ml
] = min( weights
[ml
], 65535 )
501 weights
[ml
] = max( weights
[ml
], 0 )
504 m
= float(10**TOLERENCE
)
506 key
= (int(co
[0]*m
+0.5),\
528 indice_buffer
+= [boffa
[key
]]
530 index
= c_uint32(sm
.vertex_count
)
534 indice_buffer
+= [index
]
545 v
.colour
[0] = colour
[0]
546 v
.colour
[1] = colour
[1]
547 v
.colour
[2] = colour
[2]
548 v
.colour
[3] = colour
[3]
549 v
.weights
[0] = weights
[0]
550 v
.weights
[1] = weights
[1]
551 v
.weights
[2] = weights
[2]
552 v
.weights
[3] = weights
[3]
553 v
.groups
[0] = groups
[0]
554 v
.groups
[1] = groups
[1]
555 v
.groups
[2] = groups
[2]
556 v
.groups
[3] = groups
[3]
561 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
562 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
566 if sm
.vertex_count
== 0:
571 submesh_buffer
+= [sm
]
572 node
.submesh_count
+= 1
573 header
.submesh_count
+= 1
574 header
.vertex_count
+= sm
.vertex_count
575 header
.indice_count
+= sm
.indice_count
577 mesh_cache
[obj
.data
.name
] = node
579 # Process entity data
580 # ==================================================================
581 node
.offset
= entdata_length
583 if classtype
!= 'k_classtype_none':
588 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
589 s001
= F
" L {obj.name}"
592 s004
= F
"{node.parent: 3}"
595 if classtype
== 'k_classtype_skin':
596 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
597 s005
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
599 scmp
= F
"{s002:<32} {s003:<22} {s004} {s005}"
602 if classtype
== 'k_classtype_INSTANCE' or \
603 classtype
== 'k_classtype_BONE' or \
604 classtype
== 'k_classtype_SKELETON' or \
605 classtype
== 'k_classtype_SKIN':
606 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
610 elif classtype
== 'k_classtype_skin':
613 armature
= armature_def
['obj']
614 entdata_length
+= sizeof( classtype_skin
)
616 skin
= classtype_skin()
617 skin
.skeleton
= armature
.cv_data
.uid
618 entdata_buffer
+= [skin
]
620 elif classtype
== 'k_classtype_skeleton':
622 entdata_length
+= sizeof( classtype_skeleton
)
623 skeleton
= classtype_skeleton()
625 armature_def
= graph_lookup
[obj
]
627 bones
= armature_def
['bones']
628 skeleton
.channels
= len(bones
)
629 skeleton
.ik_count
= armature_def
["ik_count"]
630 skeleton
.collider_count
= armature_def
["collider_count"]
632 if armature
.animation_data
:
633 previous_frame
= bpy
.context
.scene
.frame_current
634 previous_action
= armature
.animation_data
.action
636 skeleton
.anim_start
= len(anim_buffer
)
637 skeleton
.anim_count
= 0
639 for NLALayer
in obj
.animation_data
.nla_tracks
:
640 for NLAStrip
in NLALayer
.strips
:
642 for a
in bpy
.data
.actions
:
643 if a
.name
== NLAStrip
.name
:
644 armature
.animation_data
.action
= a
647 anim_start
= int(NLAStrip
.action_frame_start
)
648 anim_end
= int(NLAStrip
.action_frame_end
)
651 anim
= mdl_animation()
652 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
654 anim
.offset
= animdata_length
655 anim
.length
= anim_end
-anim_start
657 # Export the fucking keyframes
658 for frame
in range(anim_start
,anim_end
):
659 bpy
.context
.scene
.frame_set(frame
)
661 for bone_name
in bones
:
662 for pb
in armature
.pose
.bones
:
663 if pb
.name
== bone_name
:
664 rb
= armature
.data
.bones
[ bone_name
]
666 # relative bone matrix
667 if rb
.parent
is not None:
668 offset_mtx
= rb
.parent
.matrix_local
669 offset_mtx
= offset_mtx
.inverted_safe() @ \
672 inv_parent
= pb
.parent
.matrix
@ offset_mtx
673 inv_parent
.invert_safe()
674 fpm
= inv_parent
@ pb
.matrix
676 bone_mtx
= rb
.matrix
.to_4x4()
677 local_inv
= rb
.matrix_local
.inverted_safe()
678 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
680 loc
, rot
, sca
= fpm
.decompose()
683 final_pos
= Vector(( loc
[0], loc
[2], -loc
[1] ))
686 lc_m
= pb
.matrix_channel
.to_3x3()
687 if pb
.parent
is not None:
688 smtx
= pb
.parent
.matrix_channel
.to_3x3()
689 lc_m
= smtx
.inverted() @ lc_m
690 rq
= lc_m
.to_quaternion()
693 kf
.co
[0] = final_pos
[0]
694 kf
.co
[1] = final_pos
[1]
695 kf
.co
[2] = final_pos
[2]
707 animdata_buffer
+= [kf
]
708 animdata_length
+= sizeof(mdl_keyframe
)
711 anim_buffer
+= [anim
]
712 skeleton
.anim_count
+= 1
714 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
715 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
717 bpy
.context
.scene
.frame_set( previous_frame
)
718 armature
.animation_data
.action
= previous_action
720 entdata_buffer
+= [skeleton
]
722 elif classtype
== 'k_classtype_bone':
724 entdata_length
+= sizeof( classtype_bone
)
726 bone
= classtype_bone()
727 bone
.deform
= node_def
['deform']
729 if 'target' in node_def
:
730 bone
.ik_target
= armature_def
['bones'].index( node_def
['target'] )
731 bone
.ik_pole
= armature_def
['bones'].index( node_def
['pole'] )
736 bone
.collider
= 1 if obj
.cv_data
.collider
else 0
737 if obj
.cv_data
.collider
:
738 bone
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
739 bone
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
740 bone
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
741 bone
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
742 bone
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
743 bone
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
745 bone
.hitbox
[0][0] = 0.0
746 bone
.hitbox
[0][1] = 0.0
747 bone
.hitbox
[0][2] = 0.0
748 bone
.hitbox
[1][0] = 0.0
749 bone
.hitbox
[1][1] = 0.0
750 bone
.hitbox
[1][2] = 0.0
754 bone
.angle_limits
[0][0] = obj
.cv_data
.mins
[0]
755 bone
.angle_limits
[0][1] = obj
.cv_data
.mins
[2]
756 bone
.angle_limits
[0][2] = -obj
.cv_data
.maxs
[1]
757 bone
.angle_limits
[1][0] = obj
.cv_data
.maxs
[0]
758 bone
.angle_limits
[1][1] = obj
.cv_data
.maxs
[2]
759 bone
.angle_limits
[1][2] = -obj
.cv_data
.mins
[1]
762 bone
.angle_limits
[0][0] = 0.0
763 bone
.angle_limits
[0][1] = 0.0
764 bone
.angle_limits
[0][2] = 0.0
765 bone
.angle_limits
[1][0] = 0.0
766 bone
.angle_limits
[1][1] = 0.0
767 bone
.angle_limits
[1][2] = 0.0
769 bone
.deform
= node_def
['deform']
770 entdata_buffer
+= [bone
]
772 elif classtype
== 'k_classtype_gate':
774 entdata_length
+= sizeof( classtype_gate
)
776 gate
= classtype_gate()
778 if obj
.cv_data
.target
!= None:
779 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
781 if obj
.type == 'MESH':
782 gate
.dims
[0] = obj
.data
.cv_data
.v0
[0]
783 gate
.dims
[1] = obj
.data
.cv_data
.v0
[1]
784 gate
.dims
[2] = obj
.data
.cv_data
.v0
[2]
786 gate
.dims
[0] = obj
.cv_data
.v0
[0]
787 gate
.dims
[1] = obj
.cv_data
.v0
[1]
788 gate
.dims
[2] = obj
.cv_data
.v0
[2]
790 entdata_buffer
+= [gate
]
792 elif classtype
== 'k_classtype_block':
794 entdata_length
+= sizeof( classtype_block
)
796 source
= obj
.data
.cv_data
798 block
= classtype_block()
799 block
.bbx
[0][0] = source
.v0
[0]
800 block
.bbx
[0][1] = source
.v0
[2]
801 block
.bbx
[0][2] = -source
.v1
[1]
803 block
.bbx
[1][0] = source
.v1
[0]
804 block
.bbx
[1][1] = source
.v1
[2]
805 block
.bbx
[1][2] = -source
.v0
[1]
806 entdata_buffer
+= [block
]
808 elif classtype
== 'k_classtype_spawn':
811 elif classtype
== 'k_classtype_water':
814 elif classtype
== 'k_classtype_car_path':
816 entdata_length
+= sizeof( classtype_car_path
)
818 pn
= classtype_car_path()
822 if obj
.cv_data
.target
!= None:
823 pn
.target
= obj
.cv_data
.target
.cv_data
.uid
824 if obj
.cv_data
.target1
!= None:
825 pn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
827 entdata_buffer
+= [pn
]
829 elif obj
.is_instancer
:
830 target
= obj
.instance_collection
833 entdata_length
+= sizeof( classtype_instance
)
835 inst
= classtype_instance()
836 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
837 entdata_buffer
+= [inst
]
839 elif classtype
== 'k_classtype_capsule':
842 elif classtype
== 'k_classtype_route_node':
844 entdata_length
+= sizeof( classtype_route_node
)
846 rn
= classtype_route_node()
847 if obj
.cv_data
.target
!= None:
848 rn
.target
= obj
.cv_data
.target
.cv_data
.uid
849 if obj
.cv_data
.target1
!= None:
850 rn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
852 entdata_buffer
+= [rn
]
854 elif classtype
== 'k_classtype_route':
856 entdata_length
+= sizeof( classtype_route
)
857 r
= classtype_route()
858 r
.pstr_name
= emplace_string("not-implemented")
859 r
.colour
[0] = obj
.cv_data
.colour
[0]
860 r
.colour
[1] = obj
.cv_data
.colour
[1]
861 r
.colour
[2] = obj
.cv_data
.colour
[2]
863 if obj
.cv_data
.target
!= None:
864 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
866 entdata_buffer
+= [r
]
868 # classtype == 'k_classtype_none':
873 node_buffer
+= [node
]
877 header
.anim_count
= len(anim_buffer
)
879 print( "Writing data" )
880 fpos
= sizeof(header
)
882 print( F
"Nodes: {header.node_count}" )
883 header
.node_offset
= fpos
884 fpos
+= sizeof(mdl_node
)*header
.node_count
886 print( F
"Submeshes: {header.submesh_count}" )
887 header
.submesh_offset
= fpos
888 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
890 print( F
"Materials: {header.material_count}" )
891 header
.material_offset
= fpos
892 fpos
+= sizeof(mdl_material
)*header
.material_count
894 print( F
"Animation count: {header.anim_count}" )
895 header
.anim_offset
= fpos
896 fpos
+= sizeof(mdl_animation
)*header
.anim_count
898 print( F
"Entdata length: {entdata_length}" )
899 header
.entdata_offset
= fpos
900 fpos
+= entdata_length
902 print( F
"Vertex count: {header.vertex_count}" )
903 header
.vertex_offset
= fpos
904 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
906 print( F
"Indice count: {header.indice_count}" )
907 header
.indice_offset
= fpos
908 fpos
+= sizeof(c_uint32
)*header
.indice_count
910 print( F
"Keyframe count: {animdata_length}" )
911 header
.animdata_offset
= fpos
912 fpos
+= animdata_length
914 print( F
"Strings length: {len(strings_buffer)}" )
915 header
.strings_offset
= fpos
916 fpos
+= len(strings_buffer
)
918 header
.file_length
= fpos
920 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
921 fp
= open( path
, "wb" )
923 fp
.write( bytearray( header
) )
925 for node
in node_buffer
:
926 fp
.write( bytearray(node
) )
927 for sm
in submesh_buffer
:
928 fp
.write( bytearray(sm
) )
929 for mat
in material_buffer
:
930 fp
.write( bytearray(mat
) )
931 for a
in anim_buffer
:
932 fp
.write( bytearray(a
) )
933 for ed
in entdata_buffer
:
934 fp
.write( bytearray(ed
) )
935 for v
in vertex_buffer
:
936 fp
.write( bytearray(v
) )
937 for i
in indice_buffer
:
938 fp
.write( bytearray(i
) )
939 for kf
in animdata_buffer
:
940 fp
.write( bytearray(kf
) )
942 fp
.write( strings_buffer
)
945 print( F
"Completed {collection_name}.mdl" )
948 # ------------------------------------------------------------------------------
950 cv_view_draw_handler
= None
951 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
954 global cv_view_shader
955 cv_view_shader
.bind()
956 gpu
.state
.depth_mask_set(False)
957 gpu
.state
.line_width_set(2.0)
958 gpu
.state
.face_culling_set('BACK')
959 gpu
.state
.depth_test_set('LESS')
960 gpu
.state
.blend_set('NONE')
965 #def drawbezier(p0,h0,p1,h1,c0,c1):
966 # nonlocal verts, colours
970 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
973 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
976 # for i in range(10):
982 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
983 # verts += [(last[0],last[1],last[2])]
984 # verts += [(p[0],p[1],p[2])]
985 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
990 def drawbhandle(obj
, direction
, colour
):
991 nonlocal verts
, colours
993 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
996 colours
+= [colour
,colour
]
998 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
999 nonlocal verts
, colours
1008 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
1009 verts
+= [(last
[0],last
[1],last
[2])]
1010 verts
+= [(p
[0],p
[1],p
[2])]
1011 colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
1014 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
1015 nonlocal course_count
1017 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
1019 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
1020 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
1021 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
1022 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
1023 drawbezier(p0
,h0
,p1
,h1
,c0
,c1
)
1025 def drawbpath(o0
,o1
,c0
,c1
):
1026 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
1028 def drawbline(p0
,p1
,c0
,c1
):
1029 nonlocal verts
, colours
1033 for obj
in bpy
.context
.collection
.objects
:
1034 if obj
.type == 'ARMATURE':
1035 for bone
in obj
.data
.bones
:
1036 if bone
.cv_data
.collider
and obj
.data
.pose_position
== 'REST':
1042 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
1043 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
1044 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
1045 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
1046 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
1047 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
1048 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
1049 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
1051 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1052 (0,4),(1,5),(2,6),(3,7)]
1057 verts
+= [(v0
[0],v0
[1],v0
[2])]
1058 verts
+= [(v1
[0],v1
[1],v1
[2])]
1059 colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
1061 center
=obj
.matrix_world
@c
1063 def _angle_lim( major
, minor
, amin
, amax
, colour
):
1064 nonlocal verts
, colours
1072 a0
= amin
*(1.0-t0
)+amax
*t0
1073 a1
= amin
*(1.0-t1
)+amax
*t1
1075 p0
= c
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
1076 p1
= c
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
1078 p0
=obj
.matrix_world
@ p0
1079 p1
=obj
.matrix_world
@ p1
1081 colours
+= [colour
,colour
]
1085 colours
+= [colour
,colour
]
1088 colours
+= [colour
,colour
]
1090 verts
+= [c
+major
*1.2*f
,c
+major
*f
*0.8]
1091 colours
+= [colour
,colour
]
1093 if bone
.cv_data
.con0
:
1094 _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
1095 bone
.cv_data
.mins
[0], bone
.cv_data
.maxs
[0], \
1097 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1098 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
1100 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1101 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
1105 if obj
.cv_data
.classtype
== 'k_classtype_gate':
1106 if obj
.type == 'MESH':
1107 dims
= obj
.data
.cv_data
.v0
1109 dims
= obj
.cv_data
.v0
1112 c
= Vector((0,0,dims
[2]))
1114 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
1115 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
1116 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
1117 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
1118 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
1119 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
1120 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
1121 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
1122 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
1124 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
1129 verts
+= [(v0
[0],v0
[1],v0
[2])]
1130 verts
+= [(v1
[0],v1
[1],v1
[2])]
1131 colours
+= [(1,1,0,1),(1,1,0,1)]
1133 sw
= (0.4,0.4,0.4,0.2)
1134 if obj
.cv_data
.target
!= None:
1135 drawbline( obj
.location
, obj
.cv_data
.target
.location
, sw
,sw
)
1137 elif obj
.cv_data
.classtype
== 'k_classtype_route_node':
1138 sw
= Vector((0.4,0.4,0.4,0.2))
1139 sw2
= Vector((1.5,0.2,0.2,0.0))
1140 if obj
.cv_data
.target
!= None:
1141 drawbpath( obj
, obj
.cv_data
.target
, sw
, sw
)
1142 if obj
.cv_data
.target1
!= None:
1143 drawbpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
1145 drawbhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
1146 drawbhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
1148 p1
= obj
.location
+ \
1149 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
1150 drawbline( obj
.location
, p1
, sw
,sw2
)
1153 elif obj
.cv_data
.classtype
== 'k_classtype_block':
1154 a
= obj
.data
.cv_data
.v0
1155 b
= obj
.data
.cv_data
.v1
1158 vs
[0] = obj
.matrix_world
@ Vector((a
[0], a
[1], a
[2]))
1159 vs
[1] = obj
.matrix_world
@ Vector((a
[0], b
[1], a
[2]))
1160 vs
[2] = obj
.matrix_world
@ Vector((b
[0], b
[1], a
[2]))
1161 vs
[3] = obj
.matrix_world
@ Vector((b
[0], a
[1], a
[2]))
1162 vs
[4] = obj
.matrix_world
@ Vector((a
[0], a
[1], b
[2]))
1163 vs
[5] = obj
.matrix_world
@ Vector((a
[0], b
[1], b
[2]))
1164 vs
[6] = obj
.matrix_world
@ Vector((b
[0], b
[1], b
[2]))
1165 vs
[7] = obj
.matrix_world
@ Vector((b
[0], a
[1], b
[2]))
1167 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1168 (0,4),(1,5),(2,6),(3,7)]
1173 verts
+= [(v0
[0],v0
[1],v0
[2])]
1174 verts
+= [(v1
[0],v1
[1],v1
[2])]
1175 colours
+= [(1,1,0,1),(1,1,0,1)]
1177 elif obj
.cv_data
.classtype
== 'k_classtype_capsule':
1178 h
= obj
.data
.cv_data
.v0
[0]
1179 r
= obj
.data
.cv_data
.v0
[1]
1182 vs
[0] = obj
.matrix_world
@ Vector((0.0,0.0, h
*0.5 ))
1183 vs
[1] = obj
.matrix_world
@ Vector((0.0,0.0,-h
*0.5 ))
1184 vs
[2] = obj
.matrix_world
@ Vector(( r
,0.0, h
*0.5-r
))
1185 vs
[3] = obj
.matrix_world
@ Vector(( -r
,0.0, h
*0.5-r
))
1186 vs
[4] = obj
.matrix_world
@ Vector(( r
,0.0,-h
*0.5+r
))
1187 vs
[5] = obj
.matrix_world
@ Vector(( -r
,0.0,-h
*0.5+r
))
1188 vs
[6] = obj
.matrix_world
@ Vector((0.0, r
, h
*0.5-r
))
1189 vs
[7] = obj
.matrix_world
@ Vector((0.0,-r
, h
*0.5-r
))
1190 vs
[8] = obj
.matrix_world
@ Vector((0.0, r
,-h
*0.5+r
))
1191 vs
[9] = obj
.matrix_world
@ Vector((0.0,-r
,-h
*0.5+r
))
1193 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
1198 verts
+= [(v0
[0],v0
[1],v0
[2])]
1199 verts
+= [(v1
[0],v1
[1],v1
[2])]
1200 colours
+= [(0.5,1,0,1),(0.5,1,0,1)]
1202 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
1204 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
1205 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
1206 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
1207 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
1208 indices
= [(0,1),(1,2),(1,3)]
1212 verts
+= [(v0
[0],v0
[1],v0
[2])]
1213 verts
+= [(v1
[0],v1
[1],v1
[2])]
1214 colours
+= [(0,1,1,1),(0,1,1,1)]
1216 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1218 vs
[0] = obj
.location
1219 vs
[1] = obj
.cv_data
.target
.location
1224 verts
+= [(v0
[0],v0
[1],v0
[2])]
1225 verts
+= [(v1
[0],v1
[1],v1
[2])]
1226 colours
+= [(0,1,1,1),(0,1,1,1)]
1230 stack
[0] = obj
.cv_data
.target
1232 loop_complete
= False
1235 if stack_i
[si
-1] == 2:
1239 if si
== 0: # Loop failed to complete
1244 targets
= [None,None]
1245 targets
[0] = node
.cv_data
.target
1247 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1248 targets
[1] = node
.cv_data
.target1
1250 nextnode
= targets
[stack_i
[si
-1]]
1253 if nextnode
!= None: # branch
1254 if nextnode
== stack
[0]: # Loop completed
1255 loop_complete
= True
1259 for sj
in range(si
):
1260 if stack
[sj
] == nextnode
: # invalidated path
1266 stack
[si
] = nextnode
1271 cc
= Vector((obj
.cv_data
.colour
[0],\
1272 obj
.cv_data
.colour
[1],\
1273 obj
.cv_data
.colour
[2],\
1276 for sj
in range(si
):
1279 if stack
[sj
].cv_data
.classtype
== 'k_classtype_gate' and \
1280 stack
[sk
].cv_data
.classtype
== 'k_classtype_gate':
1281 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
1282 drawsbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
1285 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
1289 elif obj
.cv_data
.classtype
== 'k_classtype_car_path':
1290 v0
= obj
.matrix_world
.to_quaternion() @ Vector((0,1,0))
1291 c0
= Vector((v0
.x
*0.5+0.5, v0
.y
*0.5+0.5, 0.0, 1.0))
1292 drawbhandle( obj
, 1.0, (0.9,0.9,0.9,1.0) )
1294 if obj
.cv_data
.target
!= None:
1295 v1
= obj
.cv_data
.target
.matrix_world
.to_quaternion()@Vector((0,1,0))
1296 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1298 drawbhandle( obj
.cv_data
.target
, -1.0, (0.5,0.5,0.5,1.0) )
1299 drawbpath( obj
, obj
.cv_data
.target
, c0
, c1
)
1301 if obj
.cv_data
.target1
!= None:
1302 v1
= obj
.cv_data
.target1
.matrix_world
.to_quaternion()@Vector((0,1,0))
1303 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1305 drawbhandle( obj
.cv_data
.target1
, -1.0, (0.5,0.5,0.5,1.0) )
1306 drawbpath( obj
, obj
.cv_data
.target1
, c0
, c1
)
1308 lines
= batch_for_shader(\
1309 cv_view_shader
, 'LINES', \
1310 { "pos":verts
, "color":colours
})
1312 lines
.draw( cv_view_shader
)
1314 def cv_poll_target(scene
, obj
):
1315 if obj
== bpy
.context
.active_object
:
1317 if obj
.cv_data
.classtype
== 'k_classtype_none':
1321 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
1322 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1323 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1324 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
1325 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
1327 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1328 uid
: bpy
.props
.IntProperty( name
="" )
1330 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
1331 poll
=cv_poll_target
)
1332 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
1333 poll
=cv_poll_target
)
1335 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1338 classtype
: bpy
.props
.EnumProperty(
1341 ('k_classtype_none', "k_classtype_none", "", 0),
1342 ('k_classtype_gate', "k_classtype_gate", "", 1),
1343 ('k_classtype_block', "k_classtype_block", "", 2),
1344 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1345 ('k_classtype_water', "k_classtype_water", "", 4),
1346 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1347 ('k_classtype_INSTANCE', "","", 6 ),
1348 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1349 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1350 ('k_classtype_route', "k_classtype_route", "", 9 ),
1351 ('k_classtype_bone',"k_classtype_bone","",10),
1352 ('k_classtype_SKELETON', "","", 11 ),
1353 ('k_classtype_SKIN',"","",12)
1356 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
1357 collider
: bpy
.props
.BoolProperty(name
="Collider",default
=False)
1358 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1359 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1361 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
1362 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
1363 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
1365 class CV_BONE_PANEL(bpy
.types
.Panel
):
1366 bl_label
="Bone Config"
1367 bl_idname
="SCENE_PT_cv_bone"
1368 bl_space_type
='PROPERTIES'
1369 bl_region_type
='WINDOW'
1372 def draw(_
,context
):
1373 active_object
= context
.active_object
1374 if active_object
== None: return
1376 bone
= active_object
.data
.bones
.active
1377 if bone
== None: return
1379 _
.layout
.prop( bone
.cv_data
, "collider" )
1380 _
.layout
.prop( bone
.cv_data
, "v0" )
1381 _
.layout
.prop( bone
.cv_data
, "v1" )
1383 _
.layout
.label( text
="Angle Limits" )
1384 _
.layout
.prop( bone
.cv_data
, "con0" )
1385 _
.layout
.prop( bone
.cv_data
, "mins" )
1386 _
.layout
.prop( bone
.cv_data
, "maxs" )
1388 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1389 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1391 class CV_OBJ_PANEL(bpy
.types
.Panel
):
1392 bl_label
="Entity Config"
1393 bl_idname
="SCENE_PT_cv_entity"
1394 bl_space_type
='PROPERTIES'
1395 bl_region_type
='WINDOW'
1398 def draw(_
,context
):
1399 active_object
= bpy
.context
.active_object
1400 if active_object
== None: return
1401 _
.layout
.prop( active_object
.cv_data
, "classtype" )
1403 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1404 _
.layout
.prop( active_object
.cv_data
, "target" )
1406 mesh
= active_object
.data
1407 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1408 _
.layout
.prop( mesh
.cv_data
, "v0" )
1410 elif active_object
.cv_data
.classtype
== 'k_classtype_car_path' or \
1411 active_object
.cv_data
.classtype
== 'k_classtype_route_node':
1412 _
.layout
.prop( active_object
.cv_data
, "target" )
1413 _
.layout
.prop( active_object
.cv_data
, "target1" )
1415 elif active_object
.cv_data
.classtype
== 'k_classtype_route':
1416 _
.layout
.prop( active_object
.cv_data
, "target" )
1417 _
.layout
.prop( active_object
.cv_data
, "colour" )
1419 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1420 mesh
= active_object
.data
1422 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1423 _
.layout
.prop( mesh
.cv_data
, "v0" )
1424 _
.layout
.prop( mesh
.cv_data
, "v1" )
1425 _
.layout
.prop( mesh
.cv_data
, "v2" )
1426 _
.layout
.prop( mesh
.cv_data
, "v3" )
1427 elif active_object
.cv_data
.classtype
== 'k_classtype_capsule':
1428 mesh
= active_object
.data
1429 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1430 _
.layout
.prop( mesh
.cv_data
, "v0" )
1432 class CV_INTERFACE(bpy
.types
.Panel
):
1433 bl_idname
= "VIEW3D_PT_carve"
1435 bl_space_type
= 'VIEW_3D'
1436 bl_region_type
= 'UI'
1437 bl_category
= "Carve"
1439 def draw(_
, context
):
1441 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1442 layout
.operator( "carve.compile_all" )
1445 view_layer
= bpy
.context
.view_layer
1446 for col
in view_layer
.layer_collection
.children
["export"].children
:
1447 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
1448 write_model( col
.name
)
1450 class CV_COMPILE(bpy
.types
.Operator
):
1451 bl_idname
="carve.compile_all"
1452 bl_label
="Compile All"
1454 def execute(_
,context
):
1456 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1457 #for col in bpy.data.collections["export"].children:
1458 # write_model( col.name )
1462 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1463 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
1467 global cv_view_draw_handler
1470 bpy
.utils
.register_class(c
)
1472 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
1473 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
1474 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
1475 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
1477 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1478 cv_draw
,(),'WINDOW','POST_VIEW')
1481 global cv_view_draw_handler
1484 bpy
.utils
.unregister_class(c
)
1486 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')