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_uint32
)] # index into the material array
74 class mdl_material(Structure
):
76 _fields_
= [("pstr_name",c_uint32
),
79 ("surface_prop",c_uint32
),
81 ("colour1",c_float
*4),
82 ("tex_diffuse",c_uint32
),
83 ("tex_none0",c_uint32
),
84 ("tex_none1",c_uint32
)]
87 class mdl_bone(Structure
):
89 _fields_
= [("co",c_float
*3),("end",c_float
*3),
91 ("collider",c_uint32
),
92 ("ik_target",c_uint32
),
95 ("pstr_name",c_uint32
),
96 ("hitbox",(c_float
*3)*2),
97 ("conevx",c_float
*3),("conevy",c_float
*3),("coneva",c_float
*3),
101 class mdl_armature(Structure
):
103 _fields_
= [("transform",mdl_transform
),
104 ("bone_start",c_uint32
),
105 ("bone_count",c_uint32
),
106 ("anim_start",c_uint32
),
107 ("anim_count",c_uint32
)]
110 class mdl_animation(Structure
):
112 _fields_
= [("pstr_name",c_uint32
),
115 ("keyframe_start",c_uint32
)]
118 class mdl_mesh(Structure
):
120 _fields_
= [("transform",mdl_transform
),
121 ("submesh_start",c_uint32
),
122 ("submesh_count",c_uint32
),
123 ("pstr_name",c_uint32
),
124 ("entity_id",c_uint32
),
125 ("armature_id",c_uint32
)]
128 class mdl_file(Structure
):
130 _fields_
= [("path",c_uint32
),
131 ("pack_offset",c_uint32
),
132 ("pack_size",c_uint32
)]
135 class mdl_texture(Structure
):
137 _fields_
= [("file",mdl_file
),
141 class mdl_array(Structure
):
143 _fields_
= [("file_offset",c_uint32
),
144 ("item_count",c_uint32
),
145 ("item_size",c_uint32
),
149 class mdl_header(Structure
):
151 _fields_
= [("version",c_uint32
),
152 ("arrays",mdl_array
)]
155 class ent_spawn(Structure
):
157 _fields_
= [("transform",mdl_transform
),
158 ("pstr_name",c_uint32
)]
161 class ent_light(Structure
):
163 _fields_
= [("transform",mdl_transform
),
164 ("daytime",c_uint32
),
166 ("colour",c_float
*4),
169 ("inverse_world",(c_float
*3)*4), # Runtime
170 ("angle_sin_cos",(c_float
*2))] # Runtime
173 class version_refcount_union(Union
):
175 _fields_
= [("timing_version",c_uint32
),
176 ("ref_count",c_uint8
)]
179 class ent_gate(Structure
):
181 _fields_
= [("type",c_uint32
),
182 ("target", c_uint32
),
183 ("dimensions", c_float
*3),
184 ("co", (c_float
*3)*2),
185 ("q", (c_float
*4)*2),
186 ("to_world",(c_float
*3)*4),
187 ("transport",(c_float
*3)*4),
188 ("_anonymous_union",version_refcount_union
),
189 ("timing_time",c_double
),
190 ("routes",c_uint16
*4),
191 ("route_count",c_uint8
)]
194 class ent_route_node(Structure
):
196 _fields_
= [("co",c_float
*3),
197 ("ref_count",c_uint8
),
198 ("ref_total",c_uint8
)]
201 class ent_path_index(Structure
):
203 _fields_
= [("index",c_uint16
)]
206 class vg_audio_clip(Structure
):
208 _fields_
= [("path",c_uint64
),
214 class union_file_audio_clip(Union
):
216 _fields_
= [("file",mdl_file
),
217 ("reserved",vg_audio_clip
)]
220 class ent_audio_clip(Structure
):
222 _fields_
= [("_anon",union_file_audio_clip
),
223 ("probability",c_float
)]
226 class ent_checkpoint(Structure
):
228 _fields_
= [("gate_index",c_uint16
),
229 ("path_start",c_uint16
),
230 ("path_count",c_uint16
)]
233 class ent_route(Structure
):
235 _fields_
= [("transform",mdl_transform
),
236 ("pstr_name",c_uint32
),
237 ("checkpoints_start",c_uint16
),
238 ("checkpoints_count",c_uint16
),
239 ("colour",c_float
*4),
240 ("active",c_uint32
), #runtime
242 ("board_transform",(c_float
*3)*4),
244 ("latest_pass",c_double
)]
247 class ent_water(Structure
):
249 _fields_
= [("transform",mdl_transform
),
250 ("max_dist",c_float
),
251 ("reserved0",c_uint32
),
252 ("reserved1",c_uint32
)]
255 class volume_trigger(Structure
):
257 _fields_
= [("event",c_uint32
),
261 class volume_particles(Structure
):
263 _fields_
= [("blank",c_uint32
),
267 class volume_union(Union
):
269 _fields_
= [("trigger",volume_trigger
),
270 ("particles",volume_particles
)]
273 class ent_index(Structure
):
275 _fields_
= [("type",c_uint32
),
279 class ent_volume(Structure
):
281 _fields_
= [("transform",mdl_transform
),
282 ("to_world",(c_float
*3)*4),
283 ("to_local",(c_float
*3)*4),
285 ("target",ent_index
),
286 ("_anon",volume_union
)]
289 class ent_audio(Structure
):
291 _fields_
= [("transform",mdl_transform
),
293 ("clip_start",c_uint32
),
294 ("clip_count",c_uint32
),
296 ("crossfade",c_float
),
297 ("channel_behaviour",c_uint32
),
299 ("probability_curve",c_uint32
),
300 ("max_channels",c_uint32
)]
303 class ent_marker(Structure
):
305 _fields_
= [("transform",mdl_transform
),
309 class ent_glyph(Structure
):
311 _fields_
= [("size",c_float
*2),
312 ("indice_start",c_uint32
),
313 ("indice_count",c_uint32
)]
316 class ent_font_variant(Structure
):
318 _fields_
= [("name",c_uint32
),
319 ("material_id",c_uint32
)]
322 class ent_font(Structure
):
324 _fields_
= [("alias",c_uint32
),
325 ("variant_start",c_uint32
),
326 ("variant_count",c_uint32
),
327 ("glyph_start",c_uint32
),
328 ("glyph_count",c_uint32
),
329 ("glyph_utf32_base",c_uint32
)]
332 def obj_ent_type( obj
):
334 if obj
.type == 'ARMATURE': return 'mdl_armature'
335 elif obj
.type == 'LIGHT': return 'ent_light'
336 else: return obj
.SR_data
.ent_type
339 def sr_filter_ent_type( obj
, ent_types
):
341 if obj
== bpy
.context
.active_object
: return False
343 for c0
in obj
.users_collection
:#{
344 for c1
in bpy
.context
.active_object
.users_collection
:#{
346 return obj_ent_type( obj
) in ent_types
354 def compile_obj_transform( obj
, transform
):
356 co
= obj
.matrix_world
@ Vector((0,0,0))
357 q
= obj
.matrix_local
.to_quaternion()
362 transform
.co
[0] = co
[0]
363 transform
.co
[1] = co
[2]
364 transform
.co
[2] = -co
[1]
365 transform
.q
[0] = q
[1]
366 transform
.q
[1] = q
[3]
367 transform
.q
[2] = -q
[2]
368 transform
.q
[3] = q
[0]
369 transform
.s
[0] = s
[0]
370 transform
.s
[1] = s
[2]
371 transform
.s
[2] = s
[1]
374 def int_align_to( v
, align
):
376 while(v
%align
)!=0: v
+= 1
380 def bytearray_align_to( buffer, align
, w
=b
'\xaa' ):
382 while (len(buffer) % align
) != 0: buffer.extend(w
)
386 def bytearray_print_hex( s
, w
=16 ):
388 for r
in range((len(s
)+(w
-1))//w
):#{
390 i1
=min((r
+1)*w
,len(s
))
391 print( F
'{r*w:06x}| \x1B[31m', end
='')
392 print( F
"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end
='' )
393 print( "\x1B[0m", end
='')
394 print( ''.join(chr(x
) if (x
>=33 and x
<=126) else '.' for x
in s
[i0
:i1
] ) )
398 def sr_compile_string( s
):
400 if s
in sr_compile
.string_cache
: return sr_compile
.string_cache
[s
]
402 index
= len( sr_compile
.string_data
)
403 sr_compile
.string_cache
[s
] = index
404 sr_compile
.string_data
.extend( s
.encode('utf-8') )
405 sr_compile
.string_data
.extend( b
'\0' )
407 bytearray_align_to( sr_compile
.string_data
, 4 )
411 def material_tex_image(v
):
421 cxr_graph_mapping
= \
423 # Default shader setup
430 "image": "tex_diffuse"
434 "A": material_tex_image("tex_diffuse"),
435 "B": material_tex_image("tex_decal")
442 "Color": material_tex_image("tex_normal")
448 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
450 def material_info(mat
):
454 # Using the cxr_graph_mapping as a reference, go through the shader
455 # graph and gather all $props from it.
457 def _graph_read( node_def
, node
=None, depth
=0 ):#{
464 _graph_read
.extracted
= []
466 for node_idname
in node_def
:#{
467 for n
in mat
.node_tree
.nodes
:#{
468 if n
.name
== node_idname
:#{
469 node_def
= node_def
[node_idname
]
477 for link
in node_def
:#{
478 link_def
= node_def
[link
]
480 if isinstance( link_def
, dict ):#{
482 for x
in node
.inputs
:#{
483 if isinstance( x
, bpy
.types
.NodeSocketColor
):#{
491 if node_link
and node_link
.is_linked
:#{
492 # look for definitions for the connected node type
494 from_node
= node_link
.links
[0].from_node
496 node_name
= from_node
.name
.split('.')[0]
497 if node_name
in link_def
:#{
498 from_node_def
= link_def
[ node_name
]
500 _graph_read( from_node_def
, from_node
, depth
+1 )
504 if "default" in link_def
:#{
505 prop
= link_def
['default']
506 info
[prop
] = node_link
.default_value
512 info
[prop
] = getattr( node
, link
)
517 _graph_read( cxr_graph_mapping
)
523 decoded
= bytearray()
524 for i
in range(len(s
)//2):#{
525 c
= (ord(s
[i
*2+0])-0x41)
526 c |
= (ord(s
[i
*2+1])-0x41)<<4
527 decoded
.extend(bytearray(c_uint8(c
))) #??
532 def sr_pack_file( file, path
, data
):
534 file.path
= sr_compile_string( path
)
535 file.pack_offset
= len( sr_compile
.pack_data
)
536 file.pack_size
= len( data
)
538 sr_compile
.pack_data
.extend( data
)
539 bytearray_align_to( sr_compile
.pack_data
, 16 )
542 def sr_compile_texture( img
):
547 name
= os
.path
.splitext( img
.name
)[0]
549 if name
in sr_compile
.texture_cache
:
550 return sr_compile
.texture_cache
[name
]
552 texture_index
= (len(sr_compile
.texture_data
)//sizeof(mdl_texture
)) +1
557 if sr_compile
.pack_textures
:#{
558 filedata
= qoi_encode( img
)
559 sr_pack_file( tex
.file, name
, filedata
)
562 sr_compile
.texture_cache
[name
] = texture_index
563 sr_compile
.texture_data
.extend( bytearray(tex
) )
567 def sr_compile_material( mat
):
571 if mat
.name
in sr_compile
.material_cache
:
572 return sr_compile
.material_cache
[mat
.name
]
574 index
= (len(sr_compile
.material_data
)//sizeof(mdl_material
))+1
575 sr_compile
.material_cache
[mat
.name
] = index
578 m
.pstr_name
= sr_compile_string( mat
.name
)
581 if mat
.SR_data
.collision
:#{
582 flags |
= 0x2 # collision flag
583 if (mat
.SR_data
.shader
!= 'invisible') and \
584 (mat
.SR_data
.shader
!= 'boundary'):#{
585 if mat
.SR_data
.skate_surface
: flags |
= 0x1
586 if mat
.SR_data
.grow_grass
: flags |
= 0x4
587 if mat
.SR_data
.grind_surface
: flags |
= 0x8
589 if mat
.SR_data
.shader
== 'invisible': flags |
= 0x10
590 if mat
.SR_data
.shader
== 'boundary': flags |
= (0x10|
0x20)
595 m
.surface_prop
= int(mat
.SR_data
.surface_prop
)
597 if mat
.SR_data
.shader
== 'standard': m
.shader
= 0
598 if mat
.SR_data
.shader
== 'standard_cutout': m
.shader
= 1
599 if mat
.SR_data
.shader
== 'terrain_blend':#{
602 m
.colour
[0] = pow( mat
.SR_data
.sand_colour
[0], 1.0/2.2 )
603 m
.colour
[1] = pow( mat
.SR_data
.sand_colour
[1], 1.0/2.2 )
604 m
.colour
[2] = pow( mat
.SR_data
.sand_colour
[2], 1.0/2.2 )
607 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
608 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
611 if mat
.SR_data
.shader
== 'vertex_blend':#{
614 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
615 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
618 if mat
.SR_data
.shader
== 'water':#{
621 m
.colour
[0] = pow( mat
.SR_data
.shore_colour
[0], 1.0/2.2 )
622 m
.colour
[1] = pow( mat
.SR_data
.shore_colour
[1], 1.0/2.2 )
623 m
.colour
[2] = pow( mat
.SR_data
.shore_colour
[2], 1.0/2.2 )
625 m
.colour1
[0] = pow( mat
.SR_data
.ocean_colour
[0], 1.0/2.2 )
626 m
.colour1
[1] = pow( mat
.SR_data
.ocean_colour
[1], 1.0/2.2 )
627 m
.colour1
[2] = pow( mat
.SR_data
.ocean_colour
[2], 1.0/2.2 )
631 if mat
.SR_data
.shader
== 'invisible':#{
635 if mat
.SR_data
.shader
== 'boundary':#{
639 inf
= material_info( mat
)
641 if mat
.SR_data
.shader
== 'standard' or \
642 mat
.SR_data
.shader
== 'standard_cutout' or \
643 mat
.SR_data
.shader
== 'terrain_blend' or \
644 mat
.SR_data
.shader
== 'vertex_blend':
646 if 'tex_diffuse' in inf
:
647 m
.tex_diffuse
= sr_compile_texture(inf
['tex_diffuse'])
650 sr_compile
.material_data
.extend( bytearray(m
) )
654 def sr_armature_bones( armature
):
656 def _recurse_bone( b
):
659 for c
in b
.children
: yield from _recurse_bone( c
)
662 for b
in armature
.data
.bones
:
664 yield from _recurse_bone( b
)
667 def sr_compile_mesh( obj
):
670 compile_obj_transform(obj
, node
.transform
)
671 node
.pstr_name
= sr_compile_string(obj
.name
)
672 ent_type
= obj_ent_type( obj
)
676 if ent_type
!= 'none':#{
677 ent_id_lwr
= sr_compile
.entity_ids
[obj
.name
]
678 ent_id_upr
= get_entity_enum_id( obj_ent_type(obj
) )
679 node
.entity_id
= (ent_id_upr
<< 16) | ent_id_lwr
681 print( node
.entity_id
)
686 for mod
in obj
.modifiers
:#{
687 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
688 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
691 can_use_cache
= False
694 if mod
.type == 'ARMATURE': #{
695 armature
= mod
.object
696 rig_weight_groups
= \
697 ['0 [ROOT]']+[_
.name
for _
in sr_armature_bones(mod
.object)]
698 node
.armature_id
= sr_compile
.entity_ids
[armature
.name
]
700 POSE_OR_REST_CACHE
= armature
.data
.pose_position
701 armature
.data
.pose_position
= 'REST'
705 # Check the cache first
707 if can_use_cache
and (obj
.data
.name
in sr_compile
.mesh_cache
):#{
708 ref
= sr_compile
.mesh_cache
[obj
.data
.name
]
709 node
.submesh_start
= ref
[0]
710 node
.submesh_count
= ref
[1]
711 sr_compile
.mesh_data
.extend(bytearray(node
))
715 # Compile a whole new mesh
717 node
.submesh_start
= len(sr_compile
.submesh_data
)//sizeof(mdl_submesh
)
718 node
.submesh_count
= 0
720 dgraph
= bpy
.context
.evaluated_depsgraph_get()
721 data
= obj
.evaluated_get(dgraph
).data
722 data
.calc_loop_triangles()
723 data
.calc_normals_split()
725 # Mesh is split into submeshes based on their material
727 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
728 for material_id
, mat
in enumerate(mat_list
): #{
732 sm
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
733 sm
.vertex_start
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
736 sm
.material_id
= sr_compile_material( mat
)
738 INF
=99999999.99999999
744 # Keep a reference to very very very similar vertices
745 # i have no idea how to speed it up.
747 vertex_reference
= {}
749 # Write the vertex / indice data
751 for tri_index
, tri
in enumerate(data
.loop_triangles
):#{
752 if tri
.material_index
!= material_id
: continue
755 vert
= data
.vertices
[tri
.vertices
[j
]]
757 vi
= data
.loops
[li
].vertex_index
759 # Gather vertex information
762 norm
= data
.loops
[li
].normal
764 colour
= (255,255,255,255)
771 uv
= data
.uv_layers
.active
.data
[li
].uv
775 if data
.vertex_colors
:#{
776 colour
= data
.vertex_colors
.active
.data
[li
].color
777 colour
= (int(colour
[0]*255.0),\
778 int(colour
[1]*255.0),\
779 int(colour
[2]*255.0),\
780 int(colour
[3]*255.0))
783 # Weight groups: truncates to the 3 with the most influence. The
784 # fourth bone ID is never used by the shader so it
788 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
789 if obj
.vertex_groups
[_
.group
].name
in \
792 weight_groups
= sorted( src_groups
, key
= \
793 lambda a
: a
.weight
, reverse
=True )
795 for ml
in range(3):#{
796 if len(weight_groups
) > ml
:#{
797 g
= weight_groups
[ml
]
798 name
= obj
.vertex_groups
[g
.group
].name
801 groups
[ml
] = rig_weight_groups
.index(name
)
806 if len(weight_groups
) > 0:#{
807 inv_norm
= (1.0/tot
) * 65535.0
808 for ml
in range(3):#{
809 weights
[ml
] = int( weights
[ml
] * inv_norm
)
810 weights
[ml
] = min( weights
[ml
], 65535 )
811 weights
[ml
] = max( weights
[ml
], 0 )
816 li1
= tri
.loops
[(j
+1)%3]
817 vi1
= data
.loops
[li1
].vertex_index
818 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
820 if e0
.use_freestyle_mark
and \
821 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
822 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
828 TOLERENCE
= float(10**4)
829 key
= (int(co
[0]*TOLERENCE
+0.5),
830 int(co
[1]*TOLERENCE
+0.5),
831 int(co
[2]*TOLERENCE
+0.5),
832 int(norm
[0]*TOLERENCE
+0.5),
833 int(norm
[1]*TOLERENCE
+0.5),
834 int(norm
[2]*TOLERENCE
+0.5),
835 int(uv
[0]*TOLERENCE
+0.5),
836 int(uv
[1]*TOLERENCE
+0.5),
837 colour
[0], # these guys are already quantized
850 if key
in vertex_reference
:
851 index
= vertex_reference
[key
]
853 index
= bytearray(c_uint32(sm
.vertex_count
))
856 vertex_reference
[key
] = index
866 v
.colour
[0] = colour
[0]
867 v
.colour
[1] = colour
[1]
868 v
.colour
[2] = colour
[2]
869 v
.colour
[3] = colour
[3]
870 v
.weights
[0] = weights
[0]
871 v
.weights
[1] = weights
[1]
872 v
.weights
[2] = weights
[2]
873 v
.weights
[3] = weights
[3]
874 v
.groups
[0] = groups
[0]
875 v
.groups
[1] = groups
[1]
876 v
.groups
[2] = groups
[2]
877 v
.groups
[3] = groups
[3]
880 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
881 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
884 sr_compile
.vertex_data
.extend(bytearray(v
))
888 sr_compile
.indice_data
.extend( index
)
892 # Make sure bounding box isn't -inf -> inf if no vertices
894 if sm
.vertex_count
== 0:
899 # Add submesh to encoder
901 sr_compile
.submesh_data
.extend( bytearray(sm
) )
902 node
.submesh_count
+= 1
906 armature
.data
.pose_position
= POSE_OR_REST_CACHE
909 # Save a reference to this node since we want to reuse the submesh indices
911 sr_compile
.mesh_cache
[obj
.data
.name
]=(node
.submesh_start
,node
.submesh_count
)
912 sr_compile
.mesh_data
.extend(bytearray(node
))
915 def sr_compile_fonts( collection
):
917 print( F
"[SR] Compiling fonts" )
922 for obj
in collection
.all_objects
:#{
923 if obj_ent_type(obj
) != 'ent_font': continue
925 data
= obj
.SR_data
.ent_font
[0]
928 font
.alias
= sr_compile_string( data
.alias
)
929 font
.variant_start
= variant_count
930 font
.variant_count
= 0
931 font
.glyph_start
= glyph_count
933 glyph_base
= data
.glyphs
[0].utf32
934 glyph_range
= data
.glyphs
[-1].utf32
- glyph_base
936 font
.glyph_utf32_base
= glyph_base
937 font
.glyph_count
= glyph_range
939 for i
in range(len(data
.variants
)):#{
940 data_var
= data
.variants
[i
]
941 if not data_var
.mesh
: continue
943 mesh
= data_var
.mesh
.data
945 variant
= ent_font_variant()
946 variant
.name
= sr_compile_string( data_var
.tipo
)
948 # fonts (variants) only support one material each
950 if len(mesh
.materials
) != 0:
951 mat
= mesh
.materials
[0]
952 variant
.material_id
= sr_compile_material( mat
)
954 font
.variant_count
+= 1
956 islands
= mesh_utils
.mesh_linked_triangles(mesh
)
957 centroids
= [Vector((0,0)) for _
in range(len(islands
))]
959 for j
in range(len(islands
)):#{
960 for tri
in islands
[j
]:#{
961 centroids
[j
].x
+= tri
.center
[0]
962 centroids
[j
].y
+= tri
.center
[2]
965 centroids
[j
] /= len(islands
[j
])
968 for j
in range(glyph_range
):#{
969 data_glyph
= data
.glyphs
[j
]
971 glyph
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
972 glyph
.indice_count
= 0
973 glyph
.size
[0] = data_glyph
.bounds
[2]
974 glyph
.size
[1] = data_glyph
.bounds
[3]
976 vertex_reference
= {}
978 for k
in range(len(islands
)):#{
979 if centroids
[k
].x
< data_glyph
.bounds
[0] or \
980 centroids
[k
].x
> data_glyph
.bounds
[0]+data_glyph
.bounds
[2] or\
981 centroids
[k
].y
< data_glyph
.bounds
[1] or \
982 centroids
[k
].y
> data_glyph
.bounds
[1]+data_glyph
.bounds
[3]:
987 for l
in range(len(islands
[k
])):#{
990 vert
= mesh
.vertices
[tri
.vertices
[m
]]
992 vi
= mesh
.loops
[li
].vertex_index
994 # Gather vertex information
996 co
= [vert
.co
[_
] for _
in range(3)]
997 co
[0] -= data_glyph
.bounds
[0]
998 co
[2] -= data_glyph
.bounds
[1]
999 norm
= mesh
.loops
[li
].normal
1001 if mesh
.uv_layers
: uv
= mesh
.uv_layers
.active
.data
[li
].uv
1003 TOLERENCE
= float(10**4)
1004 key
= (int(co
[0]*TOLERENCE
+0.5),
1005 int(co
[1]*TOLERENCE
+0.5),
1006 int(co
[2]*TOLERENCE
+0.5),
1007 int(norm
[0]*TOLERENCE
+0.5),
1008 int(norm
[1]*TOLERENCE
+0.5),
1009 int(norm
[2]*TOLERENCE
+0.5),
1010 int(uv
[0]*TOLERENCE
+0.5),
1011 int(uv
[1]*TOLERENCE
+0.5))
1013 if key
in vertex_reference
:
1014 index
= vertex_reference
[key
]
1016 vindex
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
1017 index
= bytearray(c_uint32(vindex
))
1018 vertex_reference
[key
] = index
1025 v
.norm
[2] = -norm
[1]
1029 sr_compile
.vertex_data
.extend(bytearray(v
))
1032 glyph
.indice_count
+= 1
1033 sr_compile
.indice_data
.extend( index
)
1037 sr_ent_push( glyph
)
1039 sr_ent_push( variant
)
1045 def sr_compile_armature( obj
):
1047 node
= mdl_armature()
1048 node
.bone_start
= len(sr_compile
.bone_data
)//sizeof(mdl_bone
)
1050 node
.anim_start
= len(sr_compile
.anim_data
)//sizeof(mdl_animation
)
1053 bones
= [_
for _
in sr_armature_bones(obj
)]
1054 bones_names
= [None]+[_
.name
for _
in bones
]
1058 if b
.use_deform
: bone
.flags
= 0x1
1059 if b
.parent
: bone
.parent
= bones_names
.index(b
.parent
.name
)
1061 bone
.collider
= int(b
.SR_data
.collider
)
1063 if bone
.collider
>0:#{
1064 bone
.hitbox
[0][0] = b
.SR_data
.collider_min
[0]
1065 bone
.hitbox
[0][1] = b
.SR_data
.collider_min
[2]
1066 bone
.hitbox
[0][2] = -b
.SR_data
.collider_max
[1]
1067 bone
.hitbox
[1][0] = b
.SR_data
.collider_max
[0]
1068 bone
.hitbox
[1][1] = b
.SR_data
.collider_max
[2]
1069 bone
.hitbox
[1][2] = -b
.SR_data
.collider_min
[1]
1072 if b
.SR_data
.cone_constraint
:#{
1074 bone
.conevx
[0] = b
.SR_data
.conevx
[0]
1075 bone
.conevx
[1] = b
.SR_data
.conevx
[2]
1076 bone
.conevx
[2] = -b
.SR_data
.conevx
[1]
1077 bone
.conevy
[0] = b
.SR_data
.conevy
[0]
1078 bone
.conevy
[1] = b
.SR_data
.conevy
[2]
1079 bone
.conevy
[2] = -b
.SR_data
.conevy
[1]
1080 bone
.coneva
[0] = b
.SR_data
.coneva
[0]
1081 bone
.coneva
[1] = b
.SR_data
.coneva
[2]
1082 bone
.coneva
[2] = -b
.SR_data
.coneva
[1]
1083 bone
.conet
= b
.SR_data
.conet
1086 bone
.co
[0] = b
.head_local
[0]
1087 bone
.co
[1] = b
.head_local
[2]
1088 bone
.co
[2] = -b
.head_local
[1]
1089 bone
.end
[0] = b
.tail_local
[0] - bone
.co
[0]
1090 bone
.end
[1] = b
.tail_local
[2] - bone
.co
[1]
1091 bone
.end
[2] = -b
.tail_local
[1] - bone
.co
[2]
1092 bone
.pstr_name
= sr_compile_string( b
.name
)
1094 for c
in obj
.pose
.bones
[b
.name
].constraints
:#{
1095 if c
.type == 'IK':#{
1097 bone
.ik_target
= bones_names
.index(c
.subtarget
)
1098 bone
.ik_pole
= bones_names
.index(c
.pole_subtarget
)
1102 node
.bone_count
+= 1
1103 sr_compile
.bone_data
.extend(bytearray(bone
))
1108 if obj
.animation_data
and sr_compile
.pack_animations
: #{
1109 # So we can restore later
1111 previous_frame
= bpy
.context
.scene
.frame_current
1112 previous_action
= obj
.animation_data
.action
1113 POSE_OR_REST_CACHE
= obj
.data
.pose_position
1114 obj
.data
.pose_position
= 'POSE'
1116 for NLALayer
in obj
.animation_data
.nla_tracks
:#{
1117 for NLAStrip
in NLALayer
.strips
:#{
1120 for a
in bpy
.data
.actions
:#{
1121 if a
.name
== NLAStrip
.name
:#{
1122 obj
.animation_data
.action
= a
1127 # Clip to NLA settings
1129 anim_start
= int(NLAStrip
.action_frame_start
)
1130 anim_end
= int(NLAStrip
.action_frame_end
)
1134 anim
= mdl_animation()
1135 anim
.pstr_name
= sr_compile_string( NLAStrip
.action
.name
)
1137 anim
.keyframe_start
= len(sr_compile
.keyframe_data
)//\
1138 sizeof(mdl_transform
)
1139 anim
.length
= anim_end
-anim_start
1142 # Export the keyframes
1143 for frame
in range(anim_start
,anim_end
):#{
1144 bpy
.context
.scene
.frame_set(frame
)
1147 pb
= obj
.pose
.bones
[rb
.name
]
1149 # relative bone matrix
1150 if rb
.parent
is not None:#{
1151 offset_mtx
= rb
.parent
.matrix_local
1152 offset_mtx
= offset_mtx
.inverted_safe() @ \
1155 inv_parent
= pb
.parent
.matrix
@ offset_mtx
1156 inv_parent
.invert_safe()
1157 fpm
= inv_parent
@ pb
.matrix
1160 bone_mtx
= rb
.matrix
.to_4x4()
1161 local_inv
= rb
.matrix_local
.inverted_safe()
1162 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
1165 loc
, rot
, sca
= fpm
.decompose()
1168 lc_m
= pb
.matrix_channel
.to_3x3()
1169 if pb
.parent
is not None:#{
1170 smtx
= pb
.parent
.matrix_channel
.to_3x3()
1171 lc_m
= smtx
.inverted() @ lc_m
1173 rq
= lc_m
.to_quaternion()
1175 kf
= mdl_transform()
1186 sr_compile
.keyframe_data
.extend(bytearray(kf
))
1192 # Add to animation buffer
1194 sr_compile
.anim_data
.extend(bytearray(anim
))
1195 node
.anim_count
+= 1
1199 print( F
"[SR] | anim( {NLAStrip.action.name} )" )
1203 # Restore context to how it was before
1205 bpy
.context
.scene
.frame_set( previous_frame
)
1206 obj
.animation_data
.action
= previous_action
1207 obj
.data
.pose_position
= POSE_OR_REST_CACHE
1210 sr_compile
.armature_data
.extend(bytearray(node
))
1213 def sr_ent_push( struct
):
1215 clase
= type(struct
).__name
__
1217 if clase
not in sr_compile
.entity_data
:#{
1218 sr_compile
.entity_data
[ clase
] = bytearray()
1219 sr_compile
.entity_info
[ clase
] = { 'size': sizeof(struct
) }
1222 index
= len(sr_compile
.entity_data
[ clase
])//sizeof(struct
)
1223 sr_compile
.entity_data
[ clase
].extend( bytearray(struct
) )
1227 def sr_array_title( arr
, name
, count
, size
, offset
):
1229 for i
in range(len(name
)):#{
1230 arr
.name
[i
] = ord(name
[i
])
1232 arr
.file_offset
= offset
1233 arr
.item_count
= count
1234 arr
.item_size
= size
1237 def sr_compile( collection
):
1239 print( F
"[SR] compiler begin ({collection.name}.mdl)" )
1242 sr_compile
.pack_textures
= collection
.SR_data
.pack_textures
1243 sr_compile
.pack_animations
= collection
.SR_data
.animations
1246 sr_compile
.string_cache
= {}
1247 sr_compile
.mesh_cache
= {}
1248 sr_compile
.material_cache
= {}
1249 sr_compile
.texture_cache
= {}
1252 sr_compile
.mesh_data
= bytearray()
1253 sr_compile
.submesh_data
= bytearray()
1254 sr_compile
.vertex_data
= bytearray()
1255 sr_compile
.indice_data
= bytearray()
1256 sr_compile
.bone_data
= bytearray()
1257 sr_compile
.material_data
= bytearray()
1258 sr_compile
.armature_data
= bytearray()
1259 sr_compile
.anim_data
= bytearray()
1260 sr_compile
.keyframe_data
= bytearray()
1261 sr_compile
.texture_data
= bytearray()
1263 # just bytes not structures
1264 sr_compile
.string_data
= bytearray()
1265 sr_compile
.pack_data
= bytearray()
1268 sr_compile
.entity_data
= {}
1269 sr_compile
.entity_info
= {}
1271 print( F
"[SR] assign entity ID's" )
1272 sr_compile
.entities
= {}
1273 sr_compile
.entity_ids
= {}
1276 for obj
in collection
.all_objects
: #{
1277 if obj
.type == 'MESH': mesh_count
+= 1
1279 ent_type
= obj_ent_type( obj
)
1280 if ent_type
== 'none': continue
1282 if ent_type
not in sr_compile
.entities
: sr_compile
.entities
[ent_type
] = []
1283 sr_compile
.entity_ids
[obj
.name
] = len( sr_compile
.entities
[ent_type
] )
1284 sr_compile
.entities
[ent_type
] += [obj
]
1287 print( F
"[SR] Compiling geometry" )
1289 for obj
in collection
.all_objects
:#{
1290 if obj
.type == 'MESH':#{
1292 print( F
'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end
='\r' )
1293 sr_compile_mesh( obj
)
1297 audio_clip_count
= 0
1299 for ent_type
, arr
in sr_compile
.entities
.items():#{
1300 print(F
"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1302 for i
in range(len(arr
)):#{
1305 print( F
"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end
='\r' )
1307 if ent_type
== 'mdl_armature': sr_compile_armature(obj
)
1308 elif ent_type
== 'ent_light': #{
1310 compile_obj_transform( obj
, light
.transform
)
1311 light
.daytime
= obj
.data
.SR_data
.daytime
1312 if obj
.data
.type == 'POINT':#{
1315 elif obj
.data
.type == 'SPOT':#{
1317 light
.angle
= obj
.data
.spot_size
*0.5
1319 light
.range = obj
.data
.cutoff_distance
1320 light
.colour
[0] = obj
.data
.color
[0]
1321 light
.colour
[1] = obj
.data
.color
[1]
1322 light
.colour
[2] = obj
.data
.color
[2]
1323 light
.colour
[3] = obj
.data
.energy
1324 sr_ent_push( light
)
1326 elif ent_type
== 'ent_gate': #{
1328 obj_data
= obj
.SR_data
.ent_gate
[0]
1329 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
1331 if obj_data
.tipo
== 'default':#{
1332 if obj_data
.target
:#{
1333 gate
.target
= sr_compile
.entity_ids
[obj_data
.target
.name
]
1337 elif obj_data
.tipo
== 'nonlocal':#{
1338 gate
.target
= sr_compile_string(obj_data
.key
)
1343 gate
.dimensions
[0] = mesh_data
.dimensions
[0]
1344 gate
.dimensions
[1] = mesh_data
.dimensions
[1]
1345 gate
.dimensions
[2] = mesh_data
.dimensions
[2]
1347 q
= [obj
.matrix_local
.to_quaternion(), (0,0,0,1)]
1348 co
= [obj
.matrix_world
@ Vector((0,0,0)), (0,0,0)]
1350 if obj_data
.target
:#{
1351 q
[1] = obj_data
.target
.matrix_local
.to_quaternion()
1352 co
[1]= obj_data
.target
.matrix_world
@ Vector((0,0,0))
1357 for x
in range(2):#{
1358 gate
.co
[x
][0] = co
[x
][0]
1359 gate
.co
[x
][1] = co
[x
][2]
1360 gate
.co
[x
][2] = -co
[x
][1]
1361 gate
.q
[x
][0] = q
[x
][1]
1362 gate
.q
[x
][1] = q
[x
][3]
1363 gate
.q
[x
][2] = -q
[x
][2]
1364 gate
.q
[x
][3] = q
[x
][0]
1369 elif ent_type
== 'ent_spawn': #{
1371 compile_obj_transform( obj
, spawn
.transform
)
1372 obj_data
= obj
.SR_data
.ent_spawn
[0]
1373 spawn
.pstr_name
= sr_compile_string( obj_data
.alias
)
1374 sr_ent_push( spawn
)
1376 elif ent_type
== 'ent_water':#{
1378 compile_obj_transform( obj
, water
.transform
)
1379 water
.max_dist
= 0.0
1380 sr_ent_push( water
)
1382 elif ent_type
== 'ent_audio':#{
1383 obj_data
= obj
.SR_data
.ent_audio
[0]
1385 compile_obj_transform( obj
, audio
.transform
)
1386 audio
.clip_start
= audio_clip_count
1387 audio
.clip_count
= len(obj_data
.files
)
1388 audio_clip_count
+= audio
.clip_count
1389 audio
.max_channels
= obj_data
.max_channels
1390 audio
.volume
= obj_data
.volume
1393 # - allow/disable doppler
1394 # - channel group tags with random colours
1395 # - transition properties
1397 if obj_data
.flag_loop
: audio
.flags |
= 0x1
1398 if obj_data
.flag_nodoppler
: audio
.flags |
= 0x2
1399 if obj_data
.flag_3d
: audio
.flags |
= 0x4
1400 if obj_data
.flag_auto
: audio
.flags |
= 0x8
1401 if obj_data
.formato
== '0': audio
.flags |
= 0x000
1402 elif obj_data
.formato
== '1': audio
.flags |
= 0x400
1403 elif obj_data
.formato
== '2': audio
.flags |
= 0x1000
1405 audio
.channel_behaviour
= int(obj_data
.channel_behaviour
)
1406 if audio
.channel_behaviour
>= 1:#{
1407 audio
.group
= obj_data
.group
1409 if audio
.channel_behaviour
== 2:#{
1410 audio
.crossfade
= obj_data
.transition_duration
1412 audio
.probability_curve
= int(obj_data
.probability_curve
)
1414 for ci
in range(audio
.clip_count
):#{
1415 entry
= obj_data
.files
[ci
]
1416 clip
= ent_audio_clip()
1417 clip
.probability
= entry
.probability
1418 if obj_data
.formato
== '2':#{
1419 sr_pack_file( clip
._anon
.file, '', vg_str_bin(entry
.path
) )
1422 clip
._anon
.file.path
= sr_compile_string( entry
.path
)
1423 clip
._anon
.file.pack_offset
= 0
1424 clip
._anon
.file.pack_size
= 0
1428 sr_ent_push( audio
)
1430 elif ent_type
== 'ent_volume':#{
1431 obj_data
= obj
.SR_data
.ent_volume
[0]
1432 volume
= ent_volume()
1433 volume
.type = int(obj_data
.subtype
)
1434 compile_obj_transform( obj
, volume
.transform
)
1436 if obj_data
.target
:#{
1437 target
= obj_data
.target
1438 volume
.target
.type = get_entity_enum_id( obj_ent_type(target
) )
1439 volume
.target
.index
= sr_compile
.entity_ids
[ target
.name
]
1444 elif ent_type
== 'ent_marker':#{
1445 marker
= ent_marker()
1446 marker
.name
= sr_compile_string( obj
.SR_data
.ent_marker
[0].alias
)
1447 compile_obj_transform( obj
, marker
.transform
)
1453 sr_compile_fonts(collection
)
1455 def _children( col
):#{
1457 for c
in col
.children
:#{
1458 yield from _children(c
)
1462 checkpoint_count
= 0
1463 pathindice_count
= 0
1466 for col
in _children(collection
):#{
1467 print( F
"Adding routes for subcollection: {col.name}" )
1472 for obj
in col
.objects
:#{
1473 if obj
.type == 'ARMATURE': pass
1475 ent_type
= obj_ent_type( obj
)
1477 if ent_type
== 'ent_gate':
1478 route_gates
+= [obj
]
1479 elif ent_type
== 'ent_route_node':#{
1480 if obj
.type == 'CURVE':#{
1481 route_curves
+= [obj
]
1484 elif ent_type
== 'ent_route':
1489 dij
= create_node_graph( route_curves
, route_gates
)
1491 for obj
in routes
:#{
1492 obj_data
= obj
.SR_data
.ent_route
[0]
1494 route
.pstr_name
= sr_compile_string( obj_data
.alias
)
1495 route
.checkpoints_start
= checkpoint_count
1496 route
.checkpoints_count
= 0
1499 route
.colour
[ci
] = obj_data
.colour
[ci
]
1500 route
.colour
[3] = 1.0
1502 compile_obj_transform( obj
, route
.transform
)
1503 checkpoints
= obj_data
.gates
1505 for i
in range(len(checkpoints
)):#{
1506 gi
= checkpoints
[i
].target
1507 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
1511 dest
= gi
.SR_data
.ent_gate
[0].target
1515 if gi
==gj
: continue # error?
1516 if not gi
or not gj
: continue
1518 checkpoint
= ent_checkpoint()
1519 checkpoint
.gate_index
= sr_compile
.entity_ids
[gate
.name
]
1520 checkpoint
.path_start
= pathindice_count
1521 checkpoint
.path_count
= 0
1523 path
= solve_graph( dij
, gi
.name
, gj
.name
)
1526 for pi
in range(len(path
)):#{
1527 pathindice
= ent_path_index()
1528 pathindice
.index
= routenode_count
+ path
[pi
]
1529 sr_ent_push( pathindice
)
1531 checkpoint
.path_count
+= 1
1532 pathindice_count
+= 1
1536 sr_ent_push( checkpoint
)
1537 route
.checkpoints_count
+= 1
1538 checkpoint_count
+= 1
1541 sr_ent_push( route
)
1544 for point
in dij
.points
:#{
1545 rn
= ent_route_node()
1548 rn
.co
[2] = -point
[1]
1552 routenode_count
+= len(dij
.points
)
1556 print( F
"[SR] Writing file" )
1558 file_array_instructions
= {}
1561 def _write_array( name
, item_size
, data
):#{
1562 nonlocal file_array_instructions
, file_offset
1564 count
= len(data
)//item_size
1565 file_array_instructions
[name
] = {'count':count
, 'size':item_size
,\
1566 'data':data
, 'offset': file_offset
}
1567 file_offset
+= len(data
)
1568 file_offset
= int_align_to( file_offset
, 8 )
1571 _write_array( 'strings', 1, sr_compile
.string_data
)
1572 _write_array( 'mdl_mesh', sizeof(mdl_mesh
), sr_compile
.mesh_data
)
1573 _write_array( 'mdl_submesh', sizeof(mdl_submesh
), sr_compile
.submesh_data
)
1574 _write_array( 'mdl_material', sizeof(mdl_material
), sr_compile
.material_data
)
1575 _write_array( 'mdl_texture', sizeof(mdl_texture
), sr_compile
.texture_data
)
1576 _write_array( 'mdl_armature', sizeof(mdl_armature
), sr_compile
.armature_data
)
1577 _write_array( 'mdl_bone', sizeof(mdl_bone
), sr_compile
.bone_data
)
1579 for name
, buffer in sr_compile
.entity_data
.items():#{
1580 _write_array( name
, sr_compile
.entity_info
[name
]['size'], buffer )
1583 _write_array( 'mdl_animation', sizeof(mdl_animation
), sr_compile
.anim_data
)
1584 _write_array( 'mdl_keyframe', sizeof(mdl_transform
),sr_compile
.keyframe_data
)
1585 _write_array( 'mdl_vert', sizeof(mdl_vert
), sr_compile
.vertex_data
)
1586 _write_array( 'mdl_indice', sizeof(c_uint32
), sr_compile
.indice_data
)
1587 _write_array( 'pack', 1, sr_compile
.pack_data
)
1589 header_size
= int_align_to( sizeof(mdl_header
), 8 )
1590 index_size
= int_align_to( sizeof(mdl_array
)*len(file_array_instructions
),8 )
1592 folder
= bpy
.path
.abspath(bpy
.context
.scene
.SR_data
.export_dir
)
1593 path
= F
"{folder}{collection.name}.mdl"
1596 fp
= open( path
, "wb" )
1597 header
= mdl_header()
1599 sr_array_title( header
.arrays
, \
1600 'index', len(file_array_instructions
), \
1601 sizeof(mdl_array
), header_size
)
1603 fp
.write( bytearray_align_to( bytearray(header
), 8 ) )
1605 print( F
'[SR] {"name":>16}| count | offset' )
1607 for name
,info
in file_array_instructions
.items():#{
1609 offset
= info
['offset'] + header_size
+ index_size
1610 sr_array_title( arr
, name
, info
['count'], info
['size'], offset
)
1611 index
.extend( bytearray(arr
) )
1613 print( F
'[SR] {name:>16}| {info["count"]: 8} '+\
1614 F
' 0x{info["offset"]:02x}' )
1616 fp
.write( bytearray_align_to( index
, 8 ) )
1617 #bytearray_print_hex( index )
1619 for name
,info
in file_array_instructions
.items():#{
1620 fp
.write( bytearray_align_to( info
['data'], 8 ) )
1625 print( '[SR] done' )
1628 class SR_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1630 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1631 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
1632 gizmos
: bpy
.props
.BoolProperty( name
="Draw Gizmos", default
=True )
1634 panel
: bpy
.props
.EnumProperty(
1638 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
1639 ('ENTITY', 'Entity', '', 'MONKEY',1),
1640 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
1645 class SR_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
1647 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
1648 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
1651 def sr_get_mirror_bone( bones
):
1653 side
= bones
.active
.name
[-1:]
1654 other_name
= bones
.active
.name
[:-1]
1655 if side
== 'L': other_name
+= 'R'
1656 elif side
== 'R': other_name
+= 'L'
1660 if b
.name
== other_name
:
1667 class SR_MIRROR_BONE_X(bpy
.types
.Operator
):
1669 bl_idname
="skaterift.mirror_bone"
1670 bl_label
="Mirror bone attributes - SkateRift"
1672 def execute(_
,context
):
1674 active_object
= context
.active_object
1675 bones
= active_object
.data
.bones
1677 b
= sr_get_mirror_bone( bones
)
1679 if not b
: return {'FINISHED'}
1681 b
.SR_data
.collider
= a
.SR_data
.collider
1683 def _v3copyflipy( a
, b
):#{
1689 _v3copyflipy( a
.SR_data
.collider_min
, b
.SR_data
.collider_min
)
1690 _v3copyflipy( a
.SR_data
.collider_max
, b
.SR_data
.collider_max
)
1691 b
.SR_data
.collider_min
[1] = -a
.SR_data
.collider_max
[1]
1692 b
.SR_data
.collider_max
[1] = -a
.SR_data
.collider_min
[1]
1694 b
.SR_data
.cone_constraint
= a
.SR_data
.cone_constraint
1696 _v3copyflipy( a
.SR_data
.conevx
, b
.SR_data
.conevy
)
1697 _v3copyflipy( a
.SR_data
.conevy
, b
.SR_data
.conevx
)
1698 _v3copyflipy( a
.SR_data
.coneva
, b
.SR_data
.coneva
)
1700 b
.SR_data
.conet
= a
.SR_data
.conet
1703 ob
= bpy
.context
.scene
.objects
[0]
1704 ob
.hide_render
= ob
.hide_render
1709 class SR_COMPILE(bpy
.types
.Operator
):
1711 bl_idname
="skaterift.compile_all"
1712 bl_label
="Compile All"
1714 def execute(_
,context
):
1716 view_layer
= bpy
.context
.view_layer
1717 for col
in view_layer
.layer_collection
.children
["export"].children
:
1718 if not col
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1719 sr_compile( bpy
.data
.collections
[col
.name
] )
1725 class SR_COMPILE_THIS(bpy
.types
.Operator
):
1727 bl_idname
="skaterift.compile_this"
1728 bl_label
="Compile This collection"
1730 def execute(_
,context
):
1732 col
= bpy
.context
.collection
1739 class SR_INTERFACE(bpy
.types
.Panel
):
1741 bl_idname
= "VIEW3D_PT_skate_rift"
1742 bl_label
= "Skate Rift"
1743 bl_space_type
= 'VIEW_3D'
1744 bl_region_type
= 'UI'
1745 bl_category
= "Skate Rift"
1747 def draw(_
, context
):
1751 row
= _
.layout
.row()
1753 row
.prop( context
.scene
.SR_data
, 'panel', expand
=True )
1755 if context
.scene
.SR_data
.panel
== 'SETTINGS': #{
1756 _
.layout
.prop( context
.scene
.SR_data
, 'gizmos' )
1758 elif context
.scene
.SR_data
.panel
== 'EXPORT': #{
1759 _
.layout
.prop( context
.scene
.SR_data
, "export_dir" )
1760 col
= bpy
.context
.collection
1762 found_in_export
= False
1764 view_layer
= bpy
.context
.view_layer
1765 for c1
in view_layer
.layer_collection
.children
["export"].children
: #{
1766 if not c1
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1769 if c1
.name
== col
.name
: #{
1770 found_in_export
= True
1774 box
= _
.layout
.box()
1776 row
.alignment
= 'CENTER'
1779 if found_in_export
: #{
1780 row
.label( text
=col
.name
+ ".mdl" )
1781 box
.prop( col
.SR_data
, "pack_textures" )
1782 box
.prop( col
.SR_data
, "animations" )
1783 box
.operator( "skaterift.compile_this" )
1787 row
.label( text
=col
.name
)
1791 row
.alignment
= 'CENTER'
1793 row
.label( text
="This collection is not in the export group" )
1796 box
= _
.layout
.box()
1799 split
= row
.split( factor
=0.3, align
=True )
1800 split
.prop( context
.scene
.SR_data
, "use_hidden", text
="hidden" )
1803 if export_count
== 0:
1805 row1
.operator( "skaterift.compile_all", \
1806 text
=F
"Compile all ({export_count} collections)" )
1808 elif context
.scene
.SR_data
.panel
== 'ENTITY': #{
1809 active_object
= context
.active_object
1810 if not active_object
: return
1812 _
.layout
.operator( 'skaterift.copy_entity_data', \
1813 text
=F
'Copy entity data to {len(context.selected_objects)-1} '+\
1816 box
= _
.layout
.box()
1818 row
.alignment
= 'CENTER'
1819 row
.label( text
=active_object
.name
)
1822 def _draw_prop_collection( data
): #{
1825 row
.alignment
= 'CENTER'
1828 row
.label( text
=F
'{data[0]}' )
1830 if hasattr(type(data
[0]),'sr_inspector'):#{
1831 type(data
[0]).sr_inspector( box
, data
)
1834 for a
in data
[0].__annotations
__:
1835 box
.prop( data
[0], a
)
1839 if active_object
.type == 'ARMATURE': #{
1840 if active_object
.mode
== 'POSE': #{
1841 bones
= active_object
.data
.bones
1842 mb
= sr_get_mirror_bone( bones
)
1844 box
.operator( "skaterift.mirror_bone", \
1845 text
=F
'Mirror attributes to {mb.name}' )
1848 _draw_prop_collection( [bones
.active
.SR_data
] )
1852 row
.alignment
='CENTER'
1855 row
.label( text
="Enter pose mode to modify bone properties" )
1858 elif active_object
.type == 'LIGHT': #{
1859 _draw_prop_collection( [active_object
.data
.SR_data
] )
1861 elif active_object
.type in ['EMPTY','CURVE','MESH']:#{
1862 box
.prop( active_object
.SR_data
, "ent_type" )
1863 ent_type
= active_object
.SR_data
.ent_type
1865 col
= getattr( active_object
.SR_data
, ent_type
, None )
1866 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1868 if active_object
.type == 'MESH':#{
1869 col
= getattr( active_object
.data
.SR_data
, ent_type
, None )
1870 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1877 class SR_MATERIAL_PANEL(bpy
.types
.Panel
):
1879 bl_label
="Skate Rift material"
1880 bl_idname
="MATERIAL_PT_sr_material"
1881 bl_space_type
='PROPERTIES'
1882 bl_region_type
='WINDOW'
1883 bl_context
="material"
1885 def draw(_
,context
):
1887 active_object
= bpy
.context
.active_object
1888 if active_object
== None: return
1889 active_mat
= active_object
.active_material
1890 if active_mat
== None: return
1892 info
= material_info( active_mat
)
1894 if 'tex_diffuse' in info
:#{
1895 _
.layout
.label( icon
='INFO', \
1896 text
=F
"{info['tex_diffuse'].name} will be compiled" )
1899 _
.layout
.prop( active_mat
.SR_data
, "shader" )
1900 _
.layout
.prop( active_mat
.SR_data
, "surface_prop" )
1901 _
.layout
.prop( active_mat
.SR_data
, "collision" )
1903 if active_mat
.SR_data
.collision
:#{
1904 box
= _
.layout
.box()
1907 if (active_mat
.SR_data
.shader
!= 'invisible') and \
1908 (active_mat
.SR_data
.shader
!= 'boundary'):#{
1909 row
.prop( active_mat
.SR_data
, "skate_surface" )
1910 row
.prop( active_mat
.SR_data
, "grind_surface" )
1911 row
.prop( active_mat
.SR_data
, "grow_grass" )
1915 if active_mat
.SR_data
.shader
== "terrain_blend":#{
1916 box
= _
.layout
.box()
1917 box
.prop( active_mat
.SR_data
, "blend_offset" )
1918 box
.prop( active_mat
.SR_data
, "sand_colour" )
1920 elif active_mat
.SR_data
.shader
== "vertex_blend":#{
1921 box
= _
.layout
.box()
1922 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
1923 box
.prop( active_mat
.SR_data
, "blend_offset" )
1925 elif active_mat
.SR_data
.shader
== "water":#{
1926 box
= _
.layout
.box()
1927 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
1928 box
.prop( active_mat
.SR_data
, "shore_colour" )
1929 box
.prop( active_mat
.SR_data
, "ocean_colour" )
1934 def sr_get_type_enum( scene
, context
):
1936 items
= [('none','None',"")]
1937 mesh_entities
=['ent_gate','ent_water']
1938 point_entities
=['ent_spawn','ent_route_node','ent_route']
1940 for e
in point_entities
: items
+= [(e
,e
,'')]
1942 if context
.scene
.SR_data
.panel
== 'ENTITY': #{
1943 if context
.active_object
.type == 'MESH': #{
1944 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1948 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1954 def sr_on_type_change( _
, context
):
1956 obj
= context
.active_object
1957 ent_type
= obj
.SR_data
.ent_type
1958 if ent_type
== 'none': return
1959 if obj
.type == 'MESH':#{
1960 col
= getattr( obj
.data
.SR_data
, ent_type
, None )
1961 if col
!= None and len(col
)==0: col
.add()
1964 col
= getattr( obj
.SR_data
, ent_type
, None )
1965 if col
!= None and len(col
)==0: col
.add()
1968 class SR_OBJECT_ENT_SPAWN(bpy
.types
.PropertyGroup
):
1970 alias
: bpy
.props
.StringProperty( name
='alias' )
1973 class SR_OBJECT_ENT_GATE(bpy
.types
.PropertyGroup
):
1975 target
: bpy
.props
.PointerProperty( \
1976 type=bpy
.types
.Object
, name
="destination", \
1977 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
1979 key
: bpy
.props
.StringProperty()
1980 tipo
: bpy
.props
.EnumProperty(items
=(('default', 'Default', ""),
1981 ('nonlocal', 'Non-Local', ""),))
1984 def sr_inspector( layout
, data
):
1987 box
.prop( data
[0], 'tipo', text
="subtype" )
1989 if data
[0].tipo
== 'default': box
.prop( data
[0], 'target' )
1990 elif data
[0].tipo
== 'nonlocal': box
.prop( data
[0], 'key' )
1994 class SR_MESH_ENT_GATE(bpy
.types
.PropertyGroup
):
1996 dimensions
: bpy
.props
.FloatVectorProperty(name
="dimensions",size
=3)
1999 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy
.types
.PropertyGroup
):
2001 target
: bpy
.props
.PointerProperty( \
2002 type=bpy
.types
.Object
, name
='target', \
2003 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_gate']))
2006 class SR_UL_ROUTE_NODE_LIST(bpy
.types
.UIList
):
2008 bl_idname
= 'SR_UL_ROUTE_NODE_LIST'
2010 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2012 layout
.prop( item
, 'target', text
='', emboss
=False )
2016 def internal_listdel_execute(self
,context
,ent_name
,collection_name
):
2018 active_object
= context
.active_object
2019 data
= getattr(active_object
.SR_data
,ent_name
)[0]
2020 lista
= getattr(data
,collection_name
)
2021 index
= getattr(data
,F
'{collection_name}_index')
2025 setattr(data
,F
'{collection_name}_index', min(max(0,index
-1), len(lista
)-1))
2029 def internal_listadd_execute(self
,context
,ent_name
,collection_name
):
2031 active_object
= context
.active_object
2032 getattr(getattr(active_object
.SR_data
,ent_name
)[0],collection_name
).add()
2036 def copy_propgroup( de
, to
):
2038 for a
in de
.__annotations
__:#{
2039 if isinstance(getattr(de
,a
), bpy
.types
.bpy_prop_collection
):#{
2043 while len(cb
) != len(ca
):#{
2044 if len(cb
) < len(ca
): cb
.add()
2047 for i
in range(len(ca
)):#{
2048 copy_propgroup(ca
[i
],cb
[i
])
2052 setattr(to
,a
,getattr(de
,a
))
2057 class SR_OT_COPY_ENTITY_DATA(bpy
.types
.Operator
):
2059 bl_idname
= "skaterift.copy_entity_data"
2060 bl_label
= "Copy entity data"
2062 def execute(self
, context
):#{
2063 data
= context
.active_object
.SR_data
2064 new_type
= data
.ent_type
2065 print( F
"Copy entity data from: {context.active_object.name}" )
2067 for obj
in context
.selected_objects
:#{
2068 if obj
!= context
.active_object
:#{
2069 print( F
" To: {obj.name}" )
2071 obj
.SR_data
.ent_type
= new_type
2073 if active_object
.type == 'MESH':#{
2074 col
= getattr( obj
.data
.SR_data
, new_type
, None )
2075 if col
!= None and len(col
)==0: col
.add()
2076 mdata
= context
.active_object
.data
.SR_data
2077 copy_propgroup( getattr(mdata
,new_type
)[0], col
[0] )
2080 col
= getattr( obj
.SR_data
, new_type
, None )
2081 if col
!= None and len(col
)==0: col
.add()
2082 copy_propgroup( getattr(data
,new_type
)[0], col
[0] )
2089 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy
.types
.Operator
):
2091 bl_idname
= "skaterift.new_entry"
2092 bl_label
= "Add gate"
2094 def execute(self
, context
):#{
2095 return internal_listadd_execute(self
,context
,'ent_route','gates')
2099 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy
.types
.Operator
):
2101 bl_idname
= "skaterift.del_entry"
2102 bl_label
= "Remove gate"
2105 def poll(cls
, context
):#{
2106 active_object
= context
.active_object
2107 if obj_ent_type(active_object
) == 'ent_route':#{
2108 return active_object
.SR_data
.ent_route
[0].gates
2113 def execute(self
, context
):#{
2114 return internal_listdel_execute(self
,context
,'ent_route','gates')
2118 class SR_OT_AUDIO_LIST_NEW_ITEM(bpy
.types
.Operator
):
2120 bl_idname
= "skaterift.al_new_entry"
2121 bl_label
= "Add file"
2123 def execute(self
, context
):#{
2124 return internal_listadd_execute(self
,context
,'ent_audio','files')
2128 class SR_OT_AUDIO_LIST_DEL_ITEM(bpy
.types
.Operator
):
2130 bl_idname
= "skaterift.al_del_entry"
2131 bl_label
= "Remove file"
2134 def poll(cls
, context
):#{
2135 active_object
= context
.active_object
2136 if obj_ent_type(active_object
) == 'ent_audio':#{
2137 return active_object
.SR_data
.ent_audio
[0].files
2142 def execute(self
, context
):#{
2143 return internal_listdel_execute(self
,context
,'ent_audio','files')
2148 class SR_OT_GLYPH_LIST_NEW_ITEM(bpy
.types
.Operator
):
2150 bl_idname
= "skaterift.gl_new_entry"
2151 bl_label
= "Add glyph"
2153 def execute(self
, context
):#{
2154 active_object
= context
.active_object
2156 font
= active_object
.SR_data
.ent_font
[0]
2159 if len(font
.glyphs
) > 1:#{
2160 prev
= font
.glyphs
[-2]
2161 cur
= font
.glyphs
[-1]
2163 cur
.bounds
= prev
.bounds
2164 cur
.utf32
= prev
.utf32
+1
2171 class SR_OT_GLYPH_LIST_DEL_ITEM(bpy
.types
.Operator
):
2173 bl_idname
= "skaterift.gl_del_entry"
2174 bl_label
= "Remove Glyph"
2177 def poll(cls
, context
):#{
2178 active_object
= context
.active_object
2179 if obj_ent_type(active_object
) == 'ent_font':#{
2180 return active_object
.SR_data
.ent_font
[0].glyphs
2185 def execute(self
, context
):#{
2186 return internal_listdel_execute(self
,context
,'ent_font','glyphs')
2190 class SR_OT_GLYPH_LIST_MOVE_ITEM(bpy
.types
.Operator
):
2192 bl_idname
= "skaterift.gl_move_item"
2194 direction
: bpy
.props
.EnumProperty(items
=(('UP', 'Up', ""),
2195 ('DOWN', 'Down', ""),))
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].glyphs
2206 def execute(_
, context
):#{
2207 active_object
= context
.active_object
2208 data
= active_object
.SR_data
.ent_font
[0]
2210 index
= data
.glyphs_index
2211 neighbor
= index
+ (-1 if _
.direction
== 'UP' else 1)
2212 data
.glyphs
.move( neighbor
, index
)
2214 list_length
= len(data
.glyphs
) - 1
2215 new_index
= index
+ (-1 if _
.direction
== 'UP' else 1)
2217 data
.glyphs_index
= max(0, min(new_index
, list_length
))
2223 class SR_OT_FONT_VARIANT_LIST_NEW_ITEM(bpy
.types
.Operator
):
2225 bl_idname
= "skaterift.fv_new_entry"
2226 bl_label
= "Add variant"
2228 def execute(self
, context
):#{
2229 return internal_listadd_execute(self
,context
,'ent_font','variants')
2233 class SR_OT_FONT_VARIANT_LIST_DEL_ITEM(bpy
.types
.Operator
):
2235 bl_idname
= "skaterift.fv_del_entry"
2236 bl_label
= "Remove variant"
2239 def poll(cls
, context
):#{
2240 active_object
= context
.active_object
2241 if obj_ent_type(active_object
) == 'ent_font':#{
2242 return active_object
.SR_data
.ent_font
[0].variants
2247 def execute(self
, context
):#{
2248 return internal_listdel_execute(self
,context
,'ent_font','variants')
2252 class SR_OBJECT_ENT_AUDIO_FILE_ENTRY(bpy
.types
.PropertyGroup
):
2254 path
: bpy
.props
.StringProperty( name
="Path" )
2255 probability
: bpy
.props
.FloatProperty( name
="Probability",default
=100.0 )
2258 class SR_UL_AUDIO_LIST(bpy
.types
.UIList
):
2260 bl_idname
= 'SR_UL_AUDIO_LIST'
2262 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2264 split
= layout
.split(factor
=0.7)
2266 c
.prop( item
, 'path', text
='', emboss
=False )
2268 c
.prop( item
, 'probability', text
='%', emboss
=True )
2272 class SR_UL_FONT_VARIANT_LIST(bpy
.types
.UIList
):
2274 bl_idname
= 'SR_UL_FONT_VARIANT_LIST'
2276 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2278 layout
.prop( item
, 'mesh', emboss
=False )
2279 layout
.prop( item
, 'tipo' )
2283 class SR_UL_FONT_GLYPH_LIST(bpy
.types
.UIList
):
2285 bl_idname
= 'SR_UL_FONT_GLYPH_LIST'
2287 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
2289 s0
= layout
.split(factor
=0.3)
2291 s1
= c
.split(factor
=0.3)
2294 lbl
= chr(item
.utf32
) if item
.utf32
>= 32 and item
.utf32
<= 126 else 'ERR'
2297 c
.prop( item
, 'utf32', text
='', emboss
=True )
2300 row
.prop( item
, 'bounds', text
='', emboss
=False )
2304 class SR_OBJECT_ENT_ROUTE(bpy
.types
.PropertyGroup
):
2306 gates
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY
)
2307 gates_index
: bpy
.props
.IntProperty()
2309 colour
: bpy
.props
.FloatVectorProperty( \
2313 default
=Vector((0.79,0.63,0.48)),\
2314 description
="Route colour"\
2317 alias
: bpy
.props
.StringProperty(\
2319 default
="Untitled Course")
2322 def sr_inspector( layout
, data
):
2324 layout
.prop( data
[0], 'alias' )
2325 layout
.prop( data
[0], 'colour' )
2327 layout
.label( text
='Checkpoints' )
2328 layout
.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
2329 data
[0], 'gates', data
[0], 'gates_index', rows
=5)
2332 row
.operator( 'skaterift.new_entry', text
='Add' )
2333 row
.operator( 'skaterift.del_entry', text
='Remove' )
2337 class SR_OBJECT_ENT_VOLUME(bpy
.types
.PropertyGroup
):
2339 subtype
: bpy
.props
.EnumProperty(
2341 items
=[('0','Trigger',''),
2342 ('1','Particles (0.1s)','')]
2345 target
: bpy
.props
.PointerProperty( \
2346 type=bpy
.types
.Object
, name
="Target", \
2347 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_audio']))
2350 def sr_inspector( layout
, data
):
2353 layout
.prop( data
, 'subtype' )
2354 layout
.prop( data
, 'target' )
2358 class SR_OBJECT_ENT_AUDIO(bpy
.types
.PropertyGroup
):
2360 files
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO_FILE_ENTRY
)
2361 files_index
: bpy
.props
.IntProperty()
2363 flag_3d
: bpy
.props
.BoolProperty( name
="3D audio",default
=True )
2364 flag_loop
: bpy
.props
.BoolProperty( name
="Loop",default
=False )
2365 flag_auto
: bpy
.props
.BoolProperty( name
="Play at start",default
=False )
2366 flag_nodoppler
: bpy
.props
.BoolProperty( name
="No Doppler",default
=False )
2368 group
: bpy
.props
.IntProperty( name
="Group ID", default
=0 )
2369 formato
: bpy
.props
.EnumProperty(
2371 items
=[('0','Uncompressed Mono',''),
2372 ('1','Compressed Vorbis',''),
2373 ('2','[vg] Bird Synthesis','')]
2375 probability_curve
: bpy
.props
.EnumProperty(
2376 name
="Probability Curve",
2377 items
=[('0','Constant',''),
2378 ('1','Wildlife Daytime',''),
2379 ('2','Wildlife Nighttime','')])
2380 channel_behaviour
: bpy
.props
.EnumProperty(
2381 name
="Channel Behaviour",
2382 items
=[('0','Unlimited',''),
2383 ('1','Discard if group full', ''),
2384 ('2','Crossfade if group full','')])
2386 transition_duration
: bpy
.props
.FloatProperty(name
="Transition Time",\
2389 max_channels
: bpy
.props
.IntProperty( name
="Max Channels", default
=1 )
2390 volume
: bpy
.props
.FloatProperty( name
="Volume",default
=1.0 )
2393 def sr_inspector( layout
, data
):
2395 layout
.prop( data
[0], 'formato' )
2396 layout
.prop( data
[0], 'volume' )
2399 box
.label( text
='Channels' )
2400 split
= box
.split(factor
=0.3)
2402 c
.prop( data
[0], 'max_channels' )
2404 c
.prop( data
[0], 'channel_behaviour', text
='Behaviour' )
2405 if data
[0].channel_behaviour
>= '1':
2406 box
.prop( data
[0], 'group' )
2407 if data
[0].channel_behaviour
== '2':
2408 box
.prop( data
[0], 'transition_duration' )
2411 box
.label( text
='Flags' )
2412 box
.prop( data
[0], 'flag_3d' )
2413 if data
[0].flag_3d
: box
.prop( data
[0], 'flag_nodoppler' )
2415 box
.prop( data
[0], 'flag_loop' )
2416 box
.prop( data
[0], 'flag_auto' )
2418 split
= layout
.split(factor
=0.7)
2420 c
.label( text
='Filepath' )
2422 c
.label( text
='Chance (0.1s)' )
2424 layout
.prop( data
[0], 'probability_curve' )
2426 layout
.template_list('SR_UL_AUDIO_LIST', 'Files', \
2427 data
[0], 'files', data
[0], 'file_index', rows
=5)
2430 row
.operator( 'skaterift.al_new_entry', text
='Add' )
2431 row
.operator( 'skaterift.al_del_entry', text
='Remove' )
2435 class SR_OBJECT_ENT_MARKER(bpy
.types
.PropertyGroup
):
2437 alias
: bpy
.props
.StringProperty()
2440 class SR_OBJECT_ENT_GLYPH(bpy
.types
.PropertyGroup
):
2442 mini
: bpy
.props
.FloatVectorProperty(size
=2)
2443 maxi
: bpy
.props
.FloatVectorProperty(size
=2)
2444 utf32
: bpy
.props
.IntProperty()
2447 class SR_OBJECT_ENT_GLYPH_ENTRY(bpy
.types
.PropertyGroup
):
2449 bounds
: bpy
.props
.FloatVectorProperty(size
=4,subtype
='NONE')
2450 utf32
: bpy
.props
.IntProperty()
2453 class SR_OBJECT_ENT_FONT_VARIANT(bpy
.types
.PropertyGroup
):
2455 mesh
: bpy
.props
.PointerProperty(type=bpy
.types
.Object
)
2456 tipo
: bpy
.props
.StringProperty()
2459 class SR_OBJECT_ENT_FONT(bpy
.types
.PropertyGroup
):
2461 variants
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT_VARIANT
)
2462 glyphs
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH_ENTRY
)
2463 alias
: bpy
.props
.StringProperty()
2465 glyphs_index
: bpy
.props
.IntProperty()
2466 variants_index
: bpy
.props
.IntProperty()
2469 def sr_inspector( layout
, data
):
2471 layout
.prop( data
[0], 'alias' )
2473 layout
.label( text
='Variants' )
2474 layout
.template_list('SR_UL_FONT_VARIANT_LIST', 'Variants', \
2475 data
[0], 'variants', data
[0], 'variants_index',\
2478 row
.operator( 'skaterift.fv_new_entry', text
='Add' )
2479 row
.operator( 'skaterift.fv_del_entry', text
='Remove' )
2481 layout
.label( text
='ASCII Glyphs' )
2482 layout
.template_list('SR_UL_FONT_GLYPH_LIST', 'Glyphs', \
2483 data
[0], 'glyphs', data
[0], 'glyphs_index', rows
=5)
2486 row
.operator( 'skaterift.gl_new_entry', text
='Add' )
2487 row
.operator( 'skaterift.gl_del_entry', text
='Remove' )
2488 row
.operator( 'skaterift.gl_move_item', text
='^' ).direction
='UP'
2489 row
.operator( 'skaterift.gl_move_item', text
='v' ).direction
='DOWN'
2493 class SR_OBJECT_ENT_TRAFFIC(bpy
.types
.PropertyGroup
):
2495 track
: bpy
.props
.PointerProperty(\
2496 type=bpy
.types
.Object
, name
='track', \
2497 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,['ent_route_node']))
2500 class SR_OBJECT_PROPERTIES(bpy
.types
.PropertyGroup
):
2502 ent_gate
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GATE
)
2503 ent_spawn
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_SPAWN
)
2504 ent_route
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE
)
2505 ent_volume
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_VOLUME
)
2506 ent_audio
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_AUDIO
)
2507 ent_marker
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_MARKER
)
2508 ent_glyph
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GLYPH
)
2509 ent_font
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_FONT
)
2510 ent_traffic
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_TRAFFIC
)
2511 ent_type
: bpy
.props
.EnumProperty(
2513 items
=sr_entity_list
,
2514 update
=sr_on_type_change
2518 class SR_MESH_PROPERTIES(bpy
.types
.PropertyGroup
):
2520 ent_gate
: bpy
.props
.CollectionProperty(type=SR_MESH_ENT_GATE
)
2523 class SR_LIGHT_PROPERTIES(bpy
.types
.PropertyGroup
):
2525 daytime
: bpy
.props
.BoolProperty( name
='Daytime' )
2528 class SR_BONE_PROPERTIES(bpy
.types
.PropertyGroup
):
2530 collider
: bpy
.props
.EnumProperty( name
='Collider Type',
2531 items
=[('0','none',''),
2533 ('2','capsule','')])
2535 collider_min
: bpy
.props
.FloatVectorProperty( name
='Collider Min', size
=3 )
2536 collider_max
: bpy
.props
.FloatVectorProperty( name
='Collider Max', size
=3 )
2538 cone_constraint
: bpy
.props
.BoolProperty( name
='Cone constraint' )
2540 conevx
: bpy
.props
.FloatVectorProperty( name
='vx' )
2541 conevy
: bpy
.props
.FloatVectorProperty( name
='vy' )
2542 coneva
: bpy
.props
.FloatVectorProperty( name
='va' )
2543 conet
: bpy
.props
.FloatProperty( name
='t' )
2546 def sr_inspector( layout
, data
):
2550 box
.prop( data
, 'collider' )
2552 if int(data
.collider
)>0:#{
2554 row
.prop( data
, 'collider_min' )
2556 row
.prop( data
, 'collider_max' )
2560 box
.prop( data
, 'cone_constraint' )
2561 if data
.cone_constraint
:#{
2563 row
.prop( data
, 'conevx' )
2565 row
.prop( data
, 'conevy' )
2567 row
.prop( data
, 'coneva' )
2568 box
.prop( data
, 'conet' )
2573 class SR_MATERIAL_PROPERTIES(bpy
.types
.PropertyGroup
):
2575 shader
: bpy
.props
.EnumProperty(
2578 ('standard',"standard",''),
2579 ('standard_cutout', "standard_cutout", ''),
2580 ('terrain_blend', "terrain_blend", ''),
2581 ('vertex_blend', "vertex_blend", ''),
2582 ('water',"water",''),
2583 ('invisible','Invisible',''),
2584 ('boundary','Boundary','')
2587 surface_prop
: bpy
.props
.EnumProperty(
2588 name
="Surface Property",
2590 ('0','concrete',''),
2597 collision
: bpy
.props
.BoolProperty( \
2598 name
="Collisions Enabled",\
2600 description
= "Can the player collide with this material?"\
2602 skate_surface
: bpy
.props
.BoolProperty( \
2603 name
="Skate Target", \
2605 description
= "Should the game try to target this surface?" \
2607 grind_surface
: bpy
.props
.BoolProperty( \
2610 description
= "Can you grind on this surface?" \
2612 grow_grass
: bpy
.props
.BoolProperty( \
2613 name
="Grow Grass", \
2615 description
= "Spawn grass sprites on this surface?" \
2617 blend_offset
: bpy
.props
.FloatVectorProperty( \
2618 name
="Blend Offset", \
2620 default
=Vector((0.5,0.0)),\
2621 description
="When surface is more than 45 degrees, add this vector " +\
2624 sand_colour
: bpy
.props
.FloatVectorProperty( \
2625 name
="Sand Colour",\
2628 default
=Vector((0.79,0.63,0.48)),\
2629 description
="Blend to this colour near the 0 coordinate on UP axis"\
2631 shore_colour
: bpy
.props
.FloatVectorProperty( \
2632 name
="Shore Colour",\
2635 default
=Vector((0.03,0.32,0.61)),\
2636 description
="Water colour at the shoreline"\
2638 ocean_colour
: bpy
.props
.FloatVectorProperty( \
2639 name
="Ocean Colour",\
2642 default
=Vector((0.0,0.006,0.03)),\
2643 description
="Water colour in the deep bits"\
2647 # ---------------------------------------------------------------------------- #
2651 # ---------------------------------------------------------------------------- #
2653 cv_view_draw_handler
= None
2654 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
2656 cv_view_colours
= []
2657 cv_view_course_i
= 0
2659 # Draw axis alligned sphere at position with radius
2661 def cv_draw_sphere( pos
, radius
, colour
):
2663 global cv_view_verts
, cv_view_colours
2665 ly
= pos
+ Vector((0,0,radius
))
2666 lx
= pos
+ Vector((0,radius
,0))
2667 lz
= pos
+ Vector((0,0,radius
))
2669 pi
= 3.14159265358979323846264
2671 for i
in range(16):#{
2672 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
2676 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
2677 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
2678 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
2680 cv_view_verts
+= [ px
, lx
]
2681 cv_view_verts
+= [ py
, ly
]
2682 cv_view_verts
+= [ pz
, lz
]
2684 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2693 # Draw axis alligned sphere at position with radius
2695 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
2697 global cv_view_verts
, cv_view_colours
2699 ly
= pos
+ tz
*radius
2700 lx
= pos
+ ty
*radius
2701 lz
= pos
+ tz
*radius
2703 pi
= 3.14159265358979323846264
2705 for i
in range(16):#{
2706 t
= ((i
+1.0) * 1.0/16.0) * pi
2710 s1
= math
.sin(t
*2.0)
2711 c1
= math
.cos(t
*2.0)
2713 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
2714 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
2715 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
2717 cv_view_verts
+= [ px
, lx
]
2718 cv_view_verts
+= [ py
, ly
]
2719 cv_view_verts
+= [ pz
, lz
]
2721 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
2730 # Draw transformed -1 -> 1 cube
2732 def cv_draw_ucube( transform
, colour
, s
=Vector((1,1,1)), o
=Vector((0,0,0)) ):
2734 global cv_view_verts
, cv_view_colours
2740 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
2741 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
2742 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
2743 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
2744 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
2745 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
2746 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
2747 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
2749 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2750 (0,4),(1,5),(2,6),(3,7)]
2755 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2756 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2757 cv_view_colours
+= [colour
, colour
]
2762 # Draw line with colour
2764 def cv_draw_line( p0
, p1
, colour
):
2766 global cv_view_verts
, cv_view_colours
2768 cv_view_verts
+= [p0
,p1
]
2769 cv_view_colours
+= [colour
, colour
]
2773 # Draw line with colour(s)
2775 def cv_draw_line2( p0
, p1
, c0
, c1
):
2777 global cv_view_verts
, cv_view_colours
2779 cv_view_verts
+= [p0
,p1
]
2780 cv_view_colours
+= [c0
,c1
]
2786 def cv_tangent_basis( n
, tx
, ty
):
2788 if abs( n
[0] ) >= 0.57735027:#{
2807 # Draw coloured arrow
2809 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
2811 global cv_view_verts
, cv_view_colours
2817 tx
= Vector((1,0,0))
2818 ty
= Vector((1,0,0))
2819 cv_tangent_basis( n
, tx
, ty
)
2821 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
2822 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
2826 def cv_draw_line_dotted( p0
, p1
, c0
, dots
=10 ):
2828 global cv_view_verts
, cv_view_colours
2830 for i
in range(dots
):#{
2834 p2
= p0
*(1.0-t0
)+p1
*t0
2835 p3
= p0
*(1.0-t1
)+p1
*t1
2837 cv_view_verts
+= [p2
,p3
]
2838 cv_view_colours
+= [c0
,c0
]
2843 # Drawhandles of a bezier control point
2845 def cv_draw_bhandle( obj
, direction
, colour
):
2847 global cv_view_verts
, cv_view_colours
2850 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2852 cv_view_verts
+= [p0
]
2853 cv_view_verts
+= [h0
]
2854 cv_view_colours
+= [colour
,colour
]
2858 # Draw a bezier curve (at fixed resolution 10)
2860 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2862 global cv_view_verts
, cv_view_colours
2865 for i
in range(10):#{
2871 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2873 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2874 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2875 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2882 # I think this one extends the handles of the bezier otwards......
2884 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2886 global cv_view_course_i
2888 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2890 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2891 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2892 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2893 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2895 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2899 # Flush the lines buffers. This is called often because god help you if you want
2900 # to do fixed, fast buffers in this catastrophic programming language.
2902 def cv_draw_lines():
2904 global cv_view_shader
, cv_view_verts
, cv_view_colours
2906 if len(cv_view_verts
) < 2:
2909 lines
= batch_for_shader(\
2910 cv_view_shader
, 'LINES', \
2911 { "pos":cv_view_verts
, "color":cv_view_colours
})
2913 lines
.draw( cv_view_shader
)
2916 cv_view_colours
= []
2919 # I dont remember what this does exactly
2921 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2923 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2926 # Semi circle to show the limit. and some lines
2928 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2930 global cv_view_verts
, cv_view_colours
2935 for x
in range(16):#{
2938 a0
= amin
*(1.0-t0
)+amax
*t0
2939 a1
= amin
*(1.0-t1
)+amax
*t1
2941 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2942 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2944 p0
=obj
.matrix_world
@ p0
2945 p1
=obj
.matrix_world
@ p1
2946 cv_view_verts
+= [p0
,p1
]
2947 cv_view_colours
+= [colour
,colour
]
2950 cv_view_verts
+= [p0
,center
]
2951 cv_view_colours
+= [colour
,colour
]
2954 cv_view_verts
+= [p1
,center
]
2955 cv_view_colours
+= [colour
,colour
]
2959 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2960 cv_view_colours
+= [colour
,colour
]
2965 # Cone and twist limit
2967 def draw_cone_twist( center
, vx
, vy
, va
):
2969 global cv_view_verts
, cv_view_colours
2970 axis
= vy
.cross( vx
)
2975 cv_view_verts
+= [center
, center
+va
*size
]
2976 cv_view_colours
+= [ (1,1,1), (1,1,1) ]
2978 for x
in range(32):#{
2979 t0
= (x
/32) * math
.tau
2980 t1
= ((x
+1)/32) * math
.tau
2987 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
2988 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
2990 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
2991 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
2993 cv_view_verts
+= [center
, p0
, p0
, p1
]
2994 cv_view_colours
+= [ (0,0,0), col0
, col0
, col1
]
3000 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
3002 def draw_skeleton_helpers( obj
):
3004 global cv_view_verts
, cv_view_colours
3006 if obj
.data
.pose_position
!= 'REST':#{
3010 for bone
in obj
.data
.bones
:#{
3012 a
= Vector((bone
.SR_data
.collider_min
[0],
3013 bone
.SR_data
.collider_min
[1],
3014 bone
.SR_data
.collider_min
[2]))
3015 b
= Vector((bone
.SR_data
.collider_max
[0],
3016 bone
.SR_data
.collider_max
[1],
3017 bone
.SR_data
.collider_max
[2]))
3019 if bone
.SR_data
.collider
== '1':#{
3021 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
3022 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
3023 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
3024 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
3025 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
3026 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
3027 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
3028 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
3030 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
3031 (0,4),(1,5),(2,6),(3,7)]
3037 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
3038 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
3039 cv_view_colours
+= [(0.5,0.5,0.5),(0.5,0.5,0.5)]
3042 elif bone
.SR_data
.collider
== '2':#{
3047 for i
in range(3):#{
3048 if abs(v0
[i
]) > largest
:#{
3049 largest
= abs(v0
[i
])
3054 v1
= Vector((0,0,0))
3055 v1
[major_axis
] = 1.0
3057 tx
= Vector((0,0,0))
3058 ty
= Vector((0,0,0))
3060 cv_tangent_basis( v1
, tx
, ty
)
3061 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
3062 l
= v0
[ major_axis
] - r
*2
3064 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
3065 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
3067 colour
= [0.2,0.2,0.2]
3068 colour
[major_axis
] = 0.5
3070 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
3071 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
3072 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
3073 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
3074 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
3075 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
3081 center
= obj
.matrix_world
@ c
3082 if bone
.SR_data
.cone_constraint
:#{
3083 vx
= Vector([bone
.SR_data
.conevx
[_
] for _
in range(3)])
3084 vy
= Vector([bone
.SR_data
.conevy
[_
] for _
in range(3)])
3085 va
= Vector([bone
.SR_data
.coneva
[_
] for _
in range(3)])
3086 draw_cone_twist( center
, vx
, vy
, va
)
3091 def cv_ent_gate( obj
):
3093 global cv_view_verts
, cv_view_colours
3095 if obj
.type != 'MESH': return
3097 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
3098 data
= obj
.SR_data
.ent_gate
[0]
3099 dims
= mesh_data
.dimensions
3102 c
= Vector((0,0,dims
[2]))
3104 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
3105 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
3106 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
3107 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
3108 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
3109 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
3110 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
3111 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
3112 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
3114 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
3116 r3d
= bpy
.context
.area
.spaces
.active
.region_3d
3118 p0
= r3d
.view_matrix
.inverted().translation
3119 v0
= (obj
.matrix_world
@Vector((0,0,0))) - p0
3120 v1
= obj
.matrix_world
.to_3x3() @ Vector((0,1,0))
3122 if v0
.dot(v1
) > 0.0: cc
= (0,1,0)
3128 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
3129 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
3130 cv_view_colours
+= [cc
,cc
]
3134 if data
.target
!= None:
3135 cv_draw_arrow( obj
.location
, data
.target
.location
, sw
)
3138 def cv_ent_volume( obj
):
3140 global cv_view_verts
, cv_view_colours
3142 data
= obj
.SR_data
.ent_volume
[0]
3144 if data
.subtype
== '0':#{
3145 cv_draw_ucube( obj
.matrix_world
, (0,1,0) )
3148 cv_draw_line( obj
.location
, data
.target
.location
, (0,1,0) )
3151 elif data
.subtype
== '1':#{
3152 cv_draw_ucube( obj
.matrix_world
, (1,1,0) )
3155 cv_draw_line( obj
.location
, data
.target
.location
, (1,1,0) )
3160 def dijkstra( graph
, start_node
, target_node
):
3162 unvisited
= [_
for _
in graph
]
3167 shortest_path
[n
] = 9999999.999999
3168 shortest_path
[start_node
] = 0
3171 current_min_node
= None
3172 for n
in unvisited
:#{
3173 if current_min_node
== None:
3174 current_min_node
= n
3175 elif shortest_path
[n
] < shortest_path
[current_min_node
]:
3176 current_min_node
= n
3179 for branch
in graph
[current_min_node
]:#{
3180 tentative_value
= shortest_path
[current_min_node
]
3181 tentative_value
+= graph
[current_min_node
][branch
]
3182 if tentative_value
< shortest_path
[branch
]:#{
3183 shortest_path
[branch
] = tentative_value
3184 previous_nodes
[branch
] = current_min_node
3188 unvisited
.remove(current_min_node
)
3193 while node
!= start_node
:#{
3196 if node
not in previous_nodes
: return None
3197 node
= previous_nodes
[node
]
3200 # Add the start node manually
3201 path
.append(start_node
)
3207 def __init__(_
,points
,graph
,subsections
):#{
3210 _
.subsections
= subsections
3214 def create_node_graph( curves
, gates
):
3216 # add endpoints of curves
3223 for c
in range(len(curves
)):#{
3224 for s
in range(len(curves
[c
].data
.splines
)):#{
3225 spline
= curves
[c
].data
.splines
[s
]
3226 l
= len(spline
.points
)
3229 dist
= round(spline
.calc_length(),2)
3232 ib
= point_count
+l
-1
3234 graph
[ia
] = { ib
: dist
}
3235 graph
[ib
] = { ia
: dist
}
3237 for i
in range(len(spline
.points
)):#{
3238 wco
= curves
[c
].matrix_world
@ spline
.points
[i
].co
3239 route_points
.append(Vector((wco
[0],wco
[1],wco
[2]+0.5)))
3244 if i
== 0: previous
= -1
3245 if i
== len(spline
.points
)-1: proxima
= -1
3247 subsections
.append((spline_count
,previous
,proxima
))
3256 graph_keys
= list(graph
)
3257 for i
in range(len(graph_keys
)-1):#{
3258 for j
in range(i
+1, len(graph_keys
)):#{
3259 if i
%2==0 and i
+1==j
: continue
3263 pi
= route_points
[ni
]
3264 pj
= route_points
[nj
]
3266 dist
= round((pj
-pi
).magnitude
,2)
3269 graph
[ni
][nj
] = dist
3270 graph
[nj
][ni
] = dist
3275 # add and link gates( by name )
3276 for gate
in gates
:#{
3277 v1
= gate
.matrix_world
.to_3x3() @ Vector((0,1,0))
3278 if gate
.SR_data
.ent_gate
[0].target
:
3281 graph
[ gate
.name
] = {}
3283 for i
in range(len(graph_keys
)):#{
3285 pi
= route_points
[ni
]
3287 v0
= pi
-gate
.location
3288 if v0
.dot(v1
) < 0.0: continue
3290 dist
= round(v0
.magnitude
,2)
3293 graph
[ gate
.name
][ ni
] = dist
3294 graph
[ ni
][ gate
.name
] = dist
3299 return dij_graph(route_points
,graph
,subsections
)
3302 def solve_graph( dij
, start
, end
):
3304 path
= dijkstra( dij
.graph
, end
, start
)
3308 for sj
in range(1,len(path
)-2):#{
3311 map0
= dij
.subsections
[i0
]
3312 map1
= dij
.subsections
[i1
]
3314 if map0
[0] == map1
[0]:#{
3315 if map0
[1] == -1: direction
= 2
3320 map0
= dij
.subsections
[i0
]
3321 i1
= map0
[direction
]
3335 full
.append( path
[-2] )
3340 def cv_draw_route( route
, dij
):
3342 pole
= Vector((0.2,0.2,10))
3343 hat
= Vector((1,8,0.2))
3344 cc
= (route
.SR_data
.ent_route
[0].colour
[0],
3345 route
.SR_data
.ent_route
[0].colour
[1],
3346 route
.SR_data
.ent_route
[0].colour
[2])
3348 cv_draw_ucube(route
.matrix_world
,cc
,Vector((0.5,-7.5,6)),\
3349 Vector((0,-6.5,5.5)))
3350 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5, 0.5,0)) )
3351 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5,-13.5,0)) )
3352 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5, 12)) )
3353 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5,-1)) )
3355 checkpoints
= route
.SR_data
.ent_route
[0].gates
3357 for i
in range(len(checkpoints
)):#{
3358 gi
= checkpoints
[i
].target
3359 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
3362 dest
= gi
.SR_data
.ent_gate
[0].target
3364 cv_draw_line_dotted( gi
.location
, dest
.location
, cc
)
3368 if gi
==gj
: continue # error?
3369 if not gi
or not gj
: continue
3371 path
= solve_graph( dij
, gi
.name
, gj
.name
)
3374 cv_draw_arrow(gi
.location
,dij
.points
[path
[0]],cc
,1.5)
3375 cv_draw_arrow(dij
.points
[path
[len(path
)-1]],gj
.location
,cc
,1.5)
3376 for j
in range(len(path
)-1):#{
3379 o0
= dij
.points
[ i0
]
3380 o1
= dij
.points
[ i1
]
3381 cv_draw_arrow(o0
,o1
,cc
,1.5)
3385 cv_draw_line_dotted( gi
.location
, gj
.location
, cc
)
3392 global cv_view_shader
3393 global cv_view_verts
3394 global cv_view_colours
3395 global cv_view_course_i
3397 cv_view_course_i
= 0
3399 cv_view_colours
= []
3401 cv_view_shader
.bind()
3402 gpu
.state
.depth_mask_set(False)
3403 gpu
.state
.line_width_set(2.0)
3404 gpu
.state
.face_culling_set('BACK')
3405 gpu
.state
.depth_test_set('LESS')
3406 gpu
.state
.blend_set('NONE')
3412 for obj
in bpy
.context
.collection
.objects
:#{
3413 if obj
.type == 'ARMATURE':#{
3414 if obj
.data
.pose_position
== 'REST':
3415 draw_skeleton_helpers( obj
)
3418 ent_type
= obj_ent_type( obj
)
3420 if ent_type
== 'ent_gate':#{
3422 route_gates
+= [obj
]
3424 elif ent_type
== 'ent_route_node':#{
3425 if obj
.type == 'CURVE':#{
3426 route_curves
+= [obj
]
3429 elif ent_type
== 'ent_route':
3431 elif ent_type
== 'ent_volume':#{
3432 cv_ent_volume( obj
)
3434 elif ent_type
== 'ent_audio':#{
3435 if obj
.SR_data
.ent_audio
[0].flag_3d
:
3436 cv_draw_sphere( obj
.location
, obj
.scale
[0], (1,1,0) )
3438 elif ent_type
== 'ent_font':#{
3439 data
= obj
.SR_data
.ent_font
[0]
3441 for i
in range(len(data
.variants
)):#{
3442 sub
= data
.variants
[i
].mesh
3443 if not sub
: continue
3445 for ch
in data
.glyphs
:#{
3446 mini
= (ch
.bounds
[0],ch
.bounds
[1])
3447 maxi
= (ch
.bounds
[2]+mini
[0],ch
.bounds
[3]+mini
[1])
3448 p0
= sub
.matrix_world
@ Vector((mini
[0],0.0,mini
[1]))
3449 p1
= sub
.matrix_world
@ Vector((maxi
[0],0.0,mini
[1]))
3450 p2
= sub
.matrix_world
@ Vector((maxi
[0],0.0,maxi
[1]))
3451 p3
= sub
.matrix_world
@ Vector((mini
[0],0.0,maxi
[1]))
3453 if i
== data
.variants_index
: cc
= (0.5,0.5,0.5)
3456 cv_view_verts
+= [p0
,p1
,p1
,p2
,p2
,p3
,p3
,p0
]
3457 cv_view_colours
+= [cc
,cc
,cc
,cc
,cc
,cc
,cc
,cc
]
3464 dij
= create_node_graph( route_curves
, route_gates
)
3466 #cv_draw_route_map( route_nodes )
3467 for route
in routes
:#{
3468 cv_draw_route( route
, dij
)
3475 classes
= [ SR_INTERFACE
, SR_MATERIAL_PANEL
,\
3476 SR_COLLECTION_SETTINGS
, SR_SCENE_SETTINGS
, \
3477 SR_COMPILE
, SR_COMPILE_THIS
, SR_MIRROR_BONE_X
,\
3479 SR_OBJECT_ENT_GATE
, SR_MESH_ENT_GATE
, SR_OBJECT_ENT_SPAWN
, \
3480 SR_OBJECT_ENT_ROUTE_ENTRY
, SR_UL_ROUTE_NODE_LIST
, \
3481 SR_OBJECT_ENT_ROUTE
, SR_OT_ROUTE_LIST_NEW_ITEM
,\
3482 SR_OT_GLYPH_LIST_NEW_ITEM
, SR_OT_GLYPH_LIST_DEL_ITEM
,\
3483 SR_OT_GLYPH_LIST_MOVE_ITEM
,\
3484 SR_OT_AUDIO_LIST_NEW_ITEM
,SR_OT_AUDIO_LIST_DEL_ITEM
,\
3485 SR_OT_FONT_VARIANT_LIST_NEW_ITEM
,SR_OT_FONT_VARIANT_LIST_DEL_ITEM
,\
3486 SR_OT_COPY_ENTITY_DATA
, \
3487 SR_OBJECT_ENT_VOLUME
, \
3488 SR_UL_AUDIO_LIST
, SR_OBJECT_ENT_AUDIO_FILE_ENTRY
,\
3489 SR_OT_ROUTE_LIST_DEL_ITEM
,\
3490 SR_OBJECT_ENT_AUDIO
,SR_OBJECT_ENT_MARKER
,SR_OBJECT_ENT_GLYPH
,\
3491 SR_OBJECT_ENT_FONT_VARIANT
,
3492 SR_OBJECT_ENT_GLYPH_ENTRY
,\
3493 SR_UL_FONT_VARIANT_LIST
,SR_UL_FONT_GLYPH_LIST
,\
3494 SR_OBJECT_ENT_FONT
,SR_OBJECT_ENT_TRAFFIC
,\
3496 SR_OBJECT_PROPERTIES
, SR_LIGHT_PROPERTIES
, SR_BONE_PROPERTIES
,
3497 SR_MESH_PROPERTIES
, SR_MATERIAL_PROPERTIES \
3503 bpy
.utils
.register_class(c
)
3505 bpy
.types
.Scene
.SR_data
= \
3506 bpy
.props
.PointerProperty(type=SR_SCENE_SETTINGS
)
3507 bpy
.types
.Collection
.SR_data
= \
3508 bpy
.props
.PointerProperty(type=SR_COLLECTION_SETTINGS
)
3510 bpy
.types
.Object
.SR_data
= \
3511 bpy
.props
.PointerProperty(type=SR_OBJECT_PROPERTIES
)
3512 bpy
.types
.Light
.SR_data
= \
3513 bpy
.props
.PointerProperty(type=SR_LIGHT_PROPERTIES
)
3514 bpy
.types
.Bone
.SR_data
= \
3515 bpy
.props
.PointerProperty(type=SR_BONE_PROPERTIES
)
3516 bpy
.types
.Mesh
.SR_data
= \
3517 bpy
.props
.PointerProperty(type=SR_MESH_PROPERTIES
)
3518 bpy
.types
.Material
.SR_data
= \
3519 bpy
.props
.PointerProperty(type=SR_MATERIAL_PROPERTIES
)
3521 global cv_view_draw_handler
3522 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
3523 cv_draw
,(),'WINDOW','POST_VIEW')
3529 bpy
.utils
.unregister_class(c
)
3531 global cv_view_draw_handler
3532 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
3535 # ---------------------------------------------------------------------------- #
3539 # ---------------------------------------------------------------------------- #
3541 # Transliteration of: #
3542 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
3544 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
3545 # SPDX-License-Identifier: MIT #
3546 # QOI - The "Quite OK Image" format for fast, lossless image compression #
3548 # ---------------------------------------------------------------------------- #
3550 class qoi_rgba_t(Structure
):
3553 _fields_
= [("r",c_uint8
),
3559 QOI_OP_INDEX
= 0x00 # 00xxxxxx
3560 QOI_OP_DIFF
= 0x40 # 01xxxxxx
3561 QOI_OP_LUMA
= 0x80 # 10xxxxxx
3562 QOI_OP_RUN
= 0xc0 # 11xxxxxx
3563 QOI_OP_RGB
= 0xfe # 11111110
3564 QOI_OP_RGBA
= 0xff # 11111111
3566 QOI_MASK_2
= 0xc0 # 11000000
3568 def qoi_colour_hash( c
):
3570 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
3575 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
3580 return bytearray([ (0xff000000 & v
) >> 24, \
3581 (0x00ff0000 & v
) >> 16, \
3582 (0x0000ff00 & v
) >> 8, \
3586 def qoi_encode( img
):
3590 print(F
"{' ':<30}",end
='\r')
3591 print(F
"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end
='\r')
3593 index
= [ qoi_rgba_t() for _
in range(64) ]
3597 data
.extend( bytearray(c_uint32(0x66696f71)) )
3598 data
.extend( qoi_32bit( img
.size
[0] ) )
3599 data
.extend( qoi_32bit( img
.size
[1] ) )
3600 data
.extend( bytearray(c_uint8(4)) )
3601 data
.extend( bytearray(c_uint8(0)) )
3604 px_prev
= qoi_rgba_t()
3605 px_prev
.r
= c_uint8(0)
3606 px_prev
.g
= c_uint8(0)
3607 px_prev
.b
= c_uint8(0)
3608 px_prev
.a
= c_uint8(255)
3616 px_len
= img
.size
[0] * img
.size
[1]
3617 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
3619 for px_pos
in range( px_len
): #{
3620 idx
= px_pos
* img
.channels
3623 px
.r
= paxels
[idx
+min(0,nc
)]
3624 px
.g
= paxels
[idx
+min(1,nc
)]
3625 px
.b
= paxels
[idx
+min(2,nc
)]
3626 px
.a
= paxels
[idx
+min(3,nc
)]
3628 if qoi_eq( px
, px_prev
): #{
3631 if (run
== 62) or (px_pos
== px_len
-1): #{
3632 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3638 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
3642 index_pos
= qoi_colour_hash(px
) % 64
3644 if qoi_eq( index
[index_pos
], px
): #{
3645 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
3648 index
[ index_pos
].r
= px
.r
3649 index
[ index_pos
].g
= px
.g
3650 index
[ index_pos
].b
= px
.b
3651 index
[ index_pos
].a
= px
.a
3653 if px
.a
== px_prev
.a
: #{
3654 vr
= int(px
.r
) - int(px_prev
.r
)
3655 vg
= int(px
.g
) - int(px_prev
.g
)
3656 vb
= int(px
.b
) - int(px_prev
.b
)
3661 if (vr
> -3) and (vr
< 2) and\
3662 (vg
> -3) and (vg
< 2) and\
3663 (vb
> -3) and (vb
< 2):
3665 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
3666 data
.extend( bytearray( c_uint8(op
) ))
3668 elif (vg_r
> -9) and (vg_r
< 8) and\
3669 (vg
> -33) and (vg
< 32 ) and\
3670 (vg_b
> -9) and (vg_b
< 8):
3672 op
= QOI_OP_LUMA |
(vg
+32)
3673 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
3674 data
.extend( bytearray( c_uint8(op
) ) )
3675 data
.extend( bytearray( c_uint8(delta
) ))
3678 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
3679 data
.extend( bytearray( c_uint8(px
.r
) ))
3680 data
.extend( bytearray( c_uint8(px
.g
) ))
3681 data
.extend( bytearray( c_uint8(px
.b
) ))
3685 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
3686 data
.extend( bytearray( c_uint8(px
.r
) ))
3687 data
.extend( bytearray( c_uint8(px
.g
) ))
3688 data
.extend( bytearray( c_uint8(px
.b
) ))
3689 data
.extend( bytearray( c_uint8(px
.a
) ))
3702 data
.extend( bytearray( c_uint8(0) ))
3703 data
.extend( bytearray( c_uint8(1) ))
3704 bytearray_align_to( data
, 16, b
'\x00' )