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)]
161 class subclass_audio_channel(Structure
):
163 _fields_
= [("sound",c_uint32
),
167 class classtype_audio_system(Structure
):
171 class classtype_audio_zone(Structure
):
173 _fields_
= [("system",c_uint32
)]
176 # ==============================================================================
178 def write_model(collection_name
):
179 print( F
"Model graph | Create mode '{collection_name}'" )
181 header
= mdl_header()
182 header
.identifier
= 0xABCD0000
184 header
.vertex_count
= 0
185 header
.indice_count
= 0
186 header
.submesh_count
= 0
187 header
.node_count
= 0
188 header
.material_count
= 0
189 header
.file_length
= 0
209 def emplace_string( s
):
210 nonlocal string_cache
, strings_buffer
212 if s
in string_cache
:
213 return string_cache
[s
]
215 string_cache
[s
] = len( strings_buffer
)
216 strings_buffer
+= (s
+'\0').encode('utf-8')
217 return string_cache
[s
]
219 def emplace_material( mat
):
220 nonlocal material_cache
, material_buffer
225 if mat
.name
in material_cache
:
226 return material_cache
[mat
.name
]
228 material_cache
[mat
.name
] = header
.material_count
229 dest
= mdl_material()
230 dest
.pstr_name
= emplace_string( mat
.name
)
231 material_buffer
+= [dest
]
233 header
.material_count
+= 1
234 return material_cache
[mat
.name
]
236 # Create root or empty node and materials
237 # this is to designate id 0 as 'NULL'
239 none_material
= c_uint32(69)
240 none_material
.name
= ""
241 emplace_material( none_material
)
254 root
.pstr_name
= emplace_string('')
255 root
.submesh_start
= 0
256 root
.submesh_count
= 0
259 node_buffer
+= [root
]
263 print( " assigning ids" )
264 collection
= bpy
.data
.collections
[collection_name
]
267 # ==========================================
269 header
.node_count
= 0
272 uid
= header
.node_count
273 header
.node_count
+= 1
276 print( " creating scene graph" )
280 graph
["children"] = []
281 graph
["uid"] = _uid()
282 graph
["parent"] = None
284 graph_lookup
= {} # object can lookup its graph def here
286 for obj
in collection
.all_objects
:
289 def _extend( p
, n
, d
):
293 tree
["children"] = []
299 if n
.type == 'ARMATURE':
300 tree
["bones"] = [None] # None is the root transform
302 tree
["collider_count"] = 0
304 def _extendb( p
, n
, d
):
309 btree
["uid"] = _uid()
310 btree
["children"] = []
313 tree
["bones"] += [n
.name
]
316 _extendb( btree
, c
, d
+1 )
318 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
320 btree
["target"] = c
.subtarget
321 btree
["pole"] = c
.pole_subtarget
322 tree
["ik_count"] += 1
324 if n
.cv_data
.collider
:
325 tree
['collider_count'] += 1
327 btree
['deform'] = n
.use_deform
328 p
['children'] += [btree
]
330 for b
in n
.data
.bones
:
332 _extendb( tree
, b
, d
+1 )
334 for obj1
in n
.children
:
335 _extend( tree
, obj1
, d
+1 )
337 p
["children"] += [tree
]
338 graph_lookup
[n
] = tree
340 _extend( graph
, obj
, 1 )
344 for c
in p
['children']:
346 yield from _graph_iter(c
)
348 it
= _graph_iter(graph
)
350 root
.parent
= 0xffffffff
353 # ==============================================
354 it
= _graph_iter(graph
)
355 print( " compiling data" )
357 if 'obj' in node_def
:
358 obj
= node_def
['obj']
361 elif 'bone' in node_def
:
362 obj
= node_def
['bone']
364 objco
= obj
.head_local
366 depth
= node_def
['depth']
367 uid
= node_def
['uid']
370 node
.co
[0] = objco
[0]
371 node
.co
[1] = objco
[2]
372 node
.co
[2] = -objco
[1]
374 # Convert rotation quat to our space type
375 quat
= obj
.matrix_local
.to_quaternion()
382 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
383 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
384 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
386 node
.s
[0] = obj
.scale
[0]
387 node
.s
[1] = obj
.scale
[2]
388 node
.s
[2] = obj
.scale
[1]
390 node
.pstr_name
= emplace_string( obj
.name
)
392 if node_def
["parent"]:
393 node
.parent
= node_def
["parent"]["uid"]
396 classtype
= 'k_classtype_bone'
397 elif objt
== 'ARMATURE':
398 classtype
= 'k_classtype_skeleton'
400 classtype
= obj
.cv_data
.classtype
403 # =================================================================
406 # Dont use the cache if we have modifiers that affect the normals
414 for mod
in obj
.modifiers
:
415 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
416 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
418 can_use_cache
= False
420 if mod
.type == 'ARMATURE':
421 classtype
= 'k_classtype_skin'
422 armature_def
= graph_lookup
[mod
.object]
423 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
425 armature_def
['obj'].data
.pose_position
= 'REST'
427 if can_use_cache
and obj
.data
.name
in mesh_cache
:
428 ref
= mesh_cache
[obj
.data
.name
]
429 node
.submesh_start
= ref
.submesh_start
430 node
.submesh_count
= ref
.submesh_count
434 node
.submesh_start
= header
.submesh_count
435 node
.submesh_count
= 0
437 default_mat
= c_uint32(69)
438 default_mat
.name
= ""
440 dgraph
= bpy
.context
.evaluated_depsgraph_get()
441 data
= obj
.evaluated_get(dgraph
).data
442 data
.calc_loop_triangles()
443 data
.calc_normals_split()
445 mat_list
= data
.materials
if len(data
.materials
) > 0 else [default_mat
]
446 for material_id
, mat
in enumerate(mat_list
):
450 sm
.indice_start
= header
.indice_count
451 sm
.vertex_start
= header
.vertex_count
454 sm
.material_id
= emplace_material( mat
)
457 sm
.bbx
[0][i
] = 999999
458 sm
.bbx
[1][i
] = -999999
462 # Write the vertex / indice data
464 for tri_index
, tri
in enumerate(data
.loop_triangles
):
465 if tri
.material_index
!= material_id
:
469 vert
= data
.vertices
[tri
.vertices
[j
]]
471 vi
= data
.loops
[li
].vertex_index
474 norm
= data
.loops
[li
].normal
476 colour
= (255,255,255,255)
481 uv
= data
.uv_layers
.active
.data
[li
].uv
483 if data
.vertex_colors
:
484 colour
= data
.vertex_colors
.active
.data
[li
].color
485 colour
= (int(colour
[0]*255.0),\
486 int(colour
[1]*255.0),\
487 int(colour
[2]*255.0),\
488 int(colour
[3]*255.0))
493 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
494 if obj
.vertex_groups
[_
.group
].name
in \
495 armature_def
['bones']]
497 weight_groups
= sorted( src_groups
, key
= \
498 lambda a
: a
.weight
, reverse
=True )
501 if len(weight_groups
) > ml
:
502 g
= weight_groups
[ml
]
503 name
= obj
.vertex_groups
[g
.group
].name
507 groups
[ml
] = armature_def
['bones'].index(name
)
510 if len(weight_groups
) > 0:
511 inv_norm
= (1.0/tot
) * 65535.0
513 weights
[ml
] = int( weights
[ml
] * inv_norm
)
514 weights
[ml
] = min( weights
[ml
], 65535 )
515 weights
[ml
] = max( weights
[ml
], 0 )
518 m
= float(10**TOLERENCE
)
520 key
= (int(co
[0]*m
+0.5),\
542 indice_buffer
+= [boffa
[key
]]
544 index
= c_uint32(sm
.vertex_count
)
548 indice_buffer
+= [index
]
559 v
.colour
[0] = colour
[0]
560 v
.colour
[1] = colour
[1]
561 v
.colour
[2] = colour
[2]
562 v
.colour
[3] = colour
[3]
563 v
.weights
[0] = weights
[0]
564 v
.weights
[1] = weights
[1]
565 v
.weights
[2] = weights
[2]
566 v
.weights
[3] = weights
[3]
567 v
.groups
[0] = groups
[0]
568 v
.groups
[1] = groups
[1]
569 v
.groups
[2] = groups
[2]
570 v
.groups
[3] = groups
[3]
575 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
576 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
580 if sm
.vertex_count
== 0:
585 submesh_buffer
+= [sm
]
586 node
.submesh_count
+= 1
587 header
.submesh_count
+= 1
588 header
.vertex_count
+= sm
.vertex_count
589 header
.indice_count
+= sm
.indice_count
591 mesh_cache
[obj
.data
.name
] = node
593 # Process entity data
594 # ==================================================================
595 node
.offset
= entdata_length
597 if classtype
!= 'k_classtype_none':
602 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
603 s001
= F
" L {obj.name}"
606 s004
= F
"{node.parent: 3}"
609 if classtype
== 'k_classtype_skin':
610 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
611 s005
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
613 scmp
= F
"{s002:<32} {s003:<22} {s004} {s005}"
616 if classtype
== 'k_classtype_INSTANCE' or \
617 classtype
== 'k_classtype_BONE' or \
618 classtype
== 'k_classtype_SKELETON' or \
619 classtype
== 'k_classtype_SKIN':
620 print( "ERROR: user classtype cannot be _INSTANCE or _BONE" )
624 elif classtype
== 'k_classtype_skin':
627 armature
= armature_def
['obj']
628 entdata_length
+= sizeof( classtype_skin
)
630 skin
= classtype_skin()
631 skin
.skeleton
= armature
.cv_data
.uid
632 entdata_buffer
+= [skin
]
634 elif classtype
== 'k_classtype_skeleton':
636 entdata_length
+= sizeof( classtype_skeleton
)
637 skeleton
= classtype_skeleton()
639 armature_def
= graph_lookup
[obj
]
641 bones
= armature_def
['bones']
642 skeleton
.channels
= len(bones
)
643 skeleton
.ik_count
= armature_def
["ik_count"]
644 skeleton
.collider_count
= armature_def
["collider_count"]
646 if armature
.animation_data
:
647 previous_frame
= bpy
.context
.scene
.frame_current
648 previous_action
= armature
.animation_data
.action
650 skeleton
.anim_start
= len(anim_buffer
)
651 skeleton
.anim_count
= 0
653 for NLALayer
in obj
.animation_data
.nla_tracks
:
654 for NLAStrip
in NLALayer
.strips
:
656 for a
in bpy
.data
.actions
:
657 if a
.name
== NLAStrip
.name
:
658 armature
.animation_data
.action
= a
661 anim_start
= int(NLAStrip
.action_frame_start
)
662 anim_end
= int(NLAStrip
.action_frame_end
)
665 anim
= mdl_animation()
666 anim
.pstr_name
= emplace_string( NLAStrip
.action
.name
)
668 anim
.offset
= animdata_length
669 anim
.length
= anim_end
-anim_start
671 # Export the fucking keyframes
672 for frame
in range(anim_start
,anim_end
):
673 bpy
.context
.scene
.frame_set(frame
)
675 for bone_name
in bones
:
676 for pb
in armature
.pose
.bones
:
677 if pb
.name
== bone_name
:
678 rb
= armature
.data
.bones
[ bone_name
]
680 # relative bone matrix
681 if rb
.parent
is not None:
682 offset_mtx
= rb
.parent
.matrix_local
683 offset_mtx
= offset_mtx
.inverted_safe() @ \
686 inv_parent
= pb
.parent
.matrix
@ offset_mtx
687 inv_parent
.invert_safe()
688 fpm
= inv_parent
@ pb
.matrix
690 bone_mtx
= rb
.matrix
.to_4x4()
691 local_inv
= rb
.matrix_local
.inverted_safe()
692 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
694 loc
, rot
, sca
= fpm
.decompose()
697 final_pos
= Vector(( loc
[0], loc
[2], -loc
[1] ))
700 lc_m
= pb
.matrix_channel
.to_3x3()
701 if pb
.parent
is not None:
702 smtx
= pb
.parent
.matrix_channel
.to_3x3()
703 lc_m
= smtx
.inverted() @ lc_m
704 rq
= lc_m
.to_quaternion()
707 kf
.co
[0] = final_pos
[0]
708 kf
.co
[1] = final_pos
[1]
709 kf
.co
[2] = final_pos
[2]
721 animdata_buffer
+= [kf
]
722 animdata_length
+= sizeof(mdl_keyframe
)
725 anim_buffer
+= [anim
]
726 skeleton
.anim_count
+= 1
728 s000
= F
" [{uid: 3}/{header.node_count-1}]" + " |"*(depth
-1)
729 print( F
"{s000} | *anim: {NLAStrip.action.name}" )
731 bpy
.context
.scene
.frame_set( previous_frame
)
732 armature
.animation_data
.action
= previous_action
734 entdata_buffer
+= [skeleton
]
736 elif classtype
== 'k_classtype_bone':
738 entdata_length
+= sizeof( classtype_bone
)
740 bone
= classtype_bone()
741 bone
.deform
= node_def
['deform']
743 if 'target' in node_def
:
744 bone
.ik_target
= armature_def
['bones'].index( node_def
['target'] )
745 bone
.ik_pole
= armature_def
['bones'].index( node_def
['pole'] )
750 bone
.collider
= 1 if obj
.cv_data
.collider
else 0
751 if obj
.cv_data
.collider
:
752 bone
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
753 bone
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
754 bone
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
755 bone
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
756 bone
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
757 bone
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
759 bone
.hitbox
[0][0] = 0.0
760 bone
.hitbox
[0][1] = 0.0
761 bone
.hitbox
[0][2] = 0.0
762 bone
.hitbox
[1][0] = 0.0
763 bone
.hitbox
[1][1] = 0.0
764 bone
.hitbox
[1][2] = 0.0
768 bone
.angle_limits
[0][0] = obj
.cv_data
.mins
[0]
769 bone
.angle_limits
[0][1] = obj
.cv_data
.mins
[2]
770 bone
.angle_limits
[0][2] = -obj
.cv_data
.maxs
[1]
771 bone
.angle_limits
[1][0] = obj
.cv_data
.maxs
[0]
772 bone
.angle_limits
[1][1] = obj
.cv_data
.maxs
[2]
773 bone
.angle_limits
[1][2] = -obj
.cv_data
.mins
[1]
776 bone
.angle_limits
[0][0] = 0.0
777 bone
.angle_limits
[0][1] = 0.0
778 bone
.angle_limits
[0][2] = 0.0
779 bone
.angle_limits
[1][0] = 0.0
780 bone
.angle_limits
[1][1] = 0.0
781 bone
.angle_limits
[1][2] = 0.0
783 bone
.deform
= node_def
['deform']
784 entdata_buffer
+= [bone
]
786 elif classtype
== 'k_classtype_gate':
788 entdata_length
+= sizeof( classtype_gate
)
790 gate
= classtype_gate()
792 if obj
.cv_data
.target
!= None:
793 gate
.target
= obj
.cv_data
.target
.cv_data
.uid
795 if obj
.type == 'MESH':
796 gate
.dims
[0] = obj
.data
.cv_data
.v0
[0]
797 gate
.dims
[1] = obj
.data
.cv_data
.v0
[1]
798 gate
.dims
[2] = obj
.data
.cv_data
.v0
[2]
800 gate
.dims
[0] = obj
.cv_data
.v0
[0]
801 gate
.dims
[1] = obj
.cv_data
.v0
[1]
802 gate
.dims
[2] = obj
.cv_data
.v0
[2]
804 entdata_buffer
+= [gate
]
806 elif classtype
== 'k_classtype_block':
808 entdata_length
+= sizeof( classtype_block
)
810 source
= obj
.data
.cv_data
812 block
= classtype_block()
813 block
.bbx
[0][0] = source
.v0
[0]
814 block
.bbx
[0][1] = source
.v0
[2]
815 block
.bbx
[0][2] = -source
.v1
[1]
817 block
.bbx
[1][0] = source
.v1
[0]
818 block
.bbx
[1][1] = source
.v1
[2]
819 block
.bbx
[1][2] = -source
.v0
[1]
820 entdata_buffer
+= [block
]
822 elif classtype
== 'k_classtype_spawn':
825 elif classtype
== 'k_classtype_water':
828 elif classtype
== 'k_classtype_car_path':
830 entdata_length
+= sizeof( classtype_car_path
)
832 pn
= classtype_car_path()
836 if obj
.cv_data
.target
!= None:
837 pn
.target
= obj
.cv_data
.target
.cv_data
.uid
838 if obj
.cv_data
.target1
!= None:
839 pn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
841 entdata_buffer
+= [pn
]
843 elif obj
.is_instancer
:
844 target
= obj
.instance_collection
847 entdata_length
+= sizeof( classtype_instance
)
849 inst
= classtype_instance()
850 inst
.pstr_file
= emplace_string( F
"models/{target.name}.mdl" )
851 entdata_buffer
+= [inst
]
853 elif classtype
== 'k_classtype_capsule':
856 elif classtype
== 'k_classtype_route_node':
858 entdata_length
+= sizeof( classtype_route_node
)
860 rn
= classtype_route_node()
861 if obj
.cv_data
.target
!= None:
862 rn
.target
= obj
.cv_data
.target
.cv_data
.uid
863 if obj
.cv_data
.target1
!= None:
864 rn
.target1
= obj
.cv_data
.target1
.cv_data
.uid
866 entdata_buffer
+= [rn
]
868 elif classtype
== 'k_classtype_route':
870 entdata_length
+= sizeof( classtype_route
)
871 r
= classtype_route()
872 r
.pstr_name
= emplace_string("not-implemented")
873 r
.colour
[0] = obj
.cv_data
.colour
[0]
874 r
.colour
[1] = obj
.cv_data
.colour
[1]
875 r
.colour
[2] = obj
.cv_data
.colour
[2]
877 if obj
.cv_data
.target
!= None:
878 r
.id_start
= obj
.cv_data
.target
.cv_data
.uid
880 entdata_buffer
+= [r
]
882 # classtype == 'k_classtype_none':
887 node_buffer
+= [node
]
891 header
.anim_count
= len(anim_buffer
)
893 print( "Writing data" )
894 fpos
= sizeof(header
)
896 print( F
"Nodes: {header.node_count}" )
897 header
.node_offset
= fpos
898 fpos
+= sizeof(mdl_node
)*header
.node_count
900 print( F
"Submeshes: {header.submesh_count}" )
901 header
.submesh_offset
= fpos
902 fpos
+= sizeof(mdl_submesh
)*header
.submesh_count
904 print( F
"Materials: {header.material_count}" )
905 header
.material_offset
= fpos
906 fpos
+= sizeof(mdl_material
)*header
.material_count
908 print( F
"Animation count: {header.anim_count}" )
909 header
.anim_offset
= fpos
910 fpos
+= sizeof(mdl_animation
)*header
.anim_count
912 print( F
"Entdata length: {entdata_length}" )
913 header
.entdata_offset
= fpos
914 fpos
+= entdata_length
916 print( F
"Vertex count: {header.vertex_count}" )
917 header
.vertex_offset
= fpos
918 fpos
+= sizeof(mdl_vert
)*header
.vertex_count
920 print( F
"Indice count: {header.indice_count}" )
921 header
.indice_offset
= fpos
922 fpos
+= sizeof(c_uint32
)*header
.indice_count
924 print( F
"Keyframe count: {animdata_length}" )
925 header
.animdata_offset
= fpos
926 fpos
+= animdata_length
928 print( F
"Strings length: {len(strings_buffer)}" )
929 header
.strings_offset
= fpos
930 fpos
+= len(strings_buffer
)
932 header
.file_length
= fpos
934 path
= F
"/home/harry/Documents/carve/models_src/{collection_name}.mdl"
935 fp
= open( path
, "wb" )
937 fp
.write( bytearray( header
) )
939 for node
in node_buffer
:
940 fp
.write( bytearray(node
) )
941 for sm
in submesh_buffer
:
942 fp
.write( bytearray(sm
) )
943 for mat
in material_buffer
:
944 fp
.write( bytearray(mat
) )
945 for a
in anim_buffer
:
946 fp
.write( bytearray(a
) )
947 for ed
in entdata_buffer
:
948 fp
.write( bytearray(ed
) )
949 for v
in vertex_buffer
:
950 fp
.write( bytearray(v
) )
951 for i
in indice_buffer
:
952 fp
.write( bytearray(i
) )
953 for kf
in animdata_buffer
:
954 fp
.write( bytearray(kf
) )
956 fp
.write( strings_buffer
)
959 print( F
"Completed {collection_name}.mdl" )
962 # ------------------------------------------------------------------------------
964 cv_view_draw_handler
= None
965 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
968 global cv_view_shader
969 cv_view_shader
.bind()
970 gpu
.state
.depth_mask_set(False)
971 gpu
.state
.line_width_set(2.0)
972 gpu
.state
.face_culling_set('BACK')
973 gpu
.state
.depth_test_set('LESS')
974 gpu
.state
.blend_set('NONE')
979 #def drawbezier(p0,h0,p1,h1,c0,c1):
980 # nonlocal verts, colours
984 # colours += [(0.5,0.5,0.5,1.0),(0.5,0.5,0.5,1)]
987 # colours += [(1.0,1.0,1,1),(1,1,1,1)]
990 # for i in range(10):
996 # p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
997 # verts += [(last[0],last[1],last[2])]
998 # verts += [(p[0],p[1],p[2])]
999 # colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
1004 def drawbhandle(obj
, direction
, colour
):
1005 nonlocal verts
, colours
1007 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
1010 colours
+= [colour
,colour
]
1012 def drawbezier(p0
,h0
,p1
,h1
,c0
,c1
):
1013 nonlocal verts
, colours
1022 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
1023 verts
+= [(last
[0],last
[1],last
[2])]
1024 verts
+= [(p
[0],p
[1],p
[2])]
1025 colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
1028 def drawsbpath(o0
,o1
,c0
,c1
,s0
,s1
):
1029 nonlocal course_count
1031 offs
= ((course_count
% 2)*2-1) * course_count
* 0.02
1033 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
1034 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
1035 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
1036 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
1037 drawbezier(p0
,h0
,p1
,h1
,c0
,c1
)
1039 def drawbpath(o0
,o1
,c0
,c1
):
1040 drawsbpath(o0
,o1
,c0
,c1
,1.0,1.0)
1042 def drawbline(p0
,p1
,c0
,c1
):
1043 nonlocal verts
, colours
1047 for obj
in bpy
.context
.collection
.objects
:
1048 if obj
.type == 'ARMATURE':
1049 for bone
in obj
.data
.bones
:
1050 if bone
.cv_data
.collider
and obj
.data
.pose_position
== 'REST':
1056 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
1057 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
1058 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
1059 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
1060 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
1061 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
1062 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
1063 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
1065 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1066 (0,4),(1,5),(2,6),(3,7)]
1071 verts
+= [(v0
[0],v0
[1],v0
[2])]
1072 verts
+= [(v1
[0],v1
[1],v1
[2])]
1073 colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
1075 center
=obj
.matrix_world
@c
1077 def _angle_lim( major
, minor
, amin
, amax
, colour
):
1078 nonlocal verts
, colours
1086 a0
= amin
*(1.0-t0
)+amax
*t0
1087 a1
= amin
*(1.0-t1
)+amax
*t1
1089 p0
= c
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
1090 p1
= c
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
1092 p0
=obj
.matrix_world
@ p0
1093 p1
=obj
.matrix_world
@ p1
1095 colours
+= [colour
,colour
]
1099 colours
+= [colour
,colour
]
1102 colours
+= [colour
,colour
]
1104 verts
+= [c
+major
*1.2*f
,c
+major
*f
*0.8]
1105 colours
+= [colour
,colour
]
1107 if bone
.cv_data
.con0
:
1108 _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
1109 bone
.cv_data
.mins
[0], bone
.cv_data
.maxs
[0], \
1111 _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
1112 bone
.cv_data
.mins
[1], bone
.cv_data
.maxs
[1], \
1114 _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
1115 bone
.cv_data
.mins
[2], bone
.cv_data
.maxs
[2], \
1119 if obj
.cv_data
.classtype
== 'k_classtype_gate':
1120 if obj
.type == 'MESH':
1121 dims
= obj
.data
.cv_data
.v0
1123 dims
= obj
.cv_data
.v0
1126 c
= Vector((0,0,dims
[2]))
1128 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
1129 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
1130 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
1131 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
1132 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
1133 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
1134 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
1135 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
1136 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
1138 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
1143 verts
+= [(v0
[0],v0
[1],v0
[2])]
1144 verts
+= [(v1
[0],v1
[1],v1
[2])]
1145 colours
+= [(1,1,0,1),(1,1,0,1)]
1147 sw
= (0.4,0.4,0.4,0.2)
1148 if obj
.cv_data
.target
!= None:
1149 drawbline( obj
.location
, obj
.cv_data
.target
.location
, sw
,sw
)
1151 elif obj
.cv_data
.classtype
== 'k_classtype_route_node':
1152 sw
= Vector((0.4,0.4,0.4,0.2))
1153 sw2
= Vector((1.5,0.2,0.2,0.0))
1154 if obj
.cv_data
.target
!= None:
1155 drawbpath( obj
, obj
.cv_data
.target
, sw
, sw
)
1156 if obj
.cv_data
.target1
!= None:
1157 drawbpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
1159 drawbhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
1160 drawbhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
1162 p1
= obj
.location
+ \
1163 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
1164 drawbline( obj
.location
, p1
, sw
,sw2
)
1167 elif obj
.cv_data
.classtype
== 'k_classtype_block':
1168 a
= obj
.data
.cv_data
.v0
1169 b
= obj
.data
.cv_data
.v1
1172 vs
[0] = obj
.matrix_world
@ Vector((a
[0], a
[1], a
[2]))
1173 vs
[1] = obj
.matrix_world
@ Vector((a
[0], b
[1], a
[2]))
1174 vs
[2] = obj
.matrix_world
@ Vector((b
[0], b
[1], a
[2]))
1175 vs
[3] = obj
.matrix_world
@ Vector((b
[0], a
[1], a
[2]))
1176 vs
[4] = obj
.matrix_world
@ Vector((a
[0], a
[1], b
[2]))
1177 vs
[5] = obj
.matrix_world
@ Vector((a
[0], b
[1], b
[2]))
1178 vs
[6] = obj
.matrix_world
@ Vector((b
[0], b
[1], b
[2]))
1179 vs
[7] = obj
.matrix_world
@ Vector((b
[0], a
[1], b
[2]))
1181 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1182 (0,4),(1,5),(2,6),(3,7)]
1187 verts
+= [(v0
[0],v0
[1],v0
[2])]
1188 verts
+= [(v1
[0],v1
[1],v1
[2])]
1189 colours
+= [(1,1,0,1),(1,1,0,1)]
1191 elif obj
.cv_data
.classtype
== 'k_classtype_capsule':
1192 h
= obj
.data
.cv_data
.v0
[0]
1193 r
= obj
.data
.cv_data
.v0
[1]
1196 vs
[0] = obj
.matrix_world
@ Vector((0.0,0.0, h
*0.5 ))
1197 vs
[1] = obj
.matrix_world
@ Vector((0.0,0.0,-h
*0.5 ))
1198 vs
[2] = obj
.matrix_world
@ Vector(( r
,0.0, h
*0.5-r
))
1199 vs
[3] = obj
.matrix_world
@ Vector(( -r
,0.0, h
*0.5-r
))
1200 vs
[4] = obj
.matrix_world
@ Vector(( r
,0.0,-h
*0.5+r
))
1201 vs
[5] = obj
.matrix_world
@ Vector(( -r
,0.0,-h
*0.5+r
))
1202 vs
[6] = obj
.matrix_world
@ Vector((0.0, r
, h
*0.5-r
))
1203 vs
[7] = obj
.matrix_world
@ Vector((0.0,-r
, h
*0.5-r
))
1204 vs
[8] = obj
.matrix_world
@ Vector((0.0, r
,-h
*0.5+r
))
1205 vs
[9] = obj
.matrix_world
@ Vector((0.0,-r
,-h
*0.5+r
))
1207 indices
= [(0,1),(2,3),(4,5),(6,7),(8,9)]
1212 verts
+= [(v0
[0],v0
[1],v0
[2])]
1213 verts
+= [(v1
[0],v1
[1],v1
[2])]
1214 colours
+= [(0.5,1,0,1),(0.5,1,0,1)]
1216 elif obj
.cv_data
.classtype
== 'k_classtype_spawn':
1218 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
1219 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
1220 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
1221 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
1222 indices
= [(0,1),(1,2),(1,3)]
1226 verts
+= [(v0
[0],v0
[1],v0
[2])]
1227 verts
+= [(v1
[0],v1
[1],v1
[2])]
1228 colours
+= [(0,1,1,1),(0,1,1,1)]
1230 elif obj
.cv_data
.classtype
== 'k_classtype_route':
1232 vs
[0] = obj
.location
1233 vs
[1] = obj
.cv_data
.target
.location
1238 verts
+= [(v0
[0],v0
[1],v0
[2])]
1239 verts
+= [(v1
[0],v1
[1],v1
[2])]
1240 colours
+= [(0,1,1,1),(0,1,1,1)]
1244 stack
[0] = obj
.cv_data
.target
1246 loop_complete
= False
1249 if stack_i
[si
-1] == 2:
1253 if si
== 0: # Loop failed to complete
1258 targets
= [None,None]
1259 targets
[0] = node
.cv_data
.target
1261 if node
.cv_data
.classtype
== 'k_classtype_route_node':
1262 targets
[1] = node
.cv_data
.target1
1264 nextnode
= targets
[stack_i
[si
-1]]
1267 if nextnode
!= None: # branch
1268 if nextnode
== stack
[0]: # Loop completed
1269 loop_complete
= True
1273 for sj
in range(si
):
1274 if stack
[sj
] == nextnode
: # invalidated path
1280 stack
[si
] = nextnode
1285 cc
= Vector((obj
.cv_data
.colour
[0],\
1286 obj
.cv_data
.colour
[1],\
1287 obj
.cv_data
.colour
[2],\
1290 for sj
in range(si
):
1293 if stack
[sj
].cv_data
.classtype
== 'k_classtype_gate' and \
1294 stack
[sk
].cv_data
.classtype
== 'k_classtype_gate':
1295 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
1296 drawsbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
1299 drawbpath( stack
[sj
], stack
[sk
], cc
, cc
)
1303 elif obj
.cv_data
.classtype
== 'k_classtype_car_path':
1304 v0
= obj
.matrix_world
.to_quaternion() @ Vector((0,1,0))
1305 c0
= Vector((v0
.x
*0.5+0.5, v0
.y
*0.5+0.5, 0.0, 1.0))
1306 drawbhandle( obj
, 1.0, (0.9,0.9,0.9,1.0) )
1308 if obj
.cv_data
.target
!= None:
1309 v1
= obj
.cv_data
.target
.matrix_world
.to_quaternion()@Vector((0,1,0))
1310 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1312 drawbhandle( obj
.cv_data
.target
, -1.0, (0.5,0.5,0.5,1.0) )
1313 drawbpath( obj
, obj
.cv_data
.target
, c0
, c1
)
1315 if obj
.cv_data
.target1
!= None:
1316 v1
= obj
.cv_data
.target1
.matrix_world
.to_quaternion()@Vector((0,1,0))
1317 c1
= Vector((v1
.x
*0.5+0.5, v1
.y
*0.5+0.5, 0.0, 1.0))
1319 drawbhandle( obj
.cv_data
.target1
, -1.0, (0.5,0.5,0.5,1.0) )
1320 drawbpath( obj
, obj
.cv_data
.target1
, c0
, c1
)
1322 lines
= batch_for_shader(\
1323 cv_view_shader
, 'LINES', \
1324 { "pos":verts
, "color":colours
})
1326 lines
.draw( cv_view_shader
)
1328 def cv_poll_target(scene
, obj
):
1329 if obj
== bpy
.context
.active_object
:
1331 if obj
.cv_data
.classtype
== 'k_classtype_none':
1335 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
1336 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1337 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1338 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
1339 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
1341 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
1342 uid
: bpy
.props
.IntProperty( name
="" )
1344 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
1345 poll
=cv_poll_target
)
1346 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
1347 poll
=cv_poll_target
)
1349 colour
: bpy
.props
.FloatVectorProperty(name
="colour",subtype
='COLOR',\
1352 classtype
: bpy
.props
.EnumProperty(
1355 ('k_classtype_none', "k_classtype_none", "", 0),
1356 ('k_classtype_gate', "k_classtype_gate", "", 1),
1357 ('k_classtype_block', "k_classtype_block", "", 2),
1358 ('k_classtype_spawn', "k_classtype_spawn", "", 3),
1359 ('k_classtype_water', "k_classtype_water", "", 4),
1360 ('k_classtype_car_path', "k_classtype_car_path", "", 5),
1361 ('k_classtype_INSTANCE', "","", 6 ),
1362 ('k_classtype_capsule', "k_classtype_capsule", "", 7 ),
1363 ('k_classtype_route_node', "k_classtype_route_node", "", 8 ),
1364 ('k_classtype_route', "k_classtype_route", "", 9 ),
1365 ('k_classtype_bone',"k_classtype_bone","",10),
1366 ('k_classtype_SKELETON', "","", 11 ),
1367 ('k_classtype_SKIN',"","",12)
1370 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
1371 collider
: bpy
.props
.BoolProperty(name
="Collider",default
=False)
1372 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
1373 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
1375 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
1376 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
1377 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
1379 class CV_BONE_PANEL(bpy
.types
.Panel
):
1380 bl_label
="Bone Config"
1381 bl_idname
="SCENE_PT_cv_bone"
1382 bl_space_type
='PROPERTIES'
1383 bl_region_type
='WINDOW'
1386 def draw(_
,context
):
1387 active_object
= context
.active_object
1388 if active_object
== None: return
1390 bone
= active_object
.data
.bones
.active
1391 if bone
== None: return
1393 _
.layout
.prop( bone
.cv_data
, "collider" )
1394 _
.layout
.prop( bone
.cv_data
, "v0" )
1395 _
.layout
.prop( bone
.cv_data
, "v1" )
1397 _
.layout
.label( text
="Angle Limits" )
1398 _
.layout
.prop( bone
.cv_data
, "con0" )
1399 _
.layout
.prop( bone
.cv_data
, "mins" )
1400 _
.layout
.prop( bone
.cv_data
, "maxs" )
1402 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1403 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1405 class CV_OBJ_PANEL(bpy
.types
.Panel
):
1406 bl_label
="Entity Config"
1407 bl_idname
="SCENE_PT_cv_entity"
1408 bl_space_type
='PROPERTIES'
1409 bl_region_type
='WINDOW'
1412 def draw(_
,context
):
1413 active_object
= bpy
.context
.active_object
1414 if active_object
== None: return
1415 _
.layout
.prop( active_object
.cv_data
, "classtype" )
1417 if active_object
.cv_data
.classtype
== 'k_classtype_gate':
1418 _
.layout
.prop( active_object
.cv_data
, "target" )
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 elif active_object
.cv_data
.classtype
== 'k_classtype_car_path' or \
1425 active_object
.cv_data
.classtype
== 'k_classtype_route_node':
1426 _
.layout
.prop( active_object
.cv_data
, "target" )
1427 _
.layout
.prop( active_object
.cv_data
, "target1" )
1429 elif active_object
.cv_data
.classtype
== 'k_classtype_route':
1430 _
.layout
.prop( active_object
.cv_data
, "target" )
1431 _
.layout
.prop( active_object
.cv_data
, "colour" )
1433 elif active_object
.cv_data
.classtype
== 'k_classtype_block':
1434 mesh
= active_object
.data
1436 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1437 _
.layout
.prop( mesh
.cv_data
, "v0" )
1438 _
.layout
.prop( mesh
.cv_data
, "v1" )
1439 _
.layout
.prop( mesh
.cv_data
, "v2" )
1440 _
.layout
.prop( mesh
.cv_data
, "v3" )
1441 elif active_object
.cv_data
.classtype
== 'k_classtype_capsule':
1442 mesh
= active_object
.data
1443 _
.layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
1444 _
.layout
.prop( mesh
.cv_data
, "v0" )
1446 class CV_INTERFACE(bpy
.types
.Panel
):
1447 bl_idname
= "VIEW3D_PT_carve"
1449 bl_space_type
= 'VIEW_3D'
1450 bl_region_type
= 'UI'
1451 bl_category
= "Carve"
1453 def draw(_
, context
):
1455 layout
.prop( context
.scene
.cv_data
, "use_hidden")
1456 layout
.operator( "carve.compile_all" )
1459 view_layer
= bpy
.context
.view_layer
1460 for col
in view_layer
.layer_collection
.children
["export"].children
:
1461 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
1462 write_model( col
.name
)
1464 class CV_COMPILE(bpy
.types
.Operator
):
1465 bl_idname
="carve.compile_all"
1466 bl_label
="Compile All"
1468 def execute(_
,context
):
1470 #cProfile.runctx("test_compile()",globals(),locals(),sort=1)
1471 #for col in bpy.data.collections["export"].children:
1472 # write_model( col.name )
1476 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
1477 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
1481 global cv_view_draw_handler
1484 bpy
.utils
.register_class(c
)
1486 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
1487 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
1488 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
1489 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
1491 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
1492 cv_draw
,(),'WINDOW','POST_VIEW')
1495 global cv_view_draw_handler
1498 bpy
.utils
.unregister_class(c
)
1500 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')