2 # =============================================================================
4 # Copyright . . . -----, ,----- ,---. .---.
5 # 2021-2023 |\ /| | / | | | | /|
6 # | \ / | +-- / +----- +---' | / |
7 # | \ / | | / | | \ | / |
8 # | \/ | | / | | \ | / |
9 # ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
11 # =============================================================================
13 # Python exporter for Blender, compiles .mdl format for Skate Rift.
15 # Its really slow, sorry, I don't know how to speed it up.
16 # Also not sure why you need to put # before {} in code blocks, there is errors
20 import bpy
, math
, gpu
, os
23 from mathutils
import *
24 from gpu_extras
.batch
import batch_for_shader
27 "name":"Skate Rift model compiler",
28 "author": "Harry Godden (hgn)",
35 "category":"Import/Export",
38 class mdl_vert(Structure
): # 48 bytes. Quite large. Could compress
39 #{ # the normals and uvs to i16s. Not an
40 _pack_
= 1 # real issue, yet.
41 _fields_
= [("co",c_float
*3),
45 ("weights",c_uint16
*4),
49 class mdl_submesh(Structure
):
52 _fields_
= [("indice_start",c_uint32
),
53 ("indice_count",c_uint32
),
54 ("vertex_start",c_uint32
),
55 ("vertex_count",c_uint32
),
56 ("bbx",(c_float
*3)*2),
57 ("material_id",c_uint32
)] # index into the material array
60 class mdl_texture(Structure
):
63 _fields_
= [("pstr_name",c_uint32
),
64 ("pack_offset",c_uint32
),
65 ("pack_length",c_uint32
)]
68 class mdl_material(Structure
):
71 _fields_
= [("pstr_name",c_uint32
),
74 ("surface_prop",c_uint32
),
76 ("colour1",c_float
*4),
77 ("tex_diffuse",c_uint32
),
78 ("tex_decal",c_uint32
),
79 ("tex_normal",c_uint32
)]
82 class mdl_node(Structure
):
85 _fields_
= [("co",c_float
*3),
88 ("sub_uid",c_uint32
), # dont use
89 ("submesh_start",c_uint32
),
90 ("submesh_count",c_uint32
),
91 ("classtype",c_uint32
),
94 ("pstr_name",c_uint32
)]
97 class mdl_header(Structure
):
100 _fields_
= [("identifier",c_uint32
),
101 ("version",c_uint32
),
102 ("file_length",c_uint32
),
105 ("node_count",c_uint32
),
106 ("node_offset",c_uint32
),
108 ("submesh_count",c_uint32
),
109 ("submesh_offset",c_uint32
),
111 ("material_count",c_uint32
),
112 ("material_offset",c_uint32
),
114 ("texture_count",c_uint32
),
115 ("texture_offset",c_uint32
),
117 ("anim_count",c_uint32
),
118 ("anim_offset",c_uint32
),
120 ("entdata_size",c_uint32
),
121 ("entdata_offset",c_uint32
),
123 ("strings_size",c_uint32
),
124 ("strings_offset",c_uint32
),
126 ("keyframe_count",c_uint32
),
127 ("keyframe_offset",c_uint32
),
129 ("vertex_count",c_uint32
),
130 ("vertex_offset",c_uint32
),
132 ("indice_count",c_uint32
),
133 ("indice_offset",c_uint32
),
135 ("pack_size",c_uint32
),
136 ("pack_offset",c_uint32
)]
139 class mdl_animation(Structure
):
142 _fields_
= [("pstr_name",c_uint32
),
148 class mdl_keyframe(Structure
):
151 _fields_
= [("co",c_float
*3),
156 # ---------------------------------------------------------------------------- #
158 # Entity definitions #
160 # ---------------------------------------------------------------------------- #
162 # ctypes _fields_ defines the data which is filled in by:
163 # def encode_obj( _, node, node_def ):
165 # gizmos get drawn into the viewport via:
167 # def draw_scene_helpers( obj ):
169 # editor enterface, simiraliy:
171 # def editor_interface( layout, obj ):
176 # Purpose: A rift. must target another gate, the target gate can not have more
177 # than one target nodes of its own.
179 class classtype_gate(Structure
):
182 _fields_
= [("target",c_uint32
),
185 def encode_obj(_
, node
,node_def
):
189 obj
= node_def
['obj']
191 if obj
.cv_data
.target
!= None:
192 _
.target
= obj
.cv_data
.target
.cv_data
.uid
194 if obj
.type == 'MESH':
196 _
.dims
[0] = obj
.data
.cv_data
.v0
[0]
197 _
.dims
[1] = obj
.data
.cv_data
.v0
[1]
198 _
.dims
[2] = obj
.data
.cv_data
.v0
[2]
202 _
.dims
[0] = obj
.cv_data
.v0
[0]
203 _
.dims
[1] = obj
.cv_data
.v0
[1]
204 _
.dims
[2] = obj
.cv_data
.v0
[2]
209 def draw_scene_helpers( obj
):
211 global cv_view_verts
, cv_view_colours
213 if obj
.type == 'MESH':
214 dims
= obj
.data
.cv_data
.v0
216 dims
= obj
.cv_data
.v0
219 c
= Vector((0,0,dims
[2]))
221 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
222 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
223 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
224 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
225 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
226 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
227 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
228 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
229 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
231 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
237 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
238 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
239 cv_view_colours
+= [(1,1,0,1),(1,1,0,1)]
242 sw
= (0.4,0.4,0.4,0.2)
243 if obj
.cv_data
.target
!= None:
244 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, sw
)
248 def editor_interface( layout
, obj
):
250 layout
.prop( obj
.cv_data
, "target" )
253 layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
254 layout
.prop( mesh
.cv_data
, "v0", text
="Gate dimensions" )
258 class classtype_nonlocal_gate(classtype_gate
):
260 def encode_obj(_
,node
,node_def
):
264 obj
= node_def
['obj']
265 _
.target
= encoder_process_pstr( node_def
['obj'].cv_data
.strp
)
267 if obj
.type == 'MESH':
269 _
.dims
[0] = obj
.data
.cv_data
.v0
[0]
270 _
.dims
[1] = obj
.data
.cv_data
.v0
[1]
271 _
.dims
[2] = obj
.data
.cv_data
.v0
[2]
275 _
.dims
[0] = obj
.cv_data
.v0
[0]
276 _
.dims
[1] = obj
.cv_data
.v0
[1]
277 _
.dims
[2] = obj
.cv_data
.v0
[2]
282 def editor_interface( layout
, obj
):
284 layout
.prop( obj
.cv_data
, "strp", text
="Nonlocal ID" )
287 layout
.label( text
=F
"(i) Data is stored in {mesh.name}" )
288 layout
.prop( mesh
.cv_data
, "v0", text
="Gate dimensions" )
294 # Purpose: player can reset here, its a safe place
295 # spawns can share the same name, the closest one will be picked
297 # when the world loads it will pick the one named 'start' first.
299 class classtype_spawn(Structure
):
302 _fields_
= [("pstr_alias",c_uint32
)]
304 def encode_obj(_
, node
,node_def
):
307 _
.pstr_alias
= encoder_process_pstr( node_def
['obj'].cv_data
.strp
)
311 def draw_scene_helpers( obj
):
313 global cv_view_verts
, cv_view_colours
316 vs
[0] = obj
.matrix_world
@ Vector((0,0,0))
317 vs
[1] = obj
.matrix_world
@ Vector((0,2,0))
318 vs
[2] = obj
.matrix_world
@ Vector((0.5,1,0))
319 vs
[3] = obj
.matrix_world
@ Vector((-0.5,1,0))
320 indices
= [(0,1),(1,2),(1,3)]
327 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
328 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
329 cv_view_colours
+= [(0,1,1,1),(0,1,1,1)]
332 cv_draw_sphere( obj
.location
, 20.0, [0.1,0,0.9,0.4] )
336 def editor_interface( layout
, obj
):
338 layout
.prop( obj
.cv_data
, "strp", text
="Alias" )
344 # Purpose: Tells the game to draw water HERE, at this entity.
346 class classtype_water(Structure
):
349 _fields_
= [("temp",c_uint32
)]
351 def encode_obj(_
, node
,node_def
):
360 # Purpose: Defines a route node and links to up to two more nodes
362 class classtype_route_node(Structure
):
365 _fields_
= [("target",c_uint32
),
366 ("target1",c_uint32
)]
368 def encode_obj(_
, node
,node_def
):
371 obj
= node_def
['obj']
373 if obj
.cv_data
.target
!= None:
374 _
.target
= obj
.cv_data
.target
.cv_data
.uid
375 if obj
.cv_data
.target1
!= None:
376 _
.target1
= obj
.cv_data
.target1
.cv_data
.uid
380 def draw_scene_helpers( obj
):
382 global cv_view_verts
, cv_view_colours
384 sw
= Vector((0.4,0.4,0.4,0.2))
385 sw2
= Vector((1.5,0.2,0.2,0.0))
386 if obj
.cv_data
.target
!= None:
387 cv_draw_bpath( obj
, obj
.cv_data
.target
, sw
, sw
)
388 if obj
.cv_data
.target1
!= None:
389 cv_draw_bpath( obj
, obj
.cv_data
.target1
, sw
, sw
)
391 cv_draw_bhandle( obj
, 1.0, (0.8,0.8,0.8,1.0) )
392 cv_draw_bhandle( obj
, -1.0, (0.4,0.4,0.4,1.0) )
395 obj
.matrix_world
.to_quaternion() @ Vector((0,0,-6+1.5))
396 cv_draw_arrow( obj
.location
, p1
, sw
)
400 def editor_interface( layout
, obj
):
402 layout
.prop( obj
.cv_data
, "target", text
="Left" )
403 layout
.prop( obj
.cv_data
, "target1", text
="Right" )
409 # Purpose: Defines a route, its 'starting' point, and the colour to use for it
411 class classtype_route(Structure
):
414 _fields_
= [("id_start",c_uint32
),
415 ("pstr_name",c_uint32
),
416 ("colour",c_float
*3)]
418 def encode_obj(_
, node
,node_def
):
421 obj
= node_def
['obj']
423 _
.colour
[0] = obj
.cv_data
.colour
[0]
424 _
.colour
[1] = obj
.cv_data
.colour
[1]
425 _
.colour
[2] = obj
.cv_data
.colour
[2]
426 _
.pstr_name
= encoder_process_pstr( obj
.cv_data
.strp
)
428 if obj
.cv_data
.target
!= None:
429 _
.id_start
= obj
.cv_data
.target
.cv_data
.uid
433 def draw_scene_helpers( obj
):
435 global cv_view_verts
, cv_view_colours
, cv_view_course_i
437 if obj
.cv_data
.target
:
438 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, [1,1,1,1] )
440 # Tries to simulate how we do it in the game
444 stack
[0] = obj
.cv_data
.target
446 loop_complete
= False
450 if stack_i
[si
-1] == 2:
455 if si
== 0: # Loop failed to complete
461 targets
= [None,None]
462 targets
[0] = node
.cv_data
.target
464 if node
.cv_data
.classtype
== 'classtype_route_node':
466 targets
[1] = node
.cv_data
.target1
469 nextnode
= targets
[stack_i
[si
-1]]
472 if nextnode
!= None: # branch
474 if nextnode
== stack
[0]: # Loop completed
483 if stack
[sj
] == nextnode
: # invalidated path
502 cc
= Vector((obj
.cv_data
.colour
[0],\
503 obj
.cv_data
.colour
[1],\
504 obj
.cv_data
.colour
[2],\
511 if stack
[sj
].cv_data
.classtype
== 'classtype_gate' and \
512 stack
[sk
].cv_data
.classtype
== 'classtype_gate':
514 dist
= (stack
[sj
].location
-stack
[sk
].location
).magnitude
515 cv_draw_sbpath( stack
[sj
], stack
[sk
], cc
*0.4, cc
, dist
, dist
)
518 cv_draw_bpath( stack
[sj
], stack
[sk
], cc
, cc
)
521 cv_view_course_i
+= 1
526 def editor_interface( layout
, obj
):
528 layout
.prop( obj
.cv_data
, "target", text
="'Start' from" )
529 layout
.prop( obj
.cv_data
, "colour" )
530 layout
.prop( obj
.cv_data
, "strp", text
="Name" )
536 # Purpose: links an mesh node to a type 11
538 class classtype_skin(Structure
):
541 _fields_
= [("skeleton",c_uint32
)]
543 def encode_obj(_
, node
,node_def
):
547 armature_def
= node_def
['linked_armature']
548 _
.skeleton
= armature_def
['obj'].cv_data
.uid
554 # Purpose: defines the allocation requirements for a skeleton
556 class classtype_skeleton(Structure
):
559 _fields_
= [("channels",c_uint32
),
560 ("ik_count",c_uint32
),
561 ("collider_count",c_uint32
),
562 ("anim_start",c_uint32
),
563 ("anim_count",c_uint32
)]
565 def encode_obj(_
, node
,node_def
):
569 _
.channels
= len( node_def
['bones'] )
570 _
.ik_count
= node_def
['ik_count']
571 _
.collider_count
= node_def
['collider_count']
572 _
.anim_start
= node_def
['anim_start']
573 _
.anim_count
= node_def
['anim_count']
580 # Purpose: intrinsic bone type, stores collision information and limits too
582 class classtype_bone(Structure
):
585 _fields_
= [("flags",c_uint32
),
586 ("ik_target",c_uint32
),
587 ("ik_pole",c_uint32
),
588 ("hitbox",(c_float
*3)*2),
589 ("conevx",c_float
*3),
590 ("conevy",c_float
*3),
591 ("coneva",c_float
*3),
594 def encode_obj(_
, node
,node_def
):
598 armature_def
= node_def
['linked_armature']
599 obj
= node_def
['bone']
601 _
.flags
= node_def
['deform']
603 if 'ik_target' in node_def
:
606 _
.ik_target
= armature_def
['bones'].index( node_def
['ik_target'] )
607 _
.ik_pole
= armature_def
['bones'].index( node_def
['ik_pole'] )
612 if obj
.cv_data
.collider
!= 'collider_none':
614 if obj
.cv_data
.collider
== 'collider_box':
619 _
.hitbox
[0][0] = obj
.cv_data
.v0
[0]
620 _
.hitbox
[0][1] = obj
.cv_data
.v0
[2]
621 _
.hitbox
[0][2] = -obj
.cv_data
.v1
[1]
622 _
.hitbox
[1][0] = obj
.cv_data
.v1
[0]
623 _
.hitbox
[1][1] = obj
.cv_data
.v1
[2]
624 _
.hitbox
[1][2] = -obj
.cv_data
.v0
[1]
630 _
.conevx
[0] = obj
.cv_data
.conevx
[0]
631 _
.conevx
[1] = obj
.cv_data
.conevx
[2]
632 _
.conevx
[2] = -obj
.cv_data
.conevx
[1]
633 _
.conevy
[0] = obj
.cv_data
.conevy
[0]
634 _
.conevy
[1] = obj
.cv_data
.conevy
[2]
635 _
.conevy
[2] = -obj
.cv_data
.conevy
[1]
636 _
.coneva
[0] = obj
.cv_data
.coneva
[0]
637 _
.coneva
[1] = obj
.cv_data
.coneva
[2]
638 _
.coneva
[2] = -obj
.cv_data
.coneva
[1]
639 _
.conet
= obj
.cv_data
.conet
646 # Purpose: sends a signal to another entity
648 class classtype_trigger(Structure
):
651 _fields_
= [("target",c_uint32
)]
653 def encode_obj(_
, node
,node_def
):
656 if node_def
['obj'].cv_data
.target
:
657 _
.target
= node_def
['obj'].cv_data
.target
.cv_data
.uid
661 def draw_scene_helpers( obj
):
663 global cv_view_verts
, cv_view_colours
664 cv_draw_ucube( obj
.matrix_world
, [0,1,0,1] )
668 if obj
.cv_data
.target
:
669 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, white
, 0.7 )
673 def editor_interface( layout
, obj
):
675 layout
.prop( obj
.cv_data
, "target", text
="Triggers" )
681 # Purpose: Gives the player an achievement.
682 # No cheating! You shouldn't use this entity anyway, since only ME can
683 # add achievements to the steam ;)
685 class classtype_logic_achievement(Structure
):
688 _fields_
= [("pstr_name",c_uint32
)]
690 def encode_obj(_
, node
,node_def
):
693 _
.pstr_name
= encoder_process_pstr( node_def
['obj'].cv_data
.strp
)
697 def editor_interface( layout
, obj
):
699 layout
.prop( obj
.cv_data
, "strp", text
="Achievement ID" )
703 class union_128bit_data(Union
):
706 _fields_
= [("f32",c_float
),
716 class classtype_logic_wire(Structure
):
719 _fields_
= [("next",c_uint32
),
720 ("function",c_uint32
),
721 ("data",union_128bit_data
),
722 ("data_type",c_uint32
),
723 ("enabled",c_uint32
)]
725 function_enum
= [('0',"pass along",""),
730 def encode_obj(_
,node
,node_def
):
734 obj
= node_def
['obj']
736 if obj
.cv_data
.target
: _
.next
= obj
.cv_data
.target
.cv_data
.uid
738 _
.data_type
= obj
.cv_data
.intp1
739 _
.function
= int(obj
.cv_data
.function
)
740 _
.enabled
= obj
.cv_data
.bp0
742 if _
.data_type
== 1: # an integer
743 _
.data
.i32
= obj
.cv_data
.intp
744 elif _
.data_type
== 2: # a number
745 _
.data
.f32
= obj
.cv_data
.fltp
746 elif _
.data_type
== 3: # a target
747 if obj
.cv_data
.target2
:
748 _
.data
.u32
= obj
.cv_data
.target2
.cv_data
.uid
749 elif _
.data_type
== 4: # a string
750 _
.data
.u32
= encoder_process_pstr( obj
.cv_data
.strp
)
754 def editor_interface( layout
, obj
):
756 layout
.prop( obj
.cv_data
, "bp0", text
="Start disabled" )
758 box
.label( text
="Target" )
759 box
.prop( obj
.cv_data
, "target", text
="connection" )
762 if not obj
.cv_data
.target
:
764 row
.prop( obj
.cv_data
, "function", text
="function" )
767 box
.label( text
="Data packet" )
768 box
.prop( obj
.cv_data
, "intp1", text
="type" )
770 if obj
.cv_data
.intp1
== 1:
771 box
.prop( obj
.cv_data
, "intp", text
="Signed Integer" )
772 elif obj
.cv_data
.intp1
== 2:
773 box
.prop( obj
.cv_data
, "fltp", text
="Float" )
774 elif obj
.cv_data
.intp1
== 3:
775 box
.prop( obj
.cv_data
, "target2", text
="Object reference" )
776 elif obj
.cv_data
.intp1
== 4:
777 box
.prop( obj
.cv_data
, "strp", text
="String" )
782 row
.label( text
="this wire will not impart any data" )
787 def draw_scene_helpers( obj
):
789 global cv_view_verts
, cv_view_colours
792 purple
= (0.5,0.2,1,1)
794 if obj
.cv_data
.target
:
795 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, white
, 0.7 )
796 if (obj
.cv_data
.target2
) and (obj
.cv_data
.intp1
== 3):
797 cv_draw_arrow( obj
.cv_data
.target2
.location
, obj
.location
,purple
, 0.7 )
801 def get_targeted_methods( scene
, context
):
804 invalid
= [('0',"",""),
809 if obj
.cv_data
.target
:
811 classtype
= obj
.cv_data
.target
.cv_data
.classtype
812 if classtype
== 'classtype_none' or classtype
not in globals():
818 cl
= globals()[ classtype
]
819 if getattr( cl
, "function_enum", None ):
821 return cl
.function_enum
840 class classtype_particle_box(Structure
):
843 _fields_
= [("target",c_uint32
),
846 function_enum
= [('0',"set rate",""),
851 def encode_obj(_
, node
,node_def
):
855 obj
= node_def
['obj']
857 _
.rate
= obj
.cv_data
.fltp
858 if obj
.cv_data
.target
:
859 _
.target
= obj
.cv_data
.target
.cv_data
.uid
863 def draw_scene_helpers( obj
):
865 global cv_view_verts
, cv_view_colours
866 cv_draw_ucube( obj
.matrix_world
, [1,0.8,0,1] )
869 if obj
.cv_data
.target
:
870 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, white
, 0.7 )
874 def editor_interface( layout
, obj
):
876 layout
.prop( obj
.cv_data
, "target", text
="Triggers" )
877 layout
.prop( obj
.cv_data
, "fltp", text
="count per second" )
885 class classtype_signal_splitter(Structure
):
888 _fields_
= [("next",c_uint32
*4)]
890 function_enum
= [('0',"pass along",""),
895 def encode_obj(_
,node
,node_def
):
899 obj
= node_def
['obj']
901 if obj
.cv_data
.target
: _
.next
[0] = obj
.cv_data
.target
.cv_data
.uid
902 if obj
.cv_data
.target1
: _
.next
[1] = obj
.cv_data
.target1
.cv_data
.uid
903 if obj
.cv_data
.target2
: _
.next
[2] = obj
.cv_data
.target2
.cv_data
.uid
904 if obj
.cv_data
.target3
: _
.next
[3] = obj
.cv_data
.target3
.cv_data
.uid
908 def editor_interface( layout
, obj
):
910 layout
.label( text
="The split signals will run in order" )
911 layout
.prop( obj
.cv_data
, "target", text
="#0" )
912 layout
.prop( obj
.cv_data
, "target1", text
="#1" )
913 layout
.prop( obj
.cv_data
, "target2", text
="#2" )
914 layout
.prop( obj
.cv_data
, "target3", text
="#3" )
918 def draw_scene_helpers( obj
):
920 global cv_view_verts
, cv_view_colours
927 if obj
.cv_data
.target
:
928 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, c0
, 0.7 )
929 if obj
.cv_data
.target1
:
930 cv_draw_arrow( obj
.location
, obj
.cv_data
.target1
.location
, c1
, 0.7 )
931 if obj
.cv_data
.target2
:
932 cv_draw_arrow( obj
.location
, obj
.cv_data
.target2
.location
, c2
, 0.7 )
933 if obj
.cv_data
.target3
:
934 cv_draw_arrow( obj
.location
, obj
.cv_data
.target3
.location
, c3
, 0.7 )
942 class classtype_soundscape(Structure
):
945 _fields_
= [("max_instances",c_uint32
),
946 ("allow_transitions",c_uint32
),
947 ("transition_duration",c_float
),
950 function_enum
= [('0',"play",""),
951 ('1',"set position",""),
955 def encode_obj(_
,node
,node_def
):
959 obj
= node_def
['obj']
961 _
.max_instances
= obj
.cv_data
.intp
962 _
.allow_transitions
= obj
.cv_data
.bp0
963 _
.transition_duration
= obj
.cv_data
.fltp
964 _
.label
= encoder_process_pstr( obj
.cv_data
.strp
)
968 def editor_interface( layout
, obj
):
970 layout
.prop( obj
.cv_data
, "intp", text
="max instances" )
971 layout
.prop( obj
.cv_data
, "strp", text
="label" )
974 box
.label( text
="If its a 3d sound, where can it spawn?" )
975 box
.prop( obj
.cv_data
, "bp1", text
="Only in water" )
976 box
.prop( obj
.cv_data
, "bp2", text
="Only on grass" )
977 box
.prop( obj
.cv_data
, "bp3", text
="Only on wood" )
980 box
.prop( obj
.cv_data
, "bp0", text
="allow transitions" )
983 if not obj
.cv_data
.bp0
:
985 row
.prop( obj
.cv_data
, "fltp", text
="transition duration" )
989 class classtype_logic_chances(Structure
):
992 _fields_
= [("targets",c_uint32
*2),
995 function_enum
= [('0',"pass along",""),
996 ('1',"set ratio",""),
1000 def encode_obj(_
,node
,node_def
):
1002 node
.classtype
= 107
1004 obj
= node_def
['obj']
1006 if obj
.cv_data
.target
: _
.targets
[0] = obj
.cv_data
.target
.cv_data
.uid
1007 if obj
.cv_data
.target1
: _
.targets
[1] = obj
.cv_data
.target1
.cv_data
.uid
1009 _
.p
= obj
.cv_data
.fltp
1013 def editor_interface( layout
, obj
):
1016 box
.prop( obj
.cv_data
, "target", text
="red" )
1017 box
.prop( obj
.cv_data
, "target1", text
="black" )
1018 box
.prop( obj
.cv_data
, "fltp", text
="p(red)" )
1022 def draw_scene_helpers( obj
):
1024 global cv_view_verts
, cv_view_colours
1029 if obj
.cv_data
.target
:
1030 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, red
, 0.7 )
1031 if obj
.cv_data
.target1
:
1032 cv_draw_arrow( obj
.location
, obj
.cv_data
.target1
.location
, black
, 0.7 )
1036 # Classtype 102 [ DEPRECATED ]
1038 # Purpose: sends a signal to another entity
1040 class classtype_logic_relay(Structure
):
1043 _fields_
= [("targets",c_uint32
*4)]
1045 def encode_obj(_
, node
,node_def
):
1047 node
.classtype
= 102
1048 obj
= node_def
['obj']
1049 if obj
.cv_data
.target
:
1050 _
.targets
[0] = obj
.cv_data
.target
.cv_data
.uid
1051 if obj
.cv_data
.target1
:
1052 _
.targets
[1] = obj
.cv_data
.target1
.cv_data
.uid
1053 if obj
.cv_data
.target2
:
1054 _
.targets
[2] = obj
.cv_data
.target2
.cv_data
.uid
1055 if obj
.cv_data
.target3
:
1056 _
.targets
[3] = obj
.cv_data
.target3
.cv_data
.uid
1060 def draw_scene_helpers( obj
):
1062 global cv_view_verts
, cv_view_colours
1064 if obj
.cv_data
.target
:
1065 cv_draw_arrow( obj
.location
, obj
.cv_data
.target
.location
, [1,1,1,1] )
1066 if obj
.cv_data
.target1
:
1067 cv_draw_arrow( obj
.location
, obj
.cv_data
.target1
.location
, [1,1,1,1] )
1068 if obj
.cv_data
.target2
:
1069 cv_draw_arrow( obj
.location
, obj
.cv_data
.target2
.location
, [1,1,1,1] )
1070 if obj
.cv_data
.target3
:
1071 cv_draw_arrow( obj
.location
, obj
.cv_data
.target3
.location
, [1,1,1,1] )
1075 def editor_interface( layout
, obj
):
1077 layout
.prop( obj
.cv_data
, "target", text
="Triggers" )
1078 layout
.prop( obj
.cv_data
, "target1", text
="Triggers" )
1079 layout
.prop( obj
.cv_data
, "target2", text
="Triggers" )
1080 layout
.prop( obj
.cv_data
, "target3", text
="Triggers" )
1086 # Purpose: Plays some audio (44100hz .ogg vorbis only)
1087 # NOTE: There is a 32mb limit on the audio buffer, world audio is
1088 # decompressed and stored in signed 16 bit integers (2 bytes)
1091 # volume: not used if has 3D flag
1093 # AUDIO_FLAG_LOOP 0x1
1094 # AUDIO_FLAG_SPACIAL_3D 0x4 (Probably what you want)
1095 # AUDIO_FLAG_AUTO_START 0x8 (Play when the world starts)
1097 # the rest are just internal flags, only use the above 3.
1099 class classtype_audio(Structure
):
1102 _fields_
= [("pstr_file",c_uint32
),
1106 def encode_obj(_
, node
,node_def
):
1110 obj
= node_def
['obj']
1112 _
.pstr_file
= encoder_process_pstr( obj
.cv_data
.strp
)
1115 if obj
.cv_data
.bp0
: flags |
= 0x1
1116 if obj
.cv_data
.bp1
: flags |
= 0x4
1117 if obj
.cv_data
.bp2
: flags |
= 0x8
1119 if obj
.cv_data
.audio_format
== 'stereo':
1121 if obj
.cv_data
.audio_format
== 'remain compressed':
1123 if obj
.cv_data
.audio_format
== 'synthetic bird':
1127 _
.volume
= obj
.cv_data
.fltp
1131 def editor_interface( layout
, obj
):
1133 layout
.prop( obj
.cv_data
, "strp", text
= "File (.ogg)" )
1135 layout
.prop( obj
.cv_data
, "bp0", text
= "Looping" )
1136 layout
.prop( obj
.cv_data
, "bp1", text
= "3D Audio" )
1137 layout
.prop( obj
.cv_data
, "bp2", text
= "Auto Start" )
1138 layout
.prop( obj
.cv_data
, "audio_format" )
1140 layout
.prop( obj
.cv_data
, "fltp", text
= "Volume (0-1)" )
1144 def draw_scene_helpers( obj
):
1146 global cv_view_verts
, cv_view_colours
1148 cv_draw_sphere( obj
.location
, obj
.scale
[0], [1,1,0,1] )
1155 # Purpose: world light
1157 class classtype_world_light( Structure
):
1160 _fields_
= [("type",c_uint32
),
1161 ("colour",c_float
*4),
1165 def encode_obj(_
, node
, node_def
):
1167 node
.classtype
= 200
1169 obj
= node_def
['obj']
1171 _
.colour
[0] = data
.color
[0]
1172 _
.colour
[1] = data
.color
[1]
1173 _
.colour
[2] = data
.color
[2]
1174 _
.colour
[3] = data
.energy
1175 _
.range = data
.cutoff_distance
# this has to be manually set
1176 # TODO: At some point, automate a min
1179 if obj
.data
.type == 'POINT':
1184 elif obj
.data
.type == 'SPOT':
1187 _
.angle
= data
.spot_size
*0.5
1190 if data
.cv_data
.bp0
:
1195 def editor_interface( layout
, obj
):
1203 # Purpose: lighting settings for world
1205 class classtype_lighting_info(Structure
):
1208 _fields_
= [("colours",(c_float
*3)*3),
1209 ("directions",(c_float
*2)*3),
1210 ("states",c_uint32
*3),
1211 ("shadow_spread",c_float
),
1212 ("shadow_length",c_float
),
1213 ("ambient",c_float
*3)]
1215 def encode_obj(_
, node
, node_def
):
1217 node
.classtype
= 201
1223 def editor_interface( layout
, obj
):
1229 class classtype_spawn_link(Structure
):
1232 _fields_
= [("connections",c_uint32
*4)]
1234 def encode_obj(_
, node
,node_def
):
1240 def editor_interface( layout
, obj
):
1246 def draw_scene_helpers( obj
):
1248 global cv_view_verts
, cv_view_colours
1252 for obj1
in bpy
.context
.collection
.objects
:
1254 if (obj1
.cv_data
.classtype
!= 'classtype_spawn_link') and \
1255 (obj1
.cv_data
.classtype
!= 'classtype_spawn') :
1258 if (obj1
.location
- obj
.location
).length
< 40.0:
1260 cv_draw_line( obj
.location
, obj1
.location
, [1,1,1,1] )
1268 cv_draw_sphere( obj
.location
, 20.0, [0.5,0,0.2,0.4] )
1272 # ---------------------------------------------------------------------------- #
1274 # Compiler section #
1276 # ---------------------------------------------------------------------------- #
1278 # Current encoder state
1284 def encoder_init( collection
):
1290 # The actual file header
1292 'header': mdl_header(),
1296 'pack_textures': collection
.cv_data
.pack_textures
,
1298 # Compiled data chunks (each can be read optionally by the client)
1302 #1---------------------------------
1303 'node': [], # Metadata 'chunk'
1308 'entdata': bytearray(), # variable width
1309 'strings': bytearray(), # .
1310 #2---------------------------------
1311 'keyframe': [], # Animations
1312 #3---------------------------------
1313 'vertex': [], # Mesh data
1315 #4---------------------------------
1316 'pack': bytearray() # Other generic packed data
1319 # All objects of the model in their final heirachy
1325 # Allows us to reuse definitions
1329 'material_cache': {},
1333 g_encoder
['header'].identifier
= 0xABCD0000
1334 g_encoder
['header'].version
= 1
1336 # Add fake NoneID material and texture
1338 none_material
= mdl_material()
1339 none_material
.pstr_name
= encoder_process_pstr( "" )
1340 none_material
.texture_id
= 0
1342 none_texture
= mdl_texture()
1343 none_texture
.pstr_name
= encoder_process_pstr( "" )
1344 none_texture
.pack_offset
= 0
1345 none_texture
.pack_length
= 0
1347 g_encoder
['data']['material'] += [none_material
]
1348 g_encoder
['data']['texture'] += [none_texture
]
1350 g_encoder
['data']['pack'].extend( b
'datapack\0\0\0\0\0\0\0\0' )
1365 root
.pstr_name
= encoder_process_pstr('')
1366 root
.submesh_start
= 0
1367 root
.submesh_count
= 0
1370 root
.parent
= 0xffffffff
1372 g_encoder
['data']['node'] += [root
]
1376 # fill with 0x00 until a multiple of align. Returns how many bytes it added
1378 def bytearray_align_to( buffer, align
, offset
=0 ):
1382 while ((len(buffer)+offset
) % align
) != 0:
1384 buffer.extend( b
'\0' )
1391 # Add a string to the string buffer except if it already exists there then we
1392 # just return its ID.
1394 def encoder_process_pstr( s
):
1398 cache
= g_encoder
['string_cache']
1403 cache
[s
] = len( g_encoder
['data']['strings'] )
1405 buffer = g_encoder
['data']['strings']
1406 buffer.extend( s
.encode('utf-8') )
1407 buffer.extend( b
'\0' )
1409 bytearray_align_to( buffer, 4 )
1413 def get_texture_resource_name( img
):
1415 return os
.path
.splitext( img
.name
)[0]
1420 def encoder_process_texture( img
):
1427 cache
= g_encoder
['texture_cache']
1428 buffer = g_encoder
['data']['texture']
1429 pack
= g_encoder
['data']['pack']
1431 name
= get_texture_resource_name( img
)
1436 cache
[name
] = len( buffer )
1439 tex
.pstr_name
= encoder_process_pstr( name
)
1441 if g_encoder
['pack_textures']:
1443 tex
.pack_offset
= len( pack
)
1444 pack
.extend( qoi_encode( img
) )
1445 tex
.pack_length
= len( pack
) - tex
.pack_offset
1454 def material_tex_image(v
):
1464 cxr_graph_mapping
= \
1466 # Default shader setup
1473 "image": "tex_diffuse"
1477 "A": material_tex_image("tex_diffuse"),
1478 "B": material_tex_image("tex_decal")
1485 "Color": material_tex_image("tex_normal")
1491 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
1493 def material_info(mat
):
1497 # Using the cv_graph_mapping as a reference, go through the shader
1498 # graph and gather all $props from it.
1500 def _graph_read( node_def
, node
=None, depth
=0 ):
1509 _graph_read
.extracted
= []
1511 for node_idname
in node_def
:
1513 for n
in mat
.node_tree
.nodes
:
1515 if n
.name
== node_idname
:
1517 node_def
= node_def
[node_idname
]
1525 for link
in node_def
:
1527 link_def
= node_def
[link
]
1529 if isinstance( link_def
, dict ):
1532 for x
in node
.inputs
:
1534 if isinstance( x
, bpy
.types
.NodeSocketColor
):
1544 if node_link
and node_link
.is_linked
:
1546 # look for definitions for the connected node type
1548 from_node
= node_link
.links
[0].from_node
1550 node_name
= from_node
.name
.split('.')[0]
1551 if node_name
in link_def
:
1553 from_node_def
= link_def
[ node_name
]
1555 _graph_read( from_node_def
, from_node
, depth
+1 )
1559 # TODO: Make a warning for this?
1563 if "default" in link_def
:
1565 prop
= link_def
['default']
1566 info
[prop
] = node_link
.default_value
1573 info
[prop
] = getattr( node
, link
)
1578 _graph_read( cxr_graph_mapping
)
1582 # Add a material to the material buffer. Returns 0 (None ID) if invalid
1584 def encoder_process_material( mat
):
1591 cache
= g_encoder
['material_cache']
1592 buffer = g_encoder
['data']['material']
1594 if mat
.name
in cache
:
1595 return cache
[mat
.name
]
1597 cache
[mat
.name
] = len( buffer )
1599 dest
= mdl_material()
1600 dest
.pstr_name
= encoder_process_pstr( mat
.name
)
1603 if mat
.cv_data
.collision
:
1605 if mat
.cv_data
.skate_surface
: flags |
= 0x1
1606 if mat
.cv_data
.grind_surface
: flags |
= (0x8|
0x1)
1608 if mat
.cv_data
.grow_grass
: flags |
= 0x4
1611 if mat
.cv_data
.surface_prop
== 'concrete': dest
.surface_prop
= 0
1612 if mat
.cv_data
.surface_prop
== 'wood': dest
.surface_prop
= 1
1613 if mat
.cv_data
.surface_prop
== 'grass': dest
.surface_prop
= 2
1614 if mat
.cv_data
.surface_prop
== 'tiles': dest
.surface_prop
= 3
1616 if mat
.cv_data
.shader
== 'standard': dest
.shader
= 0
1617 if mat
.cv_data
.shader
== 'standard_cutout': dest
.shader
= 1
1618 if mat
.cv_data
.shader
== 'terrain_blend':
1622 dest
.colour
[0] = pow( mat
.cv_data
.sand_colour
[0], 1.0/2.2 )
1623 dest
.colour
[1] = pow( mat
.cv_data
.sand_colour
[1], 1.0/2.2 )
1624 dest
.colour
[2] = pow( mat
.cv_data
.sand_colour
[2], 1.0/2.2 )
1625 dest
.colour
[3] = 1.0
1627 dest
.colour1
[0] = mat
.cv_data
.blend_offset
[0]
1628 dest
.colour1
[1] = mat
.cv_data
.blend_offset
[1]
1631 if mat
.cv_data
.shader
== 'vertex_blend':
1635 dest
.colour1
[0] = mat
.cv_data
.blend_offset
[0]
1636 dest
.colour1
[1] = mat
.cv_data
.blend_offset
[1]
1639 if mat
.cv_data
.shader
== 'water':
1643 dest
.colour
[0] = pow( mat
.cv_data
.shore_colour
[0], 1.0/2.2 )
1644 dest
.colour
[1] = pow( mat
.cv_data
.shore_colour
[1], 1.0/2.2 )
1645 dest
.colour
[2] = pow( mat
.cv_data
.shore_colour
[2], 1.0/2.2 )
1646 dest
.colour
[3] = 1.0
1647 dest
.colour1
[0] = pow( mat
.cv_data
.ocean_colour
[0], 1.0/2.2 )
1648 dest
.colour1
[1] = pow( mat
.cv_data
.ocean_colour
[1], 1.0/2.2 )
1649 dest
.colour1
[2] = pow( mat
.cv_data
.ocean_colour
[2], 1.0/2.2 )
1650 dest
.colour1
[3] = 1.0
1653 inf
= material_info( mat
)
1655 if mat
.cv_data
.shader
== 'standard' or \
1656 mat
.cv_data
.shader
== 'standard_cutout' or \
1657 mat
.cv_data
.shader
== 'terrain_blend' or \
1658 mat
.cv_data
.shader
== 'vertex_blend':
1660 if 'tex_diffuse' in inf
:
1661 dest
.tex_diffuse
= encoder_process_texture(inf
['tex_diffuse'])
1665 return cache
[mat
.name
]
1668 # Create a tree structure containing all the objects in the collection
1670 def encoder_build_scene_graph( collection
):
1674 print( " creating scene graph" )
1678 graph
= g_encoder
['scene_graph']
1679 graph_lookup
= g_encoder
['graph_lookup']
1682 graph
["children"] = []
1684 graph
["parent"] = None
1689 uid
= g_encoder
['uid_count']
1690 g_encoder
['uid_count'] += 1
1694 for obj
in collection
.all_objects
:
1696 #if obj.parent: continue
1698 def _extend( p
, n
, d
):
1705 tree
["children"] = []
1711 # Descend into amature
1713 if n
.type == 'ARMATURE':
1715 tree
["bones"] = [None] # None is the root transform
1716 tree
["ik_count"] = 0
1717 tree
["collider_count"] = 0
1718 tree
["compile_animation"] = collection
.cv_data
.animations
1720 # Here also collects some information about constraints, ik and
1721 # counts colliders for the armature.
1723 def _extendb( p
, n
, d
):
1729 btree
["linked_armature"] = tree
1730 btree
["uid"] = _new_uid()
1731 btree
["children"] = []
1734 tree
["bones"] += [n
.name
]
1736 for c
in n
.children
:
1738 _extendb( btree
, c
, d
+1 )
1741 for c
in tree
['obj'].pose
.bones
[n
.name
].constraints
:
1745 btree
["ik_target"] = c
.subtarget
1746 btree
["ik_pole"] = c
.pole_subtarget
1747 tree
["ik_count"] += 1
1751 if n
.cv_data
.collider
!= 'collider_none':
1752 tree
['collider_count'] += 1
1754 btree
['deform'] = n
.use_deform
1755 p
['children'] += [btree
]
1758 for b
in n
.data
.bones
:
1760 _extendb( tree
, b
, d
+1 )
1763 # Recurse into children of this object
1765 for obj1
in n
.children
:
1767 for c1
in obj1
.users_collection
:
1769 if c1
== collection
:
1771 _extend( tree
, obj1
, d
+1 )
1777 p
["children"] += [tree
]
1778 graph_lookup
[n
] = tree
1782 _extend( graph
, obj
, 1 )
1788 # Kind of a useless thing i made but it looks cool and adds complexity!!1
1790 def encoder_graph_iterator( root
):
1792 for c
in root
['children']:
1795 yield from encoder_graph_iterator(c
)
1800 # Push a vertex into the model file, or return a cached index (c_uint32)
1802 def encoder_vertex_push( vertex_reference
, co
,norm
,uv
,colour
,groups
,weights
):
1805 buffer = g_encoder
['data']['vertex']
1808 m
= float(10**TOLERENCE
)
1810 # Would be nice to know if this can be done faster than it currently runs,
1813 key
= (int(co
[0]*m
+0.5),
1821 colour
[0], # these guys are already quantized
1834 if key
in vertex_reference
:
1835 return vertex_reference
[key
]
1838 index
= c_uint32( len(vertex_reference
) )
1839 vertex_reference
[key
] = index
1847 v
.norm
[2] = -norm
[1]
1850 v
.colour
[0] = colour
[0]
1851 v
.colour
[1] = colour
[1]
1852 v
.colour
[2] = colour
[2]
1853 v
.colour
[3] = colour
[3]
1854 v
.weights
[0] = weights
[0]
1855 v
.weights
[1] = weights
[1]
1856 v
.weights
[2] = weights
[2]
1857 v
.weights
[3] = weights
[3]
1858 v
.groups
[0] = groups
[0]
1859 v
.groups
[1] = groups
[1]
1860 v
.groups
[2] = groups
[2]
1861 v
.groups
[3] = groups
[3]
1869 # Compile a mesh (or use one from the cache) onto node, based on node_def
1872 def encoder_compile_mesh( node
, node_def
):
1876 graph
= g_encoder
['scene_graph']
1877 graph_lookup
= g_encoder
['graph_lookup']
1878 mesh_cache
= g_encoder
['mesh_cache']
1879 obj
= node_def
['obj']
1881 can_use_cache
= True
1883 # Check for modifiers that typically change the data per-instance
1884 # there is no well defined rule for the choices here, its just what i've
1885 # needed while producing the game.
1887 # It may be possible to detect these cases automatically.
1889 for mod
in obj
.modifiers
:
1891 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
1892 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
1893 mod
.type == 'ARRAY':
1895 can_use_cache
= False
1898 if mod
.type == 'ARMATURE':
1899 armature_def
= graph_lookup
[mod
.object]
1901 # Check the cache first
1903 if can_use_cache
and (obj
.data
.name
in mesh_cache
):
1905 ref
= mesh_cache
[obj
.data
.name
]
1906 node
.submesh_start
= ref
.submesh_start
1907 node
.submesh_count
= ref
.submesh_count
1911 # Compile a whole new mesh
1913 node
.submesh_start
= len( g_encoder
['data']['submesh'] )
1914 node
.submesh_count
= 0
1916 dgraph
= bpy
.context
.evaluated_depsgraph_get()
1917 data
= obj
.evaluated_get(dgraph
).data
1918 data
.calc_loop_triangles()
1919 data
.calc_normals_split()
1921 # Mesh is split into submeshes based on their material
1923 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
1924 for material_id
, mat
in enumerate(mat_list
):
1929 sm
.indice_start
= len( g_encoder
['data']['indice'] )
1930 sm
.vertex_start
= len( g_encoder
['data']['vertex'] )
1933 sm
.material_id
= encoder_process_material( mat
)
1937 sm
.bbx
[0][i
] = 999999
1938 sm
.bbx
[1][i
] = -999999
1941 # Keep a reference to very very very similar vertices
1943 vertex_reference
= {}
1945 # Write the vertex / indice data
1947 for tri_index
, tri
in enumerate(data
.loop_triangles
):
1949 if tri
.material_index
!= material_id
:
1954 vert
= data
.vertices
[tri
.vertices
[j
]]
1956 vi
= data
.loops
[li
].vertex_index
1958 # Gather vertex information
1961 norm
= data
.loops
[li
].normal
1963 colour
= (255,255,255,255)
1970 uv
= data
.uv_layers
.active
.data
[li
].uv
1974 if data
.vertex_colors
:
1976 colour
= data
.vertex_colors
.active
.data
[li
].color
1977 colour
= (int(colour
[0]*255.0),\
1978 int(colour
[1]*255.0),\
1979 int(colour
[2]*255.0),\
1980 int(colour
[3]*255.0))
1983 # Weight groups: truncates to the 3 with the most influence. The
1984 # fourth bone ID is never used by the shader so it is
1989 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
1990 if obj
.vertex_groups
[_
.group
].name
in \
1991 armature_def
['bones']]
1993 weight_groups
= sorted( src_groups
, key
= \
1994 lambda a
: a
.weight
, reverse
=True )
1998 if len(weight_groups
) > ml
:
2000 g
= weight_groups
[ml
]
2001 name
= obj
.vertex_groups
[g
.group
].name
2004 weights
[ml
] = weight
2005 groups
[ml
] = armature_def
['bones'].index(name
)
2010 if len(weight_groups
) > 0:
2012 inv_norm
= (1.0/tot
) * 65535.0
2015 weights
[ml
] = int( weights
[ml
] * inv_norm
)
2016 weights
[ml
] = min( weights
[ml
], 65535 )
2017 weights
[ml
] = max( weights
[ml
], 0 )
2023 li1
= tri
.loops
[(j
+1)%3]
2024 vi1
= data
.loops
[li1
].vertex_index
2025 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
2027 if e0
.use_freestyle_mark
and \
2028 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
2029 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
2035 # Add vertex and expand bound box
2037 index
= encoder_vertex_push( vertex_reference
, co
, \
2043 g_encoder
['data']['indice'] += [index
]
2047 # How many unique verts did we add in total
2049 sm
.vertex_count
= len(g_encoder
['data']['vertex']) - sm
.vertex_start
2050 sm
.indice_count
= len(g_encoder
['data']['indice']) - sm
.indice_start
2052 # Make sure bounding box isn't -inf -> inf if no vertices
2054 if sm
.vertex_count
== 0:
2060 for j
in range(sm
.vertex_count
):
2062 vert
= g_encoder
['data']['vertex'][ sm
.vertex_start
+ j
]
2066 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], vert
.co
[i
] )
2067 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], vert
.co
[i
] )
2072 # Add submesh to encoder
2074 g_encoder
['data']['submesh'] += [sm
]
2075 node
.submesh_count
+= 1
2079 # Save a reference to this node since we want to reuse the submesh indices
2081 g_encoder
['mesh_cache'][obj
.data
.name
] = node
2085 def encoder_compile_ent_as( name
, node
, node_def
):
2089 if name
== 'classtype_none':
2095 elif name
not in globals():
2097 print( "Classtype '" +name
+ "' is unknown!" )
2101 buffer = g_encoder
['data']['entdata']
2102 node
.offset
= len(buffer)
2104 cl
= globals()[ name
]
2106 inst
.encode_obj( node
, node_def
)
2108 buffer.extend( bytearray(inst
) )
2109 bytearray_align_to( buffer, 4 )
2112 # Compiles animation data into model and gives us some extra node_def entries
2114 def encoder_compile_armature( node
, node_def
):
2118 entdata
= g_encoder
['data']['entdata']
2119 animdata
= g_encoder
['data']['anim']
2120 keyframedata
= g_encoder
['data']['keyframe']
2121 mesh_cache
= g_encoder
['mesh_cache']
2122 obj
= node_def
['obj']
2123 bones
= node_def
['bones']
2126 node_def
['anim_start'] = len(animdata
)
2127 node_def
['anim_count'] = 0
2129 if not node_def
['compile_animation']:
2136 if obj
.animation_data
:
2138 # So we can restore later
2140 previous_frame
= bpy
.context
.scene
.frame_current
2141 previous_action
= obj
.animation_data
.action
2142 POSE_OR_REST_CACHE
= obj
.data
.pose_position
2143 obj
.data
.pose_position
= 'POSE'
2145 for NLALayer
in obj
.animation_data
.nla_tracks
:
2147 for NLAStrip
in NLALayer
.strips
:
2151 for a
in bpy
.data
.actions
:
2153 if a
.name
== NLAStrip
.name
:
2155 obj
.animation_data
.action
= a
2160 # Clip to NLA settings
2162 anim_start
= int(NLAStrip
.action_frame_start
)
2163 anim_end
= int(NLAStrip
.action_frame_end
)
2167 anim
= mdl_animation()
2168 anim
.pstr_name
= encoder_process_pstr( NLAStrip
.action
.name
)
2170 anim
.offset
= len(keyframedata
)
2171 anim
.length
= anim_end
-anim_start
2173 # Export the keyframes
2174 for frame
in range(anim_start
,anim_end
):
2176 bpy
.context
.scene
.frame_set(frame
)
2178 for bone_name
in bones
:
2180 for pb
in obj
.pose
.bones
:
2182 if pb
.name
!= bone_name
: continue
2184 rb
= obj
.data
.bones
[ bone_name
]
2186 # relative bone matrix
2187 if rb
.parent
is not None:
2189 offset_mtx
= rb
.parent
.matrix_local
2190 offset_mtx
= offset_mtx
.inverted_safe() @ \
2193 inv_parent
= pb
.parent
.matrix
@ offset_mtx
2194 inv_parent
.invert_safe()
2195 fpm
= inv_parent
@ pb
.matrix
2199 bone_mtx
= rb
.matrix
.to_4x4()
2200 local_inv
= rb
.matrix_local
.inverted_safe()
2201 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
2204 loc
, rot
, sca
= fpm
.decompose()
2207 final_pos
= Vector(( loc
[0], loc
[2], -loc
[1] ))
2210 lc_m
= pb
.matrix_channel
.to_3x3()
2211 if pb
.parent
is not None:
2213 smtx
= pb
.parent
.matrix_channel
.to_3x3()
2214 lc_m
= smtx
.inverted() @ lc_m
2216 rq
= lc_m
.to_quaternion()
2219 kf
.co
[0] = final_pos
[0]
2220 kf
.co
[1] = final_pos
[1]
2221 kf
.co
[2] = final_pos
[2]
2233 keyframedata
+= [kf
]
2239 # Add to animation buffer
2242 node_def
['anim_count'] += 1
2246 status_name
= F
" " + " |"*(node_def
['depth']-1)
2247 print( F
"{status_name} | *anim: {NLAStrip.action.name}" )
2251 # Restore context to how it was before
2253 bpy
.context
.scene
.frame_set( previous_frame
)
2254 obj
.animation_data
.action
= previous_action
2255 obj
.data
.pose_position
= POSE_OR_REST_CACHE
2259 # We are trying to compile this node_def
2261 def encoder_process_definition( node_def
):
2265 # data sources for object/bone are taken differently
2267 if 'obj' in node_def
:
2269 obj
= node_def
['obj']
2271 obj_co
= obj
.matrix_world
@ Vector((0,0,0))
2273 if obj_type
== 'ARMATURE':
2274 obj_classtype
= 'classtype_skeleton'
2275 elif obj_type
== 'LIGHT':
2277 obj_classtype
= 'classtype_world_light'
2281 obj_classtype
= obj
.cv_data
.classtype
2283 # Check for armature deform
2285 for mod
in obj
.modifiers
:
2287 if mod
.type == 'ARMATURE':
2289 obj_classtype
= 'classtype_skin'
2291 # Make sure to freeze armature in rest while we collect
2292 # vertex information
2294 armature_def
= g_encoder
['graph_lookup'][mod
.object]
2295 POSE_OR_REST_CACHE
= armature_def
['obj'].data
.pose_position
2296 armature_def
['obj'].data
.pose_position
= 'REST'
2297 node_def
['linked_armature'] = armature_def
2304 elif 'bone' in node_def
:
2306 obj
= node_def
['bone']
2308 obj_co
= obj
.head_local
2309 obj_classtype
= 'classtype_bone'
2315 node
.pstr_name
= encoder_process_pstr( obj
.name
)
2317 if node_def
["parent"]:
2318 node
.parent
= node_def
["parent"]["uid"]
2322 node
.co
[0] = obj_co
[0]
2323 node
.co
[1] = obj_co
[2]
2324 node
.co
[2] = -obj_co
[1]
2326 # Convert rotation quat to our space type
2328 quat
= obj
.matrix_local
.to_quaternion()
2331 node
.q
[2] = -quat
[2]
2334 # Bone scale is just a vector to the tail
2336 if obj_type
== 'BONE':
2338 node
.s
[0] = obj
.tail_local
[0] - node
.co
[0]
2339 node
.s
[1] = obj
.tail_local
[2] - node
.co
[1]
2340 node
.s
[2] = -obj
.tail_local
[1] - node
.co
[2]
2344 node
.s
[0] = obj
.scale
[0]
2345 node
.s
[1] = obj
.scale
[2]
2346 node
.s
[2] = obj
.scale
[1]
2351 tot_uid
= g_encoder
['uid_count']-1
2352 obj_uid
= node_def
['uid']
2353 obj_depth
= node_def
['depth']-1
2355 status_id
= F
" [{obj_uid: 3}/{tot_uid}]" + " |"*obj_depth
2356 status_name
= status_id
+ F
" L {obj.name}"
2358 if obj_classtype
!= 'classtype_none': status_type
= obj_classtype
2359 else: status_type
= obj_type
2361 status_parent
= F
"{node.parent: 3}"
2364 if obj_classtype
== 'classtype_skin':
2365 status_armref
= F
" [armature -> {armature_def['obj'].cv_data.uid}]"
2367 print(F
"{status_name:<32} {status_type:<22} {status_parent} {status_armref}")
2369 # Process mesh if needed
2371 if obj_type
== 'MESH':
2373 encoder_compile_mesh( node
, node_def
)
2375 elif obj_type
== 'ARMATURE':
2377 encoder_compile_armature( node
, node_def
)
2380 encoder_compile_ent_as( obj_classtype
, node
, node_def
)
2382 # Make sure to reset the armature we just mucked about with
2384 if obj_classtype
== 'classtype_skin':
2385 armature_def
['obj'].data
.pose_position
= POSE_OR_REST_CACHE
2387 g_encoder
['data']['node'] += [node
]
2390 # The post processing step or the pre processing to the writing step
2392 def encoder_write_to_file( path
):
2396 # Compile down to a byte array
2398 header
= g_encoder
['header']
2399 file_pos
= sizeof(header
)
2400 file_data
= bytearray()
2401 print( " Compositing data arrays" )
2403 for array_name
in g_encoder
['data']:
2405 file_pos
+= bytearray_align_to( file_data
, 16, sizeof(header
) )
2406 arr
= g_encoder
['data'][array_name
]
2408 setattr( header
, array_name
+ "_offset", file_pos
)
2410 print( F
" {array_name:<16} @{file_pos:> 8X}[{len(arr)}]" )
2412 if isinstance( arr
, bytearray
):
2414 setattr( header
, array_name
+ "_size", len(arr
) )
2416 file_data
.extend( arr
)
2417 file_pos
+= len(arr
)
2421 setattr( header
, array_name
+ "_count", len(arr
) )
2425 bbytes
= bytearray(item
)
2426 file_data
.extend( bbytes
)
2427 file_pos
+= sizeof(item
)
2432 # This imperitive for this field to be santized in the future!
2434 header
.file_length
= file_pos
2436 print( " Writing file" )
2437 # Write header and data chunk to file
2439 fp
= open( path
, "wb" )
2440 fp
.write( bytearray( header
) )
2441 fp
.write( file_data
)
2445 # Main compiler, uses string as the identifier for the collection
2447 def write_model(collection_name
):
2450 print( F
"Model graph | Create mode '{collection_name}'" )
2451 folder
= bpy
.path
.abspath(bpy
.context
.scene
.cv_data
.export_dir
)
2452 path
= F
"{folder}{collection_name}.mdl"
2455 collection
= bpy
.data
.collections
[collection_name
]
2457 encoder_init( collection
)
2458 encoder_build_scene_graph( collection
)
2462 print( " Comping objects" )
2463 it
= encoder_graph_iterator( g_encoder
['scene_graph'] )
2465 encoder_process_definition( node_def
)
2469 encoder_write_to_file( path
)
2471 print( F
"Completed {collection_name}.mdl" )
2474 # ---------------------------------------------------------------------------- #
2478 # ---------------------------------------------------------------------------- #
2480 cv_view_draw_handler
= None
2481 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
2483 cv_view_colours
= []
2484 cv_view_course_i
= 0
2486 # Draw axis alligned sphere at position with radius
2488 def cv_draw_sphere( pos
, radius
, colour
):
2490 global cv_view_verts
, cv_view_colours
2492 ly
= pos
+ Vector((0,0,radius
))
2493 lx
= pos
+ Vector((0,radius
,0))
2494 lz
= pos
+ Vector((0,0,radius
))
2496 pi
= 3.14159265358979323846264
2500 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
2504 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
2505 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
2506 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
2508 cv_view_verts
+= [ px
, lx
]
2509 cv_view_verts
+= [ py
, ly
]
2510 cv_view_verts
+= [ pz
, lz
]
2512 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2521 # Draw axis alligned sphere at position with radius
2523 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
2525 global cv_view_verts
, cv_view_colours
2527 ly
= pos
+ tz
*radius
2528 lx
= pos
+ ty
*radius
2529 lz
= pos
+ tz
*radius
2531 pi
= 3.14159265358979323846264
2535 t
= ((i
+1.0) * 1.0/16.0) * pi
2539 s1
= math
.sin(t
*2.0)
2540 c1
= math
.cos(t
*2.0)
2542 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
2543 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
2544 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
2546 cv_view_verts
+= [ px
, lx
]
2547 cv_view_verts
+= [ py
, ly
]
2548 cv_view_verts
+= [ pz
, lz
]
2550 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2559 # Draw transformed -1 -> 1 cube
2561 def cv_draw_ucube( transform
, colour
):
2563 global cv_view_verts
, cv_view_colours
2565 a
= Vector((-1,-1,-1))
2569 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
2570 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
2571 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
2572 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
2573 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
2574 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
2575 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
2576 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
2578 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2579 (0,4),(1,5),(2,6),(3,7)]
2585 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2586 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2587 cv_view_colours
+= [colour
, colour
]
2592 # Draw line with colour
2594 def cv_draw_line( p0
, p1
, colour
):
2596 global cv_view_verts
, cv_view_colours
2598 cv_view_verts
+= [p0
,p1
]
2599 cv_view_colours
+= [colour
, colour
]
2603 # Draw line with colour(s)
2605 def cv_draw_line2( p0
, p1
, c0
, c1
):
2607 global cv_view_verts
, cv_view_colours
2609 cv_view_verts
+= [p0
,p1
]
2610 cv_view_colours
+= [c0
,c1
]
2616 def cv_tangent_basis( n
, tx
, ty
):
2618 if abs( n
[0] ) >= 0.57735027:
2639 # Draw coloured arrow
2641 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
2643 global cv_view_verts
, cv_view_colours
2649 tx
= Vector((1,0,0))
2650 ty
= Vector((1,0,0))
2651 cv_tangent_basis( n
, tx
, ty
)
2653 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
2654 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
2658 # Drawhandles of a bezier control point
2660 def cv_draw_bhandle( obj
, direction
, colour
):
2662 global cv_view_verts
, cv_view_colours
2665 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2667 cv_view_verts
+= [p0
]
2668 cv_view_verts
+= [h0
]
2669 cv_view_colours
+= [colour
,colour
]
2673 # Draw a bezier curve (at fixed resolution 10)
2675 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2677 global cv_view_verts
, cv_view_colours
2687 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2689 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2690 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2691 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2698 # I think this one extends the handles of the bezier otwards......
2700 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2702 global cv_view_course_i
2704 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2706 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2707 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2708 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2709 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2711 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2715 # Flush the lines buffers. This is called often because god help you if you want
2716 # to do fixed, fast buffers in this catastrophic programming language.
2718 def cv_draw_lines():
2720 global cv_view_shader
, cv_view_verts
, cv_view_colours
2722 if len(cv_view_verts
) < 2:
2725 lines
= batch_for_shader(\
2726 cv_view_shader
, 'LINES', \
2727 { "pos":cv_view_verts
, "color":cv_view_colours
})
2729 lines
.draw( cv_view_shader
)
2732 cv_view_colours
= []
2735 # I dont remember what this does exactly
2737 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2739 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2742 # Semi circle to show the limit. and some lines
2744 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2746 global cv_view_verts
, cv_view_colours
2755 a0
= amin
*(1.0-t0
)+amax
*t0
2756 a1
= amin
*(1.0-t1
)+amax
*t1
2758 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2759 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2761 p0
=obj
.matrix_world
@ p0
2762 p1
=obj
.matrix_world
@ p1
2763 cv_view_verts
+= [p0
,p1
]
2764 cv_view_colours
+= [colour
,colour
]
2768 cv_view_verts
+= [p0
,center
]
2769 cv_view_colours
+= [colour
,colour
]
2773 cv_view_verts
+= [p1
,center
]
2774 cv_view_colours
+= [colour
,colour
]
2778 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2779 cv_view_colours
+= [colour
,colour
]
2784 # Cone and twist limit
2786 def draw_cone_twist( center
, vx
, vy
, va
):
2788 global cv_view_verts
, cv_view_colours
2789 axis
= vy
.cross( vx
)
2794 cv_view_verts
+= [center
, center
+va
*size
]
2795 cv_view_colours
+= [ (1,1,1,1), (1,1,1,1) ]
2799 t0
= (x
/32) * math
.tau
2800 t1
= ((x
+1)/32) * math
.tau
2807 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
2808 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
2810 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
2811 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
2813 cv_view_verts
+= [center
, p0
, p0
, p1
]
2814 cv_view_colours
+= [ (0,0,0,0), col0
, col0
, col1
]
2820 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
2822 def draw_skeleton_helpers( obj
):
2824 global cv_view_verts
, cv_view_colours
2826 if obj
.data
.pose_position
!= 'REST':
2831 for bone
in obj
.data
.bones
:
2834 a
= Vector((bone
.cv_data
.v0
[0], bone
.cv_data
.v0
[1], bone
.cv_data
.v0
[2]))
2835 b
= Vector((bone
.cv_data
.v1
[0], bone
.cv_data
.v1
[1], bone
.cv_data
.v1
[2]))
2837 if bone
.cv_data
.collider
== 'collider_box':
2841 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
2842 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
2843 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
2844 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
2845 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
2846 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
2847 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
2848 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
2850 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2851 (0,4),(1,5),(2,6),(3,7)]
2858 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2859 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2860 cv_view_colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
2863 elif bone
.cv_data
.collider
== 'collider_capsule':
2871 if abs(v0
[i
]) > largest
:
2873 largest
= abs(v0
[i
])
2878 v1
= Vector((0,0,0))
2879 v1
[major_axis
] = 1.0
2881 tx
= Vector((0,0,0))
2882 ty
= Vector((0,0,0))
2884 cv_tangent_basis( v1
, tx
, ty
)
2885 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
2886 l
= v0
[ major_axis
] - r
*2
2888 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
2889 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
2891 colour
= [0.2,0.2,0.2,1.0]
2892 colour
[major_axis
] = 0.5
2894 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
2895 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
2896 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
2897 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
2898 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
2899 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
2906 center
= obj
.matrix_world
@ c
2907 if bone
.cv_data
.con0
:
2909 vx
= Vector([bone
.cv_data
.conevx
[_
] for _
in range(3)])
2910 vy
= Vector([bone
.cv_data
.conevy
[_
] for _
in range(3)])
2911 va
= Vector([bone
.cv_data
.coneva
[_
] for _
in range(3)])
2912 draw_cone_twist( center
, vx
, vy
, va
)
2914 #draw_limit( obj, c, Vector((0,0,1)),Vector((0,-1,0)), \
2915 # bone.cv_data.mins[0], bone.cv_data.maxs[0], \
2917 #draw_limit( obj, c, Vector((0,-1,0)),Vector((1,0,0)), \
2918 # bone.cv_data.mins[1], bone.cv_data.maxs[1], \
2920 #draw_limit( obj, c, Vector((1,0,0)),Vector((0,0,1)), \
2921 # bone.cv_data.mins[2], bone.cv_data.maxs[2], \
2929 global cv_view_shader
2930 global cv_view_verts
2931 global cv_view_colours
2932 global cv_view_course_i
2934 cv_view_course_i
= 0
2936 cv_view_colours
= []
2938 cv_view_shader
.bind()
2939 gpu
.state
.depth_mask_set(False)
2940 gpu
.state
.line_width_set(2.0)
2941 gpu
.state
.face_culling_set('BACK')
2942 gpu
.state
.depth_test_set('LESS')
2943 gpu
.state
.blend_set('NONE')
2945 for obj
in bpy
.context
.collection
.objects
:
2947 if obj
.type == 'ARMATURE':
2949 if obj
.data
.pose_position
== 'REST':
2950 draw_skeleton_helpers( obj
)
2954 classtype
= obj
.cv_data
.classtype
2955 if (classtype
!= 'classtype_none') and (classtype
in globals()):
2957 cl
= globals()[ classtype
]
2959 if getattr( cl
, "draw_scene_helpers", None ):
2961 cl
.draw_scene_helpers( obj
)
2972 # ---------------------------------------------------------------------------- #
2976 # ---------------------------------------------------------------------------- #
2978 # Checks whether this object has a classtype assigned. we can only target other
2980 def cv_poll_target(scene
, obj
):
2982 if obj
== bpy
.context
.active_object
:
2984 if obj
.cv_data
.classtype
== 'classtype_none':
2990 class CV_MESH_SETTINGS(bpy
.types
.PropertyGroup
):
2992 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
2993 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
2994 v2
: bpy
.props
.FloatVectorProperty(name
="v2",size
=3)
2995 v3
: bpy
.props
.FloatVectorProperty(name
="v3",size
=3)
2998 class CV_LIGHT_SETTINGS(bpy
.types
.PropertyGroup
):
3000 bp0
: bpy
.props
.BoolProperty( name
="bp0" );
3003 class CV_LIGHT_PANEL(bpy
.types
.Panel
):
3005 bl_label
="[Skate Rift]"
3006 bl_idname
="SCENE_PT_cv_light"
3007 bl_space_type
='PROPERTIES'
3008 bl_region_type
='WINDOW'
3011 def draw(_
,context
):
3013 active_object
= context
.active_object
3014 if active_object
== None: return
3016 if active_object
.type != 'LIGHT': return
3018 data
= active_object
.data
.cv_data
3019 _
.layout
.prop( data
, "bp0", text
="Only on during night" )
3023 class CV_OBJ_SETTINGS(bpy
.types
.PropertyGroup
):
3025 uid
: bpy
.props
.IntProperty( name
="" )
3027 strp
: bpy
.props
.StringProperty( name
="strp" )
3028 intp
: bpy
.props
.IntProperty( name
="intp" )
3029 intp1
: bpy
.props
.IntProperty( name
="intp1" )
3030 fltp
: bpy
.props
.FloatProperty( name
="fltp" )
3031 bp0
: bpy
.props
.BoolProperty( name
="bp0" )
3032 bp1
: bpy
.props
.BoolProperty( name
="bp1" )
3033 bp2
: bpy
.props
.BoolProperty( name
="bp2" )
3034 bp3
: bpy
.props
.BoolProperty( name
="bp3" )
3036 target
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target", \
3037 poll
=cv_poll_target
)
3038 target1
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target1", \
3039 poll
=cv_poll_target
)
3040 target2
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target2", \
3041 poll
=cv_poll_target
)
3042 target3
: bpy
.props
.PointerProperty( type=bpy
.types
.Object
, name
="target3", \
3043 poll
=cv_poll_target
)
3045 colour
: bpy
.props
.FloatVectorProperty( name
="colour",subtype
='COLOR',\
3048 function
: bpy
.props
.EnumProperty(
3050 items
= classtype_logic_wire
.get_targeted_methods
3053 classtype
: bpy
.props
.EnumProperty(
3056 ('classtype_none', "classtype_none", "", 0),
3057 ('classtype_gate', "classtype_gate", "", 1),
3058 ('classtype_spawn', "classtype_spawn", "", 3),
3059 ('classtype_water', "classtype_water", "", 4),
3060 ('classtype_route_node', "classtype_route_node", "", 8 ),
3061 ('classtype_route', "classtype_route", "", 9 ),
3062 ('classtype_audio',"classtype_audio","",14),
3063 ('classtype_trigger',"classtype_trigger","",100),
3064 ('classtype_logic_achievement',"classtype_logic_achievement","",101),
3065 ('classtype_logic_relay',"classtype_logic_relay","",102),
3066 ('classtype_logic_wire',"classtype_logic_wire","",105),
3067 ('classtype_soundscape',"classtype_soundscape","",106),
3068 ('classtype_logic_chances',"classtype_logic_chances","",107),
3069 ('classtype_particle_box',"classtype_particle_box","",108),
3070 ('classtype_signal_splitter',"classtype_signal_splitter","",109),
3071 ('classtype_spawn_link',"classtype_spawn_link","",150),
3072 ('classtype_nonlocal_gate', "classtype_nonlocal_gate", "", 300)
3075 audio_format
: bpy
.props
.EnumProperty(
3076 name
="Loaded format",
3078 ('mono', "mono", "", 0),
3079 ('stereo', "stereo", "", 1),
3080 ('remain compressed', "remain compressed", "", 2),
3081 ('synthetic bird',"synthetic bird","",3)
3085 class CV_BONE_SETTINGS(bpy
.types
.PropertyGroup
):
3087 collider
: bpy
.props
.EnumProperty(
3088 name
="Collider Type",
3090 ('collider_none', "collider_none", "", 0),
3091 ('collider_box', "collider_box", "", 1),
3092 ('collider_capsule', "collider_capsule", "", 2),
3095 v0
: bpy
.props
.FloatVectorProperty(name
="v0",size
=3)
3096 v1
: bpy
.props
.FloatVectorProperty(name
="v1",size
=3)
3098 con0
: bpy
.props
.BoolProperty(name
="Constriant 0",default
=False)
3099 mins
: bpy
.props
.FloatVectorProperty(name
="mins",size
=3)
3100 maxs
: bpy
.props
.FloatVectorProperty(name
="maxs",size
=3)
3102 conevx
: bpy
.props
.FloatVectorProperty(name
="conevx",size
=3)
3103 conevy
: bpy
.props
.FloatVectorProperty(name
="conevy",size
=3)
3104 coneva
: bpy
.props
.FloatVectorProperty(name
="coneva",size
=3)
3105 conet
: bpy
.props
.FloatProperty(name
="conet")
3108 class CV_BONE_PANEL(bpy
.types
.Panel
):
3110 bl_label
="[Skate Rift]"
3111 bl_idname
="SCENE_PT_cv_bone"
3112 bl_space_type
='PROPERTIES'
3113 bl_region_type
='WINDOW'
3116 def draw(_
,context
):
3118 active_object
= context
.active_object
3119 if active_object
== None: return
3121 bone
= active_object
.data
.bones
.active
3122 if bone
== None: return
3124 _
.layout
.prop( bone
.cv_data
, "collider" )
3125 _
.layout
.prop( bone
.cv_data
, "v0" )
3126 _
.layout
.prop( bone
.cv_data
, "v1" )
3128 _
.layout
.label( text
="Angle Limits" )
3129 _
.layout
.prop( bone
.cv_data
, "con0" )
3131 _
.layout
.prop( bone
.cv_data
, "conevx" )
3132 _
.layout
.prop( bone
.cv_data
, "conevy" )
3133 _
.layout
.prop( bone
.cv_data
, "coneva" )
3134 _
.layout
.prop( bone
.cv_data
, "conet" )
3138 class CV_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
3140 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
3141 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
3144 class CV_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
3146 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
3147 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
3150 class CV_MATERIAL_SETTINGS(bpy
.types
.PropertyGroup
):
3152 shader
: bpy
.props
.EnumProperty(
3155 ('standard',"standard","",0),
3156 ('standard_cutout', "standard_cutout", "", 1),
3157 ('terrain_blend', "terrain_blend", "", 2),
3158 ('vertex_blend', "vertex_blend", "", 3),
3159 ('water',"water","",4),
3162 surface_prop
: bpy
.props
.EnumProperty(
3163 name
="Surface Property",
3165 ('concrete','concrete','',0),
3166 ('wood','wood','',1),
3167 ('grass','grass','',2),
3168 ('tiles','tiles','',3)
3171 collision
: bpy
.props
.BoolProperty( \
3172 name
="Collisions Enabled",\
3174 description
= "Can the player collide with this material"\
3176 skate_surface
: bpy
.props
.BoolProperty( \
3177 name
="Skate Surface", \
3179 description
= "Should the game try to target this surface?" \
3181 grind_surface
: bpy
.props
.BoolProperty( \
3182 name
="Grind Surface", \
3184 description
= "Grind face?" \
3186 grow_grass
: bpy
.props
.BoolProperty( \
3187 name
="Grow Grass", \
3189 description
= "Spawn grass sprites on this surface?" \
3191 blend_offset
: bpy
.props
.FloatVectorProperty( \
3192 name
="Blend Offset", \
3194 default
=Vector((0.5,0.0)),\
3195 description
="When surface is more than 45 degrees, add this vector " +\
3198 sand_colour
: bpy
.props
.FloatVectorProperty( \
3199 name
="Sand Colour",\
3202 default
=Vector((0.79,0.63,0.48)),\
3203 description
="Blend to this colour near the 0 coordinate on UP axis"\
3205 shore_colour
: bpy
.props
.FloatVectorProperty( \
3206 name
="Shore Colour",\
3209 default
=Vector((0.03,0.32,0.61)),\
3210 description
="Water colour at the shoreline"\
3212 ocean_colour
: bpy
.props
.FloatVectorProperty( \
3213 name
="Ocean Colour",\
3216 default
=Vector((0.0,0.006,0.03)),\
3217 description
="Water colour in the deep bits"\
3221 class CV_MATERIAL_PANEL(bpy
.types
.Panel
):
3223 bl_label
="Skate Rift material"
3224 bl_idname
="MATERIAL_PT_cv_material"
3225 bl_space_type
='PROPERTIES'
3226 bl_region_type
='WINDOW'
3227 bl_context
="material"
3229 def draw(_
,context
):
3231 active_object
= bpy
.context
.active_object
3232 if active_object
== None: return
3233 active_mat
= active_object
.active_material
3234 if active_mat
== None: return
3236 info
= material_info( active_mat
)
3238 if 'tex_diffuse' in info
:
3240 _
.layout
.label( icon
='INFO', \
3241 text
=F
"{info['tex_diffuse'].name} will be compiled" )
3244 _
.layout
.prop( active_mat
.cv_data
, "shader" )
3245 _
.layout
.prop( active_mat
.cv_data
, "surface_prop" )
3246 _
.layout
.prop( active_mat
.cv_data
, "collision" )
3248 if active_mat
.cv_data
.collision
:
3249 _
.layout
.prop( active_mat
.cv_data
, "skate_surface" )
3250 _
.layout
.prop( active_mat
.cv_data
, "grind_surface" )
3251 _
.layout
.prop( active_mat
.cv_data
, "grow_grass" )
3253 if active_mat
.cv_data
.shader
== "terrain_blend":
3255 box
= _
.layout
.box()
3256 box
.prop( active_mat
.cv_data
, "blend_offset" )
3257 box
.prop( active_mat
.cv_data
, "sand_colour" )
3259 elif active_mat
.cv_data
.shader
== "vertex_blend":
3261 box
= _
.layout
.box()
3262 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
3263 box
.prop( active_mat
.cv_data
, "blend_offset" )
3265 elif active_mat
.cv_data
.shader
== "water":
3267 box
= _
.layout
.box()
3268 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
3269 box
.prop( active_mat
.cv_data
, "shore_colour" )
3270 box
.prop( active_mat
.cv_data
, "ocean_colour" )
3275 class CV_OBJ_PANEL(bpy
.types
.Panel
):
3277 bl_label
="Entity Config"
3278 bl_idname
="SCENE_PT_cv_entity"
3279 bl_space_type
='PROPERTIES'
3280 bl_region_type
='WINDOW'
3283 def draw(_
,context
):
3285 active_object
= bpy
.context
.active_object
3286 if active_object
== None: return
3287 if active_object
.type == 'ARMATURE':
3289 row
= _
.layout
.row()
3291 row
.label( text
="This object has the intrinsic classtype of skeleton" )
3295 _
.layout
.prop( active_object
.cv_data
, "classtype" )
3297 classtype
= active_object
.cv_data
.classtype
3299 if (classtype
!= 'classtype_none') and (classtype
in globals()):
3301 cl
= globals()[ classtype
]
3303 if getattr( cl
, "editor_interface", None ):
3305 cl
.editor_interface( _
.layout
, active_object
)
3311 class CV_COMPILE(bpy
.types
.Operator
):
3313 bl_idname
="carve.compile_all"
3314 bl_label
="Compile All"
3316 def execute(_
,context
):
3318 view_layer
= bpy
.context
.view_layer
3319 for col
in view_layer
.layer_collection
.children
["export"].children
:
3320 if not col
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
3321 write_model( col
.name
)
3327 class CV_COMPILE_THIS(bpy
.types
.Operator
):
3329 bl_idname
="carve.compile_this"
3330 bl_label
="Compile This collection"
3332 def execute(_
,context
):
3334 col
= bpy
.context
.collection
3335 write_model( col
.name
)
3341 class CV_INTERFACE(bpy
.types
.Panel
):
3343 bl_idname
= "VIEW3D_PT_carve"
3344 bl_label
= "Skate Rift"
3345 bl_space_type
= 'VIEW_3D'
3346 bl_region_type
= 'UI'
3347 bl_category
= "Skate Rift"
3349 def draw(_
, context
):
3352 layout
.prop( context
.scene
.cv_data
, "export_dir" )
3354 col
= bpy
.context
.collection
3356 found_in_export
= False
3358 view_layer
= bpy
.context
.view_layer
3359 for c1
in view_layer
.layer_collection
.children
["export"].children
:
3361 if not c1
.hide_viewport
or bpy
.context
.scene
.cv_data
.use_hidden
:
3364 if c1
.name
== col
.name
:
3366 found_in_export
= True
3373 box
.label( text
=col
.name
+ ".mdl" )
3374 box
.prop( col
.cv_data
, "pack_textures" )
3375 box
.prop( col
.cv_data
, "animations" )
3376 box
.operator( "carve.compile_this" )
3382 row
.label( text
=col
.name
)
3383 box
.label( text
="This collection is not in the export group" )
3389 split
= row
.split( factor
= 0.3, align
=True )
3390 split
.prop( context
.scene
.cv_data
, "use_hidden", text
="hidden" )
3393 if export_count
== 0:
3395 row1
.operator( "carve.compile_all", \
3396 text
=F
"Compile all ({export_count} collections)" )
3401 classes
= [CV_OBJ_SETTINGS
,CV_OBJ_PANEL
,CV_COMPILE
,CV_INTERFACE
,\
3402 CV_MESH_SETTINGS
, CV_SCENE_SETTINGS
, CV_BONE_SETTINGS
,\
3403 CV_BONE_PANEL
, CV_COLLECTION_SETTINGS
, CV_COMPILE_THIS
,\
3404 CV_MATERIAL_SETTINGS
, CV_MATERIAL_PANEL
, CV_LIGHT_SETTINGS
,\
3409 global cv_view_draw_handler
3412 bpy
.utils
.register_class(c
)
3414 bpy
.types
.Object
.cv_data
= bpy
.props
.PointerProperty(type=CV_OBJ_SETTINGS
)
3415 bpy
.types
.Mesh
.cv_data
= bpy
.props
.PointerProperty(type=CV_MESH_SETTINGS
)
3416 bpy
.types
.Scene
.cv_data
= bpy
.props
.PointerProperty(type=CV_SCENE_SETTINGS
)
3417 bpy
.types
.Bone
.cv_data
= bpy
.props
.PointerProperty(type=CV_BONE_SETTINGS
)
3418 bpy
.types
.Collection
.cv_data
= \
3419 bpy
.props
.PointerProperty(type=CV_COLLECTION_SETTINGS
)
3420 bpy
.types
.Material
.cv_data
= \
3421 bpy
.props
.PointerProperty(type=CV_MATERIAL_SETTINGS
)
3422 bpy
.types
.Light
.cv_data
= bpy
.props
.PointerProperty(type=CV_LIGHT_SETTINGS
)
3424 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
3425 cv_draw
,(),'WINDOW','POST_VIEW')
3430 global cv_view_draw_handler
3433 bpy
.utils
.unregister_class(c
)
3435 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
3438 # ---------------------------------------------------------------------------- #
3442 # ---------------------------------------------------------------------------- #
3444 # Transliteration of: #
3445 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
3447 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
3448 # SPDX-License-Identifier: MIT #
3449 # QOI - The "Quite OK Image" format for fast, lossless image compression #
3451 # ---------------------------------------------------------------------------- #
3453 class qoi_rgba_t(Structure
):
3456 _fields_
= [("r",c_uint8
),
3462 QOI_OP_INDEX
= 0x00 # 00xxxxxx
3463 QOI_OP_DIFF
= 0x40 # 01xxxxxx
3464 QOI_OP_LUMA
= 0x80 # 10xxxxxx
3465 QOI_OP_RUN
= 0xc0 # 11xxxxxx
3466 QOI_OP_RGB
= 0xfe # 11111110
3467 QOI_OP_RGBA
= 0xff # 11111111
3469 QOI_MASK_2
= 0xc0 # 11000000
3471 def qoi_colour_hash( c
):
3473 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
3478 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
3483 return bytearray([ (0xff000000 & v
) >> 24, \
3484 (0x00ff0000 & v
) >> 16, \
3485 (0x0000ff00 & v
) >> 8, \
3489 def qoi_encode( img
):
3493 print(F
" . Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]")
3495 index
= [ qoi_rgba_t() for _
in range(64) ]
3499 data
.extend( bytearray(c_uint32(0x66696f71)) )
3500 data
.extend( qoi_32bit( img
.size
[0] ) )
3501 data
.extend( qoi_32bit( img
.size
[1] ) )
3502 data
.extend( bytearray(c_uint8(4)) )
3503 data
.extend( bytearray(c_uint8(0)) )
3506 px_prev
= qoi_rgba_t()
3507 px_prev
.r
= c_uint8(0)
3508 px_prev
.g
= c_uint8(0)
3509 px_prev
.b
= c_uint8(0)
3510 px_prev
.a
= c_uint8(255)
3518 px_len
= img
.size
[0] * img
.size
[1]
3520 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
3522 for px_pos
in range( px_len
):
3524 idx
= px_pos
* img
.channels
3527 px
.r
= paxels
[idx
+min(0,nc
)]
3528 px
.g
= paxels
[idx
+min(1,nc
)]
3529 px
.b
= paxels
[idx
+min(2,nc
)]
3530 px
.a
= paxels
[idx
+min(3,nc
)]
3532 if qoi_eq( px
, px_prev
):
3536 if (run
== 62) or (px_pos
== px_len
-1):
3538 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3546 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3550 index_pos
= qoi_colour_hash(px
) % 64
3552 if qoi_eq( index
[index_pos
], px
):
3554 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
3558 index
[ index_pos
].r
= px
.r
3559 index
[ index_pos
].g
= px
.g
3560 index
[ index_pos
].b
= px
.b
3561 index
[ index_pos
].a
= px
.a
3563 if px
.a
== px_prev
.a
:
3565 vr
= int(px
.r
) - int(px_prev
.r
)
3566 vg
= int(px
.g
) - int(px_prev
.g
)
3567 vb
= int(px
.b
) - int(px_prev
.b
)
3572 if (vr
> -3) and (vr
< 2) and\
3573 (vg
> -3) and (vg
< 2) and\
3574 (vb
> -3) and (vb
< 2):
3576 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
3577 data
.extend( bytearray( c_uint8(op
) ))
3579 elif (vg_r
> -9) and (vg_r
< 8) and\
3580 (vg
> -33) and (vg
< 32 ) and\
3581 (vg_b
> -9) and (vg_b
< 8):
3583 op
= QOI_OP_LUMA |
(vg
+32)
3584 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
3585 data
.extend( bytearray( c_uint8(op
) ) )
3586 data
.extend( bytearray( c_uint8(delta
) ))
3590 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
3591 data
.extend( bytearray( c_uint8(px
.r
) ))
3592 data
.extend( bytearray( c_uint8(px
.g
) ))
3593 data
.extend( bytearray( c_uint8(px
.b
) ))
3598 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
3599 data
.extend( bytearray( c_uint8(px
.r
) ))
3600 data
.extend( bytearray( c_uint8(px
.g
) ))
3601 data
.extend( bytearray( c_uint8(px
.b
) ))
3602 data
.extend( bytearray( c_uint8(px
.a
) ))
3615 data
.extend( bytearray( c_uint8(0) ))
3616 data
.extend( bytearray( c_uint8(1) ))
3617 bytearray_align_to( data
, 16, 0 )