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 ("anim_start",c_uint32
),
147 ("anim_count",c_uint32
)]
149 class classtype_bone(Structure
):
151 _fields_
= [("deform",c_uint32
)]
154 # ==============================================================================
156 def write_model(collection_name
):
157 print( F
"Model graph | Create mode '{collection_name}'" )
159 header
= mdl_header()
160 header
.identifier
= 0xABCD0000
162 header
.vertex_count
= 0
163 header
.indice_count
= 0
164 header
.submesh_count
= 0
165 header
.node_count
= 0
166 header
.material_count
= 0
167 header
.file_length
= 0
187 def emplace_string( s
):
188 nonlocal string_cache
, strings_buffer
190 if s
in string_cache
:
191 return string_cache
[s
]
193 string_cache
[s
] = len( strings_buffer
)
194 strings_buffer
+= (s
+'\0').encode('utf-8')
195 return string_cache
[s
]
197 def emplace_material( mat
):
198 nonlocal material_cache
, material_buffer
200 if mat
.name
in material_cache
:
201 return material_cache
[mat
.name
]
203 material_cache
[mat
.name
] = header
.material_count
204 dest
= mdl_material()
205 dest
.pstr_name
= emplace_string( mat
.name
)
206 material_buffer
+= [dest
]
208 header
.material_count
+= 1
209 return material_cache
[mat
.name
]
211 # Create root or empty node and materials
212 # this is to designate id 0 as 'NULL'
214 none_material
= c_uint32(69)
215 none_material
.name
= ""
216 emplace_material( none_material
)
229 root
.pstr_name
= emplace_string('')
230 root
.submesh_start
= 0
231 root
.submesh_count
= 0
234 node_buffer
+= [root
]
238 print( " assigning ids" )
239 collection
= bpy
.data
.collections
[collection_name
]
242 # ==========================================
244 header
.node_count
= 0
247 uid
= header
.node_count
248 header
.node_count
+= 1
251 print( " creating scene graph" )
255 graph
["children"] = []
256 graph
["uid"] = _uid()
257 graph
["parent"] = None
259 graph_lookup
= {} # object can lookup its graph def here
261 for obj
in collection
.all_objects
:
264 def _extend( p
, n
, d
):
268 tree
["children"] = []
274 if n
.type == 'ARMATURE':
275 tree
["bones"] = [None] # None is the root transform
277 def _extendb( p
, n
, d
):
282 btree
["uid"] = _uid()
283 btree
["children"] = []
288 tree
["bones"] += [n
.name
]
291 _extendb( btree
, c
, d
+1 )
293 btree
['deform'] = n
.use_deform
294 p
['children'] += [btree
]
296 for b
in n
.data
.bones
:
298 _extendb( tree
, b
, d
+1 )
300 for obj1
in n
.children
:
301 _extend( tree
, obj1
, d
+1 )
303 p
["children"] += [tree
]
304 graph_lookup
[n
] = tree
306 _extend( graph
, obj
, 1 )
310 for c
in p
['children']:
312 yield from _graph_iter(c
)
314 it
= _graph_iter(graph
)
316 root
.parent
= 0xffffffff
319 # ==============================================
320 it
= _graph_iter(graph
)
321 print( " compiling data" )
323 if 'obj' in node_def
:
324 obj
= node_def
['obj']
327 elif 'bone' in node_def
:
328 obj
= node_def
['bone']
330 objco
= obj
.head_local
332 depth
= node_def
['depth']
333 uid
= node_def
['uid']
336 node
.co
[0] = objco
[0]
337 node
.co
[1] = objco
[2]
338 node
.co
[2] = -objco
[1]
340 # Convert rotation quat to our space type
341 quat
= obj
.matrix_local
.to_quaternion()
348 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
349 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
350 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
352 node
.s
[0] = obj
.scale
[0]
353 node
.s
[1] = obj
.scale
[2]
354 node
.s
[2] = obj
.scale
[1]
356 node
.pstr_name
= emplace_string( obj
.name
)
358 if node_def
["parent"]:
359 node
.parent
= node_def
["parent"]["uid"]
362 classtype
= 'k_classtype_bone'
363 elif objt
== 'ARMATURE':
364 classtype
= 'k_classtype_skeleton'
366 classtype
= obj
.cv_data
.classtype
369 # =================================================================
372 # Dont use the cache if we have modifiers that affect the normals
380 for mod
in obj
.modifiers
:
381 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP':
382 can_use_cache
= False
384 if mod
.type == 'ARMATURE':
385 classtype
= 'k_classtype_skin'
386 armature_def
= graph_lookup
[mod
.object]
388 if can_use_cache
and obj
.data
.name
in mesh_cache
:
389 ref
= mesh_cache
[obj
.data
.name
]
390 node
.submesh_start
= ref
.submesh_start
391 node
.submesh_count
= ref
.submesh_count
395 node
.submesh_start
= header
.submesh_count
396 node
.submesh_count
= 0
398 default_mat
= c_uint32(69)
399 default_mat
.name
= ""
401 dgraph
= bpy
.context
.evaluated_depsgraph_get()
402 data
= obj
.evaluated_get(dgraph
).data
403 data
.calc_loop_triangles()
404 data
.calc_normals_split()
406 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
407 for material_id
, mat
in enumerate(mat_list
):
411 sm
.indice_start
= header
.indice_count
412 sm
.vertex_start
= header
.vertex_count
415 sm
.material_id
= emplace_material( mat
)
418 sm
.bbx
[0][i
] = 999999
419 sm
.bbx
[1][i
] = -999999
423 # Write the vertex / indice data
425 for tri_index
, tri
in enumerate(data
.loop_triangles
):
426 if tri
.material_index
!= material_id
:
430 vert
= data
.vertices
[tri
.vertices
[j
]]
432 vi
= data
.loops
[li
].vertex_index
435 norm
= data
.loops
[li
].normal
437 colour
= (255,255,255,255)
442 uv
= data
.uv_layers
.active
.data
[li
].uv
444 if data
.vertex_colors
:
445 colour
= data
.vertex_colors
.active
.data
[li
].color
446 colour
= (int(colour
[0]*255.0),\
447 int(colour
[1]*255.0),\
448 int(colour
[2]*255.0),\
449 int(colour
[3]*255.0))
454 weight_groups
= sorted( data
.vertices
[vi
].groups
, key
= \
455 lambda a
: a
.weight
, reverse
=True )
458 if len(weight_groups
) > ml
:
459 g
= weight_groups
[ml
]
460 name
= obj
.vertex_groups
[g
.group
].name
464 groups
[ml
] = armature_def
['bones'].index(name
)
467 if len(weight_groups
) > 0:
468 inv_norm
= (1.0/tot
) * 65535.0
470 weights
[ml
] = int( weights
[ml
] * inv_norm
)
471 weights
[ml
] = min( weights
[ml
], 65535 )
472 weights
[ml
] = max( weights
[ml
], 0 )
475 m
= float(10**TOLERENCE
)
477 key
= (int(co
[0]*m
+0.5),\
499 indice_buffer
+= [boffa
[key
]]
501 index
= c_uint32(sm
.vertex_count
)
505 indice_buffer
+= [index
]
516 v
.colour
[0] = colour
[0]
517 v
.colour
[1] = colour
[1]
518 v
.colour
[2] = colour
[2]
519 v
.colour
[3] = colour
[3]
520 v
.weights
[0] = weights
[0]
521 v
.weights
[1] = weights
[1]
522 v
.weights
[2] = weights
[2]
523 v
.weights
[3] = weights
[3]
524 v
.groups
[0] = groups
[0]
525 v
.groups
[1] = groups
[1]
526 v
.groups
[2] = groups
[2]
527 v
.groups
[3] = groups
[3]
532 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
533 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
537 if sm
.vertex_count
== 0:
542 submesh_buffer
+= [sm
]
543 node
.submesh_count
+= 1
544 header
.submesh_count
+= 1
545 header
.vertex_count
+= sm
.vertex_count
546 header
.indice_count
+= sm
.indice_count
548 mesh_cache
[obj
.data
.name
] = node
550 # Process entity data
551 # ==================================================================
552 node
.offset
= entdata_length
554 if classtype
!= 'k_classtype_none':
559 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
560 s001
= F
" L {obj.name}"
563 s004
= F
"{node.parent: 3}"
566 if classtype
== 'k_classtype_skin':
567 s005
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
569 scmp
= F
"{s002:<32} {s003:<16} {s004} {s005}"
572 if classtype
== 'k_classtype_INSTANCE' or \
573 classtype
== 'k_classtype_BONE' or \
574 classtype
== 'k_classtype_SKELETON' or \
575 classtype
== 'k_classtype_SKIN':
576 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
580 elif classtype
== 'k_classtype_skin':
583 armature
= armature_def
['obj']
584 entdata_length
+= sizeof( classtype_skin
)
586 skin
= classtype_skin()
587 skin
.skeleton
= armature
.cv_data
.uid
588 entdata_buffer
+= [skin
]
590 elif classtype
== 'k_classtype_skeleton':
592 entdata_length
+= sizeof( classtype_skeleton
)
593 skeleton
= classtype_skeleton()
595 armature_def
= graph_lookup
[obj
]
597 bones
= armature_def
['bones']
599 skeleton
.channels
= len(bones
)
601 if armature
.animation_data
:
602 previous_frame
= bpy
.context
.scene
.frame_current
603 previous_action
= armature
.animation_data
.action
605 skeleton
.anim_start
= len(anim_buffer
)
606 skeleton
.anim_count
= 0
608 for NLALayer
in obj
.animation_data
.nla_tracks
:
609 for NLAStrip
in NLALayer
.strips
:
611 for a
in bpy
.data
.actions
:
612 if a
.name
== NLAStrip
.name
:
613 armature
.animation_data
.action
= a
616 anim_start
= int(NLAStrip
.action_frame_start
)
617 anim_end
= int(NLAStrip
.action_frame_end
)
620 anim
= mdl_animation()
621 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
623 anim
.offset
= animdata_length
624 anim
.length
= anim_end
-anim_start
626 # Export the fucking keyframes
627 for frame
in range(anim_start
,anim_end
):
628 bpy
.context
.scene
.frame_set(frame
)
630 for bone_name
in bones
:
631 for pb
in armature
.pose
.bones
:
632 if pb
.name
== bone_name
:
633 rb
= armature
.data
.bones
[ bone_name
]
635 loc
, rot
, sca
= pb
.matrix_basis
.decompose()
639 final_pos
= Vector(( vp
[0], vp
[2], -vp
[1] ))
642 lc_m
= pb
.matrix_channel
.to_3x3()
643 if pb
.parent
is not None:
644 smtx
= pb
.parent
.matrix_channel
.to_3x3()
645 lc_m
= smtx
.inverted() @ lc_m
646 rq
= lc_m
.to_quaternion()
663 animdata_buffer
+= [kf
]
664 animdata_length
+= sizeof(mdl_keyframe
)
667 anim_buffer
+= [anim
]
668 skeleton
.anim_count
+= 1
670 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
671 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
673 bpy
.context
.scene
.frame_set( previous_frame
)
674 armature
.animation_data
.action
= previous_action
676 entdata_buffer
+= [skeleton
]
678 elif classtype
== 'k_classtype_bone':
680 entdata_length
+= sizeof( classtype_bone
)
682 bone
= classtype_bone()
683 bone
.use_deform
= node_def
['deform']
684 entdata_buffer
+= [bone
]
686 elif classtype
== 'k_classtype_gate':
688 entdata_length
+= sizeof( classtype_gate
)
690 gate
= classtype_gate()
692 if obj
.cv_data
.target
!= None:
693 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
695 if obj
.type == 'MESH':
696 gate
.dims
[0] = obj
.data
.cv_data
.v0
[0]
697 gate
.dims
[1] = obj
.data
.cv_data
.v0
[1]
698 gate
.dims
[2] = obj
.data
.cv_data
.v0
[2]
700 gate
.dims
[0] = obj
.cv_data
.v0
[0]
701 gate
.dims
[1] = obj
.cv_data
.v0
[1]
702 gate
.dims
[2] = obj
.cv_data
.v0
[2]
704 entdata_buffer
+= [gate
]
706 elif classtype
== 'k_classtype_block':
708 entdata_length
+= sizeof( classtype_block
)
710 source
= obj
.data
.cv_data
712 block
= classtype_block()
713 block
.bbx
[0][0] = source
.v0
[0]
714 block
.bbx
[0][1] = source
.v0
[2]
715 block
.bbx
[0][2] = -source
.v1
[1]
717 block
.bbx
[1][0] = source
.v1
[0]
718 block
.bbx
[1][1] = source
.v1
[2]
719 block
.bbx
[1][2] = -source
.v0
[1]
720 entdata_buffer
+= [block
]
722 elif classtype
== 'k_classtype_spawn':
725 elif classtype
== 'k_classtype_water':
728 elif classtype
== 'k_classtype_car_path':
730 entdata_length
+= sizeof( classtype_car_path
)
732 pn
= classtype_car_path()
736 if obj
.cv_data
.target
!= None:
737 pn
.target
= obj
.cv_data
.target
.cv_data
.uid
738 if obj
.cv_data
.target1
!= None:
739 pn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
741 entdata_buffer
+= [pn
]
743 elif obj
.is_instancer
:
744 target
= obj
.instance_collection
747 entdata_length
+= sizeof( classtype_instance
)
749 inst
= classtype_instance()
750 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
751 entdata_buffer
+= [inst
]
753 elif classtype
== 'k_classtype_capsule':
756 elif classtype
== 'k_classtype_route_node':
758 entdata_length
+= sizeof( classtype_route_node
)
760 rn
= classtype_route_node()
761 if obj
.cv_data
.target
!= None:
762 rn
.target
= obj
.cv_data
.target
.cv_data
.uid
763 if obj
.cv_data
.target1
!= None:
764 rn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
766 entdata_buffer
+= [rn
]
768 elif classtype
== 'k_classtype_route':
770 entdata_length
+= sizeof( classtype_route
)
771 r
= classtype_route()
772 r
.pstr_name
= emplace_string("not-implemented")
773 r
.colour
[0] = obj
.cv_data
.colour
[0]
774 r
.colour
[1] = obj
.cv_data
.colour
[1]
775 r
.colour
[2] = obj
.cv_data
.colour
[2]
777 if obj
.cv_data
.target
!= None:
778 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
780 entdata_buffer
+= [r
]
782 # classtype == 'k_classtype_none':
787 node_buffer
+= [node
]
791 header
.anim_count
= len(anim_buffer
)
793 print( "Writing data" )
794 fpos
= sizeof(header
)
796 print( F
"Nodes: {header.node_count}" )
797 header
.node_offset
= fpos
798 fpos
+= sizeof(mdl_node
)*header
.node_count
800 print( F
"Submeshes: {header.submesh_count}" )
801 header
.submesh_offset
= fpos
802 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
804 print( F
"Materials: {header.material_count}" )
805 header
.material_offset
= fpos
806 fpos
+= sizeof(mdl_material
)*header
.material_count
808 print( F
"Animation count: {header.anim_count}" )
809 header
.anim_offset
= fpos
810 fpos
+= sizeof(mdl_animation
)*header
.anim_count
812 print( F
"Entdata length: {entdata_length}" )
813 header
.entdata_offset
= fpos
814 fpos
+= entdata_length
816 print( F
"Vertex count: {header.vertex_count}" )
817 header
.vertex_offset
= fpos
818 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
820 print( F
"Indice count: {header.indice_count}" )
821 header
.indice_offset
= fpos
822 fpos
+= sizeof(c_uint32
)*header
.indice_count
824 print( F
"Keyframe count: {animdata_length}" )
825 header
.animdata_offset
= fpos
826 fpos
+= animdata_length
828 print( F
"Strings length: {len(strings_buffer)}" )
829 header
.strings_offset
= fpos
830 fpos
+= len(strings_buffer
)
832 header
.file_length
= fpos
834 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
835 fp
= open( path
, "wb" )
837 fp
.write( bytearray( header
) )
839 for node
in node_buffer
:
840 fp
.write( bytearray(node
) )
841 for sm
in submesh_buffer
:
842 fp
.write( bytearray(sm
) )
843 for mat
in material_buffer
:
844 fp
.write( bytearray(mat
) )
845 for a
in anim_buffer
:
846 fp
.write( bytearray(a
) )
847 for ed
in entdata_buffer
:
848 fp
.write( bytearray(ed
) )
849 for v
in vertex_buffer
:
850 fp
.write( bytearray(v
) )
851 for i
in indice_buffer
:
852 fp
.write( bytearray(i
) )
853 for kf
in animdata_buffer
:
854 fp
.write( bytearray(kf
) )
856 fp
.write( strings_buffer
)
859 print( F
"Completed {collection_name}.mdl" )
862 # ------------------------------------------------------------------------------
864 cv_view_draw_handler
= None
865 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
868 global cv_view_shader
869 cv_view_shader
.bind()
870 gpu
.state
.depth_mask_set(False)
871 gpu
.state
.line_width_set(2.0)
872 gpu
.state
.face_culling_set('BACK')
873 gpu
.state
.depth_test_set('LESS')
874 gpu
.state
.blend_set('NONE')
879 #def drawbezier(p0,h0,p1,h1,c0,c1):
880 # nonlocal verts, colours
884 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
887 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
890 # for i in range(10):
896 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
897 # verts += [(last[0],last[1],last[2])]
898 # verts += [(p[0],p[1],p[2])]
899 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
904 def drawbhandle(obj
, direction
, colour
):
905 nonlocal verts
, colours
907 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
910 colours
+= [colour
,colour
]
912 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
913 nonlocal verts
, colours
922 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
923 verts
+= [(last
[0],last
[1],last
[2])]
924 verts
+= [(p
[0],p
[1],p
[2])]
925 colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
928 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
929 nonlocal course_count
931 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
933 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
934 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
935 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
936 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
937 drawbezier(p0
,h0
,p1
,h1
,c0
,c1
)
939 def drawbpath(o0
,o1
,c0
,c1
):
940 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
942 def drawbline(p0
,p1
,c0
,c1
):
943 nonlocal verts
, colours
947 for obj
in bpy
.context
.collection
.objects
:
949 if obj
.cv_data
.classtype
== 'k_classtype_gate':
950 if obj
.type == 'MESH':
951 dims
= obj
.data
.cv_data
.v0
953 dims
= obj
.cv_data
.v0
956 c
= Vector((0,0,dims
[2]))
958 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
959 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
960 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
961 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
962 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
963 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
964 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
965 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
966 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
968 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
973 verts
+= [(v0
[0],v0
[1],v0
[2])]
974 verts
+= [(v1
[0],v1
[1],v1
[2])]
975 colours
+= [(1,1,0,1),(1,1,0,1)]
977 sw
= (0.4,0.4,0.4,0.2)
978 if obj
.cv_data
.target
!= None:
979 drawbline( obj
.location
, obj
.cv_data
.target
.location
, sw
,sw
)
981 elif obj
.cv_data
.classtype
== 'k_classtype_route_node':
982 sw
= Vector((0.4,0.4,0.4,0.2))
983 sw2
= Vector((1.5,0.2,0.2,0.0))
984 if obj
.cv_data
.target
!= None:
985 drawbpath( obj
, obj
.cv_data
.target
, sw
, sw
)
986 if obj
.cv_data
.target1
!= None:
987 drawbpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
989 drawbhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
990 drawbhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
993 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
994 drawbline( obj
.location
, p1
, sw
,sw2
)
997 elif obj
.cv_data
.classtype
== 'k_classtype_block':
998 a
= obj
.data
.cv_data
.v0
999 b
= obj
.data
.cv_data
.v1
1002 vs
[0] = obj
.matrix_world
@ Vector((a
[0], a
[1], a
[2]))
1003 vs
[1] = obj
.matrix_world
@ Vector((a
[0], b
[1], a
[2]))
1004 vs
[2] = obj
.matrix_world
@ Vector((b
[0], b
[1], a
[2]))
1005 vs
[3] = obj
.matrix_world
@ Vector((b
[0], a
[1], a
[2]))
1006 vs
[4] = obj
.matrix_world
@ Vector((a
[0], a
[1], b
[2]))
1007 vs
[5] = obj
.matrix_world
@ Vector((a
[0], b
[1], b
[2]))
1008 vs
[6] = obj
.matrix_world
@ Vector((b
[0], b
[1], b
[2]))
1009 vs
[7] = obj
.matrix_world
@ Vector((b
[0], a
[1], b
[2]))
1011 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1012 (0,4),(1,5),(2,6),(3,7)]
1017 verts
+= [(v0
[0],v0
[1],v0
[2])]
1018 verts
+= [(v1
[0],v1
[1],v1
[2])]
1019 colours
+= [(1,1,0,1),(1,1,0,1)]
1021 elif obj
.cv_data
.classtype
== 'k_classtype_capsule':
1022 h
= obj
.data
.cv_data
.v0
[0]
1023 r
= obj
.data
.cv_data
.v0
[1]
1026 vs
[0] = obj
.matrix_world
@ Vector((0.0,0.0, h
*0.5 ))
1027 vs
[1] = obj
.matrix_world
@ Vector((0.0,0.0,-h
*0.5 ))
1028 vs
[2] = obj
.matrix_world
@ Vector(( r
,0.0, h
*0.5-r
))
1029 vs
[3] = obj
.matrix_world
@ Vector(( -r
,0.0, h
*0.5-r
))
1030 vs
[4] = obj
.matrix_world
@ Vector(( r
,0.0,-h
*0.5+r
))
1031 vs
[5] = obj
.matrix_world
@ Vector(( -r
,0.0,-h
*0.5+r
))
1032 vs
[6] = obj
.matrix_world
@ Vector((0.0, r
, h
*0.5-r
))
1033 vs
[7] = obj
.matrix_world
@ Vector((0.0,-r
, h
*0.5-r
))
1034 vs
[8] = obj
.matrix_world
@ Vector((0.0, r
,-h
*0.5+r
))
1035 vs
[9] = obj
.matrix_world
@ Vector((0.0,-r
,-h
*0.5+r
))
1037 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
1042 verts
+= [(v0
[0],v0
[1],v0
[2])]
1043 verts
+= [(v1
[0],v1
[1],v1
[2])]
1044 colours
+= [(0.5,1,0,1),(0.5,1,0,1)]
1046 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
1048 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
1049 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
1050 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
1051 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
1052 indices
= [(0,1),(1,2),(1,3)]
1056 verts
+= [(v0
[0],v0
[1],v0
[2])]
1057 verts
+= [(v1
[0],v1
[1],v1
[2])]
1058 colours
+= [(0,1,1,1),(0,1,1,1)]
1060 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1062 vs
[0] = obj
.location
1063 vs
[1] = obj
.cv_data
.target
.location
1068 verts
+= [(v0
[0],v0
[1],v0
[2])]
1069 verts
+= [(v1
[0],v1
[1],v1
[2])]
1070 colours
+= [(0,1,1,1),(0,1,1,1)]
1074 stack
[0] = obj
.cv_data
.target
1076 loop_complete
= False
1079 if stack_i
[si
-1] == 2:
1083 if si
== 0: # Loop failed to complete
1088 targets
= [None,None]
1089 targets
[0] = node
.cv_data
.target
1091 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1092 targets
[1] = node
.cv_data
.target1
1094 nextnode
= targets
[stack_i
[si
-1]]
1097 if nextnode
!= None: # branch
1098 if nextnode
== stack
[0]: # Loop completed
1099 loop_complete
= True
1103 for sj
in range(si
):
1104 if stack
[sj
] == nextnode
: # invalidated path
1110 stack
[si
] = nextnode
1115 cc
= Vector((obj
.cv_data
.colour
[0],\
1116 obj
.cv_data
.colour
[1],\
1117 obj
.cv_data
.colour
[2],\
1120 for sj
in range(si
):
1123 if stack
[sj
].cv_data
.classtype
== 'k_classtype_gate' and \
1124 stack
[sk
].cv_data
.classtype
== 'k_classtype_gate':
1125 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
1126 drawsbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
1129 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
1133 elif obj
.cv_data
.classtype
== 'k_classtype_car_path':
1134 v0
= obj
.matrix_world
.to_quaternion() @ Vector((0,1,0))
1135 c0
= Vector((v0
.x
*0.5+0.5, v0
.y
*0.5+0.5, 0.0, 1.0))
1136 drawbhandle( obj
, 1.0, (0.9,0.9,0.9,1.0) )
1138 if obj
.cv_data
.target
!= None:
1139 v1
= obj
.cv_data
.target
.matrix_world
.to_quaternion()@Vector((0,1,0))
1140 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1142 drawbhandle( obj
.cv_data
.target
, -1.0, (0.5,0.5,0.5,1.0) )
1143 drawbpath( obj
, obj
.cv_data
.target
, c0
, c1
)
1145 if obj
.cv_data
.target1
!= None:
1146 v1
= obj
.cv_data
.target1
.matrix_world
.to_quaternion()@Vector((0,1,0))
1147 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1149 drawbhandle( obj
.cv_data
.target1
, -1.0, (0.5,0.5,0.5,1.0) )
1150 drawbpath( obj
, obj
.cv_data
.target1
, c0
, c1
)
1152 lines
= batch_for_shader(\
1153 cv_view_shader
, 'LINES', \
1154 { "pos":verts
, "color":colours
})
1156 lines
.draw( cv_view_shader
)
1158 def cv_poll_target(scene
, obj
):
1159 if obj
== bpy
.context
.active_object
:
1161 if obj
.cv_data
.classtype
== 'k_classtype_none':
1165 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
1166 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1167 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1168 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
1169 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
1171 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1172 uid
: bpy
.props
.IntProperty( name
="" )
1174 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
1175 poll
=cv_poll_target
)
1176 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
1177 poll
=cv_poll_target
)
1179 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1182 classtype
: bpy
.props
.EnumProperty(
1185 ('k_classtype_none', "k_classtype_none", "", 0),
1186 ('k_classtype_gate', "k_classtype_gate", "", 1),
1187 ('k_classtype_block', "k_classtype_block", "", 2),
1188 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1189 ('k_classtype_water', "k_classtype_water", "", 4),
1190 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1191 ('k_classtype_INSTANCE', "","", 6 ),
1192 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1193 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1194 ('k_classtype_route', "k_classtype_route", "", 9 ),
1195 ('k_classtype_bone',"k_classtype_bone","",10),
1196 ('k_classtype_SKELETON', "","", 11 ),
1197 ('k_classtype_SKIN',"","",12)
1200 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1201 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1203 class CV_OBJ_PANEL(bpy
.types
.Panel
):
1204 bl_label
="Entity Config"
1205 bl_idname
="SCENE_PT_cv_entity"
1206 bl_space_type
='PROPERTIES'
1207 bl_region_type
='WINDOW'
1210 def draw(_
,context
):
1211 active_object
= bpy
.context
.active_object
1212 if active_object
== None: return
1213 _
.layout
.prop( active_object
.cv_data
, "classtype" )
1215 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1216 _
.layout
.prop( active_object
.cv_data
, "target" )
1218 mesh
= active_object
.data
1219 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1220 _
.layout
.prop( mesh
.cv_data
, "v0" )
1222 elif active_object
.cv_data
.classtype
== 'k_classtype_car_path' or \
1223 active_object
.cv_data
.classtype
== 'k_classtype_route_node':
1224 _
.layout
.prop( active_object
.cv_data
, "target" )
1225 _
.layout
.prop( active_object
.cv_data
, "target1" )
1227 elif active_object
.cv_data
.classtype
== 'k_classtype_route':
1228 _
.layout
.prop( active_object
.cv_data
, "target" )
1229 _
.layout
.prop( active_object
.cv_data
, "colour" )
1231 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1232 mesh
= active_object
.data
1234 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1235 _
.layout
.prop( mesh
.cv_data
, "v0" )
1236 _
.layout
.prop( mesh
.cv_data
, "v1" )
1237 _
.layout
.prop( mesh
.cv_data
, "v2" )
1238 _
.layout
.prop( mesh
.cv_data
, "v3" )
1239 elif active_object
.cv_data
.classtype
== 'k_classtype_capsule':
1240 mesh
= active_object
.data
1241 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1242 _
.layout
.prop( mesh
.cv_data
, "v0" )
1244 class CV_INTERFACE(bpy
.types
.Panel
):
1245 bl_idname
= "VIEW3D_PT_carve"
1247 bl_space_type
= 'VIEW_3D'
1248 bl_region_type
= 'UI'
1249 bl_category
= "Carve"
1251 def draw(_
, context
):
1253 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1254 layout
.operator( "carve.compile_all" )
1257 view_layer
= bpy
.context
.view_layer
1258 for col
in view_layer
.layer_collection
.children
["export"].children
:
1259 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
1260 write_model( col
.name
)
1262 class CV_COMPILE(bpy
.types
.Operator
):
1263 bl_idname
="carve.compile_all"
1264 bl_label
="Compile All"
1266 def execute(_
,context
):
1268 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1269 #for col in bpy.data.collections["export"].children:
1270 # write_model( col.name )
1274 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1275 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
]
1278 global cv_view_draw_handler
1281 bpy
.utils
.register_class(c
)
1283 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
1284 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
1285 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
1287 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1288 cv_draw
,(),'WINDOW','POST_VIEW')
1291 global cv_view_draw_handler
1294 bpy
.utils
.unregister_class(c
)
1296 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')