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 ("hitbox",(c_float
*3)*2)]
160 # ==============================================================================
162 def write_model(collection_name
):
163 print( F
"Model graph | Create mode '{collection_name}'" )
165 header
= mdl_header()
166 header
.identifier
= 0xABCD0000
168 header
.vertex_count
= 0
169 header
.indice_count
= 0
170 header
.submesh_count
= 0
171 header
.node_count
= 0
172 header
.material_count
= 0
173 header
.file_length
= 0
193 def emplace_string( s
):
194 nonlocal string_cache
, strings_buffer
196 if s
in string_cache
:
197 return string_cache
[s
]
199 string_cache
[s
] = len( strings_buffer
)
200 strings_buffer
+= (s
+'\0').encode('utf-8')
201 return string_cache
[s
]
203 def emplace_material( mat
):
204 nonlocal material_cache
, material_buffer
206 if mat
.name
in material_cache
:
207 return material_cache
[mat
.name
]
209 material_cache
[mat
.name
] = header
.material_count
210 dest
= mdl_material()
211 dest
.pstr_name
= emplace_string( mat
.name
)
212 material_buffer
+= [dest
]
214 header
.material_count
+= 1
215 return material_cache
[mat
.name
]
217 # Create root or empty node and materials
218 # this is to designate id 0 as 'NULL'
220 none_material
= c_uint32(69)
221 none_material
.name
= ""
222 emplace_material( none_material
)
235 root
.pstr_name
= emplace_string('')
236 root
.submesh_start
= 0
237 root
.submesh_count
= 0
240 node_buffer
+= [root
]
244 print( " assigning ids" )
245 collection
= bpy
.data
.collections
[collection_name
]
248 # ==========================================
250 header
.node_count
= 0
253 uid
= header
.node_count
254 header
.node_count
+= 1
257 print( " creating scene graph" )
261 graph
["children"] = []
262 graph
["uid"] = _uid()
263 graph
["parent"] = None
265 graph_lookup
= {} # object can lookup its graph def here
267 for obj
in collection
.all_objects
:
270 def _extend( p
, n
, d
):
274 tree
["children"] = []
280 if n
.type == 'ARMATURE':
281 tree
["bones"] = [None] # None is the root transform
283 tree
["collider_count"] = 0
285 def _extendb( p
, n
, d
):
290 btree
["uid"] = _uid()
291 btree
["children"] = []
294 tree
["bones"] += [n
.name
]
297 _extendb( btree
, c
, d
+1 )
299 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
301 btree
["target"] = c
.subtarget
302 btree
["pole"] = c
.pole_subtarget
303 tree
["ik_count"] += 1
305 if n
.cv_data
.collider
:
306 tree
['collider_count'] += 1
308 btree
['deform'] = n
.use_deform
309 p
['children'] += [btree
]
311 for b
in n
.data
.bones
:
313 _extendb( tree
, b
, d
+1 )
315 for obj1
in n
.children
:
316 _extend( tree
, obj1
, d
+1 )
318 p
["children"] += [tree
]
319 graph_lookup
[n
] = tree
321 _extend( graph
, obj
, 1 )
325 for c
in p
['children']:
327 yield from _graph_iter(c
)
329 it
= _graph_iter(graph
)
331 root
.parent
= 0xffffffff
334 # ==============================================
335 it
= _graph_iter(graph
)
336 print( " compiling data" )
338 if 'obj' in node_def
:
339 obj
= node_def
['obj']
342 elif 'bone' in node_def
:
343 obj
= node_def
['bone']
345 objco
= obj
.head_local
347 depth
= node_def
['depth']
348 uid
= node_def
['uid']
351 node
.co
[0] = objco
[0]
352 node
.co
[1] = objco
[2]
353 node
.co
[2] = -objco
[1]
355 # Convert rotation quat to our space type
356 quat
= obj
.matrix_local
.to_quaternion()
363 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
364 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
365 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
367 node
.s
[0] = obj
.scale
[0]
368 node
.s
[1] = obj
.scale
[2]
369 node
.s
[2] = obj
.scale
[1]
371 node
.pstr_name
= emplace_string( obj
.name
)
373 if node_def
["parent"]:
374 node
.parent
= node_def
["parent"]["uid"]
377 classtype
= 'k_classtype_bone'
378 elif objt
== 'ARMATURE':
379 classtype
= 'k_classtype_skeleton'
381 classtype
= obj
.cv_data
.classtype
384 # =================================================================
387 # Dont use the cache if we have modifiers that affect the normals
395 for mod
in obj
.modifiers
:
396 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP':
397 can_use_cache
= False
399 if mod
.type == 'ARMATURE':
400 classtype
= 'k_classtype_skin'
401 armature_def
= graph_lookup
[mod
.object]
402 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
404 armature_def
['obj'].data
.pose_position
= 'REST'
406 if can_use_cache
and obj
.data
.name
in mesh_cache
:
407 ref
= mesh_cache
[obj
.data
.name
]
408 node
.submesh_start
= ref
.submesh_start
409 node
.submesh_count
= ref
.submesh_count
413 node
.submesh_start
= header
.submesh_count
414 node
.submesh_count
= 0
416 default_mat
= c_uint32(69)
417 default_mat
.name
= ""
419 dgraph
= bpy
.context
.evaluated_depsgraph_get()
420 data
= obj
.evaluated_get(dgraph
).data
421 data
.calc_loop_triangles()
422 data
.calc_normals_split()
424 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
425 for material_id
, mat
in enumerate(mat_list
):
429 sm
.indice_start
= header
.indice_count
430 sm
.vertex_start
= header
.vertex_count
433 sm
.material_id
= emplace_material( mat
)
436 sm
.bbx
[0][i
] = 999999
437 sm
.bbx
[1][i
] = -999999
441 # Write the vertex / indice data
443 for tri_index
, tri
in enumerate(data
.loop_triangles
):
444 if tri
.material_index
!= material_id
:
448 vert
= data
.vertices
[tri
.vertices
[j
]]
450 vi
= data
.loops
[li
].vertex_index
453 norm
= data
.loops
[li
].normal
455 colour
= (255,255,255,255)
460 uv
= data
.uv_layers
.active
.data
[li
].uv
462 if data
.vertex_colors
:
463 colour
= data
.vertex_colors
.active
.data
[li
].color
464 colour
= (int(colour
[0]*255.0),\
465 int(colour
[1]*255.0),\
466 int(colour
[2]*255.0),\
467 int(colour
[3]*255.0))
472 weight_groups
= sorted( data
.vertices
[vi
].groups
, key
= \
473 lambda a
: a
.weight
, reverse
=True )
476 if len(weight_groups
) > ml
:
477 g
= weight_groups
[ml
]
478 name
= obj
.vertex_groups
[g
.group
].name
482 groups
[ml
] = armature_def
['bones'].index(name
)
485 if len(weight_groups
) > 0:
486 inv_norm
= (1.0/tot
) * 65535.0
488 weights
[ml
] = int( weights
[ml
] * inv_norm
)
489 weights
[ml
] = min( weights
[ml
], 65535 )
490 weights
[ml
] = max( weights
[ml
], 0 )
493 m
= float(10**TOLERENCE
)
495 key
= (int(co
[0]*m
+0.5),\
517 indice_buffer
+= [boffa
[key
]]
519 index
= c_uint32(sm
.vertex_count
)
523 indice_buffer
+= [index
]
534 v
.colour
[0] = colour
[0]
535 v
.colour
[1] = colour
[1]
536 v
.colour
[2] = colour
[2]
537 v
.colour
[3] = colour
[3]
538 v
.weights
[0] = weights
[0]
539 v
.weights
[1] = weights
[1]
540 v
.weights
[2] = weights
[2]
541 v
.weights
[3] = weights
[3]
542 v
.groups
[0] = groups
[0]
543 v
.groups
[1] = groups
[1]
544 v
.groups
[2] = groups
[2]
545 v
.groups
[3] = groups
[3]
550 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
551 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
555 if sm
.vertex_count
== 0:
560 submesh_buffer
+= [sm
]
561 node
.submesh_count
+= 1
562 header
.submesh_count
+= 1
563 header
.vertex_count
+= sm
.vertex_count
564 header
.indice_count
+= sm
.indice_count
566 mesh_cache
[obj
.data
.name
] = node
568 # Process entity data
569 # ==================================================================
570 node
.offset
= entdata_length
572 if classtype
!= 'k_classtype_none':
577 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
578 s001
= F
" L {obj.name}"
581 s004
= F
"{node.parent: 3}"
584 if classtype
== 'k_classtype_skin':
585 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
586 s005
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
588 scmp
= F
"{s002:<32} {s003:<22} {s004} {s005}"
591 if classtype
== 'k_classtype_INSTANCE' or \
592 classtype
== 'k_classtype_BONE' or \
593 classtype
== 'k_classtype_SKELETON' or \
594 classtype
== 'k_classtype_SKIN':
595 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
599 elif classtype
== 'k_classtype_skin':
602 armature
= armature_def
['obj']
603 entdata_length
+= sizeof( classtype_skin
)
605 skin
= classtype_skin()
606 skin
.skeleton
= armature
.cv_data
.uid
607 entdata_buffer
+= [skin
]
609 elif classtype
== 'k_classtype_skeleton':
611 entdata_length
+= sizeof( classtype_skeleton
)
612 skeleton
= classtype_skeleton()
614 armature_def
= graph_lookup
[obj
]
616 bones
= armature_def
['bones']
617 skeleton
.channels
= len(bones
)
618 skeleton
.ik_count
= armature_def
["ik_count"]
619 skeleton
.collider_count
= armature_def
["collider_count"]
621 if armature
.animation_data
:
622 previous_frame
= bpy
.context
.scene
.frame_current
623 previous_action
= armature
.animation_data
.action
625 skeleton
.anim_start
= len(anim_buffer
)
626 skeleton
.anim_count
= 0
628 for NLALayer
in obj
.animation_data
.nla_tracks
:
629 for NLAStrip
in NLALayer
.strips
:
631 for a
in bpy
.data
.actions
:
632 if a
.name
== NLAStrip
.name
:
633 armature
.animation_data
.action
= a
636 anim_start
= int(NLAStrip
.action_frame_start
)
637 anim_end
= int(NLAStrip
.action_frame_end
)
640 anim
= mdl_animation()
641 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
643 anim
.offset
= animdata_length
644 anim
.length
= anim_end
-anim_start
646 # Export the fucking keyframes
647 for frame
in range(anim_start
,anim_end
):
648 bpy
.context
.scene
.frame_set(frame
)
650 for bone_name
in bones
:
651 for pb
in armature
.pose
.bones
:
652 if pb
.name
== bone_name
:
653 rb
= armature
.data
.bones
[ bone_name
]
655 loc
, rot
, sca
= pb
.matrix_basis
.decompose()
659 final_pos
= Vector(( vp
[0], vp
[2], -vp
[1] ))
662 lc_m
= pb
.matrix_channel
.to_3x3()
663 if pb
.parent
is not None:
664 smtx
= pb
.parent
.matrix_channel
.to_3x3()
665 lc_m
= smtx
.inverted() @ lc_m
666 rq
= lc_m
.to_quaternion()
669 kf
.co
[0] = final_pos
[0]
670 kf
.co
[1] = final_pos
[1]
671 kf
.co
[2] = final_pos
[2]
683 animdata_buffer
+= [kf
]
684 animdata_length
+= sizeof(mdl_keyframe
)
687 anim_buffer
+= [anim
]
688 skeleton
.anim_count
+= 1
690 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
691 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
693 bpy
.context
.scene
.frame_set( previous_frame
)
694 armature
.animation_data
.action
= previous_action
696 entdata_buffer
+= [skeleton
]
698 elif classtype
== 'k_classtype_bone':
700 entdata_length
+= sizeof( classtype_bone
)
702 bone
= classtype_bone()
703 bone
.deform
= node_def
['deform']
705 if 'target' in node_def
:
706 bone
.ik_target
= armature_def
['bones'].index( node_def
['target'] )
707 bone
.ik_pole
= armature_def
['bones'].index( node_def
['pole'] )
712 bone
.collider
= 1 if obj
.cv_data
.collider
else 0
713 if obj
.cv_data
.collider
:
714 bone
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
715 bone
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
716 bone
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
717 bone
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
718 bone
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
719 bone
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
721 bone
.hitbox
[0][0] = 0.0
722 bone
.hitbox
[0][1] = 0.0
723 bone
.hitbox
[0][2] = 0.0
724 bone
.hitbox
[1][0] = 0.0
725 bone
.hitbox
[1][1] = 0.0
726 bone
.hitbox
[1][2] = 0.0
728 bone
.deform
= node_def
['deform']
729 entdata_buffer
+= [bone
]
731 elif classtype
== 'k_classtype_gate':
733 entdata_length
+= sizeof( classtype_gate
)
735 gate
= classtype_gate()
737 if obj
.cv_data
.target
!= None:
738 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
740 if obj
.type == 'MESH':
741 gate
.dims
[0] = obj
.data
.cv_data
.v0
[0]
742 gate
.dims
[1] = obj
.data
.cv_data
.v0
[1]
743 gate
.dims
[2] = obj
.data
.cv_data
.v0
[2]
745 gate
.dims
[0] = obj
.cv_data
.v0
[0]
746 gate
.dims
[1] = obj
.cv_data
.v0
[1]
747 gate
.dims
[2] = obj
.cv_data
.v0
[2]
749 entdata_buffer
+= [gate
]
751 elif classtype
== 'k_classtype_block':
753 entdata_length
+= sizeof( classtype_block
)
755 source
= obj
.data
.cv_data
757 block
= classtype_block()
758 block
.bbx
[0][0] = source
.v0
[0]
759 block
.bbx
[0][1] = source
.v0
[2]
760 block
.bbx
[0][2] = -source
.v1
[1]
762 block
.bbx
[1][0] = source
.v1
[0]
763 block
.bbx
[1][1] = source
.v1
[2]
764 block
.bbx
[1][2] = -source
.v0
[1]
765 entdata_buffer
+= [block
]
767 elif classtype
== 'k_classtype_spawn':
770 elif classtype
== 'k_classtype_water':
773 elif classtype
== 'k_classtype_car_path':
775 entdata_length
+= sizeof( classtype_car_path
)
777 pn
= classtype_car_path()
781 if obj
.cv_data
.target
!= None:
782 pn
.target
= obj
.cv_data
.target
.cv_data
.uid
783 if obj
.cv_data
.target1
!= None:
784 pn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
786 entdata_buffer
+= [pn
]
788 elif obj
.is_instancer
:
789 target
= obj
.instance_collection
792 entdata_length
+= sizeof( classtype_instance
)
794 inst
= classtype_instance()
795 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
796 entdata_buffer
+= [inst
]
798 elif classtype
== 'k_classtype_capsule':
801 elif classtype
== 'k_classtype_route_node':
803 entdata_length
+= sizeof( classtype_route_node
)
805 rn
= classtype_route_node()
806 if obj
.cv_data
.target
!= None:
807 rn
.target
= obj
.cv_data
.target
.cv_data
.uid
808 if obj
.cv_data
.target1
!= None:
809 rn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
811 entdata_buffer
+= [rn
]
813 elif classtype
== 'k_classtype_route':
815 entdata_length
+= sizeof( classtype_route
)
816 r
= classtype_route()
817 r
.pstr_name
= emplace_string("not-implemented")
818 r
.colour
[0] = obj
.cv_data
.colour
[0]
819 r
.colour
[1] = obj
.cv_data
.colour
[1]
820 r
.colour
[2] = obj
.cv_data
.colour
[2]
822 if obj
.cv_data
.target
!= None:
823 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
825 entdata_buffer
+= [r
]
827 # classtype == 'k_classtype_none':
832 node_buffer
+= [node
]
836 header
.anim_count
= len(anim_buffer
)
838 print( "Writing data" )
839 fpos
= sizeof(header
)
841 print( F
"Nodes: {header.node_count}" )
842 header
.node_offset
= fpos
843 fpos
+= sizeof(mdl_node
)*header
.node_count
845 print( F
"Submeshes: {header.submesh_count}" )
846 header
.submesh_offset
= fpos
847 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
849 print( F
"Materials: {header.material_count}" )
850 header
.material_offset
= fpos
851 fpos
+= sizeof(mdl_material
)*header
.material_count
853 print( F
"Animation count: {header.anim_count}" )
854 header
.anim_offset
= fpos
855 fpos
+= sizeof(mdl_animation
)*header
.anim_count
857 print( F
"Entdata length: {entdata_length}" )
858 header
.entdata_offset
= fpos
859 fpos
+= entdata_length
861 print( F
"Vertex count: {header.vertex_count}" )
862 header
.vertex_offset
= fpos
863 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
865 print( F
"Indice count: {header.indice_count}" )
866 header
.indice_offset
= fpos
867 fpos
+= sizeof(c_uint32
)*header
.indice_count
869 print( F
"Keyframe count: {animdata_length}" )
870 header
.animdata_offset
= fpos
871 fpos
+= animdata_length
873 print( F
"Strings length: {len(strings_buffer)}" )
874 header
.strings_offset
= fpos
875 fpos
+= len(strings_buffer
)
877 header
.file_length
= fpos
879 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
880 fp
= open( path
, "wb" )
882 fp
.write( bytearray( header
) )
884 for node
in node_buffer
:
885 fp
.write( bytearray(node
) )
886 for sm
in submesh_buffer
:
887 fp
.write( bytearray(sm
) )
888 for mat
in material_buffer
:
889 fp
.write( bytearray(mat
) )
890 for a
in anim_buffer
:
891 fp
.write( bytearray(a
) )
892 for ed
in entdata_buffer
:
893 fp
.write( bytearray(ed
) )
894 for v
in vertex_buffer
:
895 fp
.write( bytearray(v
) )
896 for i
in indice_buffer
:
897 fp
.write( bytearray(i
) )
898 for kf
in animdata_buffer
:
899 fp
.write( bytearray(kf
) )
901 fp
.write( strings_buffer
)
904 print( F
"Completed {collection_name}.mdl" )
907 # ------------------------------------------------------------------------------
909 cv_view_draw_handler
= None
910 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
913 global cv_view_shader
914 cv_view_shader
.bind()
915 gpu
.state
.depth_mask_set(False)
916 gpu
.state
.line_width_set(2.0)
917 gpu
.state
.face_culling_set('BACK')
918 gpu
.state
.depth_test_set('LESS')
919 gpu
.state
.blend_set('NONE')
924 #def drawbezier(p0,h0,p1,h1,c0,c1):
925 # nonlocal verts, colours
929 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
932 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
935 # for i in range(10):
941 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
942 # verts += [(last[0],last[1],last[2])]
943 # verts += [(p[0],p[1],p[2])]
944 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
949 def drawbhandle(obj
, direction
, colour
):
950 nonlocal verts
, colours
952 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
955 colours
+= [colour
,colour
]
957 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
958 nonlocal verts
, colours
967 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
968 verts
+= [(last
[0],last
[1],last
[2])]
969 verts
+= [(p
[0],p
[1],p
[2])]
970 colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
973 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
974 nonlocal course_count
976 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
978 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
979 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
980 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
981 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
982 drawbezier(p0
,h0
,p1
,h1
,c0
,c1
)
984 def drawbpath(o0
,o1
,c0
,c1
):
985 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
987 def drawbline(p0
,p1
,c0
,c1
):
988 nonlocal verts
, colours
992 for obj
in bpy
.context
.collection
.objects
:
993 if obj
.type == 'ARMATURE':
994 for bone
in obj
.data
.bones
:
995 if bone
.cv_data
.collider
:
1001 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
1002 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
1003 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
1004 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
1005 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
1006 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
1007 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
1008 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
1010 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1011 (0,4),(1,5),(2,6),(3,7)]
1016 verts
+= [(v0
[0],v0
[1],v0
[2])]
1017 verts
+= [(v1
[0],v1
[1],v1
[2])]
1018 colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
1020 center
=obj
.matrix_world
@c
1022 def _angle_lim( major
, minor
, amin
, amax
, colour
):
1023 nonlocal verts
, colours
1031 a0
= amin
*(1.0-t0
)+amax
*t0
1032 a1
= amin
*(1.0-t1
)+amax
*t1
1034 p0
= c
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
1035 p1
= c
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
1037 p0
=obj
.matrix_world
@ p0
1038 p1
=obj
.matrix_world
@ p1
1040 colours
+= [colour
,colour
]
1044 colours
+= [colour
,colour
]
1047 colours
+= [colour
,colour
]
1049 verts
+= [c
+major
*1.2*f
,c
+major
*f
*0.8]
1050 colours
+= [colour
,colour
]
1052 if bone
.cv_data
.con0
:
1053 _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
1054 bone
.cv_data
.mins
[0], bone
.cv_data
.maxs
[0], \
1056 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1057 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
1059 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1060 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
1064 if obj
.cv_data
.classtype
== 'k_classtype_gate':
1065 if obj
.type == 'MESH':
1066 dims
= obj
.data
.cv_data
.v0
1068 dims
= obj
.cv_data
.v0
1071 c
= Vector((0,0,dims
[2]))
1073 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
1074 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
1075 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
1076 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
1077 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
1078 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
1079 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
1080 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
1081 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
1083 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
1088 verts
+= [(v0
[0],v0
[1],v0
[2])]
1089 verts
+= [(v1
[0],v1
[1],v1
[2])]
1090 colours
+= [(1,1,0,1),(1,1,0,1)]
1092 sw
= (0.4,0.4,0.4,0.2)
1093 if obj
.cv_data
.target
!= None:
1094 drawbline( obj
.location
, obj
.cv_data
.target
.location
, sw
,sw
)
1096 elif obj
.cv_data
.classtype
== 'k_classtype_route_node':
1097 sw
= Vector((0.4,0.4,0.4,0.2))
1098 sw2
= Vector((1.5,0.2,0.2,0.0))
1099 if obj
.cv_data
.target
!= None:
1100 drawbpath( obj
, obj
.cv_data
.target
, sw
, sw
)
1101 if obj
.cv_data
.target1
!= None:
1102 drawbpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
1104 drawbhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
1105 drawbhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
1107 p1
= obj
.location
+ \
1108 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
1109 drawbline( obj
.location
, p1
, sw
,sw2
)
1112 elif obj
.cv_data
.classtype
== 'k_classtype_block':
1113 a
= obj
.data
.cv_data
.v0
1114 b
= obj
.data
.cv_data
.v1
1117 vs
[0] = obj
.matrix_world
@ Vector((a
[0], a
[1], a
[2]))
1118 vs
[1] = obj
.matrix_world
@ Vector((a
[0], b
[1], a
[2]))
1119 vs
[2] = obj
.matrix_world
@ Vector((b
[0], b
[1], a
[2]))
1120 vs
[3] = obj
.matrix_world
@ Vector((b
[0], a
[1], a
[2]))
1121 vs
[4] = obj
.matrix_world
@ Vector((a
[0], a
[1], b
[2]))
1122 vs
[5] = obj
.matrix_world
@ Vector((a
[0], b
[1], b
[2]))
1123 vs
[6] = obj
.matrix_world
@ Vector((b
[0], b
[1], b
[2]))
1124 vs
[7] = obj
.matrix_world
@ Vector((b
[0], a
[1], b
[2]))
1126 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1127 (0,4),(1,5),(2,6),(3,7)]
1132 verts
+= [(v0
[0],v0
[1],v0
[2])]
1133 verts
+= [(v1
[0],v1
[1],v1
[2])]
1134 colours
+= [(1,1,0,1),(1,1,0,1)]
1136 elif obj
.cv_data
.classtype
== 'k_classtype_capsule':
1137 h
= obj
.data
.cv_data
.v0
[0]
1138 r
= obj
.data
.cv_data
.v0
[1]
1141 vs
[0] = obj
.matrix_world
@ Vector((0.0,0.0, h
*0.5 ))
1142 vs
[1] = obj
.matrix_world
@ Vector((0.0,0.0,-h
*0.5 ))
1143 vs
[2] = obj
.matrix_world
@ Vector(( r
,0.0, h
*0.5-r
))
1144 vs
[3] = obj
.matrix_world
@ Vector(( -r
,0.0, h
*0.5-r
))
1145 vs
[4] = obj
.matrix_world
@ Vector(( r
,0.0,-h
*0.5+r
))
1146 vs
[5] = obj
.matrix_world
@ Vector(( -r
,0.0,-h
*0.5+r
))
1147 vs
[6] = obj
.matrix_world
@ Vector((0.0, r
, h
*0.5-r
))
1148 vs
[7] = obj
.matrix_world
@ Vector((0.0,-r
, h
*0.5-r
))
1149 vs
[8] = obj
.matrix_world
@ Vector((0.0, r
,-h
*0.5+r
))
1150 vs
[9] = obj
.matrix_world
@ Vector((0.0,-r
,-h
*0.5+r
))
1152 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
1157 verts
+= [(v0
[0],v0
[1],v0
[2])]
1158 verts
+= [(v1
[0],v1
[1],v1
[2])]
1159 colours
+= [(0.5,1,0,1),(0.5,1,0,1)]
1161 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
1163 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
1164 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
1165 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
1166 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
1167 indices
= [(0,1),(1,2),(1,3)]
1171 verts
+= [(v0
[0],v0
[1],v0
[2])]
1172 verts
+= [(v1
[0],v1
[1],v1
[2])]
1173 colours
+= [(0,1,1,1),(0,1,1,1)]
1175 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1177 vs
[0] = obj
.location
1178 vs
[1] = obj
.cv_data
.target
.location
1183 verts
+= [(v0
[0],v0
[1],v0
[2])]
1184 verts
+= [(v1
[0],v1
[1],v1
[2])]
1185 colours
+= [(0,1,1,1),(0,1,1,1)]
1189 stack
[0] = obj
.cv_data
.target
1191 loop_complete
= False
1194 if stack_i
[si
-1] == 2:
1198 if si
== 0: # Loop failed to complete
1203 targets
= [None,None]
1204 targets
[0] = node
.cv_data
.target
1206 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1207 targets
[1] = node
.cv_data
.target1
1209 nextnode
= targets
[stack_i
[si
-1]]
1212 if nextnode
!= None: # branch
1213 if nextnode
== stack
[0]: # Loop completed
1214 loop_complete
= True
1218 for sj
in range(si
):
1219 if stack
[sj
] == nextnode
: # invalidated path
1225 stack
[si
] = nextnode
1230 cc
= Vector((obj
.cv_data
.colour
[0],\
1231 obj
.cv_data
.colour
[1],\
1232 obj
.cv_data
.colour
[2],\
1235 for sj
in range(si
):
1238 if stack
[sj
].cv_data
.classtype
== 'k_classtype_gate' and \
1239 stack
[sk
].cv_data
.classtype
== 'k_classtype_gate':
1240 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
1241 drawsbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
1244 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
1248 elif obj
.cv_data
.classtype
== 'k_classtype_car_path':
1249 v0
= obj
.matrix_world
.to_quaternion() @ Vector((0,1,0))
1250 c0
= Vector((v0
.x
*0.5+0.5, v0
.y
*0.5+0.5, 0.0, 1.0))
1251 drawbhandle( obj
, 1.0, (0.9,0.9,0.9,1.0) )
1253 if obj
.cv_data
.target
!= None:
1254 v1
= obj
.cv_data
.target
.matrix_world
.to_quaternion()@Vector((0,1,0))
1255 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1257 drawbhandle( obj
.cv_data
.target
, -1.0, (0.5,0.5,0.5,1.0) )
1258 drawbpath( obj
, obj
.cv_data
.target
, c0
, c1
)
1260 if obj
.cv_data
.target1
!= None:
1261 v1
= obj
.cv_data
.target1
.matrix_world
.to_quaternion()@Vector((0,1,0))
1262 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1264 drawbhandle( obj
.cv_data
.target1
, -1.0, (0.5,0.5,0.5,1.0) )
1265 drawbpath( obj
, obj
.cv_data
.target1
, c0
, c1
)
1267 lines
= batch_for_shader(\
1268 cv_view_shader
, 'LINES', \
1269 { "pos":verts
, "color":colours
})
1271 lines
.draw( cv_view_shader
)
1273 def cv_poll_target(scene
, obj
):
1274 if obj
== bpy
.context
.active_object
:
1276 if obj
.cv_data
.classtype
== 'k_classtype_none':
1280 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
1281 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1282 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1283 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
1284 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
1286 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1287 uid
: bpy
.props
.IntProperty( name
="" )
1289 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
1290 poll
=cv_poll_target
)
1291 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
1292 poll
=cv_poll_target
)
1294 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1297 classtype
: bpy
.props
.EnumProperty(
1300 ('k_classtype_none', "k_classtype_none", "", 0),
1301 ('k_classtype_gate', "k_classtype_gate", "", 1),
1302 ('k_classtype_block', "k_classtype_block", "", 2),
1303 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1304 ('k_classtype_water', "k_classtype_water", "", 4),
1305 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1306 ('k_classtype_INSTANCE', "","", 6 ),
1307 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1308 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1309 ('k_classtype_route', "k_classtype_route", "", 9 ),
1310 ('k_classtype_bone',"k_classtype_bone","",10),
1311 ('k_classtype_SKELETON', "","", 11 ),
1312 ('k_classtype_SKIN',"","",12)
1315 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
1316 collider
: bpy
.props
.BoolProperty(name
="Collider",default
=False)
1317 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1318 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1320 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
1321 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
1323 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
1324 c0
: bpy
.props
.FloatVectorProperty(name
="dir",size
=3)
1325 s0
: bpy
.props
.FloatVectorProperty(name
="limits",size
=3)
1327 con1
: bpy
.props
.BoolProperty(name
="Constriant 1",default
=False)
1328 c1
: bpy
.props
.FloatVectorProperty(name
="dir",size
=3)
1329 s1
: bpy
.props
.FloatVectorProperty(name
="limits",size
=3)
1331 class CV_BONE_PANEL(bpy
.types
.Panel
):
1332 bl_label
="Bone Config"
1333 bl_idname
="SCENE_PT_cv_bone"
1334 bl_space_type
='PROPERTIES'
1335 bl_region_type
='WINDOW'
1338 def draw(_
,context
):
1339 active_object
= context
.active_object
1340 if active_object
== None: return
1342 bone
= active_object
.data
.bones
.active
1343 if bone
== None: return
1345 _
.layout
.prop( bone
.cv_data
, "collider" )
1346 _
.layout
.prop( bone
.cv_data
, "v0" )
1347 _
.layout
.prop( bone
.cv_data
, "v1" )
1349 _
.layout
.label( text
="Angle Limits" )
1350 _
.layout
.prop( bone
.cv_data
, "mins" )
1351 _
.layout
.prop( bone
.cv_data
, "maxs" )
1353 _
.layout
.prop( bone
.cv_data
, "con0" )
1354 _
.layout
.prop( bone
.cv_data
, "c0" )
1355 _
.layout
.prop( bone
.cv_data
, "s0" )
1357 _
.layout
.prop( bone
.cv_data
, "con1" )
1358 _
.layout
.prop( bone
.cv_data
, "c1" )
1359 _
.layout
.prop( bone
.cv_data
, "s1" )
1361 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1362 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1364 class CV_OBJ_PANEL(bpy
.types
.Panel
):
1365 bl_label
="Entity Config"
1366 bl_idname
="SCENE_PT_cv_entity"
1367 bl_space_type
='PROPERTIES'
1368 bl_region_type
='WINDOW'
1371 def draw(_
,context
):
1372 active_object
= bpy
.context
.active_object
1373 if active_object
== None: return
1374 _
.layout
.prop( active_object
.cv_data
, "classtype" )
1376 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1377 _
.layout
.prop( active_object
.cv_data
, "target" )
1379 mesh
= active_object
.data
1380 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1381 _
.layout
.prop( mesh
.cv_data
, "v0" )
1383 elif active_object
.cv_data
.classtype
== 'k_classtype_car_path' or \
1384 active_object
.cv_data
.classtype
== 'k_classtype_route_node':
1385 _
.layout
.prop( active_object
.cv_data
, "target" )
1386 _
.layout
.prop( active_object
.cv_data
, "target1" )
1388 elif active_object
.cv_data
.classtype
== 'k_classtype_route':
1389 _
.layout
.prop( active_object
.cv_data
, "target" )
1390 _
.layout
.prop( active_object
.cv_data
, "colour" )
1392 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1393 mesh
= active_object
.data
1395 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1396 _
.layout
.prop( mesh
.cv_data
, "v0" )
1397 _
.layout
.prop( mesh
.cv_data
, "v1" )
1398 _
.layout
.prop( mesh
.cv_data
, "v2" )
1399 _
.layout
.prop( mesh
.cv_data
, "v3" )
1400 elif active_object
.cv_data
.classtype
== 'k_classtype_capsule':
1401 mesh
= active_object
.data
1402 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1403 _
.layout
.prop( mesh
.cv_data
, "v0" )
1405 class CV_INTERFACE(bpy
.types
.Panel
):
1406 bl_idname
= "VIEW3D_PT_carve"
1408 bl_space_type
= 'VIEW_3D'
1409 bl_region_type
= 'UI'
1410 bl_category
= "Carve"
1412 def draw(_
, context
):
1414 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1415 layout
.operator( "carve.compile_all" )
1418 view_layer
= bpy
.context
.view_layer
1419 for col
in view_layer
.layer_collection
.children
["export"].children
:
1420 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
1421 write_model( col
.name
)
1423 class CV_COMPILE(bpy
.types
.Operator
):
1424 bl_idname
="carve.compile_all"
1425 bl_label
="Compile All"
1427 def execute(_
,context
):
1429 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1430 #for col in bpy.data.collections["export"].children:
1431 # write_model( col.name )
1435 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1436 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
1440 global cv_view_draw_handler
1443 bpy
.utils
.register_class(c
)
1445 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
1446 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
1447 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
1448 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
1450 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1451 cv_draw
,(),'WINDOW','POST_VIEW')
1454 global cv_view_draw_handler
1457 bpy
.utils
.unregister_class(c
)
1459 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')