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_
= [("id_start",c_uint32
),
136 ("colour",c_float
*3)]
138 class classtype_skin(Structure
):
140 _fields_
= [("skeleton",c_uint32
)]
142 class classtype_skeleton(Structure
):
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
)]
150 class classtype_bone(Structure
):
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)]
161 # ==============================================================================
163 def write_model(collection_name
):
164 print( F
"Model graph | Create mode '{collection_name}'" )
166 header
= mdl_header()
167 header
.identifier
= 0xABCD0000
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
194 def emplace_string( s
):
195 nonlocal string_cache
, strings_buffer
197 if s
in string_cache
:
198 return string_cache
[s
]
200 string_cache
[s
] = len( strings_buffer
)
201 strings_buffer
+= (s
+'\0').encode('utf-8')
202 return string_cache
[s
]
204 def emplace_material( mat
):
205 nonlocal material_cache
, material_buffer
210 if mat
.name
in material_cache
:
211 return material_cache
[mat
.name
]
213 material_cache
[mat
.name
] = header
.material_count
214 dest
= mdl_material()
215 dest
.pstr_name
= emplace_string( mat
.name
)
216 material_buffer
+= [dest
]
218 header
.material_count
+= 1
219 return material_cache
[mat
.name
]
221 # Create root or empty node and materials
222 # this is to designate id 0 as 'NULL'
224 none_material
= c_uint32(69)
225 none_material
.name
= ""
226 emplace_material( none_material
)
239 root
.pstr_name
= emplace_string('')
240 root
.submesh_start
= 0
241 root
.submesh_count
= 0
244 node_buffer
+= [root
]
248 print( " assigning ids" )
249 collection
= bpy
.data
.collections
[collection_name
]
252 # ==========================================
254 header
.node_count
= 0
257 uid
= header
.node_count
258 header
.node_count
+= 1
261 print( " creating scene graph" )
265 graph
["children"] = []
266 graph
["uid"] = _uid()
267 graph
["parent"] = None
269 graph_lookup
= {} # object can lookup its graph def here
271 for obj
in collection
.all_objects
:
274 def _extend( p
, n
, d
):
278 tree
["children"] = []
284 if n
.type == 'ARMATURE':
285 tree
["bones"] = [None] # None is the root transform
287 tree
["collider_count"] = 0
289 def _extendb( p
, n
, d
):
294 btree
["uid"] = _uid()
295 btree
["children"] = []
298 tree
["bones"] += [n
.name
]
301 _extendb( btree
, c
, d
+1 )
303 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
305 btree
["target"] = c
.subtarget
306 btree
["pole"] = c
.pole_subtarget
307 tree
["ik_count"] += 1
309 if n
.cv_data
.collider
:
310 tree
['collider_count'] += 1
312 btree
['deform'] = n
.use_deform
313 p
['children'] += [btree
]
315 for b
in n
.data
.bones
:
317 _extendb( tree
, b
, d
+1 )
319 for obj1
in n
.children
:
320 _extend( tree
, obj1
, d
+1 )
322 p
["children"] += [tree
]
323 graph_lookup
[n
] = tree
325 _extend( graph
, obj
, 1 )
329 for c
in p
['children']:
331 yield from _graph_iter(c
)
333 it
= _graph_iter(graph
)
335 root
.parent
= 0xffffffff
338 # ==============================================
339 it
= _graph_iter(graph
)
340 print( " compiling data" )
342 if 'obj' in node_def
:
343 obj
= node_def
['obj']
346 elif 'bone' in node_def
:
347 obj
= node_def
['bone']
349 objco
= obj
.head_local
351 depth
= node_def
['depth']
352 uid
= node_def
['uid']
355 node
.co
[0] = objco
[0]
356 node
.co
[1] = objco
[2]
357 node
.co
[2] = -objco
[1]
359 # Convert rotation quat to our space type
360 quat
= obj
.matrix_local
.to_quaternion()
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]
371 node
.s
[0] = obj
.scale
[0]
372 node
.s
[1] = obj
.scale
[2]
373 node
.s
[2] = obj
.scale
[1]
375 node
.pstr_name
= emplace_string( obj
.name
)
377 if node_def
["parent"]:
378 node
.parent
= node_def
["parent"]["uid"]
381 classtype
= 'k_classtype_bone'
382 elif objt
== 'ARMATURE':
383 classtype
= 'k_classtype_skeleton'
385 classtype
= obj
.cv_data
.classtype
388 # =================================================================
391 # Dont use the cache if we have modifiers that affect the normals
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 \
403 can_use_cache
= False
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
410 armature_def
['obj'].data
.pose_position
= 'REST'
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
419 node
.submesh_start
= header
.submesh_count
420 node
.submesh_count
= 0
422 default_mat
= c_uint32(69)
423 default_mat
.name
= ""
425 dgraph
= bpy
.context
.evaluated_depsgraph_get()
426 data
= obj
.evaluated_get(dgraph
).data
427 data
.calc_loop_triangles()
428 data
.calc_normals_split()
430 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
431 for material_id
, mat
in enumerate(mat_list
):
435 sm
.indice_start
= header
.indice_count
436 sm
.vertex_start
= header
.vertex_count
439 sm
.material_id
= emplace_material( mat
)
442 sm
.bbx
[0][i
] = 999999
443 sm
.bbx
[1][i
] = -999999
447 # Write the vertex / indice data
449 for tri_index
, tri
in enumerate(data
.loop_triangles
):
450 if tri
.material_index
!= material_id
:
454 vert
= data
.vertices
[tri
.vertices
[j
]]
456 vi
= data
.loops
[li
].vertex_index
459 norm
= data
.loops
[li
].normal
461 colour
= (255,255,255,255)
466 uv
= data
.uv_layers
.active
.data
[li
].uv
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))
478 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
479 if obj
.vertex_groups
[_
.group
].name
in \
480 armature_def
['bones']]
482 weight_groups
= sorted( src_groups
, key
= \
483 lambda a
: a
.weight
, reverse
=True )
486 if len(weight_groups
) > ml
:
487 g
= weight_groups
[ml
]
488 name
= obj
.vertex_groups
[g
.group
].name
492 groups
[ml
] = armature_def
['bones'].index(name
)
495 if len(weight_groups
) > 0:
496 inv_norm
= (1.0/tot
) * 65535.0
498 weights
[ml
] = int( weights
[ml
] * inv_norm
)
499 weights
[ml
] = min( weights
[ml
], 65535 )
500 weights
[ml
] = max( weights
[ml
], 0 )
503 m
= float(10**TOLERENCE
)
505 key
= (int(co
[0]*m
+0.5),\
527 indice_buffer
+= [boffa
[key
]]
529 index
= c_uint32(sm
.vertex_count
)
533 indice_buffer
+= [index
]
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]
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
] )
565 if sm
.vertex_count
== 0:
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
576 mesh_cache
[obj
.data
.name
] = node
578 # Process entity data
579 # ==================================================================
580 node
.offset
= entdata_length
582 if classtype
!= 'k_classtype_none':
587 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
588 s001
= F
" L {obj.name}"
591 s004
= F
"{node.parent: 3}"
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}]"
598 scmp
= F
"{s002:<32} {s003:<22} {s004} {s005}"
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" )
609 elif classtype
== 'k_classtype_skin':
612 armature
= armature_def
['obj']
613 entdata_length
+= sizeof( classtype_skin
)
615 skin
= classtype_skin()
616 skin
.skeleton
= armature
.cv_data
.uid
617 entdata_buffer
+= [skin
]
619 elif classtype
== 'k_classtype_skeleton':
621 entdata_length
+= sizeof( classtype_skeleton
)
622 skeleton
= classtype_skeleton()
624 armature_def
= graph_lookup
[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"]
631 if armature
.animation_data
:
632 previous_frame
= bpy
.context
.scene
.frame_current
633 previous_action
= armature
.animation_data
.action
635 skeleton
.anim_start
= len(anim_buffer
)
636 skeleton
.anim_count
= 0
638 for NLALayer
in obj
.animation_data
.nla_tracks
:
639 for NLAStrip
in NLALayer
.strips
:
641 for a
in bpy
.data
.actions
:
642 if a
.name
== NLAStrip
.name
:
643 armature
.animation_data
.action
= a
646 anim_start
= int(NLAStrip
.action_frame_start
)
647 anim_end
= int(NLAStrip
.action_frame_end
)
650 anim
= mdl_animation()
651 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
653 anim
.offset
= animdata_length
654 anim
.length
= anim_end
-anim_start
656 # Export the fucking keyframes
657 for frame
in range(anim_start
,anim_end
):
658 bpy
.context
.scene
.frame_set(frame
)
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
]
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() @ \
671 inv_parent
= pb
.parent
.matrix
@ offset_mtx
672 inv_parent
.invert_safe()
673 fpm
= inv_parent
@ pb
.matrix
675 bone_mtx
= rb
.matrix
.to_4x4()
676 local_inv
= rb
.matrix_local
.inverted_safe()
677 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
679 loc
, rot
, sca
= fpm
.decompose()
682 final_pos
= Vector(( loc
[0], loc
[2], -loc
[1] ))
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()
692 kf
.co
[0] = final_pos
[0]
693 kf
.co
[1] = final_pos
[1]
694 kf
.co
[2] = final_pos
[2]
706 animdata_buffer
+= [kf
]
707 animdata_length
+= sizeof(mdl_keyframe
)
710 anim_buffer
+= [anim
]
711 skeleton
.anim_count
+= 1
713 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
714 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
716 bpy
.context
.scene
.frame_set( previous_frame
)
717 armature
.animation_data
.action
= previous_action
719 entdata_buffer
+= [skeleton
]
721 elif classtype
== 'k_classtype_bone':
723 entdata_length
+= sizeof( classtype_bone
)
725 bone
= classtype_bone()
726 bone
.deform
= node_def
['deform']
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'] )
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]
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
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]
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
768 bone
.deform
= node_def
['deform']
769 entdata_buffer
+= [bone
]
771 elif classtype
== 'k_classtype_gate':
773 entdata_length
+= sizeof( classtype_gate
)
775 gate
= classtype_gate()
777 if obj
.cv_data
.target
!= None:
778 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
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]
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]
789 entdata_buffer
+= [gate
]
791 elif classtype
== 'k_classtype_block':
793 entdata_length
+= sizeof( classtype_block
)
795 source
= obj
.data
.cv_data
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]
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
]
807 elif classtype
== 'k_classtype_spawn':
810 elif classtype
== 'k_classtype_water':
813 elif classtype
== 'k_classtype_car_path':
815 entdata_length
+= sizeof( classtype_car_path
)
817 pn
= classtype_car_path()
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
826 entdata_buffer
+= [pn
]
828 elif obj
.is_instancer
:
829 target
= obj
.instance_collection
832 entdata_length
+= sizeof( classtype_instance
)
834 inst
= classtype_instance()
835 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
836 entdata_buffer
+= [inst
]
838 elif classtype
== 'k_classtype_capsule':
841 elif classtype
== 'k_classtype_route_node':
843 entdata_length
+= sizeof( classtype_route_node
)
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
851 entdata_buffer
+= [rn
]
853 elif classtype
== 'k_classtype_route':
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]
861 if obj
.cv_data
.target
!= None:
862 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
864 entdata_buffer
+= [r
]
866 # classtype == 'k_classtype_none':
871 node_buffer
+= [node
]
875 header
.anim_count
= len(anim_buffer
)
877 print( "Writing data" )
878 fpos
= sizeof(header
)
880 print( F
"Nodes: {header.node_count}" )
881 header
.node_offset
= fpos
882 fpos
+= sizeof(mdl_node
)*header
.node_count
884 print( F
"Submeshes: {header.submesh_count}" )
885 header
.submesh_offset
= fpos
886 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
888 print( F
"Materials: {header.material_count}" )
889 header
.material_offset
= fpos
890 fpos
+= sizeof(mdl_material
)*header
.material_count
892 print( F
"Animation count: {header.anim_count}" )
893 header
.anim_offset
= fpos
894 fpos
+= sizeof(mdl_animation
)*header
.anim_count
896 print( F
"Entdata length: {entdata_length}" )
897 header
.entdata_offset
= fpos
898 fpos
+= entdata_length
900 print( F
"Vertex count: {header.vertex_count}" )
901 header
.vertex_offset
= fpos
902 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
904 print( F
"Indice count: {header.indice_count}" )
905 header
.indice_offset
= fpos
906 fpos
+= sizeof(c_uint32
)*header
.indice_count
908 print( F
"Keyframe count: {animdata_length}" )
909 header
.animdata_offset
= fpos
910 fpos
+= animdata_length
912 print( F
"Strings length: {len(strings_buffer)}" )
913 header
.strings_offset
= fpos
914 fpos
+= len(strings_buffer
)
916 header
.file_length
= fpos
918 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
919 fp
= open( path
, "wb" )
921 fp
.write( bytearray( header
) )
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
) )
940 fp
.write( strings_buffer
)
943 print( F
"Completed {collection_name}.mdl" )
946 # ------------------------------------------------------------------------------
948 cv_view_draw_handler
= None
949 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
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')
963 #def drawbezier(p0,h0,p1,h1,c0,c1):
964 # nonlocal verts, colours
968 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
971 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
974 # for i in range(10):
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)]
988 def drawbhandle(obj
, direction
, colour
):
989 nonlocal verts
, colours
991 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
994 colours
+= [colour
,colour
]
996 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
997 nonlocal verts
, colours
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
)]
1012 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
1013 nonlocal course_count
1015 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
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
)
1023 def drawbpath(o0
,o1
,c0
,c1
):
1024 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
1026 def drawbline(p0
,p1
,c0
,c1
):
1027 nonlocal verts
, colours
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':
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]))
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)]
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)]
1059 center
=obj
.matrix_world
@c
1061 def _angle_lim( major
, minor
, amin
, amax
, colour
):
1062 nonlocal verts
, colours
1070 a0
= amin
*(1.0-t0
)+amax
*t0
1071 a1
= amin
*(1.0-t1
)+amax
*t1
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
)
1076 p0
=obj
.matrix_world
@ p0
1077 p1
=obj
.matrix_world
@ p1
1079 colours
+= [colour
,colour
]
1083 colours
+= [colour
,colour
]
1086 colours
+= [colour
,colour
]
1088 verts
+= [c
+major
*1.2*f
,c
+major
*f
*0.8]
1089 colours
+= [colour
,colour
]
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], \
1095 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1096 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
1098 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1099 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
1103 if obj
.cv_data
.classtype
== 'k_classtype_gate':
1104 if obj
.type == 'MESH':
1105 dims
= obj
.data
.cv_data
.v0
1107 dims
= obj
.cv_data
.v0
1110 c
= Vector((0,0,dims
[2]))
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)))
1122 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
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)]
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
)
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
)
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) )
1146 p1
= obj
.location
+ \
1147 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
1148 drawbline( obj
.location
, p1
, sw
,sw2
)
1151 elif obj
.cv_data
.classtype
== 'k_classtype_block':
1152 a
= obj
.data
.cv_data
.v0
1153 b
= obj
.data
.cv_data
.v1
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]))
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)]
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)]
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]
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
))
1191 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
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)]
1200 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
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)]
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)]
1214 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1216 vs
[0] = obj
.location
1217 vs
[1] = obj
.cv_data
.target
.location
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)]
1228 stack
[0] = obj
.cv_data
.target
1230 loop_complete
= False
1233 if stack_i
[si
-1] == 2:
1237 if si
== 0: # Loop failed to complete
1242 targets
= [None,None]
1243 targets
[0] = node
.cv_data
.target
1245 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1246 targets
[1] = node
.cv_data
.target1
1248 nextnode
= targets
[stack_i
[si
-1]]
1251 if nextnode
!= None: # branch
1252 if nextnode
== stack
[0]: # Loop completed
1253 loop_complete
= True
1257 for sj
in range(si
):
1258 if stack
[sj
] == nextnode
: # invalidated path
1264 stack
[si
] = nextnode
1269 cc
= Vector((obj
.cv_data
.colour
[0],\
1270 obj
.cv_data
.colour
[1],\
1271 obj
.cv_data
.colour
[2],\
1274 for sj
in range(si
):
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
)
1283 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
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) )
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))
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
)
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))
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
)
1306 lines
= batch_for_shader(\
1307 cv_view_shader
, 'LINES', \
1308 { "pos":verts
, "color":colours
})
1310 lines
.draw( cv_view_shader
)
1312 def cv_poll_target(scene
, obj
):
1313 if obj
== bpy
.context
.active_object
:
1315 if obj
.cv_data
.classtype
== 'k_classtype_none':
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)
1325 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1326 uid
: bpy
.props
.IntProperty( name
="" )
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
)
1333 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1336 classtype
: bpy
.props
.EnumProperty(
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)
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)
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)
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'
1370 def draw(_
,context
):
1371 active_object
= context
.active_object
1372 if active_object
== None: return
1374 bone
= active_object
.data
.bones
.active
1375 if bone
== None: return
1377 _
.layout
.prop( bone
.cv_data
, "collider" )
1378 _
.layout
.prop( bone
.cv_data
, "v0" )
1379 _
.layout
.prop( bone
.cv_data
, "v1" )
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" )
1386 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1387 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
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'
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" )
1401 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1402 _
.layout
.prop( active_object
.cv_data
, "target" )
1404 mesh
= active_object
.data
1405 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1406 _
.layout
.prop( mesh
.cv_data
, "v0" )
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" )
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" )
1417 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1418 mesh
= active_object
.data
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" )
1430 class CV_INTERFACE(bpy
.types
.Panel
):
1431 bl_idname
= "VIEW3D_PT_carve"
1433 bl_space_type
= 'VIEW_3D'
1434 bl_region_type
= 'UI'
1435 bl_category
= "Carve"
1437 def draw(_
, context
):
1439 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1440 layout
.operator( "carve.compile_all" )
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
)
1448 class CV_COMPILE(bpy
.types
.Operator
):
1449 bl_idname
="carve.compile_all"
1450 bl_label
="Compile All"
1452 def execute(_
,context
):
1454 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1455 #for col in bpy.data.collections["export"].children:
1456 # write_model( col.name )
1460 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1461 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
1465 global cv_view_draw_handler
1468 bpy
.utils
.register_class(c
)
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
)
1475 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1476 cv_draw
,(),'WINDOW','POST_VIEW')
1479 global cv_view_draw_handler
1482 bpy
.utils
.unregister_class(c
)
1484 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')