1 import bpy
, math
, gpu
, os
4 from mathutils
import *
5 from gpu_extras
.batch
import batch_for_shader
6 from bpy_extras
import mesh_utils
9 "name":"Skaterift .mdl exporter",
10 "author": "Harry Godden (hgn)",
17 "category":"Import/Export",
21 ('none', 'None', '', 0 ),
22 ('ent_gate', 'Gate', '', 1 ),
23 ('ent_spawn', 'Spawn Point', '', 2 ),
24 ('ent_route_node', 'Routing Path', '', 3 ),
25 ('ent_route', 'Skate Course', '', 4 ),
26 ('ent_water', 'Water Surface', '', 5 ),
27 ('ent_volume', 'Volume/Trigger', '', 6 ),
28 ('ent_audio', 'Audio', '', 7 ),
29 ('ent_marker', 'Marker', '', 8 ),
30 ('ent_font', 'Font', '', 9 ),
31 ('ent_font_variant', 'Font:Variant', '', 10 ),
32 ('ent_traffic', 'Traffic Model', '', 11 ),
35 def get_entity_enum_id( alias
):
37 for et
in sr_entity_list
:#{
46 class mdl_vert(Structure
): # 48 bytes. Quite large. Could compress
47 #{ # the normals and uvs to i16s. Not an
48 _pack_
= 1 # real issue, yet.
49 _fields_
= [("co",c_float
*3),
53 ("weights",c_uint16
*4),
57 class mdl_transform(Structure
):
59 _fields_
= [("co",c_float
*3),
64 class mdl_submesh(Structure
):
66 _fields_
= [("indice_start",c_uint32
),
67 ("indice_count",c_uint32
),
68 ("vertex_start",c_uint32
),
69 ("vertex_count",c_uint32
),
70 ("bbx",(c_float
*3)*2),
71 ("material_id",c_uint16
), # index into the material array
75 class mdl_material(Structure
):
77 _fields_
= [("pstr_name",c_uint32
),
80 ("surface_prop",c_uint32
),
82 ("colour1",c_float
*4),
83 ("tex_diffuse",c_uint32
),
84 ("tex_none0",c_uint32
),
85 ("tex_none1",c_uint32
)]
88 class mdl_bone(Structure
):
90 _fields_
= [("co",c_float
*3),("end",c_float
*3),
92 ("collider",c_uint32
),
93 ("ik_target",c_uint32
),
96 ("pstr_name",c_uint32
),
97 ("hitbox",(c_float
*3)*2),
98 ("conevx",c_float
*3),("conevy",c_float
*3),("coneva",c_float
*3),
102 class mdl_armature(Structure
):
104 _fields_
= [("transform",mdl_transform
),
105 ("bone_start",c_uint32
),
106 ("bone_count",c_uint32
),
107 ("anim_start",c_uint32
),
108 ("anim_count",c_uint32
)]
111 class mdl_animation(Structure
):
113 _fields_
= [("pstr_name",c_uint32
),
116 ("keyframe_start",c_uint32
)]
119 class mdl_mesh(Structure
):
121 _fields_
= [("transform",mdl_transform
),
122 ("submesh_start",c_uint32
),
123 ("submesh_count",c_uint32
),
124 ("pstr_name",c_uint32
),
125 ("entity_id",c_uint32
),
126 ("armature_id",c_uint32
)]
129 class mdl_file(Structure
):
131 _fields_
= [("path",c_uint32
),
132 ("pack_offset",c_uint32
),
133 ("pack_size",c_uint32
)]
136 class mdl_texture(Structure
):
138 _fields_
= [("file",mdl_file
),
142 class mdl_array(Structure
):
144 _fields_
= [("file_offset",c_uint32
),
145 ("item_count",c_uint32
),
146 ("item_size",c_uint32
),
150 class mdl_header(Structure
):
152 _fields_
= [("version",c_uint32
),
153 ("arrays",mdl_array
)]
156 class ent_spawn(Structure
):
158 _fields_
= [("transform",mdl_transform
),
159 ("pstr_name",c_uint32
)]
162 class ent_light(Structure
):
164 _fields_
= [("transform",mdl_transform
),
165 ("daytime",c_uint32
),
167 ("colour",c_float
*4),
170 ("inverse_world",(c_float
*3)*4), # Runtime
171 ("angle_sin_cos",(c_float
*2))] # Runtime
174 class version_refcount_union(Union
):
176 _fields_
= [("timing_version",c_uint32
),
177 ("ref_count",c_uint8
)]
180 class ent_gate(Structure
):
182 _fields_
= [("type",c_uint32
),
183 ("target", c_uint32
),
184 ("dimensions", c_float
*3),
185 ("co", (c_float
*3)*2),
186 ("q", (c_float
*4)*2),
187 ("to_world",(c_float
*3)*4),
188 ("transport",(c_float
*3)*4),
189 ("_anonymous_union",version_refcount_union
),
190 ("timing_time",c_double
),
191 ("routes",c_uint16
*4),
192 ("route_count",c_uint8
)]
195 class ent_route_node(Structure
):
197 _fields_
= [("co",c_float
*3),
198 ("ref_count",c_uint8
),
199 ("ref_total",c_uint8
)]
202 class ent_path_index(Structure
):
204 _fields_
= [("index",c_uint16
)]
207 class vg_audio_clip(Structure
):
209 _fields_
= [("path",c_uint64
),
215 class union_file_audio_clip(Union
):
217 _fields_
= [("file",mdl_file
),
218 ("reserved",vg_audio_clip
)]
221 class ent_audio_clip(Structure
):
223 _fields_
= [("_anon",union_file_audio_clip
),
224 ("probability",c_float
)]
227 class ent_checkpoint(Structure
):
229 _fields_
= [("gate_index",c_uint16
),
230 ("path_start",c_uint16
),
231 ("path_count",c_uint16
)]
234 class ent_route(Structure
):
236 _fields_
= [("transform",mdl_transform
),
237 ("pstr_name",c_uint32
),
238 ("checkpoints_start",c_uint16
),
239 ("checkpoints_count",c_uint16
),
240 ("colour",c_float
*4),
241 ("active",c_uint32
), #runtime
243 ("board_transform",(c_float
*3)*4),
245 ("latest_pass",c_double
)]
248 class ent_water(Structure
):
250 _fields_
= [("transform",mdl_transform
),
251 ("max_dist",c_float
),
252 ("reserved0",c_uint32
),
253 ("reserved1",c_uint32
)]
256 class volume_trigger(Structure
):
258 _fields_
= [("event",c_uint32
),
262 class volume_particles(Structure
):
264 _fields_
= [("blank",c_uint32
),
268 class volume_union(Union
):
270 _fields_
= [("trigger",volume_trigger
),
271 ("particles",volume_particles
)]
274 class ent_index(Structure
):
276 _fields_
= [("type",c_uint32
),
280 class ent_volume(Structure
):
282 _fields_
= [("transform",mdl_transform
),
283 ("to_world",(c_float
*3)*4),
284 ("to_local",(c_float
*3)*4),
286 ("target",ent_index
),
287 ("_anon",volume_union
)]
290 class ent_audio(Structure
):
292 _fields_
= [("transform",mdl_transform
),
294 ("clip_start",c_uint32
),
295 ("clip_count",c_uint32
),
297 ("crossfade",c_float
),
298 ("channel_behaviour",c_uint32
),
300 ("probability_curve",c_uint32
),
301 ("max_channels",c_uint32
)]
304 class ent_marker(Structure
):
306 _fields_
= [("transform",mdl_transform
),
310 class ent_glyph(Structure
):
312 _fields_
= [("size",c_float
*2),
313 ("indice_start",c_uint32
),
314 ("indice_count",c_uint32
)]
317 class ent_font_variant(Structure
):
319 _fields_
= [("name",c_uint32
),
320 ("material_id",c_uint32
)]
323 class ent_font(Structure
):
325 _fields_
= [("alias",c_uint32
),
326 ("variant_start",c_uint32
),
327 ("variant_count",c_uint32
),
328 ("glyph_start",c_uint32
),
329 ("glyph_count",c_uint32
),
330 ("glyph_utf32_base",c_uint32
)]
333 class ent_traffic(Structure
):
335 _fields_
= [("transform",mdl_transform
),
336 ("submesh_start",c_uint32
),
337 ("submesh_count",c_uint32
),
338 ("start_node",c_uint32
),
339 ("node_count",c_uint32
),
345 def obj_ent_type( obj
):
347 if obj
.type == 'ARMATURE': return 'mdl_armature'
348 elif obj
.type == 'LIGHT': return 'ent_light'
349 else: return obj
.SR_data
.ent_type
352 def sr_filter_ent_type( obj
, ent_types
):
354 if obj
== bpy
.context
.active_object
: return False
356 for c0
in obj
.users_collection
:#{
357 for c1
in bpy
.context
.active_object
.users_collection
:#{
359 return obj_ent_type( obj
) in ent_types
367 def compile_obj_transform( obj
, transform
):
369 co
= obj
.matrix_world
@ Vector((0,0,0))
370 q
= obj
.matrix_local
.to_quaternion()
375 transform
.co
[0] = co
[0]
376 transform
.co
[1] = co
[2]
377 transform
.co
[2] = -co
[1]
378 transform
.q
[0] = q
[1]
379 transform
.q
[1] = q
[3]
380 transform
.q
[2] = -q
[2]
381 transform
.q
[3] = q
[0]
382 transform
.s
[0] = s
[0]
383 transform
.s
[1] = s
[2]
384 transform
.s
[2] = s
[1]
387 def int_align_to( v
, align
):
389 while(v
%align
)!=0: v
+= 1
393 def bytearray_align_to( buffer, align
, w
=b
'\xaa' ):
395 while (len(buffer) % align
) != 0: buffer.extend(w
)
399 def bytearray_print_hex( s
, w
=16 ):
401 for r
in range((len(s
)+(w
-1))//w
):#{
403 i1
=min((r
+1)*w
,len(s
))
404 print( F
'{r*w:06x}| \x1B[31m', end
='')
405 print( F
"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end
='' )
406 print( "\x1B[0m", end
='')
407 print( ''.join(chr(x
) if (x
>=33 and x
<=126) else '.' for x
in s
[i0
:i1
] ) )
411 def sr_compile_string( s
):
413 if s
in sr_compile
.string_cache
: return sr_compile
.string_cache
[s
]
415 index
= len( sr_compile
.string_data
)
416 sr_compile
.string_cache
[s
] = index
417 sr_compile
.string_data
.extend( s
.encode('utf-8') )
418 sr_compile
.string_data
.extend( b
'\0' )
420 bytearray_align_to( sr_compile
.string_data
, 4 )
424 def material_tex_image(v
):
434 cxr_graph_mapping
= \
436 # Default shader setup
443 "image": "tex_diffuse"
447 "A": material_tex_image("tex_diffuse"),
448 "B": material_tex_image("tex_decal")
455 "Color": material_tex_image("tex_normal")
461 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
463 def material_info(mat
):
467 # Using the cxr_graph_mapping as a reference, go through the shader
468 # graph and gather all $props from it.
470 def _graph_read( node_def
, node
=None, depth
=0 ):#{
477 _graph_read
.extracted
= []
479 for node_idname
in node_def
:#{
480 for n
in mat
.node_tree
.nodes
:#{
481 if n
.name
== node_idname
:#{
482 node_def
= node_def
[node_idname
]
490 for link
in node_def
:#{
491 link_def
= node_def
[link
]
493 if isinstance( link_def
, dict ):#{
495 for x
in node
.inputs
:#{
496 if isinstance( x
, bpy
.types
.NodeSocketColor
):#{
504 if node_link
and node_link
.is_linked
:#{
505 # look for definitions for the connected node type
507 from_node
= node_link
.links
[0].from_node
509 node_name
= from_node
.name
.split('.')[0]
510 if node_name
in link_def
:#{
511 from_node_def
= link_def
[ node_name
]
513 _graph_read( from_node_def
, from_node
, depth
+1 )
517 if "default" in link_def
:#{
518 prop
= link_def
['default']
519 info
[prop
] = node_link
.default_value
525 info
[prop
] = getattr( node
, link
)
530 _graph_read( cxr_graph_mapping
)
536 decoded
= bytearray()
537 for i
in range(len(s
)//2):#{
538 c
= (ord(s
[i
*2+0])-0x41)
539 c |
= (ord(s
[i
*2+1])-0x41)<<4
540 decoded
.extend(bytearray(c_uint8(c
))) #??
545 def sr_pack_file( file, path
, data
):
547 file.path
= sr_compile_string( path
)
548 file.pack_offset
= len( sr_compile
.pack_data
)
549 file.pack_size
= len( data
)
551 sr_compile
.pack_data
.extend( data
)
552 bytearray_align_to( sr_compile
.pack_data
, 16 )
555 def sr_compile_texture( img
):
560 name
= os
.path
.splitext( img
.name
)[0]
562 if name
in sr_compile
.texture_cache
:
563 return sr_compile
.texture_cache
[name
]
565 texture_index
= (len(sr_compile
.texture_data
)//sizeof(mdl_texture
)) +1
570 if sr_compile
.pack_textures
:#{
571 filedata
= qoi_encode( img
)
572 sr_pack_file( tex
.file, name
, filedata
)
575 sr_compile
.texture_cache
[name
] = texture_index
576 sr_compile
.texture_data
.extend( bytearray(tex
) )
580 def sr_compile_material( mat
):
584 if mat
.name
in sr_compile
.material_cache
:
585 return sr_compile
.material_cache
[mat
.name
]
587 index
= (len(sr_compile
.material_data
)//sizeof(mdl_material
))+1
588 sr_compile
.material_cache
[mat
.name
] = index
591 m
.pstr_name
= sr_compile_string( mat
.name
)
594 if mat
.SR_data
.collision
:#{
595 flags |
= 0x2 # collision flag
596 if (mat
.SR_data
.shader
!= 'invisible') and \
597 (mat
.SR_data
.shader
!= 'boundary'):#{
598 if mat
.SR_data
.skate_surface
: flags |
= 0x1
599 if mat
.SR_data
.grow_grass
: flags |
= 0x4
600 if mat
.SR_data
.grind_surface
: flags |
= 0x8
602 if mat
.SR_data
.shader
== 'invisible': flags |
= 0x10
603 if mat
.SR_data
.shader
== 'boundary': flags |
= (0x10|
0x20)
608 m
.surface_prop
= int(mat
.SR_data
.surface_prop
)
610 if mat
.SR_data
.shader
== 'standard': m
.shader
= 0
611 if mat
.SR_data
.shader
== 'standard_cutout': m
.shader
= 1
612 if mat
.SR_data
.shader
== 'terrain_blend':#{
615 m
.colour
[0] = pow( mat
.SR_data
.sand_colour
[0], 1.0/2.2 )
616 m
.colour
[1] = pow( mat
.SR_data
.sand_colour
[1], 1.0/2.2 )
617 m
.colour
[2] = pow( mat
.SR_data
.sand_colour
[2], 1.0/2.2 )
620 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
621 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
624 if mat
.SR_data
.shader
== 'vertex_blend':#{
627 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
628 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
631 if mat
.SR_data
.shader
== 'water':#{
634 m
.colour
[0] = pow( mat
.SR_data
.shore_colour
[0], 1.0/2.2 )
635 m
.colour
[1] = pow( mat
.SR_data
.shore_colour
[1], 1.0/2.2 )
636 m
.colour
[2] = pow( mat
.SR_data
.shore_colour
[2], 1.0/2.2 )
638 m
.colour1
[0] = pow( mat
.SR_data
.ocean_colour
[0], 1.0/2.2 )
639 m
.colour1
[1] = pow( mat
.SR_data
.ocean_colour
[1], 1.0/2.2 )
640 m
.colour1
[2] = pow( mat
.SR_data
.ocean_colour
[2], 1.0/2.2 )
644 if mat
.SR_data
.shader
== 'invisible':#{
648 if mat
.SR_data
.shader
== 'boundary':#{
652 inf
= material_info( mat
)
654 if mat
.SR_data
.shader
== 'standard' or \
655 mat
.SR_data
.shader
== 'standard_cutout' or \
656 mat
.SR_data
.shader
== 'terrain_blend' or \
657 mat
.SR_data
.shader
== 'vertex_blend':
659 if 'tex_diffuse' in inf
:
660 m
.tex_diffuse
= sr_compile_texture(inf
['tex_diffuse'])
663 sr_compile
.material_data
.extend( bytearray(m
) )
667 def sr_armature_bones( armature
):
669 def _recurse_bone( b
):
672 for c
in b
.children
: yield from _recurse_bone( c
)
675 for b
in armature
.data
.bones
:
677 yield from _recurse_bone( b
)
680 # Returns submesh_start,count and armature_id
681 def sr_compile_mesh_internal( obj
):
690 for mod
in obj
.modifiers
:#{
691 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
692 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
695 can_use_cache
= False
698 if mod
.type == 'ARMATURE': #{
699 armature
= mod
.object
700 rig_weight_groups
= \
701 ['0 [ROOT]']+[_
.name
for _
in sr_armature_bones(mod
.object)]
702 armature_id
= sr_compile
.entity_ids
[armature
.name
]
704 POSE_OR_REST_CACHE
= armature
.data
.pose_position
705 armature
.data
.pose_position
= 'REST'
709 # Check the cache first
711 if can_use_cache
and (obj
.data
.name
in sr_compile
.mesh_cache
):#{
712 ref
= sr_compile
.mesh_cache
[obj
.data
.name
]
713 submesh_start
= ref
[0]
714 submesh_count
= ref
[1]
715 return (submesh_start
,submesh_count
,armature_id
)
718 # Compile a whole new mesh
720 submesh_start
= len(sr_compile
.submesh_data
)//sizeof(mdl_submesh
)
723 dgraph
= bpy
.context
.evaluated_depsgraph_get()
724 data
= obj
.evaluated_get(dgraph
).data
725 data
.calc_loop_triangles()
726 data
.calc_normals_split()
728 # Mesh is split into submeshes based on their material
730 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
731 for material_id
, mat
in enumerate(mat_list
): #{
735 sm
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
736 sm
.vertex_start
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
739 sm
.material_id
= sr_compile_material( mat
)
741 INF
=99999999.99999999
747 # Keep a reference to very very very similar vertices
748 # i have no idea how to speed it up.
750 vertex_reference
= {}
752 # Write the vertex / indice data
754 for tri_index
, tri
in enumerate(data
.loop_triangles
):#{
755 if tri
.material_index
!= material_id
: continue
758 vert
= data
.vertices
[tri
.vertices
[j
]]
760 vi
= data
.loops
[li
].vertex_index
762 # Gather vertex information
765 norm
= data
.loops
[li
].normal
767 colour
= (255,255,255,255)
774 uv
= data
.uv_layers
.active
.data
[li
].uv
778 if data
.vertex_colors
:#{
779 colour
= data
.vertex_colors
.active
.data
[li
].color
780 colour
= (int(colour
[0]*255.0),\
781 int(colour
[1]*255.0),\
782 int(colour
[2]*255.0),\
783 int(colour
[3]*255.0))
786 # Weight groups: truncates to the 3 with the most influence. The
787 # fourth bone ID is never used by the shader so it
791 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
792 if obj
.vertex_groups
[_
.group
].name
in \
795 weight_groups
= sorted( src_groups
, key
= \
796 lambda a
: a
.weight
, reverse
=True )
798 for ml
in range(3):#{
799 if len(weight_groups
) > ml
:#{
800 g
= weight_groups
[ml
]
801 name
= obj
.vertex_groups
[g
.group
].name
804 groups
[ml
] = rig_weight_groups
.index(name
)
809 if len(weight_groups
) > 0:#{
810 inv_norm
= (1.0/tot
) * 65535.0
811 for ml
in range(3):#{
812 weights
[ml
] = int( weights
[ml
] * inv_norm
)
813 weights
[ml
] = min( weights
[ml
], 65535 )
814 weights
[ml
] = max( weights
[ml
], 0 )
819 li1
= tri
.loops
[(j
+1)%3]
820 vi1
= data
.loops
[li1
].vertex_index
821 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
823 if e0
.use_freestyle_mark
and \
824 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
825 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
831 TOLERENCE
= float(10**4)
832 key
= (int(co
[0]*TOLERENCE
+0.5),
833 int(co
[1]*TOLERENCE
+0.5),
834 int(co
[2]*TOLERENCE
+0.5),
835 int(norm
[0]*TOLERENCE
+0.5),
836 int(norm
[1]*TOLERENCE
+0.5),
837 int(norm
[2]*TOLERENCE
+0.5),
838 int(uv
[0]*TOLERENCE
+0.5),
839 int(uv
[1]*TOLERENCE
+0.5),
840 colour
[0], # these guys are already quantized
853 if key
in vertex_reference
:
854 index
= vertex_reference
[key
]
856 index
= bytearray(c_uint32(sm
.vertex_count
))
859 vertex_reference
[key
] = index
869 v
.colour
[0] = colour
[0]
870 v
.colour
[1] = colour
[1]
871 v
.colour
[2] = colour
[2]
872 v
.colour
[3] = colour
[3]
873 v
.weights
[0] = weights
[0]
874 v
.weights
[1] = weights
[1]
875 v
.weights
[2] = weights
[2]
876 v
.weights
[3] = weights
[3]
877 v
.groups
[0] = groups
[0]
878 v
.groups
[1] = groups
[1]
879 v
.groups
[2] = groups
[2]
880 v
.groups
[3] = groups
[3]
883 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
884 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
887 sr_compile
.vertex_data
.extend(bytearray(v
))
891 sr_compile
.indice_data
.extend( index
)
895 # Make sure bounding box isn't -inf -> inf if no vertices
897 if sm
.vertex_count
== 0:
902 # Add submesh to encoder
904 sr_compile
.submesh_data
.extend( bytearray(sm
) )
909 armature
.data
.pose_position
= POSE_OR_REST_CACHE
912 # Save a reference to this mesh since we want to reuse the submesh indices
914 sr_compile
.mesh_cache
[obj
.data
.name
]=(submesh_start
,submesh_count
)
915 return (submesh_start
,submesh_count
,armature_id
)
918 def sr_compile_mesh( obj
):
921 compile_obj_transform(obj
, node
.transform
)
922 node
.pstr_name
= sr_compile_string(obj
.name
)
923 ent_type
= obj_ent_type( obj
)
927 if ent_type
!= 'none':#{
928 ent_id_lwr
= sr_compile
.entity_ids
[obj
.name
]
929 ent_id_upr
= get_entity_enum_id( obj_ent_type(obj
) )
930 node
.entity_id
= (ent_id_upr
<< 16) | ent_id_lwr
933 node
.submesh_start
, node
.submesh_count
, node
.armature_id
= \
934 sr_compile_mesh_internal( obj
)
936 sr_compile
.mesh_data
.extend(bytearray(node
))
939 def sr_compile_fonts( collection
):
941 print( F
"[SR] Compiling fonts" )
946 for obj
in collection
.all_objects
:#{
947 if obj_ent_type(obj
) != 'ent_font': continue
949 data
= obj
.SR_data
.ent_font
[0]
952 font
.alias
= sr_compile_string( data
.alias
)
953 font
.variant_start
= variant_count
954 font
.variant_count
= 0
955 font
.glyph_start
= glyph_count
957 glyph_base
= data
.glyphs
[0].utf32
958 glyph_range
= data
.glyphs
[-1].utf32
- glyph_base
960 font
.glyph_utf32_base
= glyph_base
961 font
.glyph_count
= glyph_range
963 for i
in range(len(data
.variants
)):#{
964 data_var
= data
.variants
[i
]
965 if not data_var
.mesh
: continue
967 mesh
= data_var
.mesh
.data
969 variant
= ent_font_variant()
970 variant
.name
= sr_compile_string( data_var
.tipo
)
972 # fonts (variants) only support one material each
974 if len(mesh
.materials
) != 0:
975 mat
= mesh
.materials
[0]
976 variant
.material_id
= sr_compile_material( mat
)
978 font
.variant_count
+= 1
980 islands
= mesh_utils
.mesh_linked_triangles(mesh
)
981 centroids
= [Vector((0,0)) for _
in range(len(islands
))]
983 for j
in range(len(islands
)):#{
984 for tri
in islands
[j
]:#{
985 centroids
[j
].x
+= tri
.center
[0]
986 centroids
[j
].y
+= tri
.center
[2]
989 centroids
[j
] /= len(islands
[j
])
992 for j
in range(glyph_range
):#{
993 data_glyph
= data
.glyphs
[j
]
995 glyph
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
996 glyph
.indice_count
= 0
997 glyph
.size
[0] = data_glyph
.bounds
[2]
998 glyph
.size
[1] = data_glyph
.bounds
[3]
1000 vertex_reference
= {}
1002 for k
in range(len(islands
)):#{
1003 if centroids
[k
].x
< data_glyph
.bounds
[0] or \
1004 centroids
[k
].x
> data_glyph
.bounds
[0]+data_glyph
.bounds
[2] or\
1005 centroids
[k
].y
< data_glyph
.bounds
[1] or \
1006 centroids
[k
].y
> data_glyph
.bounds
[1]+data_glyph
.bounds
[3]:
1011 for l
in range(len(islands
[k
])):#{
1013 for m
in range(3):#{
1014 vert
= mesh
.vertices
[tri
.vertices
[m
]]
1016 vi
= mesh
.loops
[li
].vertex_index
1018 # Gather vertex information
1020 co
= [vert
.co
[_
] for _
in range(3)]
1021 co
[0] -= data_glyph
.bounds
[0]
1022 co
[2] -= data_glyph
.bounds
[1]
1023 norm
= mesh
.loops
[li
].normal
1025 if mesh
.uv_layers
: uv
= mesh
.uv_layers
.active
.data
[li
].uv
1027 TOLERENCE
= float(10**4)
1028 key
= (int(co
[0]*TOLERENCE
+0.5),
1029 int(co
[1]*TOLERENCE
+0.5),
1030 int(co
[2]*TOLERENCE
+0.5),
1031 int(norm
[0]*TOLERENCE
+0.5),
1032 int(norm
[1]*TOLERENCE
+0.5),
1033 int(norm
[2]*TOLERENCE
+0.5),
1034 int(uv
[0]*TOLERENCE
+0.5),
1035 int(uv
[1]*TOLERENCE
+0.5))
1037 if key
in vertex_reference
:
1038 index
= vertex_reference
[key
]
1040 vindex
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
1041 index
= bytearray(c_uint32(vindex
))
1042 vertex_reference
[key
] = index
1049 v
.norm
[2] = -norm
[1]
1053 sr_compile
.vertex_data
.extend(bytearray(v
))
1056 glyph
.indice_count
+= 1
1057 sr_compile
.indice_data
.extend( index
)
1061 sr_ent_push( glyph
)
1063 sr_ent_push( variant
)
1069 def sr_compile_armature( obj
):
1071 node
= mdl_armature()
1072 node
.bone_start
= len(sr_compile
.bone_data
)//sizeof(mdl_bone
)
1074 node
.anim_start
= len(sr_compile
.anim_data
)//sizeof(mdl_animation
)
1077 bones
= [_
for _
in sr_armature_bones(obj
)]
1078 bones_names
= [None]+[_
.name
for _
in bones
]
1082 if b
.use_deform
: bone
.flags
= 0x1
1083 if b
.parent
: bone
.parent
= bones_names
.index(b
.parent
.name
)
1085 bone
.collider
= int(b
.SR_data
.collider
)
1087 if bone
.collider
>0:#{
1088 bone
.hitbox
[0][0] = b
.SR_data
.collider_min
[0]
1089 bone
.hitbox
[0][1] = b
.SR_data
.collider_min
[2]
1090 bone
.hitbox
[0][2] = -b
.SR_data
.collider_max
[1]
1091 bone
.hitbox
[1][0] = b
.SR_data
.collider_max
[0]
1092 bone
.hitbox
[1][1] = b
.SR_data
.collider_max
[2]
1093 bone
.hitbox
[1][2] = -b
.SR_data
.collider_min
[1]
1096 if b
.SR_data
.cone_constraint
:#{
1098 bone
.conevx
[0] = b
.SR_data
.conevx
[0]
1099 bone
.conevx
[1] = b
.SR_data
.conevx
[2]
1100 bone
.conevx
[2] = -b
.SR_data
.conevx
[1]
1101 bone
.conevy
[0] = b
.SR_data
.conevy
[0]
1102 bone
.conevy
[1] = b
.SR_data
.conevy
[2]
1103 bone
.conevy
[2] = -b
.SR_data
.conevy
[1]
1104 bone
.coneva
[0] = b
.SR_data
.coneva
[0]
1105 bone
.coneva
[1] = b
.SR_data
.coneva
[2]
1106 bone
.coneva
[2] = -b
.SR_data
.coneva
[1]
1107 bone
.conet
= b
.SR_data
.conet
1110 bone
.co
[0] = b
.head_local
[0]
1111 bone
.co
[1] = b
.head_local
[2]
1112 bone
.co
[2] = -b
.head_local
[1]
1113 bone
.end
[0] = b
.tail_local
[0] - bone
.co
[0]
1114 bone
.end
[1] = b
.tail_local
[2] - bone
.co
[1]
1115 bone
.end
[2] = -b
.tail_local
[1] - bone
.co
[2]
1116 bone
.pstr_name
= sr_compile_string( b
.name
)
1118 for c
in obj
.pose
.bones
[b
.name
].constraints
:#{
1119 if c
.type == 'IK':#{
1121 bone
.ik_target
= bones_names
.index(c
.subtarget
)
1122 bone
.ik_pole
= bones_names
.index(c
.pole_subtarget
)
1126 node
.bone_count
+= 1
1127 sr_compile
.bone_data
.extend(bytearray(bone
))
1132 if obj
.animation_data
and sr_compile
.pack_animations
: #{
1133 # So we can restore later
1135 previous_frame
= bpy
.context
.scene
.frame_current
1136 previous_action
= obj
.animation_data
.action
1137 POSE_OR_REST_CACHE
= obj
.data
.pose_position
1138 obj
.data
.pose_position
= 'POSE'
1140 for NLALayer
in obj
.animation_data
.nla_tracks
:#{
1141 for NLAStrip
in NLALayer
.strips
:#{
1144 for a
in bpy
.data
.actions
:#{
1145 if a
.name
== NLAStrip
.name
:#{
1146 obj
.animation_data
.action
= a
1151 # Clip to NLA settings
1153 anim_start
= int(NLAStrip
.action_frame_start
)
1154 anim_end
= int(NLAStrip
.action_frame_end
)
1158 anim
= mdl_animation()
1159 anim
.pstr_name
= sr_compile_string( NLAStrip
.action
.name
)
1161 anim
.keyframe_start
= len(sr_compile
.keyframe_data
)//\
1162 sizeof(mdl_transform
)
1163 anim
.length
= anim_end
-anim_start
1166 # Export the keyframes
1167 for frame
in range(anim_start
,anim_end
):#{
1168 bpy
.context
.scene
.frame_set(frame
)
1171 pb
= obj
.pose
.bones
[rb
.name
]
1173 # relative bone matrix
1174 if rb
.parent
is not None:#{
1175 offset_mtx
= rb
.parent
.matrix_local
1176 offset_mtx
= offset_mtx
.inverted_safe() @ \
1179 inv_parent
= pb
.parent
.matrix
@ offset_mtx
1180 inv_parent
.invert_safe()
1181 fpm
= inv_parent
@ pb
.matrix
1184 bone_mtx
= rb
.matrix
.to_4x4()
1185 local_inv
= rb
.matrix_local
.inverted_safe()
1186 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
1189 loc
, rot
, sca
= fpm
.decompose()
1192 lc_m
= pb
.matrix_channel
.to_3x3()
1193 if pb
.parent
is not None:#{
1194 smtx
= pb
.parent
.matrix_channel
.to_3x3()
1195 lc_m
= smtx
.inverted() @ lc_m
1197 rq
= lc_m
.to_quaternion()
1199 kf
= mdl_transform()
1210 sr_compile
.keyframe_data
.extend(bytearray(kf
))
1216 # Add to animation buffer
1218 sr_compile
.anim_data
.extend(bytearray(anim
))
1219 node
.anim_count
+= 1
1223 print( F
"[SR] | anim( {NLAStrip.action.name} )" )
1227 # Restore context to how it was before
1229 bpy
.context
.scene
.frame_set( previous_frame
)
1230 obj
.animation_data
.action
= previous_action
1231 obj
.data
.pose_position
= POSE_OR_REST_CACHE
1234 sr_compile
.armature_data
.extend(bytearray(node
))
1237 def sr_ent_push( struct
):
1239 clase
= type(struct
).__name
__
1241 if clase
not in sr_compile
.entity_data
:#{
1242 sr_compile
.entity_data
[ clase
] = bytearray()
1243 sr_compile
.entity_info
[ clase
] = { 'size': sizeof(struct
) }
1246 index
= len(sr_compile
.entity_data
[ clase
])//sizeof(struct
)
1247 sr_compile
.entity_data
[ clase
].extend( bytearray(struct
) )
1251 def sr_array_title( arr
, name
, count
, size
, offset
):
1253 for i
in range(len(name
)):#{
1254 arr
.name
[i
] = ord(name
[i
])
1256 arr
.file_offset
= offset
1257 arr
.item_count
= count
1258 arr
.item_size
= size
1261 def sr_compile( collection
):
1263 print( F
"[SR] compiler begin ({collection.name}.mdl)" )
1266 sr_compile
.pack_textures
= collection
.SR_data
.pack_textures
1267 sr_compile
.pack_animations
= collection
.SR_data
.animations
1270 sr_compile
.string_cache
= {}
1271 sr_compile
.mesh_cache
= {}
1272 sr_compile
.material_cache
= {}
1273 sr_compile
.texture_cache
= {}
1276 sr_compile
.mesh_data
= bytearray()
1277 sr_compile
.submesh_data
= bytearray()
1278 sr_compile
.vertex_data
= bytearray()
1279 sr_compile
.indice_data
= bytearray()
1280 sr_compile
.bone_data
= bytearray()
1281 sr_compile
.material_data
= bytearray()
1282 sr_compile
.armature_data
= bytearray()
1283 sr_compile
.anim_data
= bytearray()
1284 sr_compile
.keyframe_data
= bytearray()
1285 sr_compile
.texture_data
= bytearray()
1287 # just bytes not structures
1288 sr_compile
.string_data
= bytearray()
1289 sr_compile
.pack_data
= bytearray()
1292 sr_compile
.entity_data
= {}
1293 sr_compile
.entity_info
= {}
1295 print( F
"[SR] assign entity ID's" )
1296 sr_compile
.entities
= {}
1297 sr_compile
.entity_ids
= {}
1300 for obj
in collection
.all_objects
: #{
1301 if obj
.type == 'MESH':#{
1305 ent_type
= obj_ent_type( obj
)
1306 if ent_type
== 'none': continue
1308 if ent_type
not in sr_compile
.entities
: sr_compile
.entities
[ent_type
] = []
1309 sr_compile
.entity_ids
[obj
.name
] = len( sr_compile
.entities
[ent_type
] )
1310 sr_compile
.entities
[ent_type
] += [obj
]
1313 print( F
"[SR] Compiling geometry" )
1315 for obj
in collection
.all_objects
:#{
1316 if obj
.type == 'MESH':#{
1319 ent_type
= obj_ent_type( obj
)
1321 # entity ignore mesh list
1323 if ent_type
== 'ent_traffic': continue
1324 if ent_type
== 'ent_font': continue
1325 if ent_type
== 'ent_font_variant': continue
1326 #--------------------------
1328 print( F
'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end
='\r' )
1329 sr_compile_mesh( obj
)
1333 audio_clip_count
= 0
1335 for ent_type
, arr
in sr_compile
.entities
.items():#{
1336 print(F
"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1338 for i
in range(len(arr
)):#{
1341 print( F
"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end
='\r' )
1343 if ent_type
== 'mdl_armature': sr_compile_armature(obj
)
1344 elif ent_type
== 'ent_light': #{
1346 compile_obj_transform( obj
, light
.transform
)
1347 light
.daytime
= obj
.data
.SR_data
.daytime
1348 if obj
.data
.type == 'POINT':#{
1351 elif obj
.data
.type == 'SPOT':#{
1353 light
.angle
= obj
.data
.spot_size
*0.5
1355 light
.range = obj
.data
.cutoff_distance
1356 light
.colour
[0] = obj
.data
.color
[0]
1357 light
.colour
[1] = obj
.data
.color
[1]
1358 light
.colour
[2] = obj
.data
.color
[2]
1359 light
.colour
[3] = obj
.data
.energy
1360 sr_ent_push( light
)
1362 elif ent_type
== 'ent_gate': #{
1364 obj_data
= obj
.SR_data
.ent_gate
[0]
1365 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
1367 if obj_data
.tipo
== 'default':#{
1368 if obj_data
.target
:#{
1369 gate
.target
= sr_compile
.entity_ids
[obj_data
.target
.name
]
1373 elif obj_data
.tipo
== 'nonlocal':#{
1374 gate
.target
= sr_compile_string(obj_data
.key
)
1379 gate
.dimensions
[0] = mesh_data
.dimensions
[0]
1380 gate
.dimensions
[1] = mesh_data
.dimensions
[1]
1381 gate
.dimensions
[2] = mesh_data
.dimensions
[2]
1383 q
= [obj
.matrix_local
.to_quaternion(), (0,0,0,1)]
1384 co
= [obj
.matrix_world
@ Vector((0,0,0)), (0,0,0)]
1386 if obj_data
.target
:#{
1387 q
[1] = obj_data
.target
.matrix_local
.to_quaternion()
1388 co
[1]= obj_data
.target
.matrix_world
@ Vector((0,0,0))
1393 for x
in range(2):#{
1394 gate
.co
[x
][0] = co
[x
][0]
1395 gate
.co
[x
][1] = co
[x
][2]
1396 gate
.co
[x
][2] = -co
[x
][1]
1397 gate
.q
[x
][0] = q
[x
][1]
1398 gate
.q
[x
][1] = q
[x
][3]
1399 gate
.q
[x
][2] = -q
[x
][2]
1400 gate
.q
[x
][3] = q
[x
][0]
1405 elif ent_type
== 'ent_spawn': #{
1407 compile_obj_transform( obj
, spawn
.transform
)
1408 obj_data
= obj
.SR_data
.ent_spawn
[0]
1409 spawn
.pstr_name
= sr_compile_string( obj_data
.alias
)
1410 sr_ent_push( spawn
)
1412 elif ent_type
== 'ent_water':#{
1414 compile_obj_transform( obj
, water
.transform
)
1415 water
.max_dist
= 0.0
1416 sr_ent_push( water
)
1418 elif ent_type
== 'ent_audio':#{
1419 obj_data
= obj
.SR_data
.ent_audio
[0]
1421 compile_obj_transform( obj
, audio
.transform
)
1422 audio
.clip_start
= audio_clip_count
1423 audio
.clip_count
= len(obj_data
.files
)
1424 audio_clip_count
+= audio
.clip_count
1425 audio
.max_channels
= obj_data
.max_channels
1426 audio
.volume
= obj_data
.volume
1429 # - allow/disable doppler
1430 # - channel group tags with random colours
1431 # - transition properties
1433 if obj_data
.flag_loop
: audio
.flags |
= 0x1
1434 if obj_data
.flag_nodoppler
: audio
.flags |
= 0x2
1435 if obj_data
.flag_3d
: audio
.flags |
= 0x4
1436 if obj_data
.flag_auto
: audio
.flags |
= 0x8
1437 if obj_data
.formato
== '0': audio
.flags |
= 0x000
1438 elif obj_data
.formato
== '1': audio
.flags |
= 0x400
1439 elif obj_data
.formato
== '2': audio
.flags |
= 0x1000
1441 audio
.channel_behaviour
= int(obj_data
.channel_behaviour
)
1442 if audio
.channel_behaviour
>= 1:#{
1443 audio
.group
= obj_data
.group
1445 if audio
.channel_behaviour
== 2:#{
1446 audio
.crossfade
= obj_data
.transition_duration
1448 audio
.probability_curve
= int(obj_data
.probability_curve
)
1450 for ci
in range(audio
.clip_count
):#{
1451 entry
= obj_data
.files
[ci
]
1452 clip
= ent_audio_clip()
1453 clip
.probability
= entry
.probability
1454 if obj_data
.formato
== '2':#{
1455 sr_pack_file( clip
._anon
.file, '', vg_str_bin(entry
.path
) )
1458 clip
._anon
.file.path
= sr_compile_string( entry
.path
)
1459 clip
._anon
.file.pack_offset
= 0
1460 clip
._anon
.file.pack_size
= 0
1464 sr_ent_push( audio
)
1466 elif ent_type
== 'ent_volume':#{
1467 obj_data
= obj
.SR_data
.ent_volume
[0]
1468 volume
= ent_volume()
1469 volume
.type = int(obj_data
.subtype
)
1470 compile_obj_transform( obj
, volume
.transform
)
1472 if obj_data
.target
:#{
1473 target
= obj_data
.target
1474 volume
.target
.type = get_entity_enum_id( obj_ent_type(target
) )
1475 volume
.target
.index
= sr_compile
.entity_ids
[ target
.name
]
1480 elif ent_type
== 'ent_marker':#{
1481 marker
= ent_marker()
1482 marker
.name
= sr_compile_string( obj
.SR_data
.ent_marker
[0].alias
)
1483 compile_obj_transform( obj
, marker
.transform
)
1489 sr_compile_fonts(collection
)
1491 def _children( col
):#{
1493 for c
in col
.children
:#{
1494 yield from _children(c
)
1498 checkpoint_count
= 0
1499 pathindice_count
= 0
1502 for col
in _children(collection
):#{
1503 print( F
"Adding routes for subcollection: {col.name}" )
1509 for obj
in col
.objects
:#{
1510 if obj
.type == 'ARMATURE': pass
1512 ent_type
= obj_ent_type( obj
)
1514 if ent_type
== 'ent_gate':
1515 route_gates
+= [obj
]
1516 elif ent_type
== 'ent_route_node':#{
1517 if obj
.type == 'CURVE':#{
1518 route_curves
+= [obj
]
1521 elif ent_type
== 'ent_route':
1523 elif ent_type
== 'ent_traffic':
1528 dij
= create_node_graph( route_curves
, route_gates
)
1530 for obj
in routes
:#{
1531 obj_data
= obj
.SR_data
.ent_route
[0]
1533 route
.pstr_name
= sr_compile_string( obj_data
.alias
)
1534 route
.checkpoints_start
= checkpoint_count
1535 route
.checkpoints_count
= 0
1538 route
.colour
[ci
] = obj_data
.colour
[ci
]
1539 route
.colour
[3] = 1.0
1541 compile_obj_transform( obj
, route
.transform
)
1542 checkpoints
= obj_data
.gates
1544 for i
in range(len(checkpoints
)):#{
1545 gi
= checkpoints
[i
].target
1546 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
1550 dest
= gi
.SR_data
.ent_gate
[0].target
1554 if gi
==gj
: continue # error?
1555 if not gi
or not gj
: continue
1557 checkpoint
= ent_checkpoint()
1558 checkpoint
.gate_index
= sr_compile
.entity_ids
[gate
.name
]
1559 checkpoint
.path_start
= pathindice_count
1560 checkpoint
.path_count
= 0
1562 path
= solve_graph( dij
, gi
.name
, gj
.name
)
1565 for pi
in range(len(path
)):#{
1566 pathindice
= ent_path_index()
1567 pathindice
.index
= routenode_count
+ path
[pi
]
1568 sr_ent_push( pathindice
)
1570 checkpoint
.path_count
+= 1
1571 pathindice_count
+= 1
1575 sr_ent_push( checkpoint
)
1576 route
.checkpoints_count
+= 1
1577 checkpoint_count
+= 1
1580 sr_ent_push( route
)
1583 for obj
in traffics
:#{
1584 traffic
= ent_traffic()
1585 compile_obj_transform( obj
, traffic
.transform
)
1586 traffic
.submesh_start
, traffic
.submesh_count
, _
= \
1587 sr_compile_mesh_internal( obj
)
1589 # find best subsection
1591 graph_keys
= list(dij
.graph
)
1595 for j
in range(len(dij
.points
)):#{
1596 point
= dij
.points
[j
]
1597 dist
= (point
-obj
.location
).magnitude
1599 if dist
< min_dist
:#{
1606 best_begin
= best_point
1607 best_end
= best_point
1610 map0
= dij
.subsections
[best_begin
]
1611 if map0
[1] == -1: break
1612 best_begin
= map0
[1]
1615 map1
= dij
.subsections
[best_end
]
1616 if map1
[2] == -1: break
1620 traffic
.start_node
= routenode_count
+ best_begin
1621 traffic
.node_count
= best_end
- best_begin
1622 traffic
.index
= best_point
- best_begin
1623 traffic
.speed
= obj
.SR_data
.ent_traffic
[0].speed
1626 sr_ent_push(traffic
)
1629 for point
in dij
.points
:#{
1630 rn
= ent_route_node()
1633 rn
.co
[2] = -point
[1]
1637 routenode_count
+= len(dij
.points
)
1640 print( F
"[SR] Writing file" )
1642 file_array_instructions
= {}
1645 def _write_array( name
, item_size
, data
):#{
1646 nonlocal file_array_instructions
, file_offset
1648 count
= len(data
)//item_size
1649 file_array_instructions
[name
] = {'count':count
, 'size':item_size
,\
1650 'data':data
, 'offset': file_offset
}
1651 file_offset
+= len(data
)
1652 file_offset
= int_align_to( file_offset
, 8 )
1655 _write_array( 'strings', 1, sr_compile
.string_data
)
1656 _write_array( 'mdl_mesh', sizeof(mdl_mesh
), sr_compile
.mesh_data
)
1657 _write_array( 'mdl_submesh', sizeof(mdl_submesh
), sr_compile
.submesh_data
)
1658 _write_array( 'mdl_material', sizeof(mdl_material
), sr_compile
.material_data
)
1659 _write_array( 'mdl_texture', sizeof(mdl_texture
), sr_compile
.texture_data
)
1660 _write_array( 'mdl_armature', sizeof(mdl_armature
), sr_compile
.armature_data
)
1661 _write_array( 'mdl_bone', sizeof(mdl_bone
), sr_compile
.bone_data
)
1663 for name
, buffer in sr_compile
.entity_data
.items():#{
1664 _write_array( name
, sr_compile
.entity_info
[name
]['size'], buffer )
1667 _write_array( 'mdl_animation', sizeof(mdl_animation
), sr_compile
.anim_data
)
1668 _write_array( 'mdl_keyframe', sizeof(mdl_transform
),sr_compile
.keyframe_data
)
1669 _write_array( 'mdl_vert', sizeof(mdl_vert
), sr_compile
.vertex_data
)
1670 _write_array( 'mdl_indice', sizeof(c_uint32
), sr_compile
.indice_data
)
1671 _write_array( 'pack', 1, sr_compile
.pack_data
)
1673 header_size
= int_align_to( sizeof(mdl_header
), 8 )
1674 index_size
= int_align_to( sizeof(mdl_array
)*len(file_array_instructions
),8 )
1676 folder
= bpy
.path
.abspath(bpy
.context
.scene
.SR_data
.export_dir
)
1677 path
= F
"{folder}{collection.name}.mdl"
1680 fp
= open( path
, "wb" )
1681 header
= mdl_header()
1682 header
.version
= 100
1683 sr_array_title( header
.arrays
, \
1684 'index', len(file_array_instructions
), \
1685 sizeof(mdl_array
), header_size
)
1687 fp
.write( bytearray_align_to( bytearray(header
), 8 ) )
1689 print( F
'[SR] {"name":>16}| count | offset' )
1691 for name
,info
in file_array_instructions
.items():#{
1693 offset
= info
['offset'] + header_size
+ index_size
1694 sr_array_title( arr
, name
, info
['count'], info
['size'], offset
)
1695 index
.extend( bytearray(arr
) )
1697 print( F
'[SR] {name:>16}| {info["count"]: 8} '+\
1698 F
' 0x{info["offset"]:02x}' )
1700 fp
.write( bytearray_align_to( index
, 8 ) )
1701 #bytearray_print_hex( index )
1703 for name
,info
in file_array_instructions
.items():#{
1704 fp
.write( bytearray_align_to( info
['data'], 8 ) )
1709 print( '[SR] done' )
1712 class SR_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1714 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1715 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
1716 gizmos
: bpy
.props
.BoolProperty( name
="Draw Gizmos", default
=True )
1718 panel
: bpy
.props
.EnumProperty(
1722 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
1723 ('ENTITY', 'Entity', '', 'MONKEY',1),
1724 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
1729 class SR_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
1731 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
1732 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
1735 def sr_get_mirror_bone( bones
):
1737 side
= bones
.active
.name
[-1:]
1738 other_name
= bones
.active
.name
[:-1]
1739 if side
== 'L': other_name
+= 'R'
1740 elif side
== 'R': other_name
+= 'L'
1744 if b
.name
== other_name
:
1751 class SR_MIRROR_BONE_X(bpy
.types
.Operator
):
1753 bl_idname
="skaterift.mirror_bone"
1754 bl_label
="Mirror bone attributes - SkateRift"
1756 def execute(_
,context
):
1758 active_object
= context
.active_object
1759 bones
= active_object
.data
.bones
1761 b
= sr_get_mirror_bone( bones
)
1763 if not b
: return {'FINISHED'}
1765 b
.SR_data
.collider
= a
.SR_data
.collider
1767 def _v3copyflipy( a
, b
):#{
1773 _v3copyflipy( a
.SR_data
.collider_min
, b
.SR_data
.collider_min
)
1774 _v3copyflipy( a
.SR_data
.collider_max
, b
.SR_data
.collider_max
)
1775 b
.SR_data
.collider_min
[1] = -a
.SR_data
.collider_max
[1]
1776 b
.SR_data
.collider_max
[1] = -a
.SR_data
.collider_min
[1]
1778 b
.SR_data
.cone_constraint
= a
.SR_data
.cone_constraint
1780 _v3copyflipy( a
.SR_data
.conevx
, b
.SR_data
.conevy
)
1781 _v3copyflipy( a
.SR_data
.conevy
, b
.SR_data
.conevx
)
1782 _v3copyflipy( a
.SR_data
.coneva
, b
.SR_data
.coneva
)
1784 b
.SR_data
.conet
= a
.SR_data
.conet
1787 ob
= bpy
.context
.scene
.objects
[0]
1788 ob
.hide_render
= ob
.hide_render
1793 class SR_COMPILE(bpy
.types
.Operator
):
1795 bl_idname
="skaterift.compile_all"
1796 bl_label
="Compile All"
1798 def execute(_
,context
):
1800 view_layer
= bpy
.context
.view_layer
1801 for col
in view_layer
.layer_collection
.children
["export"].children
:
1802 if not col
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1803 sr_compile( bpy
.data
.collections
[col
.name
] )
1809 class SR_COMPILE_THIS(bpy
.types
.Operator
):
1811 bl_idname
="skaterift.compile_this"
1812 bl_label
="Compile This collection"
1814 def execute(_
,context
):
1816 col
= bpy
.context
.collection
1823 class SR_INTERFACE(bpy
.types
.Panel
):
1825 bl_idname
= "VIEW3D_PT_skate_rift"
1826 bl_label
= "Skate Rift"
1827 bl_space_type
= 'VIEW_3D'
1828 bl_region_type
= 'UI'
1829 bl_category
= "Skate Rift"
1831 def draw(_
, context
):
1835 row
= _
.layout
.row()
1837 row
.prop( context
.scene
.SR_data
, 'panel', expand
=True )
1839 if context
.scene
.SR_data
.panel
== 'SETTINGS': #{
1840 _
.layout
.prop( context
.scene
.SR_data
, 'gizmos' )
1842 elif context
.scene
.SR_data
.panel
== 'EXPORT': #{
1843 _
.layout
.prop( context
.scene
.SR_data
, "export_dir" )
1844 col
= bpy
.context
.collection
1846 found_in_export
= False
1848 view_layer
= bpy
.context
.view_layer
1849 for c1
in view_layer
.layer_collection
.children
["export"].children
: #{
1850 if not c1
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1853 if c1
.name
== col
.name
: #{
1854 found_in_export
= True
1858 box
= _
.layout
.box()
1860 row
.alignment
= 'CENTER'
1863 if found_in_export
: #{
1864 row
.label( text
=col
.name
+ ".mdl" )
1865 box
.prop( col
.SR_data
, "pack_textures" )
1866 box
.prop( col
.SR_data
, "animations" )
1867 box
.operator( "skaterift.compile_this" )
1871 row
.label( text
=col
.name
)
1875 row
.alignment
= 'CENTER'
1877 row
.label( text
="This collection is not in the export group" )
1880 box
= _
.layout
.box()
1883 split
= row
.split( factor
=0.3, align
=True )
1884 split
.prop( context
.scene
.SR_data
, "use_hidden", text
="hidden" )
1887 if export_count
== 0:
1889 row1
.operator( "skaterift.compile_all", \
1890 text
=F
"Compile all ({export_count} collections)" )
1892 elif context
.scene
.SR_data
.panel
== 'ENTITY': #{
1893 active_object
= context
.active_object
1894 if not active_object
: return
1896 _
.layout
.operator( 'skaterift.copy_entity_data', \
1897 text
=F
'Copy entity data to {len(context.selected_objects)-1} '+\
1900 box
= _
.layout
.box()
1902 row
.alignment
= 'CENTER'
1903 row
.label( text
=active_object
.name
)
1906 def _draw_prop_collection( data
): #{
1909 row
.alignment
= 'CENTER'
1912 row
.label( text
=F
'{data[0]}' )
1914 if hasattr(type(data
[0]),'sr_inspector'):#{
1915 type(data
[0]).sr_inspector( box
, data
)
1918 for a
in data
[0].__annotations
__:
1919 box
.prop( data
[0], a
)
1923 if active_object
.type == 'ARMATURE': #{
1924 if active_object
.mode
== 'POSE': #{
1925 bones
= active_object
.data
.bones
1926 mb
= sr_get_mirror_bone( bones
)
1928 box
.operator( "skaterift.mirror_bone", \
1929 text
=F
'Mirror attributes to {mb.name}' )
1932 _draw_prop_collection( [bones
.active
.SR_data
] )
1936 row
.alignment
='CENTER'
1939 row
.label( text
="Enter pose mode to modify bone properties" )
1942 elif active_object
.type == 'LIGHT': #{
1943 _draw_prop_collection( [active_object
.data
.SR_data
] )
1945 elif active_object
.type in ['EMPTY','CURVE','MESH']:#{
1946 box
.prop( active_object
.SR_data
, "ent_type" )
1947 ent_type
= active_object
.SR_data
.ent_type
1949 col
= getattr( active_object
.SR_data
, ent_type
, None )
1950 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1952 if active_object
.type == 'MESH':#{
1953 col
= getattr( active_object
.data
.SR_data
, ent_type
, None )
1954 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1961 class SR_MATERIAL_PANEL(bpy
.types
.Panel
):
1963 bl_label
="Skate Rift material"
1964 bl_idname
="MATERIAL_PT_sr_material"
1965 bl_space_type
='PROPERTIES'
1966 bl_region_type
='WINDOW'
1967 bl_context
="material"
1969 def draw(_
,context
):
1971 active_object
= bpy
.context
.active_object
1972 if active_object
== None: return
1973 active_mat
= active_object
.active_material
1974 if active_mat
== None: return
1976 info
= material_info( active_mat
)
1978 if 'tex_diffuse' in info
:#{
1979 _
.layout
.label( icon
='INFO', \
1980 text
=F
"{info['tex_diffuse'].name} will be compiled" )
1983 _
.layout
.prop( active_mat
.SR_data
, "shader" )
1984 _
.layout
.prop( active_mat
.SR_data
, "surface_prop" )
1985 _
.layout
.prop( active_mat
.SR_data
, "collision" )
1987 if active_mat
.SR_data
.collision
:#{
1988 box
= _
.layout
.box()
1991 if (active_mat
.SR_data
.shader
!= 'invisible') and \
1992 (active_mat
.SR_data
.shader
!= 'boundary'):#{
1993 row
.prop( active_mat
.SR_data
, "skate_surface" )
1994 row
.prop( active_mat
.SR_data
, "grind_surface" )
1995 row
.prop( active_mat
.SR_data
, "grow_grass" )
1999 if active_mat
.SR_data
.shader
== "terrain_blend":#{
2000 box
= _
.layout
.box()
2001 box
.prop( active_mat
.SR_data
, "blend_offset" )
2002 box
.prop( active_mat
.SR_data
, "sand_colour" )
2004 elif active_mat
.SR_data
.shader
== "vertex_blend":#{
2005 box
= _
.layout
.box()
2006 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
2007 box
.prop( active_mat
.SR_data
, "blend_offset" )
2009 elif active_mat
.SR_data
.shader
== "water":#{
2010 box
= _
.layout
.box()
2011 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
2012 box
.prop( active_mat
.SR_data
, "shore_colour" )
2013 box
.prop( active_mat
.SR_data
, "ocean_colour" )
2018 def sr_get_type_enum( scene
, context
):
2020 items
= [('none','None',"")]
2021 mesh_entities
=['ent_gate','ent_water']
2022 point_entities
=['ent_spawn','ent_route_node','ent_route']
2024 for e
in point_entities
: items
+= [(e
,e
,'')]
2026 if context
.scene
.SR_data
.panel
== 'ENTITY': #{
2027 if context
.active_object
.type == 'MESH': #{
2028 for e
in mesh_entities
: items
+= [(e
,e
,'')]
2032 for e
in mesh_entities
: items
+= [(e
,e
,'')]
2038 def sr_on_type_change( _
, context
):
2040 obj
= context
.active_object
2041 ent_type
= obj
.SR_data
.ent_type
2042 if ent_type
== 'none': return
2043 if obj
.type == 'MESH':#{
2044 col
= getattr( obj
.data
.SR_data
, ent_type
, None )
2045 if col
!= None and len(col
)==0: col
.add()
2048 col
= getattr( obj
.SR_data
, ent_type
, None )
2049 if col
!= None and len(col
)==0: col
.add()
2052 class SR_OBJECT_ENT_SPAWN(bpy
.types
.PropertyGroup
):
2054 alias
: bpy
.props
.StringProperty( name
='alias' )
2057 class SR_OBJECT_ENT_GATE(bpy
.types
.PropertyGroup
):
2059 target
: bpy
.props
.PointerProperty( \
2060 type=bpy
.types
.Object
, name
="destination", \
2061 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
2063 key
: bpy
.props
.StringProperty()
2064 tipo
: bpy
.props
.EnumProperty(items
=(('default', 'Default', ""),
2065 ('nonlocal', 'Non-Local', ""),))
2068 def sr_inspector( layout
, data
):
2071 box
.prop( data
[0], 'tipo', text
="subtype" )
2073 if data
[0].tipo
== 'default': box
.prop( data
[0], 'target' )
2074 elif data
[0].tipo
== 'nonlocal': box
.prop( data
[0], 'key' )
2078 class SR_MESH_ENT_GATE(bpy
.types
.PropertyGroup
):
2080 dimensions
: bpy
.props
.FloatVectorProperty(name
="dimensions",size
=3)
2083 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy
.types
.PropertyGroup
):
2085 target
: bpy
.props
.PointerProperty( \
2086 type=bpy
.types
.Object
, name
='target', \
2087 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
2090 class SR_UL_ROUTE_NODE_LIST(bpy
.types
.UIList
):
2092 bl_idname
= 'SR_UL_ROUTE_NODE_LIST'
2094 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2096 layout
.prop( item
, 'target', text
='', emboss
=False )
2100 def internal_listdel_execute(self
,context
,ent_name
,collection_name
):
2102 active_object
= context
.active_object
2103 data
= getattr(active_object
.SR_data
,ent_name
)[0]
2104 lista
= getattr(data
,collection_name
)
2105 index
= getattr(data
,F
'{collection_name}_index')
2109 setattr(data
,F
'{collection_name}_index', min(max(0,index
-1), len(lista
)-1))
2113 def internal_listadd_execute(self
,context
,ent_name
,collection_name
):
2115 active_object
= context
.active_object
2116 getattr(getattr(active_object
.SR_data
,ent_name
)[0],collection_name
).add()
2120 def copy_propgroup( de
, to
):
2122 for a
in de
.__annotations
__:#{
2123 if isinstance(getattr(de
,a
), bpy
.types
.bpy_prop_collection
):#{
2127 while len(cb
) != len(ca
):#{
2128 if len(cb
) < len(ca
): cb
.add()
2131 for i
in range(len(ca
)):#{
2132 copy_propgroup(ca
[i
],cb
[i
])
2136 setattr(to
,a
,getattr(de
,a
))
2141 class SR_OT_COPY_ENTITY_DATA(bpy
.types
.Operator
):
2143 bl_idname
= "skaterift.copy_entity_data"
2144 bl_label
= "Copy entity data"
2146 def execute(self
, context
):#{
2147 data
= context
.active_object
.SR_data
2148 new_type
= data
.ent_type
2149 print( F
"Copy entity data from: {context.active_object.name}" )
2151 for obj
in context
.selected_objects
:#{
2152 if obj
!= context
.active_object
:#{
2153 print( F
" To: {obj.name}" )
2155 obj
.SR_data
.ent_type
= new_type
2157 if active_object
.type == 'MESH':#{
2158 col
= getattr( obj
.data
.SR_data
, new_type
, None )
2159 if col
!= None and len(col
)==0: col
.add()
2160 mdata
= context
.active_object
.data
.SR_data
2161 copy_propgroup( getattr(mdata
,new_type
)[0], col
[0] )
2164 col
= getattr( obj
.SR_data
, new_type
, None )
2165 if col
!= None and len(col
)==0: col
.add()
2166 copy_propgroup( getattr(data
,new_type
)[0], col
[0] )
2173 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy
.types
.Operator
):
2175 bl_idname
= "skaterift.new_entry"
2176 bl_label
= "Add gate"
2178 def execute(self
, context
):#{
2179 return internal_listadd_execute(self
,context
,'ent_route','gates')
2183 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy
.types
.Operator
):
2185 bl_idname
= "skaterift.del_entry"
2186 bl_label
= "Remove gate"
2189 def poll(cls
, context
):#{
2190 active_object
= context
.active_object
2191 if obj_ent_type(active_object
) == 'ent_route':#{
2192 return active_object
.SR_data
.ent_route
[0].gates
2197 def execute(self
, context
):#{
2198 return internal_listdel_execute(self
,context
,'ent_route','gates')
2202 class SR_OT_AUDIO_LIST_NEW_ITEM(bpy
.types
.Operator
):
2204 bl_idname
= "skaterift.al_new_entry"
2205 bl_label
= "Add file"
2207 def execute(self
, context
):#{
2208 return internal_listadd_execute(self
,context
,'ent_audio','files')
2212 class SR_OT_AUDIO_LIST_DEL_ITEM(bpy
.types
.Operator
):
2214 bl_idname
= "skaterift.al_del_entry"
2215 bl_label
= "Remove file"
2218 def poll(cls
, context
):#{
2219 active_object
= context
.active_object
2220 if obj_ent_type(active_object
) == 'ent_audio':#{
2221 return active_object
.SR_data
.ent_audio
[0].files
2226 def execute(self
, context
):#{
2227 return internal_listdel_execute(self
,context
,'ent_audio','files')
2232 class SR_OT_GLYPH_LIST_NEW_ITEM(bpy
.types
.Operator
):
2234 bl_idname
= "skaterift.gl_new_entry"
2235 bl_label
= "Add glyph"
2237 def execute(self
, context
):#{
2238 active_object
= context
.active_object
2240 font
= active_object
.SR_data
.ent_font
[0]
2243 if len(font
.glyphs
) > 1:#{
2244 prev
= font
.glyphs
[-2]
2245 cur
= font
.glyphs
[-1]
2247 cur
.bounds
= prev
.bounds
2248 cur
.utf32
= prev
.utf32
+1
2255 class SR_OT_GLYPH_LIST_DEL_ITEM(bpy
.types
.Operator
):
2257 bl_idname
= "skaterift.gl_del_entry"
2258 bl_label
= "Remove Glyph"
2261 def poll(cls
, context
):#{
2262 active_object
= context
.active_object
2263 if obj_ent_type(active_object
) == 'ent_font':#{
2264 return active_object
.SR_data
.ent_font
[0].glyphs
2269 def execute(self
, context
):#{
2270 return internal_listdel_execute(self
,context
,'ent_font','glyphs')
2274 class SR_OT_GLYPH_LIST_MOVE_ITEM(bpy
.types
.Operator
):
2276 bl_idname
= "skaterift.gl_move_item"
2278 direction
: bpy
.props
.EnumProperty(items
=(('UP', 'Up', ""),
2279 ('DOWN', 'Down', ""),))
2282 def poll(cls
, context
):#{
2283 active_object
= context
.active_object
2284 if obj_ent_type(active_object
) == 'ent_font':#{
2285 return active_object
.SR_data
.ent_font
[0].glyphs
2290 def execute(_
, context
):#{
2291 active_object
= context
.active_object
2292 data
= active_object
.SR_data
.ent_font
[0]
2294 index
= data
.glyphs_index
2295 neighbor
= index
+ (-1 if _
.direction
== 'UP' else 1)
2296 data
.glyphs
.move( neighbor
, index
)
2298 list_length
= len(data
.glyphs
) - 1
2299 new_index
= index
+ (-1 if _
.direction
== 'UP' else 1)
2301 data
.glyphs_index
= max(0, min(new_index
, list_length
))
2307 class SR_OT_FONT_VARIANT_LIST_NEW_ITEM(bpy
.types
.Operator
):
2309 bl_idname
= "skaterift.fv_new_entry"
2310 bl_label
= "Add variant"
2312 def execute(self
, context
):#{
2313 return internal_listadd_execute(self
,context
,'ent_font','variants')
2317 class SR_OT_FONT_VARIANT_LIST_DEL_ITEM(bpy
.types
.Operator
):
2319 bl_idname
= "skaterift.fv_del_entry"
2320 bl_label
= "Remove variant"
2323 def poll(cls
, context
):#{
2324 active_object
= context
.active_object
2325 if obj_ent_type(active_object
) == 'ent_font':#{
2326 return active_object
.SR_data
.ent_font
[0].variants
2331 def execute(self
, context
):#{
2332 return internal_listdel_execute(self
,context
,'ent_font','variants')
2336 class SR_OBJECT_ENT_AUDIO_FILE_ENTRY(bpy
.types
.PropertyGroup
):
2338 path
: bpy
.props
.StringProperty( name
="Path" )
2339 probability
: bpy
.props
.FloatProperty( name
="Probability",default
=100.0 )
2342 class SR_UL_AUDIO_LIST(bpy
.types
.UIList
):
2344 bl_idname
= 'SR_UL_AUDIO_LIST'
2346 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2348 split
= layout
.split(factor
=0.7)
2350 c
.prop( item
, 'path', text
='', emboss
=False )
2352 c
.prop( item
, 'probability', text
='%', emboss
=True )
2356 class SR_UL_FONT_VARIANT_LIST(bpy
.types
.UIList
):
2358 bl_idname
= 'SR_UL_FONT_VARIANT_LIST'
2360 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2362 layout
.prop( item
, 'mesh', emboss
=False )
2363 layout
.prop( item
, 'tipo' )
2367 class SR_UL_FONT_GLYPH_LIST(bpy
.types
.UIList
):
2369 bl_idname
= 'SR_UL_FONT_GLYPH_LIST'
2371 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2373 s0
= layout
.split(factor
=0.3)
2375 s1
= c
.split(factor
=0.3)
2378 lbl
= chr(item
.utf32
) if item
.utf32
>= 32 and item
.utf32
<= 126 else 'ERR'
2381 c
.prop( item
, 'utf32', text
='', emboss
=True )
2384 row
.prop( item
, 'bounds', text
='', emboss
=False )
2388 class SR_OBJECT_ENT_ROUTE(bpy
.types
.PropertyGroup
):
2390 gates
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY
)
2391 gates_index
: bpy
.props
.IntProperty()
2393 colour
: bpy
.props
.FloatVectorProperty( \
2397 default
=Vector((0.79,0.63,0.48)),\
2398 description
="Route colour"\
2401 alias
: bpy
.props
.StringProperty(\
2403 default
="Untitled Course")
2406 def sr_inspector( layout
, data
):
2408 layout
.prop( data
[0], 'alias' )
2409 layout
.prop( data
[0], 'colour' )
2411 layout
.label( text
='Checkpoints' )
2412 layout
.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
2413 data
[0], 'gates', data
[0], 'gates_index', rows
=5)
2416 row
.operator( 'skaterift.new_entry', text
='Add' )
2417 row
.operator( 'skaterift.del_entry', text
='Remove' )
2421 class SR_OBJECT_ENT_VOLUME(bpy
.types
.PropertyGroup
):
2423 subtype
: bpy
.props
.EnumProperty(
2425 items
=[('0','Trigger',''),
2426 ('1','Particles (0.1s)','')]
2429 target
: bpy
.props
.PointerProperty( \
2430 type=bpy
.types
.Object
, name
="Target", \
2431 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_audio']))
2434 def sr_inspector( layout
, data
):
2437 layout
.prop( data
, 'subtype' )
2438 layout
.prop( data
, 'target' )
2442 class SR_OBJECT_ENT_AUDIO(bpy
.types
.PropertyGroup
):
2444 files
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO_FILE_ENTRY
)
2445 files_index
: bpy
.props
.IntProperty()
2447 flag_3d
: bpy
.props
.BoolProperty( name
="3D audio",default
=True )
2448 flag_loop
: bpy
.props
.BoolProperty( name
="Loop",default
=False )
2449 flag_auto
: bpy
.props
.BoolProperty( name
="Play at start",default
=False )
2450 flag_nodoppler
: bpy
.props
.BoolProperty( name
="No Doppler",default
=False )
2452 group
: bpy
.props
.IntProperty( name
="Group ID", default
=0 )
2453 formato
: bpy
.props
.EnumProperty(
2455 items
=[('0','Uncompressed Mono',''),
2456 ('1','Compressed Vorbis',''),
2457 ('2','[vg] Bird Synthesis','')]
2459 probability_curve
: bpy
.props
.EnumProperty(
2460 name
="Probability Curve",
2461 items
=[('0','Constant',''),
2462 ('1','Wildlife Daytime',''),
2463 ('2','Wildlife Nighttime','')])
2464 channel_behaviour
: bpy
.props
.EnumProperty(
2465 name
="Channel Behaviour",
2466 items
=[('0','Unlimited',''),
2467 ('1','Discard if group full', ''),
2468 ('2','Crossfade if group full','')])
2470 transition_duration
: bpy
.props
.FloatProperty(name
="Transition Time",\
2473 max_channels
: bpy
.props
.IntProperty( name
="Max Channels", default
=1 )
2474 volume
: bpy
.props
.FloatProperty( name
="Volume",default
=1.0 )
2477 def sr_inspector( layout
, data
):
2479 layout
.prop( data
[0], 'formato' )
2480 layout
.prop( data
[0], 'volume' )
2483 box
.label( text
='Channels' )
2484 split
= box
.split(factor
=0.3)
2486 c
.prop( data
[0], 'max_channels' )
2488 c
.prop( data
[0], 'channel_behaviour', text
='Behaviour' )
2489 if data
[0].channel_behaviour
>= '1':
2490 box
.prop( data
[0], 'group' )
2491 if data
[0].channel_behaviour
== '2':
2492 box
.prop( data
[0], 'transition_duration' )
2495 box
.label( text
='Flags' )
2496 box
.prop( data
[0], 'flag_3d' )
2497 if data
[0].flag_3d
: box
.prop( data
[0], 'flag_nodoppler' )
2499 box
.prop( data
[0], 'flag_loop' )
2500 box
.prop( data
[0], 'flag_auto' )
2502 split
= layout
.split(factor
=0.7)
2504 c
.label( text
='Filepath' )
2506 c
.label( text
='Chance (0.1s)' )
2508 layout
.prop( data
[0], 'probability_curve' )
2510 layout
.template_list('SR_UL_AUDIO_LIST', 'Files', \
2511 data
[0], 'files', data
[0], 'file_index', rows
=5)
2514 row
.operator( 'skaterift.al_new_entry', text
='Add' )
2515 row
.operator( 'skaterift.al_del_entry', text
='Remove' )
2519 class SR_OBJECT_ENT_MARKER(bpy
.types
.PropertyGroup
):
2521 alias
: bpy
.props
.StringProperty()
2524 class SR_OBJECT_ENT_GLYPH(bpy
.types
.PropertyGroup
):
2526 mini
: bpy
.props
.FloatVectorProperty(size
=2)
2527 maxi
: bpy
.props
.FloatVectorProperty(size
=2)
2528 utf32
: bpy
.props
.IntProperty()
2531 class SR_OBJECT_ENT_GLYPH_ENTRY(bpy
.types
.PropertyGroup
):
2533 bounds
: bpy
.props
.FloatVectorProperty(size
=4,subtype
='NONE')
2534 utf32
: bpy
.props
.IntProperty()
2537 class SR_OBJECT_ENT_FONT_VARIANT(bpy
.types
.PropertyGroup
):
2539 mesh
: bpy
.props
.PointerProperty(type=bpy
.types
.Object
)
2540 tipo
: bpy
.props
.StringProperty()
2543 class SR_OBJECT_ENT_FONT(bpy
.types
.PropertyGroup
):
2545 variants
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT_VARIANT
)
2546 glyphs
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH_ENTRY
)
2547 alias
: bpy
.props
.StringProperty()
2549 glyphs_index
: bpy
.props
.IntProperty()
2550 variants_index
: bpy
.props
.IntProperty()
2553 def sr_inspector( layout
, data
):
2555 layout
.prop( data
[0], 'alias' )
2557 layout
.label( text
='Variants' )
2558 layout
.template_list('SR_UL_FONT_VARIANT_LIST', 'Variants', \
2559 data
[0], 'variants', data
[0], 'variants_index',\
2562 row
.operator( 'skaterift.fv_new_entry', text
='Add' )
2563 row
.operator( 'skaterift.fv_del_entry', text
='Remove' )
2565 layout
.label( text
='ASCII Glyphs' )
2566 layout
.template_list('SR_UL_FONT_GLYPH_LIST', 'Glyphs', \
2567 data
[0], 'glyphs', data
[0], 'glyphs_index', rows
=5)
2570 row
.operator( 'skaterift.gl_new_entry', text
='Add' )
2571 row
.operator( 'skaterift.gl_del_entry', text
='Remove' )
2572 row
.operator( 'skaterift.gl_move_item', text
='^' ).direction
='UP'
2573 row
.operator( 'skaterift.gl_move_item', text
='v' ).direction
='DOWN'
2577 class SR_OBJECT_ENT_TRAFFIC(bpy
.types
.PropertyGroup
):
2579 speed
: bpy
.props
.FloatProperty(default
=1.0)
2582 class SR_OBJECT_PROPERTIES(bpy
.types
.PropertyGroup
):
2584 ent_gate
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GATE
)
2585 ent_spawn
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_SPAWN
)
2586 ent_route
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE
)
2587 ent_volume
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_VOLUME
)
2588 ent_audio
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO
)
2589 ent_marker
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_MARKER
)
2590 ent_glyph
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH
)
2591 ent_font
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT
)
2592 ent_traffic
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_TRAFFIC
)
2593 ent_type
: bpy
.props
.EnumProperty(
2595 items
=sr_entity_list
,
2596 update
=sr_on_type_change
2600 class SR_MESH_PROPERTIES(bpy
.types
.PropertyGroup
):
2602 ent_gate
: bpy
.props
.CollectionProperty(type=SR_MESH_ENT_GATE
)
2605 class SR_LIGHT_PROPERTIES(bpy
.types
.PropertyGroup
):
2607 daytime
: bpy
.props
.BoolProperty( name
='Daytime' )
2610 class SR_BONE_PROPERTIES(bpy
.types
.PropertyGroup
):
2612 collider
: bpy
.props
.EnumProperty( name
='Collider Type',
2613 items
=[('0','none',''),
2615 ('2','capsule','')])
2617 collider_min
: bpy
.props
.FloatVectorProperty( name
='Collider Min', size
=3 )
2618 collider_max
: bpy
.props
.FloatVectorProperty( name
='Collider Max', size
=3 )
2620 cone_constraint
: bpy
.props
.BoolProperty( name
='Cone constraint' )
2622 conevx
: bpy
.props
.FloatVectorProperty( name
='vx' )
2623 conevy
: bpy
.props
.FloatVectorProperty( name
='vy' )
2624 coneva
: bpy
.props
.FloatVectorProperty( name
='va' )
2625 conet
: bpy
.props
.FloatProperty( name
='t' )
2628 def sr_inspector( layout
, data
):
2632 box
.prop( data
, 'collider' )
2634 if int(data
.collider
)>0:#{
2636 row
.prop( data
, 'collider_min' )
2638 row
.prop( data
, 'collider_max' )
2642 box
.prop( data
, 'cone_constraint' )
2643 if data
.cone_constraint
:#{
2645 row
.prop( data
, 'conevx' )
2647 row
.prop( data
, 'conevy' )
2649 row
.prop( data
, 'coneva' )
2650 box
.prop( data
, 'conet' )
2655 class SR_MATERIAL_PROPERTIES(bpy
.types
.PropertyGroup
):
2657 shader
: bpy
.props
.EnumProperty(
2660 ('standard',"standard",''),
2661 ('standard_cutout', "standard_cutout", ''),
2662 ('terrain_blend', "terrain_blend", ''),
2663 ('vertex_blend', "vertex_blend", ''),
2664 ('water',"water",''),
2665 ('invisible','Invisible',''),
2666 ('boundary','Boundary','')
2669 surface_prop
: bpy
.props
.EnumProperty(
2670 name
="Surface Property",
2672 ('0','concrete',''),
2679 collision
: bpy
.props
.BoolProperty( \
2680 name
="Collisions Enabled",\
2682 description
= "Can the player collide with this material?"\
2684 skate_surface
: bpy
.props
.BoolProperty( \
2685 name
="Skate Target", \
2687 description
= "Should the game try to target this surface?" \
2689 grind_surface
: bpy
.props
.BoolProperty( \
2692 description
= "Can you grind on this surface?" \
2694 grow_grass
: bpy
.props
.BoolProperty( \
2695 name
="Grow Grass", \
2697 description
= "Spawn grass sprites on this surface?" \
2699 blend_offset
: bpy
.props
.FloatVectorProperty( \
2700 name
="Blend Offset", \
2702 default
=Vector((0.5,0.0)),\
2703 description
="When surface is more than 45 degrees, add this vector " +\
2706 sand_colour
: bpy
.props
.FloatVectorProperty( \
2707 name
="Sand Colour",\
2710 default
=Vector((0.79,0.63,0.48)),\
2711 description
="Blend to this colour near the 0 coordinate on UP axis"\
2713 shore_colour
: bpy
.props
.FloatVectorProperty( \
2714 name
="Shore Colour",\
2717 default
=Vector((0.03,0.32,0.61)),\
2718 description
="Water colour at the shoreline"\
2720 ocean_colour
: bpy
.props
.FloatVectorProperty( \
2721 name
="Ocean Colour",\
2724 default
=Vector((0.0,0.006,0.03)),\
2725 description
="Water colour in the deep bits"\
2729 # ---------------------------------------------------------------------------- #
2733 # ---------------------------------------------------------------------------- #
2735 cv_view_draw_handler
= None
2736 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
2738 cv_view_colours
= []
2739 cv_view_course_i
= 0
2741 # Draw axis alligned sphere at position with radius
2743 def cv_draw_sphere( pos
, radius
, colour
):
2745 global cv_view_verts
, cv_view_colours
2747 ly
= pos
+ Vector((0,0,radius
))
2748 lx
= pos
+ Vector((0,radius
,0))
2749 lz
= pos
+ Vector((0,0,radius
))
2751 pi
= 3.14159265358979323846264
2753 for i
in range(16):#{
2754 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
2758 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
2759 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
2760 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
2762 cv_view_verts
+= [ px
, lx
]
2763 cv_view_verts
+= [ py
, ly
]
2764 cv_view_verts
+= [ pz
, lz
]
2766 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2775 # Draw axis alligned sphere at position with radius
2777 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
2779 global cv_view_verts
, cv_view_colours
2781 ly
= pos
+ tz
*radius
2782 lx
= pos
+ ty
*radius
2783 lz
= pos
+ tz
*radius
2785 pi
= 3.14159265358979323846264
2787 for i
in range(16):#{
2788 t
= ((i
+1.0) * 1.0/16.0) * pi
2792 s1
= math
.sin(t
*2.0)
2793 c1
= math
.cos(t
*2.0)
2795 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
2796 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
2797 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
2799 cv_view_verts
+= [ px
, lx
]
2800 cv_view_verts
+= [ py
, ly
]
2801 cv_view_verts
+= [ pz
, lz
]
2803 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2812 # Draw transformed -1 -> 1 cube
2814 def cv_draw_ucube( transform
, colour
, s
=Vector((1,1,1)), o
=Vector((0,0,0)) ):
2816 global cv_view_verts
, cv_view_colours
2822 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
2823 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
2824 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
2825 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
2826 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
2827 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
2828 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
2829 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
2831 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2832 (0,4),(1,5),(2,6),(3,7)]
2837 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2838 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2839 cv_view_colours
+= [colour
, colour
]
2844 # Draw line with colour
2846 def cv_draw_line( p0
, p1
, colour
):
2848 global cv_view_verts
, cv_view_colours
2850 cv_view_verts
+= [p0
,p1
]
2851 cv_view_colours
+= [colour
, colour
]
2855 # Draw line with colour(s)
2857 def cv_draw_line2( p0
, p1
, c0
, c1
):
2859 global cv_view_verts
, cv_view_colours
2861 cv_view_verts
+= [p0
,p1
]
2862 cv_view_colours
+= [c0
,c1
]
2868 def cv_tangent_basis( n
, tx
, ty
):
2870 if abs( n
[0] ) >= 0.57735027:#{
2889 # Draw coloured arrow
2891 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
2893 global cv_view_verts
, cv_view_colours
2899 tx
= Vector((1,0,0))
2900 ty
= Vector((1,0,0))
2901 cv_tangent_basis( n
, tx
, ty
)
2903 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
2904 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
2908 def cv_draw_line_dotted( p0
, p1
, c0
, dots
=10 ):
2910 global cv_view_verts
, cv_view_colours
2912 for i
in range(dots
):#{
2916 p2
= p0
*(1.0-t0
)+p1
*t0
2917 p3
= p0
*(1.0-t1
)+p1
*t1
2919 cv_view_verts
+= [p2
,p3
]
2920 cv_view_colours
+= [c0
,c0
]
2925 # Drawhandles of a bezier control point
2927 def cv_draw_bhandle( obj
, direction
, colour
):
2929 global cv_view_verts
, cv_view_colours
2932 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2934 cv_view_verts
+= [p0
]
2935 cv_view_verts
+= [h0
]
2936 cv_view_colours
+= [colour
,colour
]
2940 # Draw a bezier curve (at fixed resolution 10)
2942 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2944 global cv_view_verts
, cv_view_colours
2947 for i
in range(10):#{
2953 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2955 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2956 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2957 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2964 # I think this one extends the handles of the bezier otwards......
2966 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2968 global cv_view_course_i
2970 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2972 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2973 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2974 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2975 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2977 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2981 # Flush the lines buffers. This is called often because god help you if you want
2982 # to do fixed, fast buffers in this catastrophic programming language.
2984 def cv_draw_lines():
2986 global cv_view_shader
, cv_view_verts
, cv_view_colours
2988 if len(cv_view_verts
) < 2:
2991 lines
= batch_for_shader(\
2992 cv_view_shader
, 'LINES', \
2993 { "pos":cv_view_verts
, "color":cv_view_colours
})
2995 lines
.draw( cv_view_shader
)
2998 cv_view_colours
= []
3001 # I dont remember what this does exactly
3003 def cv_draw_bpath( o0
,o1
,c0
,c1
):
3005 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
3008 # Semi circle to show the limit. and some lines
3010 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
3012 global cv_view_verts
, cv_view_colours
3017 for x
in range(16):#{
3020 a0
= amin
*(1.0-t0
)+amax
*t0
3021 a1
= amin
*(1.0-t1
)+amax
*t1
3023 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
3024 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
3026 p0
=obj
.matrix_world
@ p0
3027 p1
=obj
.matrix_world
@ p1
3028 cv_view_verts
+= [p0
,p1
]
3029 cv_view_colours
+= [colour
,colour
]
3032 cv_view_verts
+= [p0
,center
]
3033 cv_view_colours
+= [colour
,colour
]
3036 cv_view_verts
+= [p1
,center
]
3037 cv_view_colours
+= [colour
,colour
]
3041 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
3042 cv_view_colours
+= [colour
,colour
]
3047 # Cone and twist limit
3049 def draw_cone_twist( center
, vx
, vy
, va
):
3051 global cv_view_verts
, cv_view_colours
3052 axis
= vy
.cross( vx
)
3057 cv_view_verts
+= [center
, center
+va
*size
]
3058 cv_view_colours
+= [ (1,1,1), (1,1,1) ]
3060 for x
in range(32):#{
3061 t0
= (x
/32) * math
.tau
3062 t1
= ((x
+1)/32) * math
.tau
3069 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
3070 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
3072 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
3073 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
3075 cv_view_verts
+= [center
, p0
, p0
, p1
]
3076 cv_view_colours
+= [ (0,0,0), col0
, col0
, col1
]
3082 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
3084 def draw_skeleton_helpers( obj
):
3086 global cv_view_verts
, cv_view_colours
3088 if obj
.data
.pose_position
!= 'REST':#{
3092 for bone
in obj
.data
.bones
:#{
3094 a
= Vector((bone
.SR_data
.collider_min
[0],
3095 bone
.SR_data
.collider_min
[1],
3096 bone
.SR_data
.collider_min
[2]))
3097 b
= Vector((bone
.SR_data
.collider_max
[0],
3098 bone
.SR_data
.collider_max
[1],
3099 bone
.SR_data
.collider_max
[2]))
3101 if bone
.SR_data
.collider
== '1':#{
3103 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
3104 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
3105 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
3106 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
3107 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
3108 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
3109 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
3110 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
3112 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
3113 (0,4),(1,5),(2,6),(3,7)]
3119 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
3120 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
3121 cv_view_colours
+= [(0.5,0.5,0.5),(0.5,0.5,0.5)]
3124 elif bone
.SR_data
.collider
== '2':#{
3129 for i
in range(3):#{
3130 if abs(v0
[i
]) > largest
:#{
3131 largest
= abs(v0
[i
])
3136 v1
= Vector((0,0,0))
3137 v1
[major_axis
] = 1.0
3139 tx
= Vector((0,0,0))
3140 ty
= Vector((0,0,0))
3142 cv_tangent_basis( v1
, tx
, ty
)
3143 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
3144 l
= v0
[ major_axis
] - r
*2
3146 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
3147 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
3149 colour
= [0.2,0.2,0.2]
3150 colour
[major_axis
] = 0.5
3152 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
3153 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
3154 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
3155 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
3156 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
3157 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
3163 center
= obj
.matrix_world
@ c
3164 if bone
.SR_data
.cone_constraint
:#{
3165 vx
= Vector([bone
.SR_data
.conevx
[_
] for _
in range(3)])
3166 vy
= Vector([bone
.SR_data
.conevy
[_
] for _
in range(3)])
3167 va
= Vector([bone
.SR_data
.coneva
[_
] for _
in range(3)])
3168 draw_cone_twist( center
, vx
, vy
, va
)
3173 def cv_ent_gate( obj
):
3175 global cv_view_verts
, cv_view_colours
3177 if obj
.type != 'MESH': return
3179 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
3180 data
= obj
.SR_data
.ent_gate
[0]
3181 dims
= mesh_data
.dimensions
3184 c
= Vector((0,0,dims
[2]))
3186 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
3187 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
3188 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
3189 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
3190 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
3191 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
3192 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
3193 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
3194 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
3196 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
3198 r3d
= bpy
.context
.area
.spaces
.active
.region_3d
3200 p0
= r3d
.view_matrix
.inverted().translation
3201 v0
= (obj
.matrix_world
@Vector((0,0,0))) - p0
3202 v1
= obj
.matrix_world
.to_3x3() @ Vector((0,1,0))
3204 if v0
.dot(v1
) > 0.0: cc
= (0,1,0)
3210 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
3211 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
3212 cv_view_colours
+= [cc
,cc
]
3216 if data
.target
!= None:
3217 cv_draw_arrow( obj
.location
, data
.target
.location
, sw
)
3220 def cv_ent_volume( obj
):
3222 global cv_view_verts
, cv_view_colours
3224 data
= obj
.SR_data
.ent_volume
[0]
3226 if data
.subtype
== '0':#{
3227 cv_draw_ucube( obj
.matrix_world
, (0,1,0) )
3230 cv_draw_line( obj
.location
, data
.target
.location
, (0,1,0) )
3233 elif data
.subtype
== '1':#{
3234 cv_draw_ucube( obj
.matrix_world
, (1,1,0) )
3237 cv_draw_line( obj
.location
, data
.target
.location
, (1,1,0) )
3242 def dijkstra( graph
, start_node
, target_node
):
3244 unvisited
= [_
for _
in graph
]
3249 shortest_path
[n
] = 9999999.999999
3250 shortest_path
[start_node
] = 0
3253 current_min_node
= None
3254 for n
in unvisited
:#{
3255 if current_min_node
== None:
3256 current_min_node
= n
3257 elif shortest_path
[n
] < shortest_path
[current_min_node
]:
3258 current_min_node
= n
3261 for branch
in graph
[current_min_node
]:#{
3262 tentative_value
= shortest_path
[current_min_node
]
3263 tentative_value
+= graph
[current_min_node
][branch
]
3264 if tentative_value
< shortest_path
[branch
]:#{
3265 shortest_path
[branch
] = tentative_value
3266 previous_nodes
[branch
] = current_min_node
3270 unvisited
.remove(current_min_node
)
3275 while node
!= start_node
:#{
3278 if node
not in previous_nodes
: return None
3279 node
= previous_nodes
[node
]
3282 # Add the start node manually
3283 path
.append(start_node
)
3289 def __init__(_
,points
,graph
,subsections
):#{
3292 _
.subsections
= subsections
3296 def create_node_graph( curves
, gates
):
3298 # add endpoints of curves
3305 for c
in range(len(curves
)):#{
3306 for s
in range(len(curves
[c
].data
.splines
)):#{
3307 spline
= curves
[c
].data
.splines
[s
]
3308 l
= len(spline
.points
)
3311 dist
= round(spline
.calc_length(),2)
3314 ib
= point_count
+l
-1
3316 graph
[ia
] = { ib
: dist
}
3317 graph
[ib
] = { ia
: dist
}
3319 for i
in range(len(spline
.points
)):#{
3320 wco
= curves
[c
].matrix_world
@ spline
.points
[i
].co
3321 route_points
.append(Vector((wco
[0],wco
[1],wco
[2]+0.5)))
3326 if i
== 0: previous
= -1
3327 if i
== len(spline
.points
)-1: proxima
= -1
3329 subsections
.append((spline_count
,previous
,proxima
))
3338 graph_keys
= list(graph
)
3339 for i
in range(len(graph_keys
)-1):#{
3340 for j
in range(i
+1, len(graph_keys
)):#{
3341 if i
%2==0 and i
+1==j
: continue
3345 pi
= route_points
[ni
]
3346 pj
= route_points
[nj
]
3348 dist
= round((pj
-pi
).magnitude
,2)
3351 graph
[ni
][nj
] = dist
3352 graph
[nj
][ni
] = dist
3357 # add and link gates( by name )
3358 for gate
in gates
:#{
3359 v1
= gate
.matrix_world
.to_3x3() @ Vector((0,1,0))
3360 if gate
.SR_data
.ent_gate
[0].target
:
3363 graph
[ gate
.name
] = {}
3365 for i
in range(len(graph_keys
)):#{
3367 pi
= route_points
[ni
]
3369 v0
= pi
-gate
.location
3370 if v0
.dot(v1
) < 0.0: continue
3372 dist
= round(v0
.magnitude
,2)
3375 graph
[ gate
.name
][ ni
] = dist
3376 graph
[ ni
][ gate
.name
] = dist
3381 return dij_graph(route_points
,graph
,subsections
)
3384 def solve_graph( dij
, start
, end
):
3386 path
= dijkstra( dij
.graph
, end
, start
)
3390 for sj
in range(1,len(path
)-2):#{
3393 map0
= dij
.subsections
[i0
]
3394 map1
= dij
.subsections
[i1
]
3396 if map0
[0] == map1
[0]:#{
3397 if map0
[1] == -1: direction
= 2
3402 map0
= dij
.subsections
[i0
]
3403 i1
= map0
[direction
]
3417 full
.append( path
[-2] )
3422 def cv_draw_route( route
, dij
):
3424 pole
= Vector((0.2,0.2,10))
3425 hat
= Vector((1,8,0.2))
3426 cc
= (route
.SR_data
.ent_route
[0].colour
[0],
3427 route
.SR_data
.ent_route
[0].colour
[1],
3428 route
.SR_data
.ent_route
[0].colour
[2])
3430 cv_draw_ucube(route
.matrix_world
,cc
,Vector((0.5,-7.5,6)),\
3431 Vector((0,-6.5,5.5)))
3432 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5, 0.5,0)) )
3433 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5,-13.5,0)) )
3434 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5, 12)) )
3435 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5,-1)) )
3437 checkpoints
= route
.SR_data
.ent_route
[0].gates
3439 for i
in range(len(checkpoints
)):#{
3440 gi
= checkpoints
[i
].target
3441 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
3444 dest
= gi
.SR_data
.ent_gate
[0].target
3446 cv_draw_line_dotted( gi
.location
, dest
.location
, cc
)
3450 if gi
==gj
: continue # error?
3451 if not gi
or not gj
: continue
3453 path
= solve_graph( dij
, gi
.name
, gj
.name
)
3456 cv_draw_arrow(gi
.location
,dij
.points
[path
[0]],cc
,1.5)
3457 cv_draw_arrow(dij
.points
[path
[len(path
)-1]],gj
.location
,cc
,1.5)
3458 for j
in range(len(path
)-1):#{
3461 o0
= dij
.points
[ i0
]
3462 o1
= dij
.points
[ i1
]
3463 cv_draw_arrow(o0
,o1
,cc
,1.5)
3467 cv_draw_line_dotted( gi
.location
, gj
.location
, cc
)
3474 global cv_view_shader
3475 global cv_view_verts
3476 global cv_view_colours
3477 global cv_view_course_i
3479 cv_view_course_i
= 0
3481 cv_view_colours
= []
3483 cv_view_shader
.bind()
3484 gpu
.state
.depth_mask_set(False)
3485 gpu
.state
.line_width_set(2.0)
3486 gpu
.state
.face_culling_set('BACK')
3487 gpu
.state
.depth_test_set('LESS')
3488 gpu
.state
.blend_set('NONE')
3494 for obj
in bpy
.context
.collection
.objects
:#{
3495 if obj
.type == 'ARMATURE':#{
3496 if obj
.data
.pose_position
== 'REST':
3497 draw_skeleton_helpers( obj
)
3500 ent_type
= obj_ent_type( obj
)
3502 if ent_type
== 'ent_gate':#{
3504 route_gates
+= [obj
]
3506 elif ent_type
== 'ent_route_node':#{
3507 if obj
.type == 'CURVE':#{
3508 route_curves
+= [obj
]
3511 elif ent_type
== 'ent_route':
3513 elif ent_type
== 'ent_volume':#{
3514 cv_ent_volume( obj
)
3516 elif ent_type
== 'ent_audio':#{
3517 if obj
.SR_data
.ent_audio
[0].flag_3d
:
3518 cv_draw_sphere( obj
.location
, obj
.scale
[0], (1,1,0) )
3520 elif ent_type
== 'ent_font':#{
3521 data
= obj
.SR_data
.ent_font
[0]
3523 for i
in range(len(data
.variants
)):#{
3524 sub
= data
.variants
[i
].mesh
3525 if not sub
: continue
3527 for ch
in data
.glyphs
:#{
3528 mini
= (ch
.bounds
[0],ch
.bounds
[1])
3529 maxi
= (ch
.bounds
[2]+mini
[0],ch
.bounds
[3]+mini
[1])
3530 p0
= sub
.matrix_world
@ Vector((mini
[0],0.0,mini
[1]))
3531 p1
= sub
.matrix_world
@ Vector((maxi
[0],0.0,mini
[1]))
3532 p2
= sub
.matrix_world
@ Vector((maxi
[0],0.0,maxi
[1]))
3533 p3
= sub
.matrix_world
@ Vector((mini
[0],0.0,maxi
[1]))
3535 if i
== data
.variants_index
: cc
= (0.5,0.5,0.5)
3538 cv_view_verts
+= [p0
,p1
,p1
,p2
,p2
,p3
,p3
,p0
]
3539 cv_view_colours
+= [cc
,cc
,cc
,cc
,cc
,cc
,cc
,cc
]
3546 dij
= create_node_graph( route_curves
, route_gates
)
3548 #cv_draw_route_map( route_nodes )
3549 for route
in routes
:#{
3550 cv_draw_route( route
, dij
)
3557 classes
= [ SR_INTERFACE
, SR_MATERIAL_PANEL
,\
3558 SR_COLLECTION_SETTINGS
, SR_SCENE_SETTINGS
, \
3559 SR_COMPILE
, SR_COMPILE_THIS
, SR_MIRROR_BONE_X
,\
3561 SR_OBJECT_ENT_GATE
, SR_MESH_ENT_GATE
, SR_OBJECT_ENT_SPAWN
, \
3562 SR_OBJECT_ENT_ROUTE_ENTRY
, SR_UL_ROUTE_NODE_LIST
, \
3563 SR_OBJECT_ENT_ROUTE
, SR_OT_ROUTE_LIST_NEW_ITEM
,\
3564 SR_OT_GLYPH_LIST_NEW_ITEM
, SR_OT_GLYPH_LIST_DEL_ITEM
,\
3565 SR_OT_GLYPH_LIST_MOVE_ITEM
,\
3566 SR_OT_AUDIO_LIST_NEW_ITEM
,SR_OT_AUDIO_LIST_DEL_ITEM
,\
3567 SR_OT_FONT_VARIANT_LIST_NEW_ITEM
,SR_OT_FONT_VARIANT_LIST_DEL_ITEM
,\
3568 SR_OT_COPY_ENTITY_DATA
, \
3569 SR_OBJECT_ENT_VOLUME
, \
3570 SR_UL_AUDIO_LIST
, SR_OBJECT_ENT_AUDIO_FILE_ENTRY
,\
3571 SR_OT_ROUTE_LIST_DEL_ITEM
,\
3572 SR_OBJECT_ENT_AUDIO
,SR_OBJECT_ENT_MARKER
,SR_OBJECT_ENT_GLYPH
,\
3573 SR_OBJECT_ENT_FONT_VARIANT
,
3574 SR_OBJECT_ENT_GLYPH_ENTRY
,\
3575 SR_UL_FONT_VARIANT_LIST
,SR_UL_FONT_GLYPH_LIST
,\
3576 SR_OBJECT_ENT_FONT
,SR_OBJECT_ENT_TRAFFIC
,\
3578 SR_OBJECT_PROPERTIES
, SR_LIGHT_PROPERTIES
, SR_BONE_PROPERTIES
,
3579 SR_MESH_PROPERTIES
, SR_MATERIAL_PROPERTIES \
3585 bpy
.utils
.register_class(c
)
3587 bpy
.types
.Scene
.SR_data
= \
3588 bpy
.props
.PointerProperty(type=SR_SCENE_SETTINGS
)
3589 bpy
.types
.Collection
.SR_data
= \
3590 bpy
.props
.PointerProperty(type=SR_COLLECTION_SETTINGS
)
3592 bpy
.types
.Object
.SR_data
= \
3593 bpy
.props
.PointerProperty(type=SR_OBJECT_PROPERTIES
)
3594 bpy
.types
.Light
.SR_data
= \
3595 bpy
.props
.PointerProperty(type=SR_LIGHT_PROPERTIES
)
3596 bpy
.types
.Bone
.SR_data
= \
3597 bpy
.props
.PointerProperty(type=SR_BONE_PROPERTIES
)
3598 bpy
.types
.Mesh
.SR_data
= \
3599 bpy
.props
.PointerProperty(type=SR_MESH_PROPERTIES
)
3600 bpy
.types
.Material
.SR_data
= \
3601 bpy
.props
.PointerProperty(type=SR_MATERIAL_PROPERTIES
)
3603 global cv_view_draw_handler
3604 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
3605 cv_draw
,(),'WINDOW','POST_VIEW')
3611 bpy
.utils
.unregister_class(c
)
3613 global cv_view_draw_handler
3614 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
3617 # ---------------------------------------------------------------------------- #
3621 # ---------------------------------------------------------------------------- #
3623 # Transliteration of: #
3624 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
3626 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
3627 # SPDX-License-Identifier: MIT #
3628 # QOI - The "Quite OK Image" format for fast, lossless image compression #
3630 # ---------------------------------------------------------------------------- #
3632 class qoi_rgba_t(Structure
):
3635 _fields_
= [("r",c_uint8
),
3641 QOI_OP_INDEX
= 0x00 # 00xxxxxx
3642 QOI_OP_DIFF
= 0x40 # 01xxxxxx
3643 QOI_OP_LUMA
= 0x80 # 10xxxxxx
3644 QOI_OP_RUN
= 0xc0 # 11xxxxxx
3645 QOI_OP_RGB
= 0xfe # 11111110
3646 QOI_OP_RGBA
= 0xff # 11111111
3648 QOI_MASK_2
= 0xc0 # 11000000
3650 def qoi_colour_hash( c
):
3652 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
3657 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
3662 return bytearray([ (0xff000000 & v
) >> 24, \
3663 (0x00ff0000 & v
) >> 16, \
3664 (0x0000ff00 & v
) >> 8, \
3668 def qoi_encode( img
):
3672 print(F
"{' ':<30}",end
='\r')
3673 print(F
"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end
='\r')
3675 index
= [ qoi_rgba_t() for _
in range(64) ]
3679 data
.extend( bytearray(c_uint32(0x66696f71)) )
3680 data
.extend( qoi_32bit( img
.size
[0] ) )
3681 data
.extend( qoi_32bit( img
.size
[1] ) )
3682 data
.extend( bytearray(c_uint8(4)) )
3683 data
.extend( bytearray(c_uint8(0)) )
3686 px_prev
= qoi_rgba_t()
3687 px_prev
.r
= c_uint8(0)
3688 px_prev
.g
= c_uint8(0)
3689 px_prev
.b
= c_uint8(0)
3690 px_prev
.a
= c_uint8(255)
3698 px_len
= img
.size
[0] * img
.size
[1]
3699 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
3701 for px_pos
in range( px_len
): #{
3702 idx
= px_pos
* img
.channels
3705 px
.r
= paxels
[idx
+min(0,nc
)]
3706 px
.g
= paxels
[idx
+min(1,nc
)]
3707 px
.b
= paxels
[idx
+min(2,nc
)]
3708 px
.a
= paxels
[idx
+min(3,nc
)]
3710 if qoi_eq( px
, px_prev
): #{
3713 if (run
== 62) or (px_pos
== px_len
-1): #{
3714 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3720 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3724 index_pos
= qoi_colour_hash(px
) % 64
3726 if qoi_eq( index
[index_pos
], px
): #{
3727 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
3730 index
[ index_pos
].r
= px
.r
3731 index
[ index_pos
].g
= px
.g
3732 index
[ index_pos
].b
= px
.b
3733 index
[ index_pos
].a
= px
.a
3735 if px
.a
== px_prev
.a
: #{
3736 vr
= int(px
.r
) - int(px_prev
.r
)
3737 vg
= int(px
.g
) - int(px_prev
.g
)
3738 vb
= int(px
.b
) - int(px_prev
.b
)
3743 if (vr
> -3) and (vr
< 2) and\
3744 (vg
> -3) and (vg
< 2) and\
3745 (vb
> -3) and (vb
< 2):
3747 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
3748 data
.extend( bytearray( c_uint8(op
) ))
3750 elif (vg_r
> -9) and (vg_r
< 8) and\
3751 (vg
> -33) and (vg
< 32 ) and\
3752 (vg_b
> -9) and (vg_b
< 8):
3754 op
= QOI_OP_LUMA |
(vg
+32)
3755 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
3756 data
.extend( bytearray( c_uint8(op
) ) )
3757 data
.extend( bytearray( c_uint8(delta
) ))
3760 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
3761 data
.extend( bytearray( c_uint8(px
.r
) ))
3762 data
.extend( bytearray( c_uint8(px
.g
) ))
3763 data
.extend( bytearray( c_uint8(px
.b
) ))
3767 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
3768 data
.extend( bytearray( c_uint8(px
.r
) ))
3769 data
.extend( bytearray( c_uint8(px
.g
) ))
3770 data
.extend( bytearray( c_uint8(px
.b
) ))
3771 data
.extend( bytearray( c_uint8(px
.a
) ))
3784 data
.extend( bytearray( c_uint8(0) ))
3785 data
.extend( bytearray( c_uint8(1) ))
3786 bytearray_align_to( data
, 16, b
'\x00' )