04fd6c344b69245f172dc139801d24196f5c1b04
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
208 if mat
.name
in material_cache
:
209 return material_cache
[mat
.name
]
211 material_cache
[mat
.name
] = header
.material_count
212 dest
= mdl_material()
213 dest
.pstr_name
= emplace_string( mat
.name
)
214 material_buffer
+= [dest
]
216 header
.material_count
+= 1
217 return material_cache
[mat
.name
]
219 # Create root or empty node and materials
220 # this is to designate id 0 as 'NULL'
222 none_material
= c_uint32(69)
223 none_material
.name
= ""
224 emplace_material( none_material
)
237 root
.pstr_name
= emplace_string('')
238 root
.submesh_start
= 0
239 root
.submesh_count
= 0
242 node_buffer
+= [root
]
246 print( " assigning ids" )
247 collection
= bpy
.data
.collections
[collection_name
]
250 # ==========================================
252 header
.node_count
= 0
255 uid
= header
.node_count
256 header
.node_count
+= 1
259 print( " creating scene graph" )
263 graph
["children"] = []
264 graph
["uid"] = _uid()
265 graph
["parent"] = None
267 graph_lookup
= {} # object can lookup its graph def here
269 for obj
in collection
.all_objects
:
272 def _extend( p
, n
, d
):
276 tree
["children"] = []
282 if n
.type == 'ARMATURE':
283 tree
["bones"] = [None] # None is the root transform
285 tree
["collider_count"] = 0
287 def _extendb( p
, n
, d
):
292 btree
["uid"] = _uid()
293 btree
["children"] = []
296 tree
["bones"] += [n
.name
]
299 _extendb( btree
, c
, d
+1 )
301 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
303 btree
["target"] = c
.subtarget
304 btree
["pole"] = c
.pole_subtarget
305 tree
["ik_count"] += 1
307 if n
.cv_data
.collider
:
308 tree
['collider_count'] += 1
310 btree
['deform'] = n
.use_deform
311 p
['children'] += [btree
]
313 for b
in n
.data
.bones
:
315 _extendb( tree
, b
, d
+1 )
317 for obj1
in n
.children
:
318 _extend( tree
, obj1
, d
+1 )
320 p
["children"] += [tree
]
321 graph_lookup
[n
] = tree
323 _extend( graph
, obj
, 1 )
327 for c
in p
['children']:
329 yield from _graph_iter(c
)
331 it
= _graph_iter(graph
)
333 root
.parent
= 0xffffffff
336 # ==============================================
337 it
= _graph_iter(graph
)
338 print( " compiling data" )
340 if 'obj' in node_def
:
341 obj
= node_def
['obj']
344 elif 'bone' in node_def
:
345 obj
= node_def
['bone']
347 objco
= obj
.head_local
349 depth
= node_def
['depth']
350 uid
= node_def
['uid']
353 node
.co
[0] = objco
[0]
354 node
.co
[1] = objco
[2]
355 node
.co
[2] = -objco
[1]
357 # Convert rotation quat to our space type
358 quat
= obj
.matrix_local
.to_quaternion()
365 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
366 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
367 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
369 node
.s
[0] = obj
.scale
[0]
370 node
.s
[1] = obj
.scale
[2]
371 node
.s
[2] = obj
.scale
[1]
373 node
.pstr_name
= emplace_string( obj
.name
)
375 if node_def
["parent"]:
376 node
.parent
= node_def
["parent"]["uid"]
379 classtype
= 'k_classtype_bone'
380 elif objt
== 'ARMATURE':
381 classtype
= 'k_classtype_skeleton'
383 classtype
= obj
.cv_data
.classtype
386 # =================================================================
389 # Dont use the cache if we have modifiers that affect the normals
397 for mod
in obj
.modifiers
:
398 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP':
399 can_use_cache
= False
401 if mod
.type == 'ARMATURE':
402 classtype
= 'k_classtype_skin'
403 armature_def
= graph_lookup
[mod
.object]
404 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
406 armature_def
['obj'].data
.pose_position
= 'REST'
408 if can_use_cache
and obj
.data
.name
in mesh_cache
:
409 ref
= mesh_cache
[obj
.data
.name
]
410 node
.submesh_start
= ref
.submesh_start
411 node
.submesh_count
= ref
.submesh_count
415 node
.submesh_start
= header
.submesh_count
416 node
.submesh_count
= 0
418 default_mat
= c_uint32(69)
419 default_mat
.name
= ""
421 dgraph
= bpy
.context
.evaluated_depsgraph_get()
422 data
= obj
.evaluated_get(dgraph
).data
423 data
.calc_loop_triangles()
424 data
.calc_normals_split()
426 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
427 for material_id
, mat
in enumerate(mat_list
):
431 sm
.indice_start
= header
.indice_count
432 sm
.vertex_start
= header
.vertex_count
435 sm
.material_id
= emplace_material( mat
)
438 sm
.bbx
[0][i
] = 999999
439 sm
.bbx
[1][i
] = -999999
443 # Write the vertex / indice data
445 for tri_index
, tri
in enumerate(data
.loop_triangles
):
446 if tri
.material_index
!= material_id
:
450 vert
= data
.vertices
[tri
.vertices
[j
]]
452 vi
= data
.loops
[li
].vertex_index
455 norm
= data
.loops
[li
].normal
457 colour
= (255,255,255,255)
462 uv
= data
.uv_layers
.active
.data
[li
].uv
464 if data
.vertex_colors
:
465 colour
= data
.vertex_colors
.active
.data
[li
].color
466 colour
= (int(colour
[0]*255.0),\
467 int(colour
[1]*255.0),\
468 int(colour
[2]*255.0),\
469 int(colour
[3]*255.0))
474 weight_groups
= sorted( data
.vertices
[vi
].groups
, key
= \
475 lambda a
: a
.weight
, reverse
=True )
478 if len(weight_groups
) > ml
:
479 g
= weight_groups
[ml
]
480 name
= obj
.vertex_groups
[g
.group
].name
484 groups
[ml
] = armature_def
['bones'].index(name
)
487 if len(weight_groups
) > 0:
488 inv_norm
= (1.0/tot
) * 65535.0
490 weights
[ml
] = int( weights
[ml
] * inv_norm
)
491 weights
[ml
] = min( weights
[ml
], 65535 )
492 weights
[ml
] = max( weights
[ml
], 0 )
495 m
= float(10**TOLERENCE
)
497 key
= (int(co
[0]*m
+0.5),\
519 indice_buffer
+= [boffa
[key
]]
521 index
= c_uint32(sm
.vertex_count
)
525 indice_buffer
+= [index
]
536 v
.colour
[0] = colour
[0]
537 v
.colour
[1] = colour
[1]
538 v
.colour
[2] = colour
[2]
539 v
.colour
[3] = colour
[3]
540 v
.weights
[0] = weights
[0]
541 v
.weights
[1] = weights
[1]
542 v
.weights
[2] = weights
[2]
543 v
.weights
[3] = weights
[3]
544 v
.groups
[0] = groups
[0]
545 v
.groups
[1] = groups
[1]
546 v
.groups
[2] = groups
[2]
547 v
.groups
[3] = groups
[3]
552 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
553 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
557 if sm
.vertex_count
== 0:
562 submesh_buffer
+= [sm
]
563 node
.submesh_count
+= 1
564 header
.submesh_count
+= 1
565 header
.vertex_count
+= sm
.vertex_count
566 header
.indice_count
+= sm
.indice_count
568 mesh_cache
[obj
.data
.name
] = node
570 # Process entity data
571 # ==================================================================
572 node
.offset
= entdata_length
574 if classtype
!= 'k_classtype_none':
579 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
580 s001
= F
" L {obj.name}"
583 s004
= F
"{node.parent: 3}"
586 if classtype
== 'k_classtype_skin':
587 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
588 s005
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
590 scmp
= F
"{s002:<32} {s003:<22} {s004} {s005}"
593 if classtype
== 'k_classtype_INSTANCE' or \
594 classtype
== 'k_classtype_BONE' or \
595 classtype
== 'k_classtype_SKELETON' or \
596 classtype
== 'k_classtype_SKIN':
597 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
601 elif classtype
== 'k_classtype_skin':
604 armature
= armature_def
['obj']
605 entdata_length
+= sizeof( classtype_skin
)
607 skin
= classtype_skin()
608 skin
.skeleton
= armature
.cv_data
.uid
609 entdata_buffer
+= [skin
]
611 elif classtype
== 'k_classtype_skeleton':
613 entdata_length
+= sizeof( classtype_skeleton
)
614 skeleton
= classtype_skeleton()
616 armature_def
= graph_lookup
[obj
]
618 bones
= armature_def
['bones']
619 skeleton
.channels
= len(bones
)
620 skeleton
.ik_count
= armature_def
["ik_count"]
621 skeleton
.collider_count
= armature_def
["collider_count"]
623 if armature
.animation_data
:
624 previous_frame
= bpy
.context
.scene
.frame_current
625 previous_action
= armature
.animation_data
.action
627 skeleton
.anim_start
= len(anim_buffer
)
628 skeleton
.anim_count
= 0
630 for NLALayer
in obj
.animation_data
.nla_tracks
:
631 for NLAStrip
in NLALayer
.strips
:
633 for a
in bpy
.data
.actions
:
634 if a
.name
== NLAStrip
.name
:
635 armature
.animation_data
.action
= a
638 anim_start
= int(NLAStrip
.action_frame_start
)
639 anim_end
= int(NLAStrip
.action_frame_end
)
642 anim
= mdl_animation()
643 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
645 anim
.offset
= animdata_length
646 anim
.length
= anim_end
-anim_start
648 # Export the fucking keyframes
649 for frame
in range(anim_start
,anim_end
):
650 bpy
.context
.scene
.frame_set(frame
)
652 for bone_name
in bones
:
653 for pb
in armature
.pose
.bones
:
654 if pb
.name
== bone_name
:
655 rb
= armature
.data
.bones
[ bone_name
]
657 loc
, rot
, sca
= pb
.matrix_basis
.decompose()
661 final_pos
= Vector(( vp
[0], vp
[2], -vp
[1] ))
664 lc_m
= pb
.matrix_channel
.to_3x3()
665 if pb
.parent
is not None:
666 smtx
= pb
.parent
.matrix_channel
.to_3x3()
667 lc_m
= smtx
.inverted() @ lc_m
668 rq
= lc_m
.to_quaternion()
671 kf
.co
[0] = final_pos
[0]
672 kf
.co
[1] = final_pos
[1]
673 kf
.co
[2] = final_pos
[2]
685 animdata_buffer
+= [kf
]
686 animdata_length
+= sizeof(mdl_keyframe
)
689 anim_buffer
+= [anim
]
690 skeleton
.anim_count
+= 1
692 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
693 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
695 bpy
.context
.scene
.frame_set( previous_frame
)
696 armature
.animation_data
.action
= previous_action
698 entdata_buffer
+= [skeleton
]
700 elif classtype
== 'k_classtype_bone':
702 entdata_length
+= sizeof( classtype_bone
)
704 bone
= classtype_bone()
705 bone
.deform
= node_def
['deform']
707 if 'target' in node_def
:
708 bone
.ik_target
= armature_def
['bones'].index( node_def
['target'] )
709 bone
.ik_pole
= armature_def
['bones'].index( node_def
['pole'] )
714 bone
.collider
= 1 if obj
.cv_data
.collider
else 0
715 if obj
.cv_data
.collider
:
716 bone
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
717 bone
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
718 bone
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
719 bone
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
720 bone
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
721 bone
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
723 bone
.hitbox
[0][0] = 0.0
724 bone
.hitbox
[0][1] = 0.0
725 bone
.hitbox
[0][2] = 0.0
726 bone
.hitbox
[1][0] = 0.0
727 bone
.hitbox
[1][1] = 0.0
728 bone
.hitbox
[1][2] = 0.0
732 bone
.angle_limits
[0][0] = obj
.cv_data
.mins
[0]
733 bone
.angle_limits
[0][1] = obj
.cv_data
.mins
[2]
734 bone
.angle_limits
[0][2] = -obj
.cv_data
.maxs
[1]
735 bone
.angle_limits
[1][0] = obj
.cv_data
.maxs
[0]
736 bone
.angle_limits
[1][1] = obj
.cv_data
.maxs
[2]
737 bone
.angle_limits
[1][2] = -obj
.cv_data
.mins
[1]
740 bone
.angle_limits
[0][0] = 0.0
741 bone
.angle_limits
[0][1] = 0.0
742 bone
.angle_limits
[0][2] = 0.0
743 bone
.angle_limits
[1][0] = 0.0
744 bone
.angle_limits
[1][1] = 0.0
745 bone
.angle_limits
[1][2] = 0.0
747 bone
.deform
= node_def
['deform']
748 entdata_buffer
+= [bone
]
750 elif classtype
== 'k_classtype_gate':
752 entdata_length
+= sizeof( classtype_gate
)
754 gate
= classtype_gate()
756 if obj
.cv_data
.target
!= None:
757 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
759 if obj
.type == 'MESH':
760 gate
.dims
[0] = obj
.data
.cv_data
.v0
[0]
761 gate
.dims
[1] = obj
.data
.cv_data
.v0
[1]
762 gate
.dims
[2] = obj
.data
.cv_data
.v0
[2]
764 gate
.dims
[0] = obj
.cv_data
.v0
[0]
765 gate
.dims
[1] = obj
.cv_data
.v0
[1]
766 gate
.dims
[2] = obj
.cv_data
.v0
[2]
768 entdata_buffer
+= [gate
]
770 elif classtype
== 'k_classtype_block':
772 entdata_length
+= sizeof( classtype_block
)
774 source
= obj
.data
.cv_data
776 block
= classtype_block()
777 block
.bbx
[0][0] = source
.v0
[0]
778 block
.bbx
[0][1] = source
.v0
[2]
779 block
.bbx
[0][2] = -source
.v1
[1]
781 block
.bbx
[1][0] = source
.v1
[0]
782 block
.bbx
[1][1] = source
.v1
[2]
783 block
.bbx
[1][2] = -source
.v0
[1]
784 entdata_buffer
+= [block
]
786 elif classtype
== 'k_classtype_spawn':
789 elif classtype
== 'k_classtype_water':
792 elif classtype
== 'k_classtype_car_path':
794 entdata_length
+= sizeof( classtype_car_path
)
796 pn
= classtype_car_path()
800 if obj
.cv_data
.target
!= None:
801 pn
.target
= obj
.cv_data
.target
.cv_data
.uid
802 if obj
.cv_data
.target1
!= None:
803 pn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
805 entdata_buffer
+= [pn
]
807 elif obj
.is_instancer
:
808 target
= obj
.instance_collection
811 entdata_length
+= sizeof( classtype_instance
)
813 inst
= classtype_instance()
814 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
815 entdata_buffer
+= [inst
]
817 elif classtype
== 'k_classtype_capsule':
820 elif classtype
== 'k_classtype_route_node':
822 entdata_length
+= sizeof( classtype_route_node
)
824 rn
= classtype_route_node()
825 if obj
.cv_data
.target
!= None:
826 rn
.target
= obj
.cv_data
.target
.cv_data
.uid
827 if obj
.cv_data
.target1
!= None:
828 rn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
830 entdata_buffer
+= [rn
]
832 elif classtype
== 'k_classtype_route':
834 entdata_length
+= sizeof( classtype_route
)
835 r
= classtype_route()
836 r
.pstr_name
= emplace_string("not-implemented")
837 r
.colour
[0] = obj
.cv_data
.colour
[0]
838 r
.colour
[1] = obj
.cv_data
.colour
[1]
839 r
.colour
[2] = obj
.cv_data
.colour
[2]
841 if obj
.cv_data
.target
!= None:
842 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
844 entdata_buffer
+= [r
]
846 # classtype == 'k_classtype_none':
851 node_buffer
+= [node
]
855 header
.anim_count
= len(anim_buffer
)
857 print( "Writing data" )
858 fpos
= sizeof(header
)
860 print( F
"Nodes: {header.node_count}" )
861 header
.node_offset
= fpos
862 fpos
+= sizeof(mdl_node
)*header
.node_count
864 print( F
"Submeshes: {header.submesh_count}" )
865 header
.submesh_offset
= fpos
866 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
868 print( F
"Materials: {header.material_count}" )
869 header
.material_offset
= fpos
870 fpos
+= sizeof(mdl_material
)*header
.material_count
872 print( F
"Animation count: {header.anim_count}" )
873 header
.anim_offset
= fpos
874 fpos
+= sizeof(mdl_animation
)*header
.anim_count
876 print( F
"Entdata length: {entdata_length}" )
877 header
.entdata_offset
= fpos
878 fpos
+= entdata_length
880 print( F
"Vertex count: {header.vertex_count}" )
881 header
.vertex_offset
= fpos
882 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
884 print( F
"Indice count: {header.indice_count}" )
885 header
.indice_offset
= fpos
886 fpos
+= sizeof(c_uint32
)*header
.indice_count
888 print( F
"Keyframe count: {animdata_length}" )
889 header
.animdata_offset
= fpos
890 fpos
+= animdata_length
892 print( F
"Strings length: {len(strings_buffer)}" )
893 header
.strings_offset
= fpos
894 fpos
+= len(strings_buffer
)
896 header
.file_length
= fpos
898 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
899 fp
= open( path
, "wb" )
901 fp
.write( bytearray( header
) )
903 for node
in node_buffer
:
904 fp
.write( bytearray(node
) )
905 for sm
in submesh_buffer
:
906 fp
.write( bytearray(sm
) )
907 for mat
in material_buffer
:
908 fp
.write( bytearray(mat
) )
909 for a
in anim_buffer
:
910 fp
.write( bytearray(a
) )
911 for ed
in entdata_buffer
:
912 fp
.write( bytearray(ed
) )
913 for v
in vertex_buffer
:
914 fp
.write( bytearray(v
) )
915 for i
in indice_buffer
:
916 fp
.write( bytearray(i
) )
917 for kf
in animdata_buffer
:
918 fp
.write( bytearray(kf
) )
920 fp
.write( strings_buffer
)
923 print( F
"Completed {collection_name}.mdl" )
926 # ------------------------------------------------------------------------------
928 cv_view_draw_handler
= None
929 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
932 global cv_view_shader
933 cv_view_shader
.bind()
934 gpu
.state
.depth_mask_set(False)
935 gpu
.state
.line_width_set(2.0)
936 gpu
.state
.face_culling_set('BACK')
937 gpu
.state
.depth_test_set('LESS')
938 gpu
.state
.blend_set('NONE')
943 #def drawbezier(p0,h0,p1,h1,c0,c1):
944 # nonlocal verts, colours
948 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
951 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
954 # for i in range(10):
960 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
961 # verts += [(last[0],last[1],last[2])]
962 # verts += [(p[0],p[1],p[2])]
963 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
968 def drawbhandle(obj
, direction
, colour
):
969 nonlocal verts
, colours
971 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
974 colours
+= [colour
,colour
]
976 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
977 nonlocal verts
, colours
986 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
987 verts
+= [(last
[0],last
[1],last
[2])]
988 verts
+= [(p
[0],p
[1],p
[2])]
989 colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
992 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
993 nonlocal course_count
995 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
997 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
998 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
999 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
1000 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
1001 drawbezier(p0
,h0
,p1
,h1
,c0
,c1
)
1003 def drawbpath(o0
,o1
,c0
,c1
):
1004 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
1006 def drawbline(p0
,p1
,c0
,c1
):
1007 nonlocal verts
, colours
1011 for obj
in bpy
.context
.collection
.objects
:
1012 if obj
.type == 'ARMATURE':
1013 for bone
in obj
.data
.bones
:
1014 if bone
.cv_data
.collider
:
1020 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
1021 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
1022 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
1023 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
1024 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
1025 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
1026 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
1027 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
1029 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1030 (0,4),(1,5),(2,6),(3,7)]
1035 verts
+= [(v0
[0],v0
[1],v0
[2])]
1036 verts
+= [(v1
[0],v1
[1],v1
[2])]
1037 colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
1039 center
=obj
.matrix_world
@c
1041 def _angle_lim( major
, minor
, amin
, amax
, colour
):
1042 nonlocal verts
, colours
1050 a0
= amin
*(1.0-t0
)+amax
*t0
1051 a1
= amin
*(1.0-t1
)+amax
*t1
1053 p0
= c
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
1054 p1
= c
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
1056 p0
=obj
.matrix_world
@ p0
1057 p1
=obj
.matrix_world
@ p1
1059 colours
+= [colour
,colour
]
1063 colours
+= [colour
,colour
]
1066 colours
+= [colour
,colour
]
1068 verts
+= [c
+major
*1.2*f
,c
+major
*f
*0.8]
1069 colours
+= [colour
,colour
]
1071 if bone
.cv_data
.con0
:
1072 _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
1073 bone
.cv_data
.mins
[0], bone
.cv_data
.maxs
[0], \
1075 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1076 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
1078 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1079 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
1083 if obj
.cv_data
.classtype
== 'k_classtype_gate':
1084 if obj
.type == 'MESH':
1085 dims
= obj
.data
.cv_data
.v0
1087 dims
= obj
.cv_data
.v0
1090 c
= Vector((0,0,dims
[2]))
1092 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
1093 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
1094 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
1095 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
1096 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
1097 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
1098 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
1099 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
1100 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
1102 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
1107 verts
+= [(v0
[0],v0
[1],v0
[2])]
1108 verts
+= [(v1
[0],v1
[1],v1
[2])]
1109 colours
+= [(1,1,0,1),(1,1,0,1)]
1111 sw
= (0.4,0.4,0.4,0.2)
1112 if obj
.cv_data
.target
!= None:
1113 drawbline( obj
.location
, obj
.cv_data
.target
.location
, sw
,sw
)
1115 elif obj
.cv_data
.classtype
== 'k_classtype_route_node':
1116 sw
= Vector((0.4,0.4,0.4,0.2))
1117 sw2
= Vector((1.5,0.2,0.2,0.0))
1118 if obj
.cv_data
.target
!= None:
1119 drawbpath( obj
, obj
.cv_data
.target
, sw
, sw
)
1120 if obj
.cv_data
.target1
!= None:
1121 drawbpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
1123 drawbhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
1124 drawbhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
1126 p1
= obj
.location
+ \
1127 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
1128 drawbline( obj
.location
, p1
, sw
,sw2
)
1131 elif obj
.cv_data
.classtype
== 'k_classtype_block':
1132 a
= obj
.data
.cv_data
.v0
1133 b
= obj
.data
.cv_data
.v1
1136 vs
[0] = obj
.matrix_world
@ Vector((a
[0], a
[1], a
[2]))
1137 vs
[1] = obj
.matrix_world
@ Vector((a
[0], b
[1], a
[2]))
1138 vs
[2] = obj
.matrix_world
@ Vector((b
[0], b
[1], a
[2]))
1139 vs
[3] = obj
.matrix_world
@ Vector((b
[0], a
[1], a
[2]))
1140 vs
[4] = obj
.matrix_world
@ Vector((a
[0], a
[1], b
[2]))
1141 vs
[5] = obj
.matrix_world
@ Vector((a
[0], b
[1], b
[2]))
1142 vs
[6] = obj
.matrix_world
@ Vector((b
[0], b
[1], b
[2]))
1143 vs
[7] = obj
.matrix_world
@ Vector((b
[0], a
[1], b
[2]))
1145 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1146 (0,4),(1,5),(2,6),(3,7)]
1151 verts
+= [(v0
[0],v0
[1],v0
[2])]
1152 verts
+= [(v1
[0],v1
[1],v1
[2])]
1153 colours
+= [(1,1,0,1),(1,1,0,1)]
1155 elif obj
.cv_data
.classtype
== 'k_classtype_capsule':
1156 h
= obj
.data
.cv_data
.v0
[0]
1157 r
= obj
.data
.cv_data
.v0
[1]
1160 vs
[0] = obj
.matrix_world
@ Vector((0.0,0.0, h
*0.5 ))
1161 vs
[1] = obj
.matrix_world
@ Vector((0.0,0.0,-h
*0.5 ))
1162 vs
[2] = obj
.matrix_world
@ Vector(( r
,0.0, h
*0.5-r
))
1163 vs
[3] = obj
.matrix_world
@ Vector(( -r
,0.0, h
*0.5-r
))
1164 vs
[4] = obj
.matrix_world
@ Vector(( r
,0.0,-h
*0.5+r
))
1165 vs
[5] = obj
.matrix_world
@ Vector(( -r
,0.0,-h
*0.5+r
))
1166 vs
[6] = obj
.matrix_world
@ Vector((0.0, r
, h
*0.5-r
))
1167 vs
[7] = obj
.matrix_world
@ Vector((0.0,-r
, h
*0.5-r
))
1168 vs
[8] = obj
.matrix_world
@ Vector((0.0, r
,-h
*0.5+r
))
1169 vs
[9] = obj
.matrix_world
@ Vector((0.0,-r
,-h
*0.5+r
))
1171 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
1176 verts
+= [(v0
[0],v0
[1],v0
[2])]
1177 verts
+= [(v1
[0],v1
[1],v1
[2])]
1178 colours
+= [(0.5,1,0,1),(0.5,1,0,1)]
1180 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
1182 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
1183 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
1184 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
1185 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
1186 indices
= [(0,1),(1,2),(1,3)]
1190 verts
+= [(v0
[0],v0
[1],v0
[2])]
1191 verts
+= [(v1
[0],v1
[1],v1
[2])]
1192 colours
+= [(0,1,1,1),(0,1,1,1)]
1194 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1196 vs
[0] = obj
.location
1197 vs
[1] = obj
.cv_data
.target
.location
1202 verts
+= [(v0
[0],v0
[1],v0
[2])]
1203 verts
+= [(v1
[0],v1
[1],v1
[2])]
1204 colours
+= [(0,1,1,1),(0,1,1,1)]
1208 stack
[0] = obj
.cv_data
.target
1210 loop_complete
= False
1213 if stack_i
[si
-1] == 2:
1217 if si
== 0: # Loop failed to complete
1222 targets
= [None,None]
1223 targets
[0] = node
.cv_data
.target
1225 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1226 targets
[1] = node
.cv_data
.target1
1228 nextnode
= targets
[stack_i
[si
-1]]
1231 if nextnode
!= None: # branch
1232 if nextnode
== stack
[0]: # Loop completed
1233 loop_complete
= True
1237 for sj
in range(si
):
1238 if stack
[sj
] == nextnode
: # invalidated path
1244 stack
[si
] = nextnode
1249 cc
= Vector((obj
.cv_data
.colour
[0],\
1250 obj
.cv_data
.colour
[1],\
1251 obj
.cv_data
.colour
[2],\
1254 for sj
in range(si
):
1257 if stack
[sj
].cv_data
.classtype
== 'k_classtype_gate' and \
1258 stack
[sk
].cv_data
.classtype
== 'k_classtype_gate':
1259 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
1260 drawsbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
1263 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
1267 elif obj
.cv_data
.classtype
== 'k_classtype_car_path':
1268 v0
= obj
.matrix_world
.to_quaternion() @ Vector((0,1,0))
1269 c0
= Vector((v0
.x
*0.5+0.5, v0
.y
*0.5+0.5, 0.0, 1.0))
1270 drawbhandle( obj
, 1.0, (0.9,0.9,0.9,1.0) )
1272 if obj
.cv_data
.target
!= None:
1273 v1
= obj
.cv_data
.target
.matrix_world
.to_quaternion()@Vector((0,1,0))
1274 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1276 drawbhandle( obj
.cv_data
.target
, -1.0, (0.5,0.5,0.5,1.0) )
1277 drawbpath( obj
, obj
.cv_data
.target
, c0
, c1
)
1279 if obj
.cv_data
.target1
!= None:
1280 v1
= obj
.cv_data
.target1
.matrix_world
.to_quaternion()@Vector((0,1,0))
1281 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1283 drawbhandle( obj
.cv_data
.target1
, -1.0, (0.5,0.5,0.5,1.0) )
1284 drawbpath( obj
, obj
.cv_data
.target1
, c0
, c1
)
1286 lines
= batch_for_shader(\
1287 cv_view_shader
, 'LINES', \
1288 { "pos":verts
, "color":colours
})
1290 lines
.draw( cv_view_shader
)
1292 def cv_poll_target(scene
, obj
):
1293 if obj
== bpy
.context
.active_object
:
1295 if obj
.cv_data
.classtype
== 'k_classtype_none':
1299 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
1300 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1301 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1302 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
1303 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
1305 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1306 uid
: bpy
.props
.IntProperty( name
="" )
1308 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
1309 poll
=cv_poll_target
)
1310 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
1311 poll
=cv_poll_target
)
1313 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1316 classtype
: bpy
.props
.EnumProperty(
1319 ('k_classtype_none', "k_classtype_none", "", 0),
1320 ('k_classtype_gate', "k_classtype_gate", "", 1),
1321 ('k_classtype_block', "k_classtype_block", "", 2),
1322 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1323 ('k_classtype_water', "k_classtype_water", "", 4),
1324 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1325 ('k_classtype_INSTANCE', "","", 6 ),
1326 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1327 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1328 ('k_classtype_route', "k_classtype_route", "", 9 ),
1329 ('k_classtype_bone',"k_classtype_bone","",10),
1330 ('k_classtype_SKELETON', "","", 11 ),
1331 ('k_classtype_SKIN',"","",12)
1334 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
1335 collider
: bpy
.props
.BoolProperty(name
="Collider",default
=False)
1336 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1337 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1339 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
1340 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
1342 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
1343 c0
: bpy
.props
.FloatVectorProperty(name
="dir",size
=3)
1344 s0
: bpy
.props
.FloatVectorProperty(name
="limits",size
=3)
1346 con1
: bpy
.props
.BoolProperty(name
="Constriant 1",default
=False)
1347 c1
: bpy
.props
.FloatVectorProperty(name
="dir",size
=3)
1348 s1
: bpy
.props
.FloatVectorProperty(name
="limits",size
=3)
1350 class CV_BONE_PANEL(bpy
.types
.Panel
):
1351 bl_label
="Bone Config"
1352 bl_idname
="SCENE_PT_cv_bone"
1353 bl_space_type
='PROPERTIES'
1354 bl_region_type
='WINDOW'
1357 def draw(_
,context
):
1358 active_object
= context
.active_object
1359 if active_object
== None: return
1361 bone
= active_object
.data
.bones
.active
1362 if bone
== None: return
1364 _
.layout
.prop( bone
.cv_data
, "collider" )
1365 _
.layout
.prop( bone
.cv_data
, "v0" )
1366 _
.layout
.prop( bone
.cv_data
, "v1" )
1368 _
.layout
.label( text
="Angle Limits" )
1369 _
.layout
.prop( bone
.cv_data
, "mins" )
1370 _
.layout
.prop( bone
.cv_data
, "maxs" )
1372 _
.layout
.prop( bone
.cv_data
, "con0" )
1373 _
.layout
.prop( bone
.cv_data
, "c0" )
1374 _
.layout
.prop( bone
.cv_data
, "s0" )
1376 _
.layout
.prop( bone
.cv_data
, "con1" )
1377 _
.layout
.prop( bone
.cv_data
, "c1" )
1378 _
.layout
.prop( bone
.cv_data
, "s1" )
1380 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1381 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1383 class CV_OBJ_PANEL(bpy
.types
.Panel
):
1384 bl_label
="Entity Config"
1385 bl_idname
="SCENE_PT_cv_entity"
1386 bl_space_type
='PROPERTIES'
1387 bl_region_type
='WINDOW'
1390 def draw(_
,context
):
1391 active_object
= bpy
.context
.active_object
1392 if active_object
== None: return
1393 _
.layout
.prop( active_object
.cv_data
, "classtype" )
1395 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1396 _
.layout
.prop( active_object
.cv_data
, "target" )
1398 mesh
= active_object
.data
1399 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1400 _
.layout
.prop( mesh
.cv_data
, "v0" )
1402 elif active_object
.cv_data
.classtype
== 'k_classtype_car_path' or \
1403 active_object
.cv_data
.classtype
== 'k_classtype_route_node':
1404 _
.layout
.prop( active_object
.cv_data
, "target" )
1405 _
.layout
.prop( active_object
.cv_data
, "target1" )
1407 elif active_object
.cv_data
.classtype
== 'k_classtype_route':
1408 _
.layout
.prop( active_object
.cv_data
, "target" )
1409 _
.layout
.prop( active_object
.cv_data
, "colour" )
1411 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1412 mesh
= active_object
.data
1414 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1415 _
.layout
.prop( mesh
.cv_data
, "v0" )
1416 _
.layout
.prop( mesh
.cv_data
, "v1" )
1417 _
.layout
.prop( mesh
.cv_data
, "v2" )
1418 _
.layout
.prop( mesh
.cv_data
, "v3" )
1419 elif active_object
.cv_data
.classtype
== 'k_classtype_capsule':
1420 mesh
= active_object
.data
1421 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1422 _
.layout
.prop( mesh
.cv_data
, "v0" )
1424 class CV_INTERFACE(bpy
.types
.Panel
):
1425 bl_idname
= "VIEW3D_PT_carve"
1427 bl_space_type
= 'VIEW_3D'
1428 bl_region_type
= 'UI'
1429 bl_category
= "Carve"
1431 def draw(_
, context
):
1433 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1434 layout
.operator( "carve.compile_all" )
1437 view_layer
= bpy
.context
.view_layer
1438 for col
in view_layer
.layer_collection
.children
["export"].children
:
1439 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
1440 write_model( col
.name
)
1442 class CV_COMPILE(bpy
.types
.Operator
):
1443 bl_idname
="carve.compile_all"
1444 bl_label
="Compile All"
1446 def execute(_
,context
):
1448 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1449 #for col in bpy.data.collections["export"].children:
1450 # write_model( col.name )
1454 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1455 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
1459 global cv_view_draw_handler
1462 bpy
.utils
.register_class(c
)
1464 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
1465 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
1466 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
1467 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
1469 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1470 cv_draw
,(),'WINDOW','POST_VIEW')
1473 global cv_view_draw_handler
1476 bpy
.utils
.unregister_class(c
)
1478 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')