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",
32 class mdl_vert(Structure
): # 48 bytes. Quite large. Could compress
33 #{ # the normals and uvs to i16s. Not an
34 _pack_
= 1 # real issue, yet.
35 _fields_
= [("co",c_float
*3),
39 ("weights",c_uint16
*4),
43 class mdl_transform(Structure
):
45 _fields_
= [("co",c_float
*3),
50 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_material(Structure
):
62 _fields_
= [("pstr_name",c_uint32
),
65 ("surface_prop",c_uint32
),
67 ("colour1",c_float
*4),
68 ("tex_diffuse",c_uint32
),
69 ("tex_none0",c_uint32
),
70 ("tex_none1",c_uint32
)]
73 class mdl_bone(Structure
):
75 _fields_
= [("co",c_float
*3),("end",c_float
*3),
77 ("collider",c_uint32
),
78 ("ik_target",c_uint32
),
81 ("pstr_name",c_uint32
),
82 ("hitbox",(c_float
*3)*2),
83 ("conevx",c_float
*3),("conevy",c_float
*3),("coneva",c_float
*3),
87 class mdl_armature(Structure
):
89 _fields_
= [("transform",mdl_transform
),
90 ("bone_start",c_uint32
),
91 ("bone_count",c_uint32
),
92 ("anim_start",c_uint32
),
93 ("anim_count",c_uint32
)]
96 class mdl_animation(Structure
):
98 _fields_
= [("pstr_name",c_uint32
),
101 ("keyframe_start",c_uint32
)]
104 class mdl_mesh(Structure
):
106 _fields_
= [("transform",mdl_transform
),
107 ("submesh_start",c_uint32
),
108 ("submesh_count",c_uint32
),
109 ("pstr_name",c_uint32
),
111 ("armature_id",c_uint32
)]
114 class mdl_file(Structure
):
116 _fields_
= [("path",c_uint32
),
117 ("pack_offset",c_uint32
),
118 ("pack_size",c_uint32
)]
121 class mdl_texture(Structure
):
123 _fields_
= [("file",mdl_file
),
127 class mdl_array(Structure
):
129 _fields_
= [("file_offset",c_uint32
),
130 ("item_count",c_uint32
),
131 ("item_size",c_uint32
),
135 class mdl_header(Structure
):
137 _fields_
= [("version",c_uint32
),
138 ("arrays",mdl_array
)]
141 class ent_spawn(Structure
):
143 _fields_
= [("transform",mdl_transform
),
144 ("pstr_name",c_uint32
)]
147 class ent_light(Structure
):
149 _fields_
= [("transform",mdl_transform
),
150 ("daytime",c_uint32
),
152 ("colour",c_float
*4),
155 ("inverse_world",(c_float
*3)*4), # Runtime
156 ("angle_sin_cos",(c_float
*2))] # Runtime
159 class version_refcount_union(Union
):
161 _fields_
= [("timing_version",c_uint32
),
162 ("ref_count",c_uint8
)]
165 class ent_gate(Structure
):
167 _fields_
= [("type",c_uint32
),
168 ("target", c_uint32
),
169 ("dimensions", c_float
*3),
170 ("co", (c_float
*3)*2),
171 ("q", (c_float
*4)*2),
172 ("to_world",(c_float
*3)*4),
173 ("transport",(c_float
*3)*4),
174 ("_anonymous_union",version_refcount_union
),
175 ("timing_time",c_double
),
176 ("routes",c_uint16
*4),
177 ("route_count",c_uint8
)]
180 class ent_route_node(Structure
):
182 _fields_
= [("co",c_float
*3),
183 ("ref_count",c_uint8
),
184 ("ref_total",c_uint8
)]
187 class ent_path_index(Structure
):
189 _fields_
= [("index",c_uint16
)]
192 class vg_audio_clip(Structure
):
194 _fields_
= [("path",c_uint64
),
200 class union_file_audio_clip(Union
):
202 _fields_
= [("file",mdl_file
),
203 ("reserved",vg_audio_clip
)]
206 class ent_audio_clip(Structure
):
208 _fields_
= [("_anon",union_file_audio_clip
),
209 ("probability",c_float
)]
212 class ent_checkpoint(Structure
):
214 _fields_
= [("gate_index",c_uint16
),
215 ("path_start",c_uint16
),
216 ("path_count",c_uint16
)]
219 class ent_route(Structure
):
221 _fields_
= [("transform",mdl_transform
),
222 ("pstr_name",c_uint32
),
223 ("checkpoints_start",c_uint16
),
224 ("checkpoints_count",c_uint16
),
225 ("colour",c_float
*4),
226 ("active",c_uint32
), #runtime
228 ("board_transform",(c_float
*3)*4),
230 ("latest_pass",c_double
)]
233 class ent_water(Structure
):
235 _fields_
= [("transform",mdl_transform
),
236 ("max_dist",c_float
),
237 ("reserved0",c_uint32
),
238 ("reserved1",c_uint32
)]
241 class volume_trigger(Structure
):
243 _fields_
= [("event",c_uint32
),
247 class volume_particles(Structure
):
249 _fields_
= [("blank",c_uint32
),
253 class volume_union(Union
):
255 _fields_
= [("trigger",volume_trigger
),
256 ("particles",volume_particles
)]
259 class ent_index(Structure
):
261 _fields_
= [("type",c_uint32
),
265 class ent_volume(Structure
):
267 _fields_
= [("transform",mdl_transform
),
268 ("to_world",(c_float
*3)*4),
269 ("to_local",(c_float
*3)*4),
271 ("target",ent_index
),
272 ("_anon",volume_union
)]
275 class ent_audio(Structure
):
277 _fields_
= [("transform",mdl_transform
),
279 ("clip_start",c_uint32
),
280 ("clip_count",c_uint32
),
282 ("crossfade",c_float
),
283 ("channel_behaviour",c_uint32
),
285 ("probability_curve",c_uint32
),
286 ("max_channels",c_uint32
)]
289 class ent_marker(Structure
):
291 _fields_
= [("transform",mdl_transform
),
295 class ent_glyph(Structure
):
297 _fields_
= [("size",c_float
*2),
298 ("indice_start",c_uint32
),
299 ("indice_count",c_uint32
)]
302 class ent_font_variant(Structure
):
304 _fields_
= [("name",c_uint32
),
305 ("material_id",c_uint32
)]
308 class ent_font(Structure
):
310 _fields_
= [("alias",c_uint32
),
311 ("variant_start",c_uint32
),
312 ("variant_count",c_uint32
),
313 ("glyph_start",c_uint32
),
314 ("glyph_count",c_uint32
),
315 ("glyph_utf32_base",c_uint32
)]
318 def obj_ent_type( obj
):
320 if obj
.type == 'ARMATURE': return 'mdl_armature'
321 elif obj
.type == 'LIGHT': return 'ent_light'
322 else: return obj
.SR_data
.ent_type
325 def sr_filter_ent_type( obj
, ent_types
):
327 if obj
== bpy
.context
.active_object
: return False
329 for c0
in obj
.users_collection
:#{
330 for c1
in bpy
.context
.active_object
.users_collection
:#{
332 return obj_ent_type( obj
) in ent_types
340 def compile_obj_transform( obj
, transform
):
342 co
= obj
.matrix_world
@ Vector((0,0,0))
343 q
= obj
.matrix_local
.to_quaternion()
348 transform
.co
[0] = co
[0]
349 transform
.co
[1] = co
[2]
350 transform
.co
[2] = -co
[1]
351 transform
.q
[0] = q
[1]
352 transform
.q
[1] = q
[3]
353 transform
.q
[2] = -q
[2]
354 transform
.q
[3] = q
[0]
355 transform
.s
[0] = s
[0]
356 transform
.s
[1] = s
[2]
357 transform
.s
[2] = s
[1]
360 def int_align_to( v
, align
):
362 while(v
%align
)!=0: v
+= 1
366 def bytearray_align_to( buffer, align
, w
=b
'\xaa' ):
368 while (len(buffer) % align
) != 0: buffer.extend(w
)
372 def bytearray_print_hex( s
, w
=16 ):
374 for r
in range((len(s
)+(w
-1))//w
):#{
376 i1
=min((r
+1)*w
,len(s
))
377 print( F
'{r*w:06x}| \x1B[31m', end
='')
378 print( F
"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end
='' )
379 print( "\x1B[0m", end
='')
380 print( ''.join(chr(x
) if (x
>=33 and x
<=126) else '.' for x
in s
[i0
:i1
] ) )
384 def sr_compile_string( s
):
386 if s
in sr_compile
.string_cache
: return sr_compile
.string_cache
[s
]
388 index
= len( sr_compile
.string_data
)
389 sr_compile
.string_cache
[s
] = index
390 sr_compile
.string_data
.extend( s
.encode('utf-8') )
391 sr_compile
.string_data
.extend( b
'\0' )
393 bytearray_align_to( sr_compile
.string_data
, 4 )
397 def material_tex_image(v
):
407 cxr_graph_mapping
= \
409 # Default shader setup
416 "image": "tex_diffuse"
420 "A": material_tex_image("tex_diffuse"),
421 "B": material_tex_image("tex_decal")
428 "Color": material_tex_image("tex_normal")
434 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
436 def material_info(mat
):
440 # Using the cxr_graph_mapping as a reference, go through the shader
441 # graph and gather all $props from it.
443 def _graph_read( node_def
, node
=None, depth
=0 ):#{
450 _graph_read
.extracted
= []
452 for node_idname
in node_def
:#{
453 for n
in mat
.node_tree
.nodes
:#{
454 if n
.name
== node_idname
:#{
455 node_def
= node_def
[node_idname
]
463 for link
in node_def
:#{
464 link_def
= node_def
[link
]
466 if isinstance( link_def
, dict ):#{
468 for x
in node
.inputs
:#{
469 if isinstance( x
, bpy
.types
.NodeSocketColor
):#{
477 if node_link
and node_link
.is_linked
:#{
478 # look for definitions for the connected node type
480 from_node
= node_link
.links
[0].from_node
482 node_name
= from_node
.name
.split('.')[0]
483 if node_name
in link_def
:#{
484 from_node_def
= link_def
[ node_name
]
486 _graph_read( from_node_def
, from_node
, depth
+1 )
490 if "default" in link_def
:#{
491 prop
= link_def
['default']
492 info
[prop
] = node_link
.default_value
498 info
[prop
] = getattr( node
, link
)
503 _graph_read( cxr_graph_mapping
)
509 decoded
= bytearray()
510 for i
in range(len(s
)//2):#{
511 c
= (ord(s
[i
*2+0])-0x41)
512 c |
= (ord(s
[i
*2+1])-0x41)<<4
513 decoded
.extend(bytearray(c_uint8(c
))) #??
518 def sr_pack_file( file, path
, data
):
520 file.path
= sr_compile_string( path
)
521 file.pack_offset
= len( sr_compile
.pack_data
)
522 file.pack_size
= len( data
)
524 sr_compile
.pack_data
.extend( data
)
525 bytearray_align_to( sr_compile
.pack_data
, 16 )
528 def sr_compile_texture( img
):
533 name
= os
.path
.splitext( img
.name
)[0]
535 if name
in sr_compile
.texture_cache
:
536 return sr_compile
.texture_cache
[name
]
538 texture_index
= (len(sr_compile
.texture_data
)//sizeof(mdl_texture
)) +1
543 if sr_compile
.pack_textures
:#{
544 filedata
= qoi_encode( img
)
545 sr_pack_file( tex
.file, name
, filedata
)
548 sr_compile
.texture_cache
[name
] = texture_index
549 sr_compile
.texture_data
.extend( bytearray(tex
) )
553 def sr_compile_material( mat
):
557 if mat
.name
in sr_compile
.material_cache
:
558 return sr_compile
.material_cache
[mat
.name
]
560 index
= (len(sr_compile
.material_data
)//sizeof(mdl_material
))+1
561 sr_compile
.material_cache
[mat
.name
] = index
564 m
.pstr_name
= sr_compile_string( mat
.name
)
567 if mat
.SR_data
.collision
:#{
569 if mat
.SR_data
.skate_surface
: flags |
= 0x1
570 if mat
.SR_data
.grind_surface
: flags |
= (0x8|
0x1)
573 if mat
.SR_data
.grow_grass
: flags |
= 0x4
576 m
.surface_prop
= int(mat
.SR_data
.surface_prop
)
578 if mat
.SR_data
.shader
== 'standard': m
.shader
= 0
579 if mat
.SR_data
.shader
== 'standard_cutout': m
.shader
= 1
580 if mat
.SR_data
.shader
== 'terrain_blend':#{
583 m
.colour
[0] = pow( mat
.SR_data
.sand_colour
[0], 1.0/2.2 )
584 m
.colour
[1] = pow( mat
.SR_data
.sand_colour
[1], 1.0/2.2 )
585 m
.colour
[2] = pow( mat
.SR_data
.sand_colour
[2], 1.0/2.2 )
588 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
589 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
592 if mat
.SR_data
.shader
== 'vertex_blend':#{
595 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
596 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
599 if mat
.SR_data
.shader
== 'water':#{
602 m
.colour
[0] = pow( mat
.SR_data
.shore_colour
[0], 1.0/2.2 )
603 m
.colour
[1] = pow( mat
.SR_data
.shore_colour
[1], 1.0/2.2 )
604 m
.colour
[2] = pow( mat
.SR_data
.shore_colour
[2], 1.0/2.2 )
606 m
.colour1
[0] = pow( mat
.SR_data
.ocean_colour
[0], 1.0/2.2 )
607 m
.colour1
[1] = pow( mat
.SR_data
.ocean_colour
[1], 1.0/2.2 )
608 m
.colour1
[2] = pow( mat
.SR_data
.ocean_colour
[2], 1.0/2.2 )
612 inf
= material_info( mat
)
614 if mat
.SR_data
.shader
== 'standard' or \
615 mat
.SR_data
.shader
== 'standard_cutout' or \
616 mat
.SR_data
.shader
== 'terrain_blend' or \
617 mat
.SR_data
.shader
== 'vertex_blend':
619 if 'tex_diffuse' in inf
:
620 m
.tex_diffuse
= sr_compile_texture(inf
['tex_diffuse'])
623 sr_compile
.material_data
.extend( bytearray(m
) )
627 def sr_armature_bones( armature
):
629 def _recurse_bone( b
):
632 for c
in b
.children
: yield from _recurse_bone( c
)
635 for b
in armature
.data
.bones
:
637 yield from _recurse_bone( b
)
640 def sr_compile_mesh( obj
):
643 compile_obj_transform(obj
, node
.transform
)
644 node
.pstr_name
= sr_compile_string(obj
.name
)
650 for mod
in obj
.modifiers
:#{
651 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
652 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
655 can_use_cache
= False
658 if mod
.type == 'ARMATURE': #{
660 armature
= mod
.object
661 rig_weight_groups
= \
662 ['0 [ROOT]']+[_
.name
for _
in sr_armature_bones(mod
.object)]
663 node
.armature_id
= sr_compile
.entity_ids
[armature
.name
]
665 POSE_OR_REST_CACHE
= armature
.data
.pose_position
666 armature
.data
.pose_position
= 'REST'
670 # Check the cache first
672 if can_use_cache
and (obj
.data
.name
in sr_compile
.mesh_cache
):#{
673 ref
= sr_compile
.mesh_cache
[obj
.data
.name
]
674 node
.submesh_start
= ref
[0]
675 node
.submesh_count
= ref
[1]
676 sr_compile
.mesh_data
.extend(bytearray(node
))
680 # Compile a whole new mesh
682 node
.submesh_start
= len(sr_compile
.submesh_data
)//sizeof(mdl_submesh
)
683 node
.submesh_count
= 0
685 dgraph
= bpy
.context
.evaluated_depsgraph_get()
686 data
= obj
.evaluated_get(dgraph
).data
687 data
.calc_loop_triangles()
688 data
.calc_normals_split()
690 # Mesh is split into submeshes based on their material
692 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
693 for material_id
, mat
in enumerate(mat_list
): #{
697 sm
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
698 sm
.vertex_start
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
701 sm
.material_id
= sr_compile_material( mat
)
703 INF
=99999999.99999999
709 # Keep a reference to very very very similar vertices
710 # i have no idea how to speed it up.
712 vertex_reference
= {}
714 # Write the vertex / indice data
716 for tri_index
, tri
in enumerate(data
.loop_triangles
):#{
717 if tri
.material_index
!= material_id
: continue
720 vert
= data
.vertices
[tri
.vertices
[j
]]
722 vi
= data
.loops
[li
].vertex_index
724 # Gather vertex information
727 norm
= data
.loops
[li
].normal
729 colour
= (255,255,255,255)
736 uv
= data
.uv_layers
.active
.data
[li
].uv
740 if data
.vertex_colors
:#{
741 colour
= data
.vertex_colors
.active
.data
[li
].color
742 colour
= (int(colour
[0]*255.0),\
743 int(colour
[1]*255.0),\
744 int(colour
[2]*255.0),\
745 int(colour
[3]*255.0))
748 # Weight groups: truncates to the 3 with the most influence. The
749 # fourth bone ID is never used by the shader so it
753 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
754 if obj
.vertex_groups
[_
.group
].name
in \
757 weight_groups
= sorted( src_groups
, key
= \
758 lambda a
: a
.weight
, reverse
=True )
760 for ml
in range(3):#{
761 if len(weight_groups
) > ml
:#{
762 g
= weight_groups
[ml
]
763 name
= obj
.vertex_groups
[g
.group
].name
766 groups
[ml
] = rig_weight_groups
.index(name
)
771 if len(weight_groups
) > 0:#{
772 inv_norm
= (1.0/tot
) * 65535.0
773 for ml
in range(3):#{
774 weights
[ml
] = int( weights
[ml
] * inv_norm
)
775 weights
[ml
] = min( weights
[ml
], 65535 )
776 weights
[ml
] = max( weights
[ml
], 0 )
781 li1
= tri
.loops
[(j
+1)%3]
782 vi1
= data
.loops
[li1
].vertex_index
783 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
785 if e0
.use_freestyle_mark
and \
786 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
787 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
793 TOLERENCE
= float(10**4)
794 key
= (int(co
[0]*TOLERENCE
+0.5),
795 int(co
[1]*TOLERENCE
+0.5),
796 int(co
[2]*TOLERENCE
+0.5),
797 int(norm
[0]*TOLERENCE
+0.5),
798 int(norm
[1]*TOLERENCE
+0.5),
799 int(norm
[2]*TOLERENCE
+0.5),
800 int(uv
[0]*TOLERENCE
+0.5),
801 int(uv
[1]*TOLERENCE
+0.5),
802 colour
[0], # these guys are already quantized
815 if key
in vertex_reference
:
816 index
= vertex_reference
[key
]
818 index
= bytearray(c_uint32(sm
.vertex_count
))
821 vertex_reference
[key
] = index
831 v
.colour
[0] = colour
[0]
832 v
.colour
[1] = colour
[1]
833 v
.colour
[2] = colour
[2]
834 v
.colour
[3] = colour
[3]
835 v
.weights
[0] = weights
[0]
836 v
.weights
[1] = weights
[1]
837 v
.weights
[2] = weights
[2]
838 v
.weights
[3] = weights
[3]
839 v
.groups
[0] = groups
[0]
840 v
.groups
[1] = groups
[1]
841 v
.groups
[2] = groups
[2]
842 v
.groups
[3] = groups
[3]
845 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
846 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
849 sr_compile
.vertex_data
.extend(bytearray(v
))
853 sr_compile
.indice_data
.extend( index
)
857 # Make sure bounding box isn't -inf -> inf if no vertices
859 if sm
.vertex_count
== 0:
864 # Add submesh to encoder
866 sr_compile
.submesh_data
.extend( bytearray(sm
) )
867 node
.submesh_count
+= 1
871 armature
.data
.pose_position
= POSE_OR_REST_CACHE
874 # Save a reference to this node since we want to reuse the submesh indices
876 sr_compile
.mesh_cache
[obj
.data
.name
]=(node
.submesh_start
,node
.submesh_count
)
877 sr_compile
.mesh_data
.extend(bytearray(node
))
880 def sr_compile_fonts( collection
):
882 print( F
"[SR] Compiling fonts" )
887 for obj
in collection
.all_objects
:#{
888 if obj_ent_type(obj
) != 'ent_font': continue
890 data
= obj
.SR_data
.ent_font
[0]
893 font
.alias
= sr_compile_string( data
.alias
)
894 font
.variant_start
= variant_count
895 font
.variant_count
= 0
896 font
.glyph_start
= glyph_count
898 glyph_base
= data
.glyphs
[0].utf32
899 glyph_range
= data
.glyphs
[-1].utf32
- glyph_base
901 font
.glyph_utf32_base
= glyph_base
902 font
.glyph_count
= glyph_range
904 for i
in range(len(data
.variants
)):#{
905 data_var
= data
.variants
[i
]
906 if not data_var
.mesh
: continue
908 mesh
= data_var
.mesh
.data
910 variant
= ent_font_variant()
911 variant
.name
= sr_compile_string( data_var
.tipo
)
913 # fonts (variants) only support one material each
915 if len(mesh
.materials
) != 0:
916 mat
= mesh
.materials
[0]
917 variant
.material_id
= sr_compile_material( mat
)
919 font
.variant_count
+= 1
921 islands
= mesh_utils
.mesh_linked_triangles(mesh
)
922 centroids
= [Vector((0,0)) for _
in range(len(islands
))]
924 for j
in range(len(islands
)):#{
925 for tri
in islands
[j
]:#{
926 centroids
[j
].x
+= tri
.center
[0]
927 centroids
[j
].y
+= tri
.center
[2]
930 centroids
[j
] /= len(islands
[j
])
933 for j
in range(glyph_range
):#{
934 data_glyph
= data
.glyphs
[j
]
936 glyph
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
937 glyph
.indice_count
= 0
938 glyph
.size
[0] = data_glyph
.bounds
[2]
939 glyph
.size
[1] = data_glyph
.bounds
[3]
941 vertex_reference
= {}
943 for k
in range(len(islands
)):#{
944 if centroids
[k
].x
< data_glyph
.bounds
[0] or \
945 centroids
[k
].x
> data_glyph
.bounds
[0]+data_glyph
.bounds
[2] or\
946 centroids
[k
].y
< data_glyph
.bounds
[1] or \
947 centroids
[k
].y
> data_glyph
.bounds
[1]+data_glyph
.bounds
[3]:
952 for l
in range(len(islands
[k
])):#{
955 vert
= mesh
.vertices
[tri
.vertices
[m
]]
957 vi
= mesh
.loops
[li
].vertex_index
959 # Gather vertex information
961 co
= [vert
.co
[_
] for _
in range(3)]
962 co
[0] -= data_glyph
.bounds
[0]
963 co
[2] -= data_glyph
.bounds
[1]
964 norm
= mesh
.loops
[li
].normal
966 if mesh
.uv_layers
: uv
= mesh
.uv_layers
.active
.data
[li
].uv
968 TOLERENCE
= float(10**4)
969 key
= (int(co
[0]*TOLERENCE
+0.5),
970 int(co
[1]*TOLERENCE
+0.5),
971 int(co
[2]*TOLERENCE
+0.5),
972 int(norm
[0]*TOLERENCE
+0.5),
973 int(norm
[1]*TOLERENCE
+0.5),
974 int(norm
[2]*TOLERENCE
+0.5),
975 int(uv
[0]*TOLERENCE
+0.5),
976 int(uv
[1]*TOLERENCE
+0.5))
978 if key
in vertex_reference
:
979 index
= vertex_reference
[key
]
981 vindex
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
982 index
= bytearray(c_uint32(vindex
))
983 vertex_reference
[key
] = index
994 sr_compile
.vertex_data
.extend(bytearray(v
))
997 glyph
.indice_count
+= 1
998 sr_compile
.indice_data
.extend( index
)
1002 sr_ent_push( glyph
)
1004 sr_ent_push( variant
)
1010 def sr_compile_armature( obj
):
1012 node
= mdl_armature()
1013 node
.bone_start
= len(sr_compile
.bone_data
)//sizeof(mdl_bone
)
1015 node
.anim_start
= len(sr_compile
.anim_data
)//sizeof(mdl_animation
)
1018 bones
= [_
for _
in sr_armature_bones(obj
)]
1019 bones_names
= [None]+[_
.name
for _
in bones
]
1023 if b
.use_deform
: bone
.flags
= 0x1
1024 if b
.parent
: bone
.parent
= bones_names
.index(b
.parent
.name
)
1026 bone
.collider
= int(b
.SR_data
.collider
)
1028 if bone
.collider
>0:#{
1029 bone
.hitbox
[0][0] = b
.SR_data
.collider_min
[0]
1030 bone
.hitbox
[0][1] = b
.SR_data
.collider_min
[2]
1031 bone
.hitbox
[0][2] = -b
.SR_data
.collider_max
[1]
1032 bone
.hitbox
[1][0] = b
.SR_data
.collider_max
[0]
1033 bone
.hitbox
[1][1] = b
.SR_data
.collider_max
[2]
1034 bone
.hitbox
[1][2] = -b
.SR_data
.collider_min
[1]
1037 if b
.SR_data
.cone_constraint
:#{
1039 bone
.conevx
[0] = b
.SR_data
.conevx
[0]
1040 bone
.conevx
[1] = b
.SR_data
.conevx
[2]
1041 bone
.conevx
[2] = -b
.SR_data
.conevx
[1]
1042 bone
.conevy
[0] = b
.SR_data
.conevy
[0]
1043 bone
.conevy
[1] = b
.SR_data
.conevy
[2]
1044 bone
.conevy
[2] = -b
.SR_data
.conevy
[1]
1045 bone
.coneva
[0] = b
.SR_data
.coneva
[0]
1046 bone
.coneva
[1] = b
.SR_data
.coneva
[2]
1047 bone
.coneva
[2] = -b
.SR_data
.coneva
[1]
1048 bone
.conet
= b
.SR_data
.conet
1051 bone
.co
[0] = b
.head_local
[0]
1052 bone
.co
[1] = b
.head_local
[2]
1053 bone
.co
[2] = -b
.head_local
[1]
1054 bone
.end
[0] = b
.tail_local
[0] - bone
.co
[0]
1055 bone
.end
[1] = b
.tail_local
[2] - bone
.co
[1]
1056 bone
.end
[2] = -b
.tail_local
[1] - bone
.co
[2]
1057 bone
.pstr_name
= sr_compile_string( b
.name
)
1059 for c
in obj
.pose
.bones
[b
.name
].constraints
:#{
1060 if c
.type == 'IK':#{
1062 bone
.ik_target
= bones_names
.index(c
.subtarget
)
1063 bone
.ik_pole
= bones_names
.index(c
.pole_subtarget
)
1067 node
.bone_count
+= 1
1068 sr_compile
.bone_data
.extend(bytearray(bone
))
1073 if obj
.animation_data
and sr_compile
.pack_animations
: #{
1074 # So we can restore later
1076 previous_frame
= bpy
.context
.scene
.frame_current
1077 previous_action
= obj
.animation_data
.action
1078 POSE_OR_REST_CACHE
= obj
.data
.pose_position
1079 obj
.data
.pose_position
= 'POSE'
1081 for NLALayer
in obj
.animation_data
.nla_tracks
:#{
1082 for NLAStrip
in NLALayer
.strips
:#{
1085 for a
in bpy
.data
.actions
:#{
1086 if a
.name
== NLAStrip
.name
:#{
1087 obj
.animation_data
.action
= a
1092 # Clip to NLA settings
1094 anim_start
= int(NLAStrip
.action_frame_start
)
1095 anim_end
= int(NLAStrip
.action_frame_end
)
1099 anim
= mdl_animation()
1100 anim
.pstr_name
= sr_compile_string( NLAStrip
.action
.name
)
1102 anim
.keyframe_start
= len(sr_compile
.keyframe_data
)//\
1103 sizeof(mdl_transform
)
1104 anim
.length
= anim_end
-anim_start
1107 # Export the keyframes
1108 for frame
in range(anim_start
,anim_end
):#{
1109 bpy
.context
.scene
.frame_set(frame
)
1112 pb
= obj
.pose
.bones
[rb
.name
]
1114 # relative bone matrix
1115 if rb
.parent
is not None:#{
1116 offset_mtx
= rb
.parent
.matrix_local
1117 offset_mtx
= offset_mtx
.inverted_safe() @ \
1120 inv_parent
= pb
.parent
.matrix
@ offset_mtx
1121 inv_parent
.invert_safe()
1122 fpm
= inv_parent
@ pb
.matrix
1125 bone_mtx
= rb
.matrix
.to_4x4()
1126 local_inv
= rb
.matrix_local
.inverted_safe()
1127 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
1130 loc
, rot
, sca
= fpm
.decompose()
1133 lc_m
= pb
.matrix_channel
.to_3x3()
1134 if pb
.parent
is not None:#{
1135 smtx
= pb
.parent
.matrix_channel
.to_3x3()
1136 lc_m
= smtx
.inverted() @ lc_m
1138 rq
= lc_m
.to_quaternion()
1140 kf
= mdl_transform()
1151 sr_compile
.keyframe_data
.extend(bytearray(kf
))
1157 # Add to animation buffer
1159 sr_compile
.anim_data
.extend(bytearray(anim
))
1160 node
.anim_count
+= 1
1164 print( F
"[SR] | anim( {NLAStrip.action.name} )" )
1168 # Restore context to how it was before
1170 bpy
.context
.scene
.frame_set( previous_frame
)
1171 obj
.animation_data
.action
= previous_action
1172 obj
.data
.pose_position
= POSE_OR_REST_CACHE
1175 sr_compile
.armature_data
.extend(bytearray(node
))
1178 def sr_ent_push( struct
):
1180 clase
= type(struct
).__name
__
1182 if clase
not in sr_compile
.entity_data
:#{
1183 sr_compile
.entity_data
[ clase
] = bytearray()
1184 sr_compile
.entity_info
[ clase
] = { 'size': sizeof(struct
) }
1187 index
= len(sr_compile
.entity_data
[ clase
])//sizeof(struct
)
1188 sr_compile
.entity_data
[ clase
].extend( bytearray(struct
) )
1192 def sr_array_title( arr
, name
, count
, size
, offset
):
1194 for i
in range(len(name
)):#{
1195 arr
.name
[i
] = ord(name
[i
])
1197 arr
.file_offset
= offset
1198 arr
.item_count
= count
1199 arr
.item_size
= size
1202 def sr_compile( collection
):
1204 print( F
"[SR] compiler begin ({collection.name}.mdl)" )
1207 sr_compile
.pack_textures
= collection
.SR_data
.pack_textures
1208 sr_compile
.pack_animations
= collection
.SR_data
.animations
1211 sr_compile
.string_cache
= {}
1212 sr_compile
.mesh_cache
= {}
1213 sr_compile
.material_cache
= {}
1214 sr_compile
.texture_cache
= {}
1217 sr_compile
.mesh_data
= bytearray()
1218 sr_compile
.submesh_data
= bytearray()
1219 sr_compile
.vertex_data
= bytearray()
1220 sr_compile
.indice_data
= bytearray()
1221 sr_compile
.bone_data
= bytearray()
1222 sr_compile
.material_data
= bytearray()
1223 sr_compile
.armature_data
= bytearray()
1224 sr_compile
.anim_data
= bytearray()
1225 sr_compile
.keyframe_data
= bytearray()
1226 sr_compile
.texture_data
= bytearray()
1228 # just bytes not structures
1229 sr_compile
.string_data
= bytearray()
1230 sr_compile
.pack_data
= bytearray()
1233 sr_compile
.entity_data
= {}
1234 sr_compile
.entity_info
= {}
1236 print( F
"[SR] assign entity ID's" )
1237 sr_compile
.entities
= {}
1238 sr_compile
.entity_ids
= {}
1241 for obj
in collection
.all_objects
: #{
1242 if obj
.type == 'MESH': mesh_count
+= 1
1244 ent_type
= obj_ent_type( obj
)
1245 if ent_type
== 'none': continue
1247 if ent_type
not in sr_compile
.entities
: sr_compile
.entities
[ent_type
] = []
1248 sr_compile
.entity_ids
[obj
.name
] = len( sr_compile
.entities
[ent_type
] )
1249 sr_compile
.entities
[ent_type
] += [obj
]
1252 print( F
"[SR] Compiling geometry" )
1254 for obj
in collection
.all_objects
:#{
1255 if obj
.type == 'MESH':#{
1257 print( F
'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end
='\r' )
1258 sr_compile_mesh( obj
)
1262 audio_clip_count
= 0
1264 for ent_type
, arr
in sr_compile
.entities
.items():#{
1265 print(F
"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1267 for i
in range(len(arr
)):#{
1270 print( F
"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end
='\r' )
1272 if ent_type
== 'mdl_armature': sr_compile_armature(obj
)
1273 elif ent_type
== 'ent_light': #{
1275 compile_obj_transform( obj
, light
.transform
)
1276 light
.daytime
= obj
.data
.SR_data
.daytime
1277 if obj
.data
.type == 'POINT':#{
1280 elif obj
.data
.type == 'SPOT':#{
1282 light
.angle
= obj
.data
.spot_size
*0.5
1284 light
.range = obj
.data
.cutoff_distance
1285 light
.colour
[0] = obj
.data
.color
[0]
1286 light
.colour
[1] = obj
.data
.color
[1]
1287 light
.colour
[2] = obj
.data
.color
[2]
1288 light
.colour
[3] = obj
.data
.energy
1289 sr_ent_push( light
)
1291 elif ent_type
== 'ent_gate': #{
1293 obj_data
= obj
.SR_data
.ent_gate
[0]
1294 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
1296 if obj_data
.tipo
== 'default':#{
1297 if obj_data
.target
:#{
1298 gate
.target
= sr_compile
.entity_ids
[obj_data
.target
.name
]
1302 elif obj_data
.tipo
== 'nonlocal':#{
1303 gate
.target
= sr_compile_string(obj_data
.key
)
1308 gate
.dimensions
[0] = mesh_data
.dimensions
[0]
1309 gate
.dimensions
[1] = mesh_data
.dimensions
[1]
1310 gate
.dimensions
[2] = mesh_data
.dimensions
[2]
1312 q
= [obj
.matrix_local
.to_quaternion(), (0,0,0,1)]
1313 co
= [obj
.matrix_world
@ Vector((0,0,0)), (0,0,0)]
1315 if obj_data
.target
:#{
1316 q
[1] = obj_data
.target
.matrix_local
.to_quaternion()
1317 co
[1]= obj_data
.target
.matrix_world
@ Vector((0,0,0))
1322 for x
in range(2):#{
1323 gate
.co
[x
][0] = co
[x
][0]
1324 gate
.co
[x
][1] = co
[x
][2]
1325 gate
.co
[x
][2] = -co
[x
][1]
1326 gate
.q
[x
][0] = q
[x
][1]
1327 gate
.q
[x
][1] = q
[x
][3]
1328 gate
.q
[x
][2] = -q
[x
][2]
1329 gate
.q
[x
][3] = q
[x
][0]
1334 elif ent_type
== 'ent_spawn': #{
1336 compile_obj_transform( obj
, spawn
.transform
)
1337 obj_data
= obj
.SR_data
.ent_spawn
[0]
1338 spawn
.pstr_name
= sr_compile_string( obj_data
.alias
)
1339 sr_ent_push( spawn
)
1341 elif ent_type
== 'ent_water':#{
1343 compile_obj_transform( obj
, water
.transform
)
1344 water
.max_dist
= 0.0
1345 sr_ent_push( water
)
1347 elif ent_type
== 'ent_audio':#{
1348 obj_data
= obj
.SR_data
.ent_audio
[0]
1350 compile_obj_transform( obj
, audio
.transform
)
1351 audio
.clip_start
= audio_clip_count
1352 audio
.clip_count
= len(obj_data
.files
)
1353 audio_clip_count
+= audio
.clip_count
1354 audio
.max_channels
= obj_data
.max_channels
1355 audio
.volume
= obj_data
.volume
1358 # - allow/disable doppler
1359 # - channel group tags with random colours
1360 # - transition properties
1362 if obj_data
.flag_loop
: audio
.flags |
= 0x1
1363 if obj_data
.flag_nodoppler
: audio
.flags |
= 0x2
1364 if obj_data
.flag_3d
: audio
.flags |
= 0x4
1365 if obj_data
.flag_auto
: audio
.flags |
= 0x8
1366 if obj_data
.formato
== '0': audio
.flags |
= 0x000
1367 elif obj_data
.formato
== '1': audio
.flags |
= 0x400
1368 elif obj_data
.formato
== '2': audio
.flags |
= 0x1000
1370 audio
.channel_behaviour
= int(obj_data
.channel_behaviour
)
1371 if audio
.channel_behaviour
>= 1:#{
1372 audio
.group
= obj_data
.group
1374 if audio
.channel_behaviour
== 2:#{
1375 audio
.crossfade
= obj_data
.transition_duration
1377 audio
.probability_curve
= int(obj_data
.probability_curve
)
1379 for ci
in range(audio
.clip_count
):#{
1380 entry
= obj_data
.files
[ci
]
1381 clip
= ent_audio_clip()
1382 clip
.probability
= entry
.probability
1383 if obj_data
.formato
== '2':#{
1384 sr_pack_file( clip
._anon
.file, '', vg_str_bin(entry
.path
) )
1387 clip
._anon
.file.path
= sr_compile_string( entry
.path
)
1388 clip
._anon
.file.pack_offset
= 0
1389 clip
._anon
.file.pack_size
= 0
1393 sr_ent_push( audio
)
1395 elif ent_type
== 'ent_volume':#{
1396 obj_data
= obj
.SR_data
.ent_volume
[0]
1397 volume
= ent_volume()
1398 volume
.type = int(obj_data
.subtype
)
1399 compile_obj_transform( obj
, volume
.transform
)
1401 if obj_data
.target
:#{
1402 target
= obj_data
.target
1403 volume
.target
.type = sr_entity_alias
[obj_ent_type(target
)]
1404 volume
.target
.index
= sr_compile
.entity_ids
[ target
.name
]
1409 elif ent_type
== 'ent_marker':#{
1410 marker
= ent_marker()
1411 marker
.name
= sr_compile_string( obj
.SR_data
.ent_marker
[0].alias
)
1412 compile_obj_transform( obj
, marker
.transform
)
1418 sr_compile_fonts(collection
)
1420 def _children( col
):#{
1422 for c
in col
.children
:#{
1423 yield from _children(c
)
1427 checkpoint_count
= 0
1428 pathindice_count
= 0
1431 for col
in _children(collection
):#{
1432 print( F
"Adding routes for subcollection: {col.name}" )
1437 for obj
in col
.objects
:#{
1438 if obj
.type == 'ARMATURE': pass
1440 ent_type
= obj_ent_type( obj
)
1442 if ent_type
== 'ent_gate':
1443 route_gates
+= [obj
]
1444 elif ent_type
== 'ent_route_node':#{
1445 if obj
.type == 'CURVE':#{
1446 route_curves
+= [obj
]
1449 elif ent_type
== 'ent_route':
1454 dij
= create_node_graph( route_curves
, route_gates
)
1456 for obj
in routes
:#{
1457 obj_data
= obj
.SR_data
.ent_route
[0]
1459 route
.pstr_name
= sr_compile_string( obj_data
.alias
)
1460 route
.checkpoints_start
= checkpoint_count
1461 route
.checkpoints_count
= 0
1464 route
.colour
[ci
] = obj_data
.colour
[ci
]
1465 route
.colour
[3] = 1.0
1467 compile_obj_transform( obj
, route
.transform
)
1468 checkpoints
= obj_data
.gates
1470 for i
in range(len(checkpoints
)):#{
1471 gi
= checkpoints
[i
].target
1472 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
1476 dest
= gi
.SR_data
.ent_gate
[0].target
1480 if gi
==gj
: continue # error?
1481 if not gi
or not gj
: continue
1483 checkpoint
= ent_checkpoint()
1484 checkpoint
.gate_index
= sr_compile
.entity_ids
[gate
.name
]
1485 checkpoint
.path_start
= pathindice_count
1486 checkpoint
.path_count
= 0
1488 path
= solve_graph( dij
, gi
.name
, gj
.name
)
1491 for pi
in range(len(path
)):#{
1492 pathindice
= ent_path_index()
1493 pathindice
.index
= routenode_count
+ path
[pi
]
1494 sr_ent_push( pathindice
)
1496 checkpoint
.path_count
+= 1
1497 pathindice_count
+= 1
1501 sr_ent_push( checkpoint
)
1502 route
.checkpoints_count
+= 1
1503 checkpoint_count
+= 1
1506 sr_ent_push( route
)
1509 for point
in dij
.points
:#{
1510 rn
= ent_route_node()
1513 rn
.co
[2] = -point
[1]
1517 routenode_count
+= len(dij
.points
)
1521 print( F
"[SR] Writing file" )
1523 file_array_instructions
= {}
1526 def _write_array( name
, item_size
, data
):#{
1527 nonlocal file_array_instructions
, file_offset
1529 count
= len(data
)//item_size
1530 file_array_instructions
[name
] = {'count':count
, 'size':item_size
,\
1531 'data':data
, 'offset': file_offset
}
1532 file_offset
+= len(data
)
1533 file_offset
= int_align_to( file_offset
, 8 )
1536 _write_array( 'strings', 1, sr_compile
.string_data
)
1537 _write_array( 'mdl_mesh', sizeof(mdl_mesh
), sr_compile
.mesh_data
)
1538 _write_array( 'mdl_submesh', sizeof(mdl_submesh
), sr_compile
.submesh_data
)
1539 _write_array( 'mdl_material', sizeof(mdl_material
), sr_compile
.material_data
)
1540 _write_array( 'mdl_texture', sizeof(mdl_texture
), sr_compile
.texture_data
)
1541 _write_array( 'mdl_armature', sizeof(mdl_armature
), sr_compile
.armature_data
)
1542 _write_array( 'mdl_bone', sizeof(mdl_bone
), sr_compile
.bone_data
)
1544 for name
, buffer in sr_compile
.entity_data
.items():#{
1545 _write_array( name
, sr_compile
.entity_info
[name
]['size'], buffer )
1548 _write_array( 'mdl_animation', sizeof(mdl_animation
), sr_compile
.anim_data
)
1549 _write_array( 'mdl_keyframe', sizeof(mdl_transform
),sr_compile
.keyframe_data
)
1550 _write_array( 'mdl_vert', sizeof(mdl_vert
), sr_compile
.vertex_data
)
1551 _write_array( 'mdl_indice', sizeof(c_uint32
), sr_compile
.indice_data
)
1552 _write_array( 'pack', 1, sr_compile
.pack_data
)
1554 header_size
= int_align_to( sizeof(mdl_header
), 8 )
1555 index_size
= int_align_to( sizeof(mdl_array
)*len(file_array_instructions
),8 )
1557 folder
= bpy
.path
.abspath(bpy
.context
.scene
.SR_data
.export_dir
)
1558 path
= F
"{folder}{collection.name}.mdl"
1561 fp
= open( path
, "wb" )
1562 header
= mdl_header()
1564 sr_array_title( header
.arrays
, \
1565 'index', len(file_array_instructions
), \
1566 sizeof(mdl_array
), header_size
)
1568 fp
.write( bytearray_align_to( bytearray(header
), 8 ) )
1570 print( F
'[SR] {"name":>16}| count | offset' )
1572 for name
,info
in file_array_instructions
.items():#{
1574 offset
= info
['offset'] + header_size
+ index_size
1575 sr_array_title( arr
, name
, info
['count'], info
['size'], offset
)
1576 index
.extend( bytearray(arr
) )
1578 print( F
'[SR] {name:>16}| {info["count"]: 8} '+\
1579 F
' 0x{info["offset"]:02x}' )
1581 fp
.write( bytearray_align_to( index
, 8 ) )
1582 #bytearray_print_hex( index )
1584 for name
,info
in file_array_instructions
.items():#{
1585 fp
.write( bytearray_align_to( info
['data'], 8 ) )
1590 print( '[SR] done' )
1593 class SR_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1595 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1596 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
1597 gizmos
: bpy
.props
.BoolProperty( name
="Draw Gizmos", default
=True )
1599 panel
: bpy
.props
.EnumProperty(
1603 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
1604 ('ENTITY', 'Entity', '', 'MONKEY',1),
1605 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
1610 class SR_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
1612 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
1613 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
1616 def sr_get_mirror_bone( bones
):
1618 side
= bones
.active
.name
[-1:]
1619 other_name
= bones
.active
.name
[:-1]
1620 if side
== 'L': other_name
+= 'R'
1621 elif side
== 'R': other_name
+= 'L'
1625 if b
.name
== other_name
:
1632 class SR_MIRROR_BONE_X(bpy
.types
.Operator
):
1634 bl_idname
="skaterift.mirror_bone"
1635 bl_label
="Mirror bone attributes - SkateRift"
1637 def execute(_
,context
):
1639 active_object
= context
.active_object
1640 bones
= active_object
.data
.bones
1642 b
= sr_get_mirror_bone( bones
)
1644 if not b
: return {'FINISHED'}
1646 b
.SR_data
.collider
= a
.SR_data
.collider
1648 def _v3copyflipy( a
, b
):#{
1654 _v3copyflipy( a
.SR_data
.collider_min
, b
.SR_data
.collider_min
)
1655 _v3copyflipy( a
.SR_data
.collider_max
, b
.SR_data
.collider_max
)
1656 b
.SR_data
.collider_min
[1] = -a
.SR_data
.collider_max
[1]
1657 b
.SR_data
.collider_max
[1] = -a
.SR_data
.collider_min
[1]
1659 b
.SR_data
.cone_constraint
= a
.SR_data
.cone_constraint
1661 _v3copyflipy( a
.SR_data
.conevx
, b
.SR_data
.conevy
)
1662 _v3copyflipy( a
.SR_data
.conevy
, b
.SR_data
.conevx
)
1663 _v3copyflipy( a
.SR_data
.coneva
, b
.SR_data
.coneva
)
1665 b
.SR_data
.conet
= a
.SR_data
.conet
1668 ob
= bpy
.context
.scene
.objects
[0]
1669 ob
.hide_render
= ob
.hide_render
1674 class SR_COMPILE(bpy
.types
.Operator
):
1676 bl_idname
="skaterift.compile_all"
1677 bl_label
="Compile All"
1679 def execute(_
,context
):
1681 view_layer
= bpy
.context
.view_layer
1682 for col
in view_layer
.layer_collection
.children
["export"].children
:
1683 if not col
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1684 sr_compile( bpy
.data
.collections
[col
.name
] )
1690 class SR_COMPILE_THIS(bpy
.types
.Operator
):
1692 bl_idname
="skaterift.compile_this"
1693 bl_label
="Compile This collection"
1695 def execute(_
,context
):
1697 col
= bpy
.context
.collection
1704 class SR_INTERFACE(bpy
.types
.Panel
):
1706 bl_idname
= "VIEW3D_PT_skate_rift"
1707 bl_label
= "Skate Rift"
1708 bl_space_type
= 'VIEW_3D'
1709 bl_region_type
= 'UI'
1710 bl_category
= "Skate Rift"
1712 def draw(_
, context
):
1716 row
= _
.layout
.row()
1718 row
.prop( context
.scene
.SR_data
, 'panel', expand
=True )
1720 if context
.scene
.SR_data
.panel
== 'SETTINGS': #{
1721 _
.layout
.prop( context
.scene
.SR_data
, 'gizmos' )
1723 elif context
.scene
.SR_data
.panel
== 'EXPORT': #{
1724 _
.layout
.prop( context
.scene
.SR_data
, "export_dir" )
1725 col
= bpy
.context
.collection
1727 found_in_export
= False
1729 view_layer
= bpy
.context
.view_layer
1730 for c1
in view_layer
.layer_collection
.children
["export"].children
: #{
1731 if not c1
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1734 if c1
.name
== col
.name
: #{
1735 found_in_export
= True
1739 box
= _
.layout
.box()
1741 row
.alignment
= 'CENTER'
1744 if found_in_export
: #{
1745 row
.label( text
=col
.name
+ ".mdl" )
1746 box
.prop( col
.SR_data
, "pack_textures" )
1747 box
.prop( col
.SR_data
, "animations" )
1748 box
.operator( "skaterift.compile_this" )
1752 row
.label( text
=col
.name
)
1756 row
.alignment
= 'CENTER'
1758 row
.label( text
="This collection is not in the export group" )
1761 box
= _
.layout
.box()
1764 split
= row
.split( factor
=0.3, align
=True )
1765 split
.prop( context
.scene
.SR_data
, "use_hidden", text
="hidden" )
1768 if export_count
== 0:
1770 row1
.operator( "skaterift.compile_all", \
1771 text
=F
"Compile all ({export_count} collections)" )
1773 elif context
.scene
.SR_data
.panel
== 'ENTITY': #{
1774 active_object
= context
.active_object
1775 if not active_object
: return
1777 _
.layout
.operator( 'skaterift.copy_entity_data', \
1778 text
=F
'Copy entity data to {len(context.selected_objects)-1} '+\
1781 box
= _
.layout
.box()
1783 row
.alignment
= 'CENTER'
1784 row
.label( text
=active_object
.name
)
1787 def _draw_prop_collection( data
): #{
1790 row
.alignment
= 'CENTER'
1793 row
.label( text
=F
'{data[0]}' )
1795 if hasattr(type(data
[0]),'sr_inspector'):#{
1796 type(data
[0]).sr_inspector( box
, data
)
1799 for a
in data
[0].__annotations
__:
1800 box
.prop( data
[0], a
)
1804 if active_object
.type == 'ARMATURE': #{
1805 if active_object
.mode
== 'POSE': #{
1806 bones
= active_object
.data
.bones
1807 mb
= sr_get_mirror_bone( bones
)
1809 box
.operator( "skaterift.mirror_bone", \
1810 text
=F
'Mirror attributes to {mb.name}' )
1813 _draw_prop_collection( [bones
.active
.SR_data
] )
1817 row
.alignment
='CENTER'
1820 row
.label( text
="Enter pose mode to modify bone properties" )
1823 elif active_object
.type == 'LIGHT': #{
1824 _draw_prop_collection( [active_object
.data
.SR_data
] )
1826 elif active_object
.type in ['EMPTY','CURVE','MESH']:#{
1827 box
.prop( active_object
.SR_data
, "ent_type" )
1828 ent_type
= active_object
.SR_data
.ent_type
1830 col
= getattr( active_object
.SR_data
, ent_type
, None )
1831 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1833 if active_object
.type == 'MESH':#{
1834 col
= getattr( active_object
.data
.SR_data
, ent_type
, None )
1835 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1842 class SR_MATERIAL_PANEL(bpy
.types
.Panel
):
1844 bl_label
="Skate Rift material"
1845 bl_idname
="MATERIAL_PT_sr_material"
1846 bl_space_type
='PROPERTIES'
1847 bl_region_type
='WINDOW'
1848 bl_context
="material"
1850 def draw(_
,context
):
1852 active_object
= bpy
.context
.active_object
1853 if active_object
== None: return
1854 active_mat
= active_object
.active_material
1855 if active_mat
== None: return
1857 info
= material_info( active_mat
)
1859 if 'tex_diffuse' in info
:#{
1860 _
.layout
.label( icon
='INFO', \
1861 text
=F
"{info['tex_diffuse'].name} will be compiled" )
1864 _
.layout
.prop( active_mat
.SR_data
, "shader" )
1865 _
.layout
.prop( active_mat
.SR_data
, "surface_prop" )
1866 _
.layout
.prop( active_mat
.SR_data
, "collision" )
1868 if active_mat
.SR_data
.collision
:#{
1869 _
.layout
.prop( active_mat
.SR_data
, "skate_surface" )
1870 _
.layout
.prop( active_mat
.SR_data
, "grind_surface" )
1871 _
.layout
.prop( active_mat
.SR_data
, "grow_grass" )
1874 if active_mat
.SR_data
.shader
== "terrain_blend":#{
1875 box
= _
.layout
.box()
1876 box
.prop( active_mat
.SR_data
, "blend_offset" )
1877 box
.prop( active_mat
.SR_data
, "sand_colour" )
1879 elif active_mat
.SR_data
.shader
== "vertex_blend":#{
1880 box
= _
.layout
.box()
1881 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
1882 box
.prop( active_mat
.SR_data
, "blend_offset" )
1884 elif active_mat
.SR_data
.shader
== "water":#{
1885 box
= _
.layout
.box()
1886 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
1887 box
.prop( active_mat
.SR_data
, "shore_colour" )
1888 box
.prop( active_mat
.SR_data
, "ocean_colour" )
1893 def sr_get_type_enum( scene
, context
):
1895 items
= [('none','None',"")]
1896 mesh_entities
=['ent_gate','ent_water']
1897 point_entities
=['ent_spawn','ent_route_node','ent_route']
1899 for e
in point_entities
: items
+= [(e
,e
,'')]
1901 if context
.scene
.SR_data
.panel
== 'ENTITY': #{
1902 if context
.active_object
.type == 'MESH': #{
1903 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1907 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1913 def sr_on_type_change( _
, context
):
1915 obj
= context
.active_object
1916 ent_type
= obj
.SR_data
.ent_type
1917 if ent_type
== 'none': return
1918 if obj
.type == 'MESH':#{
1919 col
= getattr( obj
.data
.SR_data
, ent_type
, None )
1920 if col
!= None and len(col
)==0: col
.add()
1923 col
= getattr( obj
.SR_data
, ent_type
, None )
1924 if col
!= None and len(col
)==0: col
.add()
1927 class SR_OBJECT_ENT_SPAWN(bpy
.types
.PropertyGroup
):
1929 alias
: bpy
.props
.StringProperty( name
='alias' )
1932 class SR_OBJECT_ENT_GATE(bpy
.types
.PropertyGroup
):
1934 target
: bpy
.props
.PointerProperty( \
1935 type=bpy
.types
.Object
, name
="destination", \
1936 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
1938 key
: bpy
.props
.StringProperty()
1939 tipo
: bpy
.props
.EnumProperty(items
=(('default', 'Default', ""),
1940 ('nonlocal', 'Non-Local', ""),))
1943 def sr_inspector( layout
, data
):
1946 box
.prop( data
[0], 'tipo', text
="subtype" )
1948 if data
[0].tipo
== 'default': box
.prop( data
[0], 'target' )
1949 elif data
[0].tipo
== 'nonlocal': box
.prop( data
[0], 'key' )
1953 class SR_MESH_ENT_GATE(bpy
.types
.PropertyGroup
):
1955 dimensions
: bpy
.props
.FloatVectorProperty(name
="dimensions",size
=3)
1958 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy
.types
.PropertyGroup
):
1960 target
: bpy
.props
.PointerProperty( \
1961 type=bpy
.types
.Object
, name
='target', \
1962 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
1965 class SR_UL_ROUTE_NODE_LIST(bpy
.types
.UIList
):
1967 bl_idname
= 'SR_UL_ROUTE_NODE_LIST'
1969 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
1971 layout
.prop( item
, 'target', text
='', emboss
=False )
1975 def internal_listdel_execute(self
,context
,ent_name
,collection_name
):
1977 active_object
= context
.active_object
1978 data
= getattr(active_object
.SR_data
,ent_name
)[0]
1979 lista
= getattr(data
,collection_name
)
1980 index
= getattr(data
,F
'{collection_name}_index')
1984 setattr(data
,F
'{collection_name}_index', min(max(0,index
-1), len(lista
)-1))
1988 def internal_listadd_execute(self
,context
,ent_name
,collection_name
):
1990 active_object
= context
.active_object
1991 getattr(getattr(active_object
.SR_data
,ent_name
)[0],collection_name
).add()
1995 def copy_propgroup( de
, to
):
1997 for a
in de
.__annotations
__:#{
1998 if isinstance(getattr(de
,a
), bpy
.types
.bpy_prop_collection
):#{
2002 while len(cb
) != len(ca
):#{
2003 if len(cb
) < len(ca
): cb
.add()
2006 for i
in range(len(ca
)):#{
2007 copy_propgroup(ca
[i
],cb
[i
])
2011 setattr(to
,a
,getattr(de
,a
))
2016 class SR_OT_COPY_ENTITY_DATA(bpy
.types
.Operator
):
2018 bl_idname
= "skaterift.copy_entity_data"
2019 bl_label
= "Copy entity data"
2021 def execute(self
, context
):#{
2022 data
= context
.active_object
.SR_data
2023 new_type
= data
.ent_type
2024 print( F
"Copy entity data from: {context.active_object.name}" )
2026 for obj
in context
.selected_objects
:#{
2027 if obj
!= context
.active_object
:#{
2028 print( F
" To: {obj.name}" )
2030 obj
.SR_data
.ent_type
= new_type
2032 if active_object
.type == 'MESH':#{
2033 col
= getattr( obj
.data
.SR_data
, new_type
, None )
2034 if col
!= None and len(col
)==0: col
.add()
2035 mdata
= context
.active_object
.data
.SR_data
2036 copy_propgroup( getattr(mdata
,new_type
)[0], col
[0] )
2039 col
= getattr( obj
.SR_data
, new_type
, None )
2040 if col
!= None and len(col
)==0: col
.add()
2041 copy_propgroup( getattr(data
,new_type
)[0], col
[0] )
2048 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy
.types
.Operator
):
2050 bl_idname
= "skaterift.new_entry"
2051 bl_label
= "Add gate"
2053 def execute(self
, context
):#{
2054 return internal_listadd_execute(self
,context
,'ent_route','gates')
2058 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy
.types
.Operator
):
2060 bl_idname
= "skaterift.del_entry"
2061 bl_label
= "Remove gate"
2064 def poll(cls
, context
):#{
2065 active_object
= context
.active_object
2066 if obj_ent_type(active_object
) == 'ent_route':#{
2067 return active_object
.SR_data
.ent_route
[0].gates
2072 def execute(self
, context
):#{
2073 return internal_listdel_execute(self
,context
,'ent_route','gates')
2077 class SR_OT_AUDIO_LIST_NEW_ITEM(bpy
.types
.Operator
):
2079 bl_idname
= "skaterift.al_new_entry"
2080 bl_label
= "Add file"
2082 def execute(self
, context
):#{
2083 return internal_listadd_execute(self
,context
,'ent_audio','files')
2087 class SR_OT_AUDIO_LIST_DEL_ITEM(bpy
.types
.Operator
):
2089 bl_idname
= "skaterift.al_del_entry"
2090 bl_label
= "Remove file"
2093 def poll(cls
, context
):#{
2094 active_object
= context
.active_object
2095 if obj_ent_type(active_object
) == 'ent_audio':#{
2096 return active_object
.SR_data
.ent_audio
[0].files
2101 def execute(self
, context
):#{
2102 return internal_listdel_execute(self
,context
,'ent_audio','files')
2107 class SR_OT_GLYPH_LIST_NEW_ITEM(bpy
.types
.Operator
):
2109 bl_idname
= "skaterift.gl_new_entry"
2110 bl_label
= "Add glyph"
2112 def execute(self
, context
):#{
2113 active_object
= context
.active_object
2115 font
= active_object
.SR_data
.ent_font
[0]
2118 if len(font
.glyphs
) > 1:#{
2119 prev
= font
.glyphs
[-2]
2120 cur
= font
.glyphs
[-1]
2122 cur
.bounds
= prev
.bounds
2123 cur
.utf32
= prev
.utf32
+1
2130 class SR_OT_GLYPH_LIST_DEL_ITEM(bpy
.types
.Operator
):
2132 bl_idname
= "skaterift.gl_del_entry"
2133 bl_label
= "Remove Glyph"
2136 def poll(cls
, context
):#{
2137 active_object
= context
.active_object
2138 if obj_ent_type(active_object
) == 'ent_font':#{
2139 return active_object
.SR_data
.ent_font
[0].glyphs
2144 def execute(self
, context
):#{
2145 return internal_listdel_execute(self
,context
,'ent_font','glyphs')
2149 class SR_OT_GLYPH_LIST_MOVE_ITEM(bpy
.types
.Operator
):
2151 bl_idname
= "skaterift.gl_move_item"
2153 direction
: bpy
.props
.EnumProperty(items
=(('UP', 'Up', ""),
2154 ('DOWN', 'Down', ""),))
2157 def poll(cls
, context
):#{
2158 active_object
= context
.active_object
2159 if obj_ent_type(active_object
) == 'ent_font':#{
2160 return active_object
.SR_data
.ent_font
[0].glyphs
2165 def execute(_
, context
):#{
2166 active_object
= context
.active_object
2167 data
= active_object
.SR_data
.ent_font
[0]
2169 index
= data
.glyphs_index
2170 neighbor
= index
+ (-1 if _
.direction
== 'UP' else 1)
2171 data
.glyphs
.move( neighbor
, index
)
2173 list_length
= len(data
.glyphs
) - 1
2174 new_index
= index
+ (-1 if _
.direction
== 'UP' else 1)
2176 data
.glyphs_index
= max(0, min(new_index
, list_length
))
2182 class SR_OT_FONT_VARIANT_LIST_NEW_ITEM(bpy
.types
.Operator
):
2184 bl_idname
= "skaterift.fv_new_entry"
2185 bl_label
= "Add variant"
2187 def execute(self
, context
):#{
2188 return internal_listadd_execute(self
,context
,'ent_font','variants')
2192 class SR_OT_FONT_VARIANT_LIST_DEL_ITEM(bpy
.types
.Operator
):
2194 bl_idname
= "skaterift.fv_del_entry"
2195 bl_label
= "Remove variant"
2198 def poll(cls
, context
):#{
2199 active_object
= context
.active_object
2200 if obj_ent_type(active_object
) == 'ent_font':#{
2201 return active_object
.SR_data
.ent_font
[0].variants
2206 def execute(self
, context
):#{
2207 return internal_listdel_execute(self
,context
,'ent_font','variants')
2211 class SR_OBJECT_ENT_AUDIO_FILE_ENTRY(bpy
.types
.PropertyGroup
):
2213 path
: bpy
.props
.StringProperty( name
="Path" )
2214 probability
: bpy
.props
.FloatProperty( name
="Probability",default
=100.0 )
2217 class SR_UL_AUDIO_LIST(bpy
.types
.UIList
):
2219 bl_idname
= 'SR_UL_AUDIO_LIST'
2221 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2223 split
= layout
.split(factor
=0.7)
2225 c
.prop( item
, 'path', text
='', emboss
=False )
2227 c
.prop( item
, 'probability', text
='%', emboss
=True )
2231 class SR_UL_FONT_VARIANT_LIST(bpy
.types
.UIList
):
2233 bl_idname
= 'SR_UL_FONT_VARIANT_LIST'
2235 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2237 layout
.prop( item
, 'mesh', emboss
=False )
2238 layout
.prop( item
, 'tipo' )
2242 class SR_UL_FONT_GLYPH_LIST(bpy
.types
.UIList
):
2244 bl_idname
= 'SR_UL_FONT_GLYPH_LIST'
2246 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2248 s0
= layout
.split(factor
=0.3)
2250 s1
= c
.split(factor
=0.3)
2253 lbl
= chr(item
.utf32
) if item
.utf32
>= 32 and item
.utf32
<= 126 else 'ERR'
2256 c
.prop( item
, 'utf32', text
='', emboss
=True )
2259 row
.prop( item
, 'bounds', text
='', emboss
=False )
2263 class SR_OBJECT_ENT_ROUTE(bpy
.types
.PropertyGroup
):
2265 gates
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY
)
2266 gates_index
: bpy
.props
.IntProperty()
2268 colour
: bpy
.props
.FloatVectorProperty( \
2272 default
=Vector((0.79,0.63,0.48)),\
2273 description
="Route colour"\
2276 alias
: bpy
.props
.StringProperty(\
2278 default
="Untitled Course")
2281 def sr_inspector( layout
, data
):
2283 layout
.prop( data
[0], 'alias' )
2284 layout
.prop( data
[0], 'colour' )
2286 layout
.label( text
='Checkpoints' )
2287 layout
.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
2288 data
[0], 'gates', data
[0], 'gates_index', rows
=5)
2291 row
.operator( 'skaterift.new_entry', text
='Add' )
2292 row
.operator( 'skaterift.del_entry', text
='Remove' )
2296 class SR_OBJECT_ENT_VOLUME(bpy
.types
.PropertyGroup
):
2298 subtype
: bpy
.props
.EnumProperty(
2300 items
=[('0','Trigger',''),
2301 ('1','Particles (0.1s)','')]
2304 target
: bpy
.props
.PointerProperty( \
2305 type=bpy
.types
.Object
, name
="Target", \
2306 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_audio']))
2309 def sr_inspector( layout
, data
):
2312 layout
.prop( data
, 'subtype' )
2313 layout
.prop( data
, 'target' )
2317 class SR_OBJECT_ENT_AUDIO(bpy
.types
.PropertyGroup
):
2319 files
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO_FILE_ENTRY
)
2320 files_index
: bpy
.props
.IntProperty()
2322 flag_3d
: bpy
.props
.BoolProperty( name
="3D audio",default
=True )
2323 flag_loop
: bpy
.props
.BoolProperty( name
="Loop",default
=False )
2324 flag_auto
: bpy
.props
.BoolProperty( name
="Play at start",default
=False )
2325 flag_nodoppler
: bpy
.props
.BoolProperty( name
="No Doppler",default
=False )
2327 group
: bpy
.props
.IntProperty( name
="Group ID", default
=0 )
2328 formato
: bpy
.props
.EnumProperty(
2330 items
=[('0','Uncompressed Mono',''),
2331 ('1','Compressed Vorbis',''),
2332 ('2','[vg] Bird Synthesis','')]
2334 probability_curve
: bpy
.props
.EnumProperty(
2335 name
="Probability Curve",
2336 items
=[('0','Constant',''),
2337 ('1','Wildlife Daytime',''),
2338 ('2','Wildlife Nighttime','')])
2339 channel_behaviour
: bpy
.props
.EnumProperty(
2340 name
="Channel Behaviour",
2341 items
=[('0','Unlimited',''),
2342 ('1','Discard if group full', ''),
2343 ('2','Crossfade if group full','')])
2345 transition_duration
: bpy
.props
.FloatProperty(name
="Transition Time",\
2348 max_channels
: bpy
.props
.IntProperty( name
="Max Channels", default
=1 )
2349 volume
: bpy
.props
.FloatProperty( name
="Volume",default
=1.0 )
2352 def sr_inspector( layout
, data
):
2354 layout
.prop( data
[0], 'formato' )
2355 layout
.prop( data
[0], 'volume' )
2358 box
.label( text
='Channels' )
2359 split
= box
.split(factor
=0.3)
2361 c
.prop( data
[0], 'max_channels' )
2363 c
.prop( data
[0], 'channel_behaviour', text
='Behaviour' )
2364 if data
[0].channel_behaviour
>= '1':
2365 box
.prop( data
[0], 'group' )
2366 if data
[0].channel_behaviour
== '2':
2367 box
.prop( data
[0], 'transition_duration' )
2370 box
.label( text
='Flags' )
2371 box
.prop( data
[0], 'flag_3d' )
2372 if data
[0].flag_3d
: box
.prop( data
[0], 'flag_nodoppler' )
2374 box
.prop( data
[0], 'flag_loop' )
2375 box
.prop( data
[0], 'flag_auto' )
2377 split
= layout
.split(factor
=0.7)
2379 c
.label( text
='Filepath' )
2381 c
.label( text
='Chance (0.1s)' )
2383 layout
.prop( data
[0], 'probability_curve' )
2385 layout
.template_list('SR_UL_AUDIO_LIST', 'Files', \
2386 data
[0], 'files', data
[0], 'file_index', rows
=5)
2389 row
.operator( 'skaterift.al_new_entry', text
='Add' )
2390 row
.operator( 'skaterift.al_del_entry', text
='Remove' )
2394 class SR_OBJECT_ENT_MARKER(bpy
.types
.PropertyGroup
):
2396 alias
: bpy
.props
.StringProperty()
2399 class SR_OBJECT_ENT_GLYPH(bpy
.types
.PropertyGroup
):
2401 mini
: bpy
.props
.FloatVectorProperty(size
=2)
2402 maxi
: bpy
.props
.FloatVectorProperty(size
=2)
2403 utf32
: bpy
.props
.IntProperty()
2406 class SR_OBJECT_ENT_GLYPH_ENTRY(bpy
.types
.PropertyGroup
):
2408 bounds
: bpy
.props
.FloatVectorProperty(size
=4,subtype
='NONE')
2409 utf32
: bpy
.props
.IntProperty()
2412 class SR_OBJECT_ENT_FONT_VARIANT(bpy
.types
.PropertyGroup
):
2414 mesh
: bpy
.props
.PointerProperty(type=bpy
.types
.Object
)
2415 tipo
: bpy
.props
.StringProperty()
2418 class SR_OBJECT_ENT_FONT(bpy
.types
.PropertyGroup
):
2420 variants
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT_VARIANT
)
2421 glyphs
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH_ENTRY
)
2422 alias
: bpy
.props
.StringProperty()
2424 glyphs_index
: bpy
.props
.IntProperty()
2425 variants_index
: bpy
.props
.IntProperty()
2428 def sr_inspector( layout
, data
):
2430 layout
.prop( data
[0], 'alias' )
2432 layout
.label( text
='Variants' )
2433 layout
.template_list('SR_UL_FONT_VARIANT_LIST', 'Variants', \
2434 data
[0], 'variants', data
[0], 'variants_index',\
2437 row
.operator( 'skaterift.fv_new_entry', text
='Add' )
2438 row
.operator( 'skaterift.fv_del_entry', text
='Remove' )
2440 layout
.label( text
='ASCII Glyphs' )
2441 layout
.template_list('SR_UL_FONT_GLYPH_LIST', 'Glyphs', \
2442 data
[0], 'glyphs', data
[0], 'glyphs_index', rows
=5)
2445 row
.operator( 'skaterift.gl_new_entry', text
='Add' )
2446 row
.operator( 'skaterift.gl_del_entry', text
='Remove' )
2447 row
.operator( 'skaterift.gl_move_item', text
='^' ).direction
='UP'
2448 row
.operator( 'skaterift.gl_move_item', text
='v' ).direction
='DOWN'
2452 class SR_OBJECT_PROPERTIES(bpy
.types
.PropertyGroup
):
2454 ent_gate
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GATE
)
2455 ent_spawn
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_SPAWN
)
2456 ent_route
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE
)
2457 ent_volume
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_VOLUME
)
2458 ent_audio
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO
)
2459 ent_marker
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_MARKER
)
2460 ent_glyph
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH
)
2461 ent_font
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT
)
2462 ent_type
: bpy
.props
.EnumProperty(
2464 items
=[('none', 'None', '', 0),
2465 ('ent_gate','Gate','', 1),
2466 ('ent_spawn','Spawn','', 2),
2467 ('ent_route_node', 'Route Node', '', 3 ),
2468 ('ent_route', 'Route', '', 4),
2469 ('ent_water', 'Water Surface', '', 5),
2470 ('ent_volume', 'Volume', '', 6 ),
2471 ('ent_audio', 'Audio Files', '', 7),
2472 ('ent_marker', 'Marker', '', 8),
2473 ('ent_font', 'Font', '', 9),
2474 ('ent_font_variant','Font variant','',10)],
2475 update
=sr_on_type_change
2479 class SR_MESH_PROPERTIES(bpy
.types
.PropertyGroup
):
2481 ent_gate
: bpy
.props
.CollectionProperty(type=SR_MESH_ENT_GATE
)
2484 class SR_LIGHT_PROPERTIES(bpy
.types
.PropertyGroup
):
2486 daytime
: bpy
.props
.BoolProperty( name
='Daytime' )
2489 class SR_BONE_PROPERTIES(bpy
.types
.PropertyGroup
):
2491 collider
: bpy
.props
.EnumProperty( name
='Collider Type',
2492 items
=[('0','none',''),
2494 ('2','capsule','')])
2496 collider_min
: bpy
.props
.FloatVectorProperty( name
='Collider Min', size
=3 )
2497 collider_max
: bpy
.props
.FloatVectorProperty( name
='Collider Max', size
=3 )
2499 cone_constraint
: bpy
.props
.BoolProperty( name
='Cone constraint' )
2501 conevx
: bpy
.props
.FloatVectorProperty( name
='vx' )
2502 conevy
: bpy
.props
.FloatVectorProperty( name
='vy' )
2503 coneva
: bpy
.props
.FloatVectorProperty( name
='va' )
2504 conet
: bpy
.props
.FloatProperty( name
='t' )
2507 def sr_inspector( layout
, data
):
2511 box
.prop( data
, 'collider' )
2513 if int(data
.collider
)>0:#{
2515 row
.prop( data
, 'collider_min' )
2517 row
.prop( data
, 'collider_max' )
2521 box
.prop( data
, 'cone_constraint' )
2522 if data
.cone_constraint
:#{
2524 row
.prop( data
, 'conevx' )
2526 row
.prop( data
, 'conevy' )
2528 row
.prop( data
, 'coneva' )
2529 box
.prop( data
, 'conet' )
2534 class SR_MATERIAL_PROPERTIES(bpy
.types
.PropertyGroup
):
2536 shader
: bpy
.props
.EnumProperty(
2539 ('standard',"standard",''),
2540 ('standard_cutout', "standard_cutout", ''),
2541 ('terrain_blend', "terrain_blend", ''),
2542 ('vertex_blend', "vertex_blend", ''),
2543 ('water',"water",'')
2546 surface_prop
: bpy
.props
.EnumProperty(
2547 name
="Surface Property",
2549 ('0','concrete',''),
2556 collision
: bpy
.props
.BoolProperty( \
2557 name
="Collisions Enabled",\
2559 description
= "Can the player collide with this material"\
2561 skate_surface
: bpy
.props
.BoolProperty( \
2562 name
="Skate Surface", \
2564 description
= "Should the game try to target this surface?" \
2566 grind_surface
: bpy
.props
.BoolProperty( \
2567 name
="Grind Surface", \
2569 description
= "Grind face?" \
2571 grow_grass
: bpy
.props
.BoolProperty( \
2572 name
="Grow Grass", \
2574 description
= "Spawn grass sprites on this surface?" \
2576 blend_offset
: bpy
.props
.FloatVectorProperty( \
2577 name
="Blend Offset", \
2579 default
=Vector((0.5,0.0)),\
2580 description
="When surface is more than 45 degrees, add this vector " +\
2583 sand_colour
: bpy
.props
.FloatVectorProperty( \
2584 name
="Sand Colour",\
2587 default
=Vector((0.79,0.63,0.48)),\
2588 description
="Blend to this colour near the 0 coordinate on UP axis"\
2590 shore_colour
: bpy
.props
.FloatVectorProperty( \
2591 name
="Shore Colour",\
2594 default
=Vector((0.03,0.32,0.61)),\
2595 description
="Water colour at the shoreline"\
2597 ocean_colour
: bpy
.props
.FloatVectorProperty( \
2598 name
="Ocean Colour",\
2601 default
=Vector((0.0,0.006,0.03)),\
2602 description
="Water colour in the deep bits"\
2606 # ---------------------------------------------------------------------------- #
2610 # ---------------------------------------------------------------------------- #
2612 cv_view_draw_handler
= None
2613 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
2615 cv_view_colours
= []
2616 cv_view_course_i
= 0
2618 # Draw axis alligned sphere at position with radius
2620 def cv_draw_sphere( pos
, radius
, colour
):
2622 global cv_view_verts
, cv_view_colours
2624 ly
= pos
+ Vector((0,0,radius
))
2625 lx
= pos
+ Vector((0,radius
,0))
2626 lz
= pos
+ Vector((0,0,radius
))
2628 pi
= 3.14159265358979323846264
2630 for i
in range(16):#{
2631 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
2635 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
2636 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
2637 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
2639 cv_view_verts
+= [ px
, lx
]
2640 cv_view_verts
+= [ py
, ly
]
2641 cv_view_verts
+= [ pz
, lz
]
2643 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2652 # Draw axis alligned sphere at position with radius
2654 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
2656 global cv_view_verts
, cv_view_colours
2658 ly
= pos
+ tz
*radius
2659 lx
= pos
+ ty
*radius
2660 lz
= pos
+ tz
*radius
2662 pi
= 3.14159265358979323846264
2664 for i
in range(16):#{
2665 t
= ((i
+1.0) * 1.0/16.0) * pi
2669 s1
= math
.sin(t
*2.0)
2670 c1
= math
.cos(t
*2.0)
2672 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
2673 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
2674 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
2676 cv_view_verts
+= [ px
, lx
]
2677 cv_view_verts
+= [ py
, ly
]
2678 cv_view_verts
+= [ pz
, lz
]
2680 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2689 # Draw transformed -1 -> 1 cube
2691 def cv_draw_ucube( transform
, colour
, s
=Vector((1,1,1)), o
=Vector((0,0,0)) ):
2693 global cv_view_verts
, cv_view_colours
2699 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
2700 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
2701 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
2702 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
2703 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
2704 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
2705 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
2706 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
2708 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2709 (0,4),(1,5),(2,6),(3,7)]
2714 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2715 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2716 cv_view_colours
+= [colour
, colour
]
2721 # Draw line with colour
2723 def cv_draw_line( p0
, p1
, colour
):
2725 global cv_view_verts
, cv_view_colours
2727 cv_view_verts
+= [p0
,p1
]
2728 cv_view_colours
+= [colour
, colour
]
2732 # Draw line with colour(s)
2734 def cv_draw_line2( p0
, p1
, c0
, c1
):
2736 global cv_view_verts
, cv_view_colours
2738 cv_view_verts
+= [p0
,p1
]
2739 cv_view_colours
+= [c0
,c1
]
2745 def cv_tangent_basis( n
, tx
, ty
):
2747 if abs( n
[0] ) >= 0.57735027:#{
2766 # Draw coloured arrow
2768 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
2770 global cv_view_verts
, cv_view_colours
2776 tx
= Vector((1,0,0))
2777 ty
= Vector((1,0,0))
2778 cv_tangent_basis( n
, tx
, ty
)
2780 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
2781 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
2785 def cv_draw_line_dotted( p0
, p1
, c0
, dots
=10 ):
2787 global cv_view_verts
, cv_view_colours
2789 for i
in range(dots
):#{
2793 p2
= p0
*(1.0-t0
)+p1
*t0
2794 p3
= p0
*(1.0-t1
)+p1
*t1
2796 cv_view_verts
+= [p2
,p3
]
2797 cv_view_colours
+= [c0
,c0
]
2802 # Drawhandles of a bezier control point
2804 def cv_draw_bhandle( obj
, direction
, colour
):
2806 global cv_view_verts
, cv_view_colours
2809 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2811 cv_view_verts
+= [p0
]
2812 cv_view_verts
+= [h0
]
2813 cv_view_colours
+= [colour
,colour
]
2817 # Draw a bezier curve (at fixed resolution 10)
2819 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2821 global cv_view_verts
, cv_view_colours
2824 for i
in range(10):#{
2830 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2832 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2833 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2834 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2841 # I think this one extends the handles of the bezier otwards......
2843 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2845 global cv_view_course_i
2847 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2849 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2850 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2851 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2852 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2854 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2858 # Flush the lines buffers. This is called often because god help you if you want
2859 # to do fixed, fast buffers in this catastrophic programming language.
2861 def cv_draw_lines():
2863 global cv_view_shader
, cv_view_verts
, cv_view_colours
2865 if len(cv_view_verts
) < 2:
2868 lines
= batch_for_shader(\
2869 cv_view_shader
, 'LINES', \
2870 { "pos":cv_view_verts
, "color":cv_view_colours
})
2872 lines
.draw( cv_view_shader
)
2875 cv_view_colours
= []
2878 # I dont remember what this does exactly
2880 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2882 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2885 # Semi circle to show the limit. and some lines
2887 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2889 global cv_view_verts
, cv_view_colours
2894 for x
in range(16):#{
2897 a0
= amin
*(1.0-t0
)+amax
*t0
2898 a1
= amin
*(1.0-t1
)+amax
*t1
2900 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2901 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2903 p0
=obj
.matrix_world
@ p0
2904 p1
=obj
.matrix_world
@ p1
2905 cv_view_verts
+= [p0
,p1
]
2906 cv_view_colours
+= [colour
,colour
]
2909 cv_view_verts
+= [p0
,center
]
2910 cv_view_colours
+= [colour
,colour
]
2913 cv_view_verts
+= [p1
,center
]
2914 cv_view_colours
+= [colour
,colour
]
2918 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2919 cv_view_colours
+= [colour
,colour
]
2924 # Cone and twist limit
2926 def draw_cone_twist( center
, vx
, vy
, va
):
2928 global cv_view_verts
, cv_view_colours
2929 axis
= vy
.cross( vx
)
2934 cv_view_verts
+= [center
, center
+va
*size
]
2935 cv_view_colours
+= [ (1,1,1), (1,1,1) ]
2937 for x
in range(32):#{
2938 t0
= (x
/32) * math
.tau
2939 t1
= ((x
+1)/32) * math
.tau
2946 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
2947 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
2949 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
2950 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
2952 cv_view_verts
+= [center
, p0
, p0
, p1
]
2953 cv_view_colours
+= [ (0,0,0), col0
, col0
, col1
]
2959 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
2961 def draw_skeleton_helpers( obj
):
2963 global cv_view_verts
, cv_view_colours
2965 if obj
.data
.pose_position
!= 'REST':#{
2969 for bone
in obj
.data
.bones
:#{
2971 a
= Vector((bone
.SR_data
.collider_min
[0],
2972 bone
.SR_data
.collider_min
[1],
2973 bone
.SR_data
.collider_min
[2]))
2974 b
= Vector((bone
.SR_data
.collider_max
[0],
2975 bone
.SR_data
.collider_max
[1],
2976 bone
.SR_data
.collider_max
[2]))
2978 if bone
.SR_data
.collider
== '1':#{
2980 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
2981 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
2982 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
2983 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
2984 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
2985 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
2986 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
2987 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
2989 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2990 (0,4),(1,5),(2,6),(3,7)]
2996 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2997 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2998 cv_view_colours
+= [(0.5,0.5,0.5),(0.5,0.5,0.5)]
3001 elif bone
.SR_data
.collider
== '2':#{
3006 for i
in range(3):#{
3007 if abs(v0
[i
]) > largest
:#{
3008 largest
= abs(v0
[i
])
3013 v1
= Vector((0,0,0))
3014 v1
[major_axis
] = 1.0
3016 tx
= Vector((0,0,0))
3017 ty
= Vector((0,0,0))
3019 cv_tangent_basis( v1
, tx
, ty
)
3020 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
3021 l
= v0
[ major_axis
] - r
*2
3023 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
3024 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
3026 colour
= [0.2,0.2,0.2]
3027 colour
[major_axis
] = 0.5
3029 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
3030 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
3031 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
3032 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
3033 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
3034 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
3040 center
= obj
.matrix_world
@ c
3041 if bone
.SR_data
.cone_constraint
:#{
3042 vx
= Vector([bone
.SR_data
.conevx
[_
] for _
in range(3)])
3043 vy
= Vector([bone
.SR_data
.conevy
[_
] for _
in range(3)])
3044 va
= Vector([bone
.SR_data
.coneva
[_
] for _
in range(3)])
3045 draw_cone_twist( center
, vx
, vy
, va
)
3050 def cv_ent_gate( obj
):
3052 global cv_view_verts
, cv_view_colours
3054 if obj
.type != 'MESH': return
3056 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
3057 data
= obj
.SR_data
.ent_gate
[0]
3058 dims
= mesh_data
.dimensions
3061 c
= Vector((0,0,dims
[2]))
3063 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
3064 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
3065 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
3066 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
3067 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
3068 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
3069 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
3070 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
3071 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
3073 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
3075 r3d
= bpy
.context
.area
.spaces
.active
.region_3d
3077 p0
= r3d
.view_matrix
.inverted().translation
3078 v0
= (obj
.matrix_world
@Vector((0,0,0))) - p0
3079 v1
= obj
.matrix_world
.to_3x3() @ Vector((0,1,0))
3081 if v0
.dot(v1
) > 0.0: cc
= (0,1,0)
3087 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
3088 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
3089 cv_view_colours
+= [cc
,cc
]
3093 if data
.target
!= None:
3094 cv_draw_arrow( obj
.location
, data
.target
.location
, sw
)
3097 def cv_ent_volume( obj
):
3099 global cv_view_verts
, cv_view_colours
3101 data
= obj
.SR_data
.ent_volume
[0]
3103 if data
.subtype
== '0':#{
3104 cv_draw_ucube( obj
.matrix_world
, (0,1,0) )
3107 cv_draw_line( obj
.location
, data
.target
.location
, (0,1,0) )
3110 elif data
.subtype
== '1':#{
3111 cv_draw_ucube( obj
.matrix_world
, (1,1,0) )
3114 cv_draw_line( obj
.location
, data
.target
.location
, (1,1,0) )
3119 def dijkstra( graph
, start_node
, target_node
):
3121 unvisited
= [_
for _
in graph
]
3126 shortest_path
[n
] = 9999999.999999
3127 shortest_path
[start_node
] = 0
3130 current_min_node
= None
3131 for n
in unvisited
:#{
3132 if current_min_node
== None:
3133 current_min_node
= n
3134 elif shortest_path
[n
] < shortest_path
[current_min_node
]:
3135 current_min_node
= n
3138 for branch
in graph
[current_min_node
]:#{
3139 tentative_value
= shortest_path
[current_min_node
]
3140 tentative_value
+= graph
[current_min_node
][branch
]
3141 if tentative_value
< shortest_path
[branch
]:#{
3142 shortest_path
[branch
] = tentative_value
3143 previous_nodes
[branch
] = current_min_node
3147 unvisited
.remove(current_min_node
)
3152 while node
!= start_node
:#{
3155 if node
not in previous_nodes
: return None
3156 node
= previous_nodes
[node
]
3159 # Add the start node manually
3160 path
.append(start_node
)
3166 def __init__(_
,points
,graph
,subsections
):#{
3169 _
.subsections
= subsections
3173 def create_node_graph( curves
, gates
):
3175 # add endpoints of curves
3182 for c
in range(len(curves
)):#{
3183 for s
in range(len(curves
[c
].data
.splines
)):#{
3184 spline
= curves
[c
].data
.splines
[s
]
3185 l
= len(spline
.points
)
3188 dist
= round(spline
.calc_length(),2)
3191 ib
= point_count
+l
-1
3193 graph
[ia
] = { ib
: dist
}
3194 graph
[ib
] = { ia
: dist
}
3196 for i
in range(len(spline
.points
)):#{
3197 wco
= curves
[c
].matrix_world
@ spline
.points
[i
].co
3198 route_points
.append(Vector((wco
[0],wco
[1],wco
[2]+0.5)))
3203 if i
== 0: previous
= -1
3204 if i
== len(spline
.points
)-1: proxima
= -1
3206 subsections
.append((spline_count
,previous
,proxima
))
3215 graph_keys
= list(graph
)
3216 for i
in range(len(graph_keys
)-1):#{
3217 for j
in range(i
+1, len(graph_keys
)):#{
3218 if i
%2==0 and i
+1==j
: continue
3222 pi
= route_points
[ni
]
3223 pj
= route_points
[nj
]
3225 dist
= round((pj
-pi
).magnitude
,2)
3228 graph
[ni
][nj
] = dist
3229 graph
[nj
][ni
] = dist
3234 # add and link gates( by name )
3235 for gate
in gates
:#{
3236 v1
= gate
.matrix_world
.to_3x3() @ Vector((0,1,0))
3237 if gate
.SR_data
.ent_gate
[0].target
:
3240 graph
[ gate
.name
] = {}
3242 for i
in range(len(graph_keys
)):#{
3244 pi
= route_points
[ni
]
3246 v0
= pi
-gate
.location
3247 if v0
.dot(v1
) < 0.0: continue
3249 dist
= round(v0
.magnitude
,2)
3252 graph
[ gate
.name
][ ni
] = dist
3253 graph
[ ni
][ gate
.name
] = dist
3258 return dij_graph(route_points
,graph
,subsections
)
3261 def solve_graph( dij
, start
, end
):
3263 path
= dijkstra( dij
.graph
, end
, start
)
3267 for sj
in range(1,len(path
)-2):#{
3270 map0
= dij
.subsections
[i0
]
3271 map1
= dij
.subsections
[i1
]
3273 if map0
[0] == map1
[0]:#{
3274 if map0
[1] == -1: direction
= 2
3279 map0
= dij
.subsections
[i0
]
3280 i1
= map0
[direction
]
3294 full
.append( path
[-2] )
3299 def cv_draw_route( route
, dij
):
3301 pole
= Vector((0.2,0.2,10))
3302 hat
= Vector((1,8,0.2))
3303 cc
= (route
.SR_data
.ent_route
[0].colour
[0],
3304 route
.SR_data
.ent_route
[0].colour
[1],
3305 route
.SR_data
.ent_route
[0].colour
[2])
3307 cv_draw_ucube(route
.matrix_world
,cc
,Vector((0.5,-7.5,6)),\
3308 Vector((0,-6.5,5.5)))
3309 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5, 0.5,0)) )
3310 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5,-13.5,0)) )
3311 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5, 12)) )
3312 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5,-1)) )
3314 checkpoints
= route
.SR_data
.ent_route
[0].gates
3316 for i
in range(len(checkpoints
)):#{
3317 gi
= checkpoints
[i
].target
3318 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
3321 dest
= gi
.SR_data
.ent_gate
[0].target
3323 cv_draw_line_dotted( gi
.location
, dest
.location
, cc
)
3327 if gi
==gj
: continue # error?
3328 if not gi
or not gj
: continue
3330 path
= solve_graph( dij
, gi
.name
, gj
.name
)
3333 cv_draw_arrow(gi
.location
,dij
.points
[path
[0]],cc
,1.5)
3334 cv_draw_arrow(dij
.points
[path
[len(path
)-1]],gj
.location
,cc
,1.5)
3335 for j
in range(len(path
)-1):#{
3338 o0
= dij
.points
[ i0
]
3339 o1
= dij
.points
[ i1
]
3340 cv_draw_arrow(o0
,o1
,cc
,1.5)
3344 cv_draw_line_dotted( gi
.location
, gj
.location
, cc
)
3351 global cv_view_shader
3352 global cv_view_verts
3353 global cv_view_colours
3354 global cv_view_course_i
3356 cv_view_course_i
= 0
3358 cv_view_colours
= []
3360 cv_view_shader
.bind()
3361 gpu
.state
.depth_mask_set(False)
3362 gpu
.state
.line_width_set(2.0)
3363 gpu
.state
.face_culling_set('BACK')
3364 gpu
.state
.depth_test_set('LESS')
3365 gpu
.state
.blend_set('NONE')
3371 for obj
in bpy
.context
.collection
.objects
:#{
3372 if obj
.type == 'ARMATURE':#{
3373 if obj
.data
.pose_position
== 'REST':
3374 draw_skeleton_helpers( obj
)
3377 ent_type
= obj_ent_type( obj
)
3379 if ent_type
== 'ent_gate':#{
3381 route_gates
+= [obj
]
3383 elif ent_type
== 'ent_route_node':#{
3384 if obj
.type == 'CURVE':#{
3385 route_curves
+= [obj
]
3388 elif ent_type
== 'ent_route':
3390 elif ent_type
== 'ent_volume':#{
3391 cv_ent_volume( obj
)
3393 elif ent_type
== 'ent_audio':#{
3394 if obj
.SR_data
.ent_audio
[0].flag_3d
:
3395 cv_draw_sphere( obj
.location
, obj
.scale
[0], (1,1,0) )
3397 elif ent_type
== 'ent_font':#{
3398 data
= obj
.SR_data
.ent_font
[0]
3400 for i
in range(len(data
.variants
)):#{
3401 sub
= data
.variants
[i
].mesh
3402 if not sub
: continue
3404 for ch
in data
.glyphs
:#{
3405 mini
= (ch
.bounds
[0],ch
.bounds
[1])
3406 maxi
= (ch
.bounds
[2]+mini
[0],ch
.bounds
[3]+mini
[1])
3407 p0
= sub
.matrix_world
@ Vector((mini
[0],0.0,mini
[1]))
3408 p1
= sub
.matrix_world
@ Vector((maxi
[0],0.0,mini
[1]))
3409 p2
= sub
.matrix_world
@ Vector((maxi
[0],0.0,maxi
[1]))
3410 p3
= sub
.matrix_world
@ Vector((mini
[0],0.0,maxi
[1]))
3412 if i
== data
.variants_index
: cc
= (0.5,0.5,0.5)
3415 cv_view_verts
+= [p0
,p1
,p1
,p2
,p2
,p3
,p3
,p0
]
3416 cv_view_colours
+= [cc
,cc
,cc
,cc
,cc
,cc
,cc
,cc
]
3423 dij
= create_node_graph( route_curves
, route_gates
)
3425 #cv_draw_route_map( route_nodes )
3426 for route
in routes
:#{
3427 cv_draw_route( route
, dij
)
3434 classes
= [ SR_INTERFACE
, SR_MATERIAL_PANEL
,\
3435 SR_COLLECTION_SETTINGS
, SR_SCENE_SETTINGS
, \
3436 SR_COMPILE
, SR_COMPILE_THIS
, SR_MIRROR_BONE_X
,\
3438 SR_OBJECT_ENT_GATE
, SR_MESH_ENT_GATE
, SR_OBJECT_ENT_SPAWN
, \
3439 SR_OBJECT_ENT_ROUTE_ENTRY
, SR_UL_ROUTE_NODE_LIST
, \
3440 SR_OBJECT_ENT_ROUTE
, SR_OT_ROUTE_LIST_NEW_ITEM
,\
3441 SR_OT_GLYPH_LIST_NEW_ITEM
, SR_OT_GLYPH_LIST_DEL_ITEM
,\
3442 SR_OT_GLYPH_LIST_MOVE_ITEM
,\
3443 SR_OT_AUDIO_LIST_NEW_ITEM
,SR_OT_AUDIO_LIST_DEL_ITEM
,\
3444 SR_OT_FONT_VARIANT_LIST_NEW_ITEM
,SR_OT_FONT_VARIANT_LIST_DEL_ITEM
,\
3445 SR_OT_COPY_ENTITY_DATA
, \
3446 SR_OBJECT_ENT_VOLUME
, \
3447 SR_UL_AUDIO_LIST
, SR_OBJECT_ENT_AUDIO_FILE_ENTRY
,\
3448 SR_OT_ROUTE_LIST_DEL_ITEM
,\
3449 SR_OBJECT_ENT_AUDIO
,SR_OBJECT_ENT_MARKER
,SR_OBJECT_ENT_GLYPH
,\
3450 SR_OBJECT_ENT_FONT_VARIANT
,
3451 SR_OBJECT_ENT_GLYPH_ENTRY
,\
3452 SR_UL_FONT_VARIANT_LIST
,SR_UL_FONT_GLYPH_LIST
,\
3453 SR_OBJECT_ENT_FONT
,\
3455 SR_OBJECT_PROPERTIES
, SR_LIGHT_PROPERTIES
, SR_BONE_PROPERTIES
,
3456 SR_MESH_PROPERTIES
, SR_MATERIAL_PROPERTIES \
3462 bpy
.utils
.register_class(c
)
3464 bpy
.types
.Scene
.SR_data
= \
3465 bpy
.props
.PointerProperty(type=SR_SCENE_SETTINGS
)
3466 bpy
.types
.Collection
.SR_data
= \
3467 bpy
.props
.PointerProperty(type=SR_COLLECTION_SETTINGS
)
3469 bpy
.types
.Object
.SR_data
= \
3470 bpy
.props
.PointerProperty(type=SR_OBJECT_PROPERTIES
)
3471 bpy
.types
.Light
.SR_data
= \
3472 bpy
.props
.PointerProperty(type=SR_LIGHT_PROPERTIES
)
3473 bpy
.types
.Bone
.SR_data
= \
3474 bpy
.props
.PointerProperty(type=SR_BONE_PROPERTIES
)
3475 bpy
.types
.Mesh
.SR_data
= \
3476 bpy
.props
.PointerProperty(type=SR_MESH_PROPERTIES
)
3477 bpy
.types
.Material
.SR_data
= \
3478 bpy
.props
.PointerProperty(type=SR_MATERIAL_PROPERTIES
)
3480 global cv_view_draw_handler
3481 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
3482 cv_draw
,(),'WINDOW','POST_VIEW')
3488 bpy
.utils
.unregister_class(c
)
3490 global cv_view_draw_handler
3491 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
3494 # ---------------------------------------------------------------------------- #
3498 # ---------------------------------------------------------------------------- #
3500 # Transliteration of: #
3501 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
3503 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
3504 # SPDX-License-Identifier: MIT #
3505 # QOI - The "Quite OK Image" format for fast, lossless image compression #
3507 # ---------------------------------------------------------------------------- #
3509 class qoi_rgba_t(Structure
):
3512 _fields_
= [("r",c_uint8
),
3518 QOI_OP_INDEX
= 0x00 # 00xxxxxx
3519 QOI_OP_DIFF
= 0x40 # 01xxxxxx
3520 QOI_OP_LUMA
= 0x80 # 10xxxxxx
3521 QOI_OP_RUN
= 0xc0 # 11xxxxxx
3522 QOI_OP_RGB
= 0xfe # 11111110
3523 QOI_OP_RGBA
= 0xff # 11111111
3525 QOI_MASK_2
= 0xc0 # 11000000
3527 def qoi_colour_hash( c
):
3529 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
3534 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
3539 return bytearray([ (0xff000000 & v
) >> 24, \
3540 (0x00ff0000 & v
) >> 16, \
3541 (0x0000ff00 & v
) >> 8, \
3545 def qoi_encode( img
):
3549 print(F
"{' ':<30}",end
='\r')
3550 print(F
"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end
='\r')
3552 index
= [ qoi_rgba_t() for _
in range(64) ]
3556 data
.extend( bytearray(c_uint32(0x66696f71)) )
3557 data
.extend( qoi_32bit( img
.size
[0] ) )
3558 data
.extend( qoi_32bit( img
.size
[1] ) )
3559 data
.extend( bytearray(c_uint8(4)) )
3560 data
.extend( bytearray(c_uint8(0)) )
3563 px_prev
= qoi_rgba_t()
3564 px_prev
.r
= c_uint8(0)
3565 px_prev
.g
= c_uint8(0)
3566 px_prev
.b
= c_uint8(0)
3567 px_prev
.a
= c_uint8(255)
3575 px_len
= img
.size
[0] * img
.size
[1]
3576 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
3578 for px_pos
in range( px_len
): #{
3579 idx
= px_pos
* img
.channels
3582 px
.r
= paxels
[idx
+min(0,nc
)]
3583 px
.g
= paxels
[idx
+min(1,nc
)]
3584 px
.b
= paxels
[idx
+min(2,nc
)]
3585 px
.a
= paxels
[idx
+min(3,nc
)]
3587 if qoi_eq( px
, px_prev
): #{
3590 if (run
== 62) or (px_pos
== px_len
-1): #{
3591 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3597 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3601 index_pos
= qoi_colour_hash(px
) % 64
3603 if qoi_eq( index
[index_pos
], px
): #{
3604 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
3607 index
[ index_pos
].r
= px
.r
3608 index
[ index_pos
].g
= px
.g
3609 index
[ index_pos
].b
= px
.b
3610 index
[ index_pos
].a
= px
.a
3612 if px
.a
== px_prev
.a
: #{
3613 vr
= int(px
.r
) - int(px_prev
.r
)
3614 vg
= int(px
.g
) - int(px_prev
.g
)
3615 vb
= int(px
.b
) - int(px_prev
.b
)
3620 if (vr
> -3) and (vr
< 2) and\
3621 (vg
> -3) and (vg
< 2) and\
3622 (vb
> -3) and (vb
< 2):
3624 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
3625 data
.extend( bytearray( c_uint8(op
) ))
3627 elif (vg_r
> -9) and (vg_r
< 8) and\
3628 (vg
> -33) and (vg
< 32 ) and\
3629 (vg_b
> -9) and (vg_b
< 8):
3631 op
= QOI_OP_LUMA |
(vg
+32)
3632 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
3633 data
.extend( bytearray( c_uint8(op
) ) )
3634 data
.extend( bytearray( c_uint8(delta
) ))
3637 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
3638 data
.extend( bytearray( c_uint8(px
.r
) ))
3639 data
.extend( bytearray( c_uint8(px
.g
) ))
3640 data
.extend( bytearray( c_uint8(px
.b
) ))
3644 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
3645 data
.extend( bytearray( c_uint8(px
.r
) ))
3646 data
.extend( bytearray( c_uint8(px
.g
) ))
3647 data
.extend( bytearray( c_uint8(px
.b
) ))
3648 data
.extend( bytearray( c_uint8(px
.a
) ))
3661 data
.extend( bytearray( c_uint8(0) ))
3662 data
.extend( bytearray( c_uint8(1) ))
3663 bytearray_align_to( data
, 16, b
'\x00' )