1 import bpy
, math
, gpu
, os
4 from mathutils
import *
5 from gpu_extras
.batch
import batch_for_shader
8 "name":"Skaterift .mdl exporter",
9 "author": "Harry Godden (hgn)",
16 "category":"Import/Export",
19 class mdl_vert(Structure
): # 48 bytes. Quite large. Could compress
20 #{ # the normals and uvs to i16s. Not an
21 _pack_
= 1 # real issue, yet.
22 _fields_
= [("co",c_float
*3),
26 ("weights",c_uint16
*4),
30 class mdl_transform(Structure
):
32 _fields_
= [("co",c_float
*3),
37 class mdl_submesh(Structure
):
39 _fields_
= [("indice_start",c_uint32
),
40 ("indice_count",c_uint32
),
41 ("vertex_start",c_uint32
),
42 ("vertex_count",c_uint32
),
43 ("bbx",(c_float
*3)*2),
44 ("material_id",c_uint32
)] # index into the material array
47 class mdl_material(Structure
):
49 _fields_
= [("pstr_name",c_uint32
),
52 ("surface_prop",c_uint32
),
54 ("colour1",c_float
*4),
55 ("tex_diffuse",c_uint32
),
56 ("tex_none0",c_uint32
),
57 ("tex_none1",c_uint32
)]
60 class mdl_bone(Structure
):
62 _fields_
= [("co",c_float
*3),("end",c_float
*3),
64 ("collider",c_uint32
),
65 ("ik_target",c_uint32
),
68 ("pstr_name",c_uint32
),
69 ("hitbox",(c_float
*3)*2),
70 ("conevx",c_float
*3),("conevy",c_float
*3),("coneva",c_float
*3),
74 class mdl_armature(Structure
):
76 _fields_
= [("transform",mdl_transform
),
77 ("bone_start",c_uint32
),
78 ("bone_count",c_uint32
),
79 ("anim_start",c_uint32
),
80 ("anim_count",c_uint32
)]
83 class mdl_animation(Structure
):
85 _fields_
= [("pstr_name",c_uint32
),
88 ("keyframe_start",c_uint32
)]
91 class mdl_mesh(Structure
):
93 _fields_
= [("transform",mdl_transform
),
94 ("submesh_start",c_uint32
),
95 ("submesh_count",c_uint32
),
96 ("pstr_name",c_uint32
),
98 ("armature_id",c_uint32
)]
101 class mdl_file(Structure
):
103 _fields_
= [("path",c_uint32
),
104 ("pack_offset",c_uint32
),
105 ("pack_size",c_uint32
)]
108 class mdl_texture(Structure
):
110 _fields_
= [("file",mdl_file
),
114 class mdl_array(Structure
):
116 _fields_
= [("file_offset",c_uint32
),
117 ("item_count",c_uint32
),
118 ("item_size",c_uint32
),
122 class mdl_header(Structure
):
124 _fields_
= [("version",c_uint32
),
125 ("arrays",mdl_array
)]
128 class ent_spawn(Structure
):
130 _fields_
= [("transform",mdl_transform
),
131 ("pstr_name",c_uint32
)]
134 class ent_light(Structure
):
136 _fields_
= [("transform",mdl_transform
),
137 ("daytime",c_uint32
),
139 ("colour",c_float
*4),
142 ("inverse_world",(c_float
*3)*4), # Runtime
143 ("angle_sin_cos",(c_float
*2))] # Runtime
146 class version_refcount_union(Union
):
148 _fields_
= [("timing_version",c_uint32
),
149 ("ref_count",c_uint8
)]
152 class ent_gate(Structure
):
154 _fields_
= [("type",c_uint32
),
155 ("target", c_uint32
),
156 ("dimensions", c_float
*3),
157 ("co", (c_float
*3)*2),
158 ("q", (c_float
*4)*2),
159 ("to_world",(c_float
*3)*4),
160 ("transport",(c_float
*3)*4),
161 ("_anonymous_union",version_refcount_union
),
162 ("timing_time",c_double
),
163 ("routes",c_uint16
*4)]
166 class ent_route_node(Structure
):
168 _fields_
= [("co",c_float
*3),
169 ("ref_count",c_uint8
),
170 ("ref_total",c_uint8
)]
173 class ent_path_index(Structure
):
175 _fields_
= [("index",c_uint16
)]
178 class ent_checkpoint(Structure
):
180 _fields_
= [("gate_index",c_uint16
),
181 ("path_start",c_uint16
),
182 ("path_count",c_uint16
)]
185 class ent_route(Structure
):
187 _fields_
= [("transform",mdl_transform
),
188 ("pstr_name",c_uint32
),
189 ("checkpoints_start",c_uint16
),
190 ("checkpoints_count",c_uint16
),
191 ("colour",c_float
*4),
192 ("active",c_uint32
), #runtime
194 ("board_transform",(c_float
*3)*4),
196 ("latest_pass",c_double
)]
199 def obj_ent_type( obj
):
201 if obj
.type == 'ARMATURE': return 'mdl_armature'
202 elif obj
.type == 'LIGHT': return 'ent_light'
203 else: return obj
.SR_data
.ent_type
206 def sr_filter_ent_type( obj
, ent_type
):
208 if obj
== bpy
.context
.active_object
: return False
210 for c0
in obj
.users_collection
:#{
211 for c1
in bpy
.context
.active_object
.users_collection
:#{
213 return ent_type
== obj_ent_type( obj
)
221 def compile_obj_transform( obj
, transform
):
223 co
= obj
.matrix_world
@ Vector((0,0,0))
224 q
= obj
.matrix_local
.to_quaternion()
229 transform
.co
[0] = co
[0]
230 transform
.co
[1] = co
[2]
231 transform
.co
[2] = -co
[1]
232 transform
.q
[0] = q
[1]
233 transform
.q
[1] = q
[3]
234 transform
.q
[2] = -q
[2]
235 transform
.q
[3] = q
[0]
236 transform
.s
[0] = s
[0]
237 transform
.s
[1] = s
[2]
238 transform
.s
[2] = s
[1]
241 def int_align_to( v
, align
):
243 while(v
%align
)!=0: v
+= 1
247 def bytearray_align_to( buffer, align
, w
=b
'\xaa' ):
249 while (len(buffer) % align
) != 0: buffer.extend(w
)
253 def bytearray_print_hex( s
, w
=16 ):
255 for r
in range((len(s
)+(w
-1))//w
):#{
257 i1
=min((r
+1)*w
,len(s
))
258 print( F
'{r*w:06x}| \x1B[31m', end
='')
259 print( F
"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end
='' )
260 print( "\x1B[0m", end
='')
261 print( ''.join(chr(x
) if (x
>=33 and x
<=126) else '.' for x
in s
[i0
:i1
] ) )
265 def sr_compile_string( s
):
267 if s
in sr_compile
.string_cache
: return sr_compile
.string_cache
[s
]
269 index
= len( sr_compile
.string_data
)
270 sr_compile
.string_cache
[s
] = index
271 sr_compile
.string_data
.extend( s
.encode('utf-8') )
272 sr_compile
.string_data
.extend( b
'\0' )
274 bytearray_align_to( sr_compile
.string_data
, 4 )
278 def material_tex_image(v
):
288 cxr_graph_mapping
= \
290 # Default shader setup
297 "image": "tex_diffuse"
301 "A": material_tex_image("tex_diffuse"),
302 "B": material_tex_image("tex_decal")
309 "Color": material_tex_image("tex_normal")
315 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
317 def material_info(mat
):
321 # Using the cxr_graph_mapping as a reference, go through the shader
322 # graph and gather all $props from it.
324 def _graph_read( node_def
, node
=None, depth
=0 ):#{
331 _graph_read
.extracted
= []
333 for node_idname
in node_def
:#{
334 for n
in mat
.node_tree
.nodes
:#{
335 if n
.name
== node_idname
:#{
336 node_def
= node_def
[node_idname
]
344 for link
in node_def
:#{
345 link_def
= node_def
[link
]
347 if isinstance( link_def
, dict ):#{
349 for x
in node
.inputs
:#{
350 if isinstance( x
, bpy
.types
.NodeSocketColor
):#{
358 if node_link
and node_link
.is_linked
:#{
359 # look for definitions for the connected node type
361 from_node
= node_link
.links
[0].from_node
363 node_name
= from_node
.name
.split('.')[0]
364 if node_name
in link_def
:#{
365 from_node_def
= link_def
[ node_name
]
367 _graph_read( from_node_def
, from_node
, depth
+1 )
371 # TODO: Make a warning for this?
374 if "default" in link_def
:#{
375 prop
= link_def
['default']
376 info
[prop
] = node_link
.default_value
382 info
[prop
] = getattr( node
, link
)
387 _graph_read( cxr_graph_mapping
)
391 def sr_pack_file( file, path
, data
):
393 file.path
= sr_compile_string( path
)
394 file.pack_offset
= len( sr_compile
.pack_data
)
395 file.pack_size
= len( data
)
397 sr_compile
.pack_data
.extend( data
)
398 bytearray_align_to( sr_compile
.pack_data
, 16 )
401 def sr_compile_texture( img
):
406 name
= os
.path
.splitext( img
.name
)[0]
408 if name
in sr_compile
.texture_cache
:
409 return sr_compile
.texture_cache
[name
]
411 texture_index
= (len(sr_compile
.texture_data
)//sizeof(mdl_texture
)) +1
416 if sr_compile
.pack_textures
:#{
417 filedata
= qoi_encode( img
)
418 sr_pack_file( tex
.file, name
, filedata
)
421 sr_compile
.texture_cache
[name
] = texture_index
422 sr_compile
.texture_data
.extend( bytearray(tex
) )
426 def sr_compile_material( mat
):
430 if mat
.name
in sr_compile
.material_cache
:
431 return sr_compile
.material_cache
[mat
.name
]
433 print( "\n"+ mat
.name
+"\n" )
435 index
= (len(sr_compile
.material_data
)//sizeof(mdl_material
))+1
436 sr_compile
.material_cache
[mat
.name
] = index
439 m
.pstr_name
= sr_compile_string( mat
.name
)
442 if mat
.SR_data
.collision
:#{
444 if mat
.SR_data
.skate_surface
: flags |
= 0x1
445 if mat
.SR_data
.grind_surface
: flags |
= (0x8|
0x1)
448 if mat
.SR_data
.grow_grass
: flags |
= 0x4
451 m
.surface_prop
= int(mat
.SR_data
.surface_prop
)
453 if mat
.SR_data
.shader
== 'standard': m
.shader
= 0
454 if mat
.SR_data
.shader
== 'standard_cutout': m
.shader
= 1
455 if mat
.SR_data
.shader
== 'terrain_blend':#{
458 m
.colour
[0] = pow( mat
.SR_data
.sand_colour
[0], 1.0/2.2 )
459 m
.colour
[1] = pow( mat
.SR_data
.sand_colour
[1], 1.0/2.2 )
460 m
.colour
[2] = pow( mat
.SR_data
.sand_colour
[2], 1.0/2.2 )
463 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
464 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
467 if mat
.SR_data
.shader
== 'vertex_blend':#{
470 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
471 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
474 if mat
.SR_data
.shader
== 'water':#{
477 m
.colour
[0] = pow( mat
.SR_data
.shore_colour
[0], 1.0/2.2 )
478 m
.colour
[1] = pow( mat
.SR_data
.shore_colour
[1], 1.0/2.2 )
479 m
.colour
[2] = pow( mat
.SR_data
.shore_colour
[2], 1.0/2.2 )
481 m
.colour1
[0] = pow( mat
.SR_data
.ocean_colour
[0], 1.0/2.2 )
482 m
.colour1
[1] = pow( mat
.SR_data
.ocean_colour
[1], 1.0/2.2 )
483 m
.colour1
[2] = pow( mat
.SR_data
.ocean_colour
[2], 1.0/2.2 )
487 inf
= material_info( mat
)
489 if mat
.SR_data
.shader
== 'standard' or \
490 mat
.SR_data
.shader
== 'standard_cutout' or \
491 mat
.SR_data
.shader
== 'terrain_blend' or \
492 mat
.SR_data
.shader
== 'vertex_blend':
494 if 'tex_diffuse' in inf
:
495 m
.tex_diffuse
= sr_compile_texture(inf
['tex_diffuse'])
498 sr_compile
.material_data
.extend( bytearray(m
) )
502 def sr_armature_bones( armature
):
504 def _recurse_bone( b
):
507 for c
in b
.children
: yield from _recurse_bone( c
)
510 for b
in armature
.data
.bones
:
512 yield from _recurse_bone( b
)
515 def sr_compile_mesh( obj
):
518 compile_obj_transform(obj
, node
.transform
)
519 node
.pstr_name
= sr_compile_string(obj
.name
)
525 for mod
in obj
.modifiers
:#{
526 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
527 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
530 can_use_cache
= False
533 if mod
.type == 'ARMATURE': #{
535 armature
= mod
.object
536 rig_weight_groups
= \
537 ['0 [ROOT]']+[_
.name
for _
in sr_armature_bones(mod
.object)]
538 node
.armature_id
= sr_compile
.entity_ids
[armature
.name
]
540 POSE_OR_REST_CACHE
= armature
.data
.pose_position
541 armature
.data
.pose_position
= 'REST'
545 # Check the cache first
547 if can_use_cache
and (obj
.data
.name
in sr_compile
.mesh_cache
):#{
548 ref
= sr_compile
.mesh_cache
[obj
.data
.name
]
549 node
.submesh_start
= ref
[0]
550 node
.submesh_count
= ref
[1]
551 sr_compile
.mesh_data
.extend(bytearray(node
))
555 # Compile a whole new mesh
557 node
.submesh_start
= len(sr_compile
.submesh_data
)//sizeof(mdl_submesh
)
558 node
.submesh_count
= 0
560 dgraph
= bpy
.context
.evaluated_depsgraph_get()
561 data
= obj
.evaluated_get(dgraph
).data
562 data
.calc_loop_triangles()
563 data
.calc_normals_split()
565 # Mesh is split into submeshes based on their material
567 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
568 for material_id
, mat
in enumerate(mat_list
): #{
572 sm
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
573 sm
.vertex_start
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
576 sm
.material_id
= sr_compile_material( mat
)
578 INF
=99999999.99999999
584 # Keep a reference to very very very similar vertices
585 # i have no idea how to speed it up.
587 vertex_reference
= {}
589 # Write the vertex / indice data
591 for tri_index
, tri
in enumerate(data
.loop_triangles
):#{
592 if tri
.material_index
!= material_id
:
596 vert
= data
.vertices
[tri
.vertices
[j
]]
598 vi
= data
.loops
[li
].vertex_index
600 # Gather vertex information
603 norm
= data
.loops
[li
].normal
605 colour
= (255,255,255,255)
612 uv
= data
.uv_layers
.active
.data
[li
].uv
616 if data
.vertex_colors
:#{
617 colour
= data
.vertex_colors
.active
.data
[li
].color
618 colour
= (int(colour
[0]*255.0),\
619 int(colour
[1]*255.0),\
620 int(colour
[2]*255.0),\
621 int(colour
[3]*255.0))
624 # Weight groups: truncates to the 3 with the most influence. The
625 # fourth bone ID is never used by the shader so it
629 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
630 if obj
.vertex_groups
[_
.group
].name
in \
633 weight_groups
= sorted( src_groups
, key
= \
634 lambda a
: a
.weight
, reverse
=True )
636 for ml
in range(3):#{
637 if len(weight_groups
) > ml
:#{
638 g
= weight_groups
[ml
]
639 name
= obj
.vertex_groups
[g
.group
].name
642 groups
[ml
] = rig_weight_groups
.index(name
)
647 if len(weight_groups
) > 0:#{
648 inv_norm
= (1.0/tot
) * 65535.0
649 for ml
in range(3):#{
650 weights
[ml
] = int( weights
[ml
] * inv_norm
)
651 weights
[ml
] = min( weights
[ml
], 65535 )
652 weights
[ml
] = max( weights
[ml
], 0 )
657 li1
= tri
.loops
[(j
+1)%3]
658 vi1
= data
.loops
[li1
].vertex_index
659 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
661 if e0
.use_freestyle_mark
and \
662 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
663 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
669 TOLERENCE
= float(10**4)
670 key
= (int(co
[0]*TOLERENCE
+0.5),
671 int(co
[1]*TOLERENCE
+0.5),
672 int(co
[2]*TOLERENCE
+0.5),
673 int(norm
[0]*TOLERENCE
+0.5),
674 int(norm
[1]*TOLERENCE
+0.5),
675 int(norm
[2]*TOLERENCE
+0.5),
676 int(uv
[0]*TOLERENCE
+0.5),
677 int(uv
[1]*TOLERENCE
+0.5),
678 colour
[0], # these guys are already quantized
691 if key
in vertex_reference
:
692 index
= vertex_reference
[key
]
694 index
= bytearray(c_uint32(sm
.vertex_count
))
697 vertex_reference
[key
] = index
707 v
.colour
[0] = colour
[0]
708 v
.colour
[1] = colour
[1]
709 v
.colour
[2] = colour
[2]
710 v
.colour
[3] = colour
[3]
711 v
.weights
[0] = weights
[0]
712 v
.weights
[1] = weights
[1]
713 v
.weights
[2] = weights
[2]
714 v
.weights
[3] = weights
[3]
715 v
.groups
[0] = groups
[0]
716 v
.groups
[1] = groups
[1]
717 v
.groups
[2] = groups
[2]
718 v
.groups
[3] = groups
[3]
721 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
722 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
725 sr_compile
.vertex_data
.extend(bytearray(v
))
729 sr_compile
.indice_data
.extend( index
)
733 # Make sure bounding box isn't -inf -> inf if no vertices
735 if sm
.vertex_count
== 0:
740 # Add submesh to encoder
742 sr_compile
.submesh_data
.extend( bytearray(sm
) )
743 node
.submesh_count
+= 1
747 armature
.data
.pose_position
= POSE_OR_REST_CACHE
750 # Save a reference to this node since we want to reuse the submesh indices
752 sr_compile
.mesh_cache
[obj
.data
.name
]=(node
.submesh_start
,node
.submesh_count
)
753 sr_compile
.mesh_data
.extend(bytearray(node
))
756 def sr_compile_armature( obj
):
758 node
= mdl_armature()
759 node
.bone_start
= len(sr_compile
.bone_data
)//sizeof(mdl_bone
)
761 node
.anim_start
= len(sr_compile
.anim_data
)//sizeof(mdl_animation
)
764 bones
= [_
for _
in sr_armature_bones(obj
)]
765 bones_names
= [None]+[_
.name
for _
in bones
]
769 if b
.use_deform
: bone
.flags
= 0x1
770 if b
.parent
: bone
.parent
= bones_names
.index(b
.parent
.name
)
772 bone
.collider
= int(b
.SR_data
.collider
)
774 if bone
.collider
>0:#{
775 bone
.hitbox
[0][0] = b
.SR_data
.collider_min
[0]
776 bone
.hitbox
[0][1] = b
.SR_data
.collider_min
[2]
777 bone
.hitbox
[0][2] = -b
.SR_data
.collider_max
[1]
778 bone
.hitbox
[1][0] = b
.SR_data
.collider_max
[0]
779 bone
.hitbox
[1][1] = b
.SR_data
.collider_max
[2]
780 bone
.hitbox
[1][2] = -b
.SR_data
.collider_min
[1]
783 if b
.SR_data
.cone_constraint
:#{
785 bone
.conevx
[0] = b
.SR_data
.conevx
[0]
786 bone
.conevx
[1] = b
.SR_data
.conevx
[2]
787 bone
.conevx
[2] = -b
.SR_data
.conevx
[1]
788 bone
.conevy
[0] = b
.SR_data
.conevy
[0]
789 bone
.conevy
[1] = b
.SR_data
.conevy
[2]
790 bone
.conevy
[2] = -b
.SR_data
.conevy
[1]
791 bone
.coneva
[0] = b
.SR_data
.coneva
[0]
792 bone
.coneva
[1] = b
.SR_data
.coneva
[2]
793 bone
.coneva
[2] = -b
.SR_data
.coneva
[1]
794 bone
.conet
= b
.SR_data
.conet
797 bone
.co
[0] = b
.head_local
[0]
798 bone
.co
[1] = b
.head_local
[2]
799 bone
.co
[2] = -b
.head_local
[1]
800 bone
.end
[0] = b
.tail_local
[0] - bone
.co
[0]
801 bone
.end
[1] = b
.tail_local
[2] - bone
.co
[1]
802 bone
.end
[2] = -b
.tail_local
[1] - bone
.co
[2]
803 bone
.pstr_name
= sr_compile_string( b
.name
)
805 for c
in obj
.pose
.bones
[b
.name
].constraints
:#{
808 bone
.ik_target
= bones_names
.index(c
.subtarget
)
809 bone
.ik_pole
= bones_names
.index(c
.pole_subtarget
)
814 sr_compile
.bone_data
.extend(bytearray(bone
))
819 if obj
.animation_data
and sr_compile
.pack_animations
: #{
820 # So we can restore later
822 previous_frame
= bpy
.context
.scene
.frame_current
823 previous_action
= obj
.animation_data
.action
824 POSE_OR_REST_CACHE
= obj
.data
.pose_position
825 obj
.data
.pose_position
= 'POSE'
827 for NLALayer
in obj
.animation_data
.nla_tracks
:#{
828 for NLAStrip
in NLALayer
.strips
:#{
831 for a
in bpy
.data
.actions
:#{
832 if a
.name
== NLAStrip
.name
:#{
833 obj
.animation_data
.action
= a
838 # Clip to NLA settings
840 anim_start
= int(NLAStrip
.action_frame_start
)
841 anim_end
= int(NLAStrip
.action_frame_end
)
845 anim
= mdl_animation()
846 anim
.pstr_name
= sr_compile_string( NLAStrip
.action
.name
)
848 anim
.keyframe_start
= len(sr_compile
.keyframe_data
)//\
849 sizeof(mdl_transform
)
850 anim
.length
= anim_end
-anim_start
853 # Export the keyframes
854 for frame
in range(anim_start
,anim_end
):#{
855 bpy
.context
.scene
.frame_set(frame
)
858 pb
= obj
.pose
.bones
[rb
.name
]
860 # relative bone matrix
861 if rb
.parent
is not None:#{
862 offset_mtx
= rb
.parent
.matrix_local
863 offset_mtx
= offset_mtx
.inverted_safe() @ \
866 inv_parent
= pb
.parent
.matrix
@ offset_mtx
867 inv_parent
.invert_safe()
868 fpm
= inv_parent
@ pb
.matrix
871 bone_mtx
= rb
.matrix
.to_4x4()
872 local_inv
= rb
.matrix_local
.inverted_safe()
873 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
876 loc
, rot
, sca
= fpm
.decompose()
879 lc_m
= pb
.matrix_channel
.to_3x3()
880 if pb
.parent
is not None:#{
881 smtx
= pb
.parent
.matrix_channel
.to_3x3()
882 lc_m
= smtx
.inverted() @ lc_m
884 rq
= lc_m
.to_quaternion()
897 sr_compile
.keyframe_data
.extend(bytearray(kf
))
903 # Add to animation buffer
905 sr_compile
.anim_data
.extend(bytearray(anim
))
910 print( F
"[SR] | anim( {NLAStrip.action.name} )" )
914 # Restore context to how it was before
916 bpy
.context
.scene
.frame_set( previous_frame
)
917 obj
.animation_data
.action
= previous_action
918 obj
.data
.pose_position
= POSE_OR_REST_CACHE
921 sr_compile
.armature_data
.extend(bytearray(node
))
924 def sr_ent_push( struct
):
926 clase
= type(struct
).__name
__
928 if clase
not in sr_compile
.entity_data
:#{
929 sr_compile
.entity_data
[ clase
] = bytearray()
930 sr_compile
.entity_info
[ clase
] = { 'size': sizeof(struct
) }
933 index
= len(sr_compile
.entity_data
[ clase
])//sizeof(struct
)
934 sr_compile
.entity_data
[ clase
].extend( bytearray(struct
) )
938 def sr_array_title( arr
, name
, count
, size
, offset
):
940 for i
in range(len(name
)):#{
941 arr
.name
[i
] = ord(name
[i
])
943 arr
.file_offset
= offset
944 arr
.item_count
= count
948 def sr_compile( collection
):
950 print( F
"[SR] compiler begin ({collection.name}.mdl)" )
953 sr_compile
.pack_textures
= collection
.SR_data
.pack_textures
954 sr_compile
.pack_animations
= collection
.SR_data
.animations
957 sr_compile
.string_cache
= {}
958 sr_compile
.mesh_cache
= {}
959 sr_compile
.material_cache
= {}
960 sr_compile
.texture_cache
= {}
963 sr_compile
.mesh_data
= bytearray()
964 sr_compile
.submesh_data
= bytearray()
965 sr_compile
.vertex_data
= bytearray()
966 sr_compile
.indice_data
= bytearray()
967 sr_compile
.bone_data
= bytearray()
968 sr_compile
.material_data
= bytearray()
969 sr_compile
.armature_data
= bytearray()
970 sr_compile
.anim_data
= bytearray()
971 sr_compile
.keyframe_data
= bytearray()
972 sr_compile
.texture_data
= bytearray()
974 # just bytes not structures
975 sr_compile
.string_data
= bytearray()
976 sr_compile
.pack_data
= bytearray()
979 sr_compile
.entity_data
= {}
980 sr_compile
.entity_info
= {}
982 print( F
"[SR] assign entity ID's" )
983 sr_compile
.entities
= {}
984 sr_compile
.entity_ids
= {}
987 for obj
in collection
.all_objects
: #{
988 if obj
.type == 'MESH': mesh_count
+= 1
990 ent_type
= obj_ent_type( obj
)
991 if ent_type
== 'none': continue
993 if ent_type
not in sr_compile
.entities
: sr_compile
.entities
[ent_type
] = []
994 sr_compile
.entity_ids
[obj
.name
] = len( sr_compile
.entities
[ent_type
] )
995 sr_compile
.entities
[ent_type
] += [obj
]
998 print( F
"[SR] Compiling geometry" )
1000 for obj
in collection
.all_objects
:#{
1001 if obj
.type == 'MESH':#{
1003 print( F
'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end
='\r' )
1004 sr_compile_mesh( obj
)
1008 checkpoint_count
= 0
1009 pathindice_count
= 0
1011 for ent_type
, arr
in sr_compile
.entities
.items():#{
1012 print(F
"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1014 for i
in range(len(arr
)):#{
1017 print( F
"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end
='\r' )
1019 if ent_type
== 'mdl_armature': sr_compile_armature(obj
)
1020 elif ent_type
== 'ent_light': #{
1022 compile_obj_transform( obj
, light
.transform
)
1023 light
.daytime
= obj
.data
.SR_data
.daytime
1024 if obj
.data
.type == 'POINT':#{
1027 elif obj
.data
.type == 'SPOT':#{
1029 light
.angle
= obj
.data
.spot_size
*0.5
1031 light
.range = obj
.data
.cutoff_distance
1032 light
.colour
[0] = obj
.data
.color
[0]
1033 light
.colour
[1] = obj
.data
.color
[1]
1034 light
.colour
[2] = obj
.data
.color
[2]
1035 light
.colour
[3] = obj
.data
.energy
1036 sr_ent_push( light
)
1038 elif ent_type
== 'ent_gate': #{
1041 obj_data
= obj
.SR_data
.ent_gate
[0]
1042 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
1043 if obj_data
.target
:#{
1044 gate
.target
= sr_compile
.entity_ids
[obj_data
.target
.name
]
1047 gate
.dimensions
[0] = mesh_data
.dimensions
[0]
1048 gate
.dimensions
[1] = mesh_data
.dimensions
[1]
1049 gate
.dimensions
[2] = mesh_data
.dimensions
[2]
1051 q
= [obj
.matrix_local
.to_quaternion(), (0,0,0,1)]
1052 co
= [obj
.matrix_world
@ Vector((0,0,0)), (0,0,0)]
1054 if obj_data
.target
:#{
1055 q
[1] = obj_data
.target
.matrix_local
.to_quaternion()
1056 co
[1]= obj_data
.target
.matrix_world
@ Vector((0,0,0))
1061 for x
in range(2):#{
1062 gate
.co
[x
][0] = co
[x
][0]
1063 gate
.co
[x
][1] = co
[x
][2]
1064 gate
.co
[x
][2] = -co
[x
][1]
1065 gate
.q
[x
][0] = q
[x
][1]
1066 gate
.q
[x
][1] = q
[x
][3]
1067 gate
.q
[x
][2] = -q
[x
][2]
1068 gate
.q
[x
][3] = q
[x
][0]
1073 elif ent_type
== 'ent_spawn': #{
1075 compile_obj_transform( obj
, spawn
.transform
)
1076 obj_data
= obj
.SR_data
.ent_spawn
[0]
1077 spawn
.pstr_name
= sr_compile_string( obj_data
.name
)
1078 sr_ent_push( spawn
)
1080 elif ent_type
== 'ent_route': #{
1081 obj_data
= obj
.SR_data
.ent_route
[0]
1083 route
.pstr_name
= sr_compile_string( obj_data
.alias
) #TODO
1084 route
.checkpoints_start
= checkpoint_count
1085 route
.checkpoints_count
= 0
1088 route
.colour
[ci
] = obj_data
.colour
[ci
]
1089 route
.colour
[3] = 1.0
1091 compile_obj_transform( obj
, route
.transform
)
1093 checkpoints
= obj_data
.gates
1096 for uc
in obj
.users_collection
[0].objects
:#{
1097 uc_type
= obj_ent_type( uc
)
1098 if uc_type
== 'ent_gate' or uc_type
== 'ent_route_node':
1101 graph
= node_graph( route_nodes
)
1103 for i
in range(len(checkpoints
)):#{
1104 gi
= checkpoints
[i
].target
1105 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
1109 dest
= gi
.SR_data
.ent_gate
[0].target
1113 if gi
==gj
: continue # error?
1114 if not gi
or not gj
: continue
1116 checkpoint
= ent_checkpoint()
1117 checkpoint
.gate_index
= sr_compile
.entity_ids
[gate
.name
]
1118 checkpoint
.path_start
= pathindice_count
1119 checkpoint
.path_count
= 0
1121 path
= dijkstra( graph
, gj
.name
, gi
.name
)
1123 for pi
in range(1,len(path
)-1):#{
1124 pathindice
= ent_path_index()
1125 pathindice
.index
= sr_compile
.entity_ids
[path
[pi
]]
1126 sr_ent_push( pathindice
)
1128 checkpoint
.path_count
+= 1
1129 pathindice_count
+= 1
1133 sr_ent_push( checkpoint
)
1134 route
.checkpoints_count
+= 1
1135 checkpoint_count
+= 1
1138 sr_ent_push( route
)
1140 elif ent_type
== 'ent_route_node':#{
1141 rn
= ent_route_node()
1142 rn
.co
[0] = obj
.location
[0]
1143 rn
.co
[1] = obj
.location
[2]
1144 rn
.co
[2] = -obj
.location
[1]
1150 print( F
"[SR] Writing file" )
1152 file_array_instructions
= {}
1155 def _write_array( name
, item_size
, data
):#{
1156 nonlocal file_array_instructions
, file_offset
1158 count
= len(data
)//item_size
1159 file_array_instructions
[name
] = {'count':count
, 'size':item_size
,\
1160 'data':data
, 'offset': file_offset
}
1161 file_offset
+= len(data
)
1162 file_offset
= int_align_to( file_offset
, 8 )
1165 _write_array( 'strings', 1, sr_compile
.string_data
)
1166 _write_array( 'mdl_mesh', sizeof(mdl_mesh
), sr_compile
.mesh_data
)
1167 _write_array( 'mdl_submesh', sizeof(mdl_submesh
), sr_compile
.submesh_data
)
1168 _write_array( 'mdl_material', sizeof(mdl_material
), sr_compile
.material_data
)
1169 _write_array( 'mdl_texture', sizeof(mdl_texture
), sr_compile
.texture_data
)
1170 _write_array( 'mdl_armature', sizeof(mdl_armature
), sr_compile
.armature_data
)
1171 _write_array( 'mdl_bone', sizeof(mdl_bone
), sr_compile
.bone_data
)
1173 for name
, buffer in sr_compile
.entity_data
.items():#{
1174 _write_array( name
, sr_compile
.entity_info
[name
]['size'], buffer )
1177 _write_array( 'mdl_animation', sizeof(mdl_animation
), sr_compile
.anim_data
)
1178 _write_array( 'mdl_keyframe', sizeof(mdl_transform
),sr_compile
.keyframe_data
)
1179 _write_array( 'mdl_vert', sizeof(mdl_vert
), sr_compile
.vertex_data
)
1180 _write_array( 'mdl_indice', sizeof(c_uint32
), sr_compile
.indice_data
)
1181 _write_array( 'pack', 1, sr_compile
.pack_data
)
1183 header_size
= int_align_to( sizeof(mdl_header
), 8 )
1184 index_size
= int_align_to( sizeof(mdl_array
)*len(file_array_instructions
),8 )
1186 folder
= bpy
.path
.abspath(bpy
.context
.scene
.SR_data
.export_dir
)
1187 path
= F
"{folder}{collection.name}.mdl"
1190 fp
= open( path
, "wb" )
1191 header
= mdl_header()
1193 sr_array_title( header
.arrays
, \
1194 'index', len(file_array_instructions
), \
1195 sizeof(mdl_array
), header_size
)
1197 fp
.write( bytearray_align_to( bytearray(header
), 8 ) )
1199 print( F
'[SR] {"name":>16}| count | offset' )
1201 for name
,info
in file_array_instructions
.items():#{
1203 offset
= info
['offset'] + header_size
+ index_size
1204 sr_array_title( arr
, name
, info
['count'], info
['size'], offset
)
1205 index
.extend( bytearray(arr
) )
1207 print( F
'[SR] {name:>16}| {info["count"]: 8} '+\
1208 F
' 0x{info["offset"]:02x}' )
1210 fp
.write( bytearray_align_to( index
, 8 ) )
1211 #bytearray_print_hex( index )
1213 for name
,info
in file_array_instructions
.items():#{
1214 fp
.write( bytearray_align_to( info
['data'], 8 ) )
1219 print( '[SR] done' )
1222 class SR_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1224 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1225 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
1226 gizmos
: bpy
.props
.BoolProperty( name
="Draw Gizmos", default
=True )
1228 panel
: bpy
.props
.EnumProperty(
1232 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
1233 ('ENTITY', 'Entity', '', 'MONKEY',1),
1234 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
1239 class SR_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
1241 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
1242 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
1245 def sr_get_mirror_bone( bones
):
1247 side
= bones
.active
.name
[-1:]
1248 other_name
= bones
.active
.name
[:-1]
1249 if side
== 'L': other_name
+= 'R'
1250 elif side
== 'R': other_name
+= 'L'
1254 if b
.name
== other_name
:
1261 class SR_MIRROR_BONE_X(bpy
.types
.Operator
):
1263 bl_idname
="skaterift.mirror_bone"
1264 bl_label
="Mirror bone attributes - SkateRift"
1266 def execute(_
,context
):
1268 active_object
= context
.active_object
1269 bones
= active_object
.data
.bones
1271 b
= sr_get_mirror_bone( bones
)
1273 if not b
: return {'FINISHED'}
1275 b
.SR_data
.collider
= a
.SR_data
.collider
1277 def _v3copyflipy( a
, b
):#{
1283 _v3copyflipy( a
.SR_data
.collider_min
, b
.SR_data
.collider_min
)
1284 _v3copyflipy( a
.SR_data
.collider_max
, b
.SR_data
.collider_max
)
1285 b
.SR_data
.collider_min
[1] = -a
.SR_data
.collider_max
[1]
1286 b
.SR_data
.collider_max
[1] = -a
.SR_data
.collider_min
[1]
1288 b
.SR_data
.cone_constraint
= a
.SR_data
.cone_constraint
1290 _v3copyflipy( a
.SR_data
.conevx
, b
.SR_data
.conevy
)
1291 _v3copyflipy( a
.SR_data
.conevy
, b
.SR_data
.conevx
)
1292 _v3copyflipy( a
.SR_data
.coneva
, b
.SR_data
.coneva
)
1294 b
.SR_data
.conet
= a
.SR_data
.conet
1297 ob
= bpy
.context
.scene
.objects
[0]
1298 ob
.hide_render
= ob
.hide_render
1303 class SR_COMPILE(bpy
.types
.Operator
):
1305 bl_idname
="skaterift.compile_all"
1306 bl_label
="Compile All"
1308 def execute(_
,context
):
1310 view_layer
= bpy
.context
.view_layer
1311 for col
in view_layer
.layer_collection
.children
["export"].children
:
1312 if not col
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1313 sr_compile( bpy
.data
.collections
[col
.name
] )
1319 class SR_COMPILE_THIS(bpy
.types
.Operator
):
1321 bl_idname
="skaterift.compile_this"
1322 bl_label
="Compile This collection"
1324 def execute(_
,context
):
1326 col
= bpy
.context
.collection
1333 class SR_INTERFACE(bpy
.types
.Panel
):
1335 bl_idname
= "VIEW3D_PT_skate_rift"
1336 bl_label
= "Skate Rift"
1337 bl_space_type
= 'VIEW_3D'
1338 bl_region_type
= 'UI'
1339 bl_category
= "Skate Rift"
1341 def draw(_
, context
):
1345 row
= _
.layout
.row()
1347 row
.prop( context
.scene
.SR_data
, 'panel', expand
=True )
1349 if context
.scene
.SR_data
.panel
== 'SETTINGS': #{
1350 _
.layout
.prop( context
.scene
.SR_data
, 'gizmos' )
1352 elif context
.scene
.SR_data
.panel
== 'EXPORT': #{
1353 _
.layout
.prop( context
.scene
.SR_data
, "export_dir" )
1354 col
= bpy
.context
.collection
1356 found_in_export
= False
1358 view_layer
= bpy
.context
.view_layer
1359 for c1
in view_layer
.layer_collection
.children
["export"].children
: #{
1360 if not c1
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1363 if c1
.name
== col
.name
: #{
1364 found_in_export
= True
1368 box
= _
.layout
.box()
1370 row
.alignment
= 'CENTER'
1373 if found_in_export
: #{
1374 row
.label( text
=col
.name
+ ".mdl" )
1375 box
.prop( col
.SR_data
, "pack_textures" )
1376 box
.prop( col
.SR_data
, "animations" )
1377 box
.operator( "skaterift.compile_this" )
1381 row
.label( text
=col
.name
)
1385 row
.alignment
= 'CENTER'
1387 row
.label( text
="This collection is not in the export group" )
1390 box
= _
.layout
.box()
1393 split
= row
.split( factor
=0.3, align
=True )
1394 split
.prop( context
.scene
.SR_data
, "use_hidden", text
="hidden" )
1397 if export_count
== 0:
1399 row1
.operator( "skaterift.compile_all", \
1400 text
=F
"Compile all ({export_count} collections)" )
1402 elif context
.scene
.SR_data
.panel
== 'ENTITY': #{
1403 active_object
= context
.active_object
1404 if not active_object
: return
1406 box
= _
.layout
.box()
1408 row
.alignment
= 'CENTER'
1409 row
.label( text
=active_object
.name
)
1412 def _draw_prop_collection( data
): #{
1415 row
.alignment
= 'CENTER'
1418 row
.label( text
=F
'{data[0]}' )
1420 if hasattr(type(data
[0]),'sr_inspector'):#{
1421 type(data
[0]).sr_inspector( box
, data
)
1424 for a
in data
[0].__annotations
__:
1425 box
.prop( data
[0], a
)
1429 if active_object
.type == 'ARMATURE': #{
1430 if active_object
.mode
== 'POSE': #{
1431 bones
= active_object
.data
.bones
1432 mb
= sr_get_mirror_bone( bones
)
1434 box
.operator( "skaterift.mirror_bone", \
1435 text
=F
'Mirror attributes to {mb.name}' )
1438 _draw_prop_collection( [bones
.active
.SR_data
] )
1442 row
.alignment
='CENTER'
1445 row
.label( text
="Enter pose mode to modify bone properties" )
1448 elif active_object
.type == 'LIGHT': #{
1449 _draw_prop_collection( [active_object
.data
.SR_data
] )
1451 elif active_object
.type == 'EMPTY' or active_object
.type == 'MESH': #{
1452 box
.prop( active_object
.SR_data
, "ent_type" )
1453 ent_type
= active_object
.SR_data
.ent_type
1455 col
= getattr( active_object
.SR_data
, ent_type
, None )
1456 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1458 if active_object
.type == 'MESH':#{
1459 col
= getattr( active_object
.data
.SR_data
, ent_type
, None )
1460 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1467 class SR_MATERIAL_PANEL(bpy
.types
.Panel
):
1469 bl_label
="Skate Rift material"
1470 bl_idname
="MATERIAL_PT_sr_material"
1471 bl_space_type
='PROPERTIES'
1472 bl_region_type
='WINDOW'
1473 bl_context
="material"
1475 def draw(_
,context
):
1477 active_object
= bpy
.context
.active_object
1478 if active_object
== None: return
1479 active_mat
= active_object
.active_material
1480 if active_mat
== None: return
1482 info
= material_info( active_mat
)
1484 if 'tex_diffuse' in info
:#{
1485 _
.layout
.label( icon
='INFO', \
1486 text
=F
"{info['tex_diffuse'].name} will be compiled" )
1489 _
.layout
.prop( active_mat
.SR_data
, "shader" )
1490 _
.layout
.prop( active_mat
.SR_data
, "surface_prop" )
1491 _
.layout
.prop( active_mat
.SR_data
, "collision" )
1493 if active_mat
.SR_data
.collision
:#{
1494 _
.layout
.prop( active_mat
.SR_data
, "skate_surface" )
1495 _
.layout
.prop( active_mat
.SR_data
, "grind_surface" )
1496 _
.layout
.prop( active_mat
.SR_data
, "grow_grass" )
1499 if active_mat
.SR_data
.shader
== "terrain_blend":#{
1500 box
= _
.layout
.box()
1501 box
.prop( active_mat
.SR_data
, "blend_offset" )
1502 box
.prop( active_mat
.SR_data
, "sand_colour" )
1504 elif active_mat
.SR_data
.shader
== "vertex_blend":#{
1505 box
= _
.layout
.box()
1506 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
1507 box
.prop( active_mat
.SR_data
, "blend_offset" )
1509 elif active_mat
.SR_data
.shader
== "water":#{
1510 box
= _
.layout
.box()
1511 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
1512 box
.prop( active_mat
.SR_data
, "shore_colour" )
1513 box
.prop( active_mat
.SR_data
, "ocean_colour" )
1518 def sr_get_type_enum( scene
, context
):
1520 items
= [('none','None',"")]
1521 mesh_entities
=['ent_gate']
1522 point_entities
=['ent_spawn','ent_route_node','ent_route']
1524 for e
in point_entities
: items
+= [(e
,e
,'')]
1526 if context
.scene
.SR_data
.panel
== 'ENTITY': #{
1527 if context
.active_object
.type == 'MESH': #{
1528 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1532 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1538 def sr_on_type_change( _
, context
):
1540 obj
= context
.active_object
1541 ent_type
= obj
.SR_data
.ent_type
1542 if ent_type
== 'none': return
1543 if obj
.type == 'MESH':#{
1544 col
= getattr( obj
.data
.SR_data
, ent_type
, None )
1545 if col
!= None and len(col
)==0: col
.add()
1548 col
= getattr( obj
.SR_data
, ent_type
, None )
1549 if col
!= None and len(col
)==0: col
.add()
1552 class SR_OBJECT_ENT_SPAWN(bpy
.types
.PropertyGroup
):
1554 alias
: bpy
.props
.StringProperty( name
='alias' )
1557 class SR_OBJECT_ENT_GATE(bpy
.types
.PropertyGroup
):
1559 target
: bpy
.props
.PointerProperty( \
1560 type=bpy
.types
.Object
, name
="destination", \
1561 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,'ent_gate'))
1564 class SR_MESH_ENT_GATE(bpy
.types
.PropertyGroup
):
1566 dimensions
: bpy
.props
.FloatVectorProperty(name
="dimensions",size
=3)
1569 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy
.types
.PropertyGroup
):
1571 target
: bpy
.props
.PointerProperty( \
1572 type=bpy
.types
.Object
, name
='target', \
1573 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,'ent_gate'))
1576 class SR_UL_ROUTE_NODE_LIST(bpy
.types
.UIList
):
1578 bl_idname
= 'SR_UL_ROUTE_NODE_LIST'
1580 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
1582 layout
.prop( item
, 'target', text
='', emboss
=False )
1586 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy
.types
.Operator
):
1588 bl_idname
= "skaterift.new_entry"
1589 bl_label
= "Add gate"
1591 def execute(self
, context
):#{
1592 active_object
= context
.active_object
1593 active_object
.SR_data
.ent_route
[0].gates
.add()
1598 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy
.types
.Operator
):
1600 bl_idname
= "skaterift.del_entry"
1601 bl_label
= "Remove gate"
1604 def poll(cls
, context
):#{
1605 active_object
= context
.active_object
1606 if obj_ent_type
== 'ent_gate':#{
1607 return active_object
.SR_data
.ent_route
[0].gates
1612 def execute(self
, context
):#{
1613 active_object
= context
.active_object
1614 lista
= active_object
.SR_data
.ent_route
[0].gates
1615 index
= active_object
.SR_data
.ent_route
[0].gates_index
1617 active_object
.SR_data
.ent_route
[0].gates_index
= \
1618 min(max(0, index
-1), len(lista
) - 1)
1623 class SR_OBJECT_ENT_ROUTE(bpy
.types
.PropertyGroup
):
1625 gates
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY
)
1626 gates_index
: bpy
.props
.IntProperty()
1628 colour
: bpy
.props
.FloatVectorProperty( \
1632 default
=Vector((0.79,0.63,0.48)),\
1633 description
="Route colour"\
1636 alias
: bpy
.props
.StringProperty(\
1638 default
="Untitled Course")
1641 def sr_inspector( layout
, data
):
1643 layout
.prop( data
[0], 'alias' )
1644 layout
.prop( data
[0], 'colour' )
1646 layout
.label( text
='Checkpoints' )
1647 layout
.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
1648 data
[0], 'gates', data
[0], 'gates_index', rows
=5)
1651 row
.operator( 'skaterift.new_entry', text
='Add' )
1652 row
.operator( 'skaterift.del_entry', text
='Remove' )
1656 class SR_OBJECT_PROPERTIES(bpy
.types
.PropertyGroup
):
1658 ent_gate
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GATE
)
1659 ent_spawn
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_SPAWN
)
1660 ent_route
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE
)
1662 ent_type
: bpy
.props
.EnumProperty(
1664 items
=[('none', 'None', '', 0),
1665 ('ent_gate','Gate','', 1),
1666 ('ent_spawn','Spawn','', 2),
1667 ('ent_route_node', 'Route Node', '', 3 ),
1668 ('ent_route', 'Route', '', 4)],
1669 update
=sr_on_type_change
1673 class SR_MESH_PROPERTIES(bpy
.types
.PropertyGroup
):
1675 ent_gate
: bpy
.props
.CollectionProperty(type=SR_MESH_ENT_GATE
)
1678 class SR_LIGHT_PROPERTIES(bpy
.types
.PropertyGroup
):
1680 daytime
: bpy
.props
.BoolProperty( name
='Daytime' )
1683 class SR_BONE_PROPERTIES(bpy
.types
.PropertyGroup
):
1685 collider
: bpy
.props
.EnumProperty( name
='Collider Type',
1686 items
=[('0','none',''),
1688 ('2','capsule','')])
1690 collider_min
: bpy
.props
.FloatVectorProperty( name
='Collider Min', size
=3 )
1691 collider_max
: bpy
.props
.FloatVectorProperty( name
='Collider Max', size
=3 )
1693 cone_constraint
: bpy
.props
.BoolProperty( name
='Cone constraint' )
1695 conevx
: bpy
.props
.FloatVectorProperty( name
='vx' )
1696 conevy
: bpy
.props
.FloatVectorProperty( name
='vy' )
1697 coneva
: bpy
.props
.FloatVectorProperty( name
='va' )
1698 conet
: bpy
.props
.FloatProperty( name
='t' )
1701 def sr_inspector( layout
, data
):
1705 box
.prop( data
, 'collider' )
1707 if int(data
.collider
)>0:#{
1709 row
.prop( data
, 'collider_min' )
1711 row
.prop( data
, 'collider_max' )
1715 box
.prop( data
, 'cone_constraint' )
1716 if data
.cone_constraint
:#{
1718 row
.prop( data
, 'conevx' )
1720 row
.prop( data
, 'conevy' )
1722 row
.prop( data
, 'coneva' )
1723 box
.prop( data
, 'conet' )
1728 class SR_MATERIAL_PROPERTIES(bpy
.types
.PropertyGroup
):
1730 shader
: bpy
.props
.EnumProperty(
1733 ('standard',"standard",''),
1734 ('standard_cutout', "standard_cutout", ''),
1735 ('terrain_blend', "terrain_blend", ''),
1736 ('vertex_blend', "vertex_blend", ''),
1737 ('water',"water",'')
1740 surface_prop
: bpy
.props
.EnumProperty(
1741 name
="Surface Property",
1743 ('0','concrete',''),
1750 collision
: bpy
.props
.BoolProperty( \
1751 name
="Collisions Enabled",\
1753 description
= "Can the player collide with this material"\
1755 skate_surface
: bpy
.props
.BoolProperty( \
1756 name
="Skate Surface", \
1758 description
= "Should the game try to target this surface?" \
1760 grind_surface
: bpy
.props
.BoolProperty( \
1761 name
="Grind Surface", \
1763 description
= "Grind face?" \
1765 grow_grass
: bpy
.props
.BoolProperty( \
1766 name
="Grow Grass", \
1768 description
= "Spawn grass sprites on this surface?" \
1770 blend_offset
: bpy
.props
.FloatVectorProperty( \
1771 name
="Blend Offset", \
1773 default
=Vector((0.5,0.0)),\
1774 description
="When surface is more than 45 degrees, add this vector " +\
1777 sand_colour
: bpy
.props
.FloatVectorProperty( \
1778 name
="Sand Colour",\
1781 default
=Vector((0.79,0.63,0.48)),\
1782 description
="Blend to this colour near the 0 coordinate on UP axis"\
1784 shore_colour
: bpy
.props
.FloatVectorProperty( \
1785 name
="Shore Colour",\
1788 default
=Vector((0.03,0.32,0.61)),\
1789 description
="Water colour at the shoreline"\
1791 ocean_colour
: bpy
.props
.FloatVectorProperty( \
1792 name
="Ocean Colour",\
1795 default
=Vector((0.0,0.006,0.03)),\
1796 description
="Water colour in the deep bits"\
1800 # ---------------------------------------------------------------------------- #
1804 # ---------------------------------------------------------------------------- #
1806 cv_view_draw_handler
= None
1807 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
1809 cv_view_colours
= []
1810 cv_view_course_i
= 0
1812 # Draw axis alligned sphere at position with radius
1814 def cv_draw_sphere( pos
, radius
, colour
):
1816 global cv_view_verts
, cv_view_colours
1818 ly
= pos
+ Vector((0,0,radius
))
1819 lx
= pos
+ Vector((0,radius
,0))
1820 lz
= pos
+ Vector((0,0,radius
))
1822 pi
= 3.14159265358979323846264
1826 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
1830 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
1831 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
1832 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
1834 cv_view_verts
+= [ px
, lx
]
1835 cv_view_verts
+= [ py
, ly
]
1836 cv_view_verts
+= [ pz
, lz
]
1838 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
1847 # Draw axis alligned sphere at position with radius
1849 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
1851 global cv_view_verts
, cv_view_colours
1853 ly
= pos
+ tz
*radius
1854 lx
= pos
+ ty
*radius
1855 lz
= pos
+ tz
*radius
1857 pi
= 3.14159265358979323846264
1861 t
= ((i
+1.0) * 1.0/16.0) * pi
1865 s1
= math
.sin(t
*2.0)
1866 c1
= math
.cos(t
*2.0)
1868 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
1869 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
1870 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
1872 cv_view_verts
+= [ px
, lx
]
1873 cv_view_verts
+= [ py
, ly
]
1874 cv_view_verts
+= [ pz
, lz
]
1876 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
1885 # Draw transformed -1 -> 1 cube
1887 def cv_draw_ucube( transform
, colour
, s
=Vector((1,1,1)), o
=Vector((0,0,0)) ):
1889 global cv_view_verts
, cv_view_colours
1895 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
1896 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
1897 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
1898 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
1899 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
1900 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
1901 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
1902 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
1904 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1905 (0,4),(1,5),(2,6),(3,7)]
1911 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
1912 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
1913 cv_view_colours
+= [colour
, colour
]
1918 # Draw line with colour
1920 def cv_draw_line( p0
, p1
, colour
):
1922 global cv_view_verts
, cv_view_colours
1924 cv_view_verts
+= [p0
,p1
]
1925 cv_view_colours
+= [colour
, colour
]
1929 # Draw line with colour(s)
1931 def cv_draw_line2( p0
, p1
, c0
, c1
):
1933 global cv_view_verts
, cv_view_colours
1935 cv_view_verts
+= [p0
,p1
]
1936 cv_view_colours
+= [c0
,c1
]
1942 def cv_tangent_basis( n
, tx
, ty
):
1944 if abs( n
[0] ) >= 0.57735027:
1965 # Draw coloured arrow
1967 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
1969 global cv_view_verts
, cv_view_colours
1975 tx
= Vector((1,0,0))
1976 ty
= Vector((1,0,0))
1977 cv_tangent_basis( n
, tx
, ty
)
1979 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
1980 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
1984 def cv_draw_line_dotted( p0
, p1
, c0
, dots
=10 ):
1986 global cv_view_verts
, cv_view_colours
1988 for i
in range(dots
):#{
1992 p2
= p0
*(1.0-t0
)+p1
*t0
1993 p3
= p0
*(1.0-t1
)+p1
*t1
1995 cv_view_verts
+= [p2
,p3
]
1996 cv_view_colours
+= [c0
,c0
]
2001 # Drawhandles of a bezier control point
2003 def cv_draw_bhandle( obj
, direction
, colour
):
2005 global cv_view_verts
, cv_view_colours
2008 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2010 cv_view_verts
+= [p0
]
2011 cv_view_verts
+= [h0
]
2012 cv_view_colours
+= [colour
,colour
]
2016 # Draw a bezier curve (at fixed resolution 10)
2018 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2020 global cv_view_verts
, cv_view_colours
2030 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2032 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2033 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2034 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2041 # I think this one extends the handles of the bezier otwards......
2043 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2045 global cv_view_course_i
2047 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2049 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2050 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2051 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2052 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2054 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2058 # Flush the lines buffers. This is called often because god help you if you want
2059 # to do fixed, fast buffers in this catastrophic programming language.
2061 def cv_draw_lines():
2063 global cv_view_shader
, cv_view_verts
, cv_view_colours
2065 if len(cv_view_verts
) < 2:
2068 lines
= batch_for_shader(\
2069 cv_view_shader
, 'LINES', \
2070 { "pos":cv_view_verts
, "color":cv_view_colours
})
2072 lines
.draw( cv_view_shader
)
2075 cv_view_colours
= []
2078 # I dont remember what this does exactly
2080 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2082 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2085 # Semi circle to show the limit. and some lines
2087 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2089 global cv_view_verts
, cv_view_colours
2094 for x
in range(16):#{
2097 a0
= amin
*(1.0-t0
)+amax
*t0
2098 a1
= amin
*(1.0-t1
)+amax
*t1
2100 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2101 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2103 p0
=obj
.matrix_world
@ p0
2104 p1
=obj
.matrix_world
@ p1
2105 cv_view_verts
+= [p0
,p1
]
2106 cv_view_colours
+= [colour
,colour
]
2109 cv_view_verts
+= [p0
,center
]
2110 cv_view_colours
+= [colour
,colour
]
2113 cv_view_verts
+= [p1
,center
]
2114 cv_view_colours
+= [colour
,colour
]
2118 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2119 cv_view_colours
+= [colour
,colour
]
2124 # Cone and twist limit
2126 def draw_cone_twist( center
, vx
, vy
, va
):
2128 global cv_view_verts
, cv_view_colours
2129 axis
= vy
.cross( vx
)
2134 cv_view_verts
+= [center
, center
+va
*size
]
2135 cv_view_colours
+= [ (1,1,1,1), (1,1,1,1) ]
2137 for x
in range(32):#{
2138 t0
= (x
/32) * math
.tau
2139 t1
= ((x
+1)/32) * math
.tau
2146 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
2147 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
2149 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
2150 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
2152 cv_view_verts
+= [center
, p0
, p0
, p1
]
2153 cv_view_colours
+= [ (0,0,0,0), col0
, col0
, col1
]
2159 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
2161 def draw_skeleton_helpers( obj
):
2163 global cv_view_verts
, cv_view_colours
2165 if obj
.data
.pose_position
!= 'REST':#{
2169 for bone
in obj
.data
.bones
:#{
2171 a
= Vector((bone
.SR_data
.collider_min
[0],
2172 bone
.SR_data
.collider_min
[1],
2173 bone
.SR_data
.collider_min
[2]))
2174 b
= Vector((bone
.SR_data
.collider_max
[0],
2175 bone
.SR_data
.collider_max
[1],
2176 bone
.SR_data
.collider_max
[2]))
2178 if bone
.SR_data
.collider
== '1':#{
2180 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
2181 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
2182 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
2183 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
2184 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
2185 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
2186 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
2187 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
2189 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2190 (0,4),(1,5),(2,6),(3,7)]
2196 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2197 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2198 cv_view_colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
2201 elif bone
.SR_data
.collider
== '2':#{
2206 for i
in range(3):#{
2207 if abs(v0
[i
]) > largest
:#{
2208 largest
= abs(v0
[i
])
2213 v1
= Vector((0,0,0))
2214 v1
[major_axis
] = 1.0
2216 tx
= Vector((0,0,0))
2217 ty
= Vector((0,0,0))
2219 cv_tangent_basis( v1
, tx
, ty
)
2220 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
2221 l
= v0
[ major_axis
] - r
*2
2223 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
2224 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
2226 colour
= [0.2,0.2,0.2,1.0]
2227 colour
[major_axis
] = 0.5
2229 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
2230 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
2231 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
2232 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
2233 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
2234 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
2240 center
= obj
.matrix_world
@ c
2241 if bone
.SR_data
.cone_constraint
:#{
2242 vx
= Vector([bone
.SR_data
.conevx
[_
] for _
in range(3)])
2243 vy
= Vector([bone
.SR_data
.conevy
[_
] for _
in range(3)])
2244 va
= Vector([bone
.SR_data
.coneva
[_
] for _
in range(3)])
2245 draw_cone_twist( center
, vx
, vy
, va
)
2250 def cv_ent_gate( obj
):
2252 global cv_view_verts
, cv_view_colours
2254 if obj
.type != 'MESH': return
2256 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
2257 data
= obj
.SR_data
.ent_gate
[0]
2258 dims
= mesh_data
.dimensions
2261 c
= Vector((0,0,dims
[2]))
2263 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
2264 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
2265 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
2266 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
2267 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
2268 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
2269 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
2270 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
2271 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
2273 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
2278 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2279 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2280 cv_view_colours
+= [(1,1,0,1),(1,1,0,1)]
2283 sw
= (0.4,0.4,0.4,0.2)
2284 if data
.target
!= None:
2285 cv_draw_arrow( obj
.location
, data
.target
.location
, sw
)
2288 def dijkstra( graph
, start_node
, target_node
):
2290 unvisited
= [_
for _
in graph
]
2295 shortest_path
[n
] = 9999999.999999
2296 shortest_path
[start_node
] = 0
2299 current_min_node
= None
2300 for n
in unvisited
:#{
2301 if current_min_node
== None:
2302 current_min_node
= n
2303 elif shortest_path
[n
] < shortest_path
[current_min_node
]:
2304 current_min_node
= n
2307 for branch
in graph
[current_min_node
]:#{
2308 tentative_value
= shortest_path
[current_min_node
]
2309 tentative_value
+= graph
[current_min_node
][branch
]
2310 if tentative_value
< shortest_path
[branch
]:#{
2311 shortest_path
[branch
] = tentative_value
2312 previous_nodes
[branch
] = current_min_node
2316 unvisited
.remove(current_min_node
)
2321 while node
!= start_node
:#{
2324 if node
not in previous_nodes
: return None
2325 node
= previous_nodes
[node
]
2328 # Add the start node manually
2329 path
.append(start_node
)
2333 def node_graph( route_nodes
):
2336 for n
in route_nodes
:
2339 for i
in range(len(route_nodes
)-1):#{
2340 for j
in range(i
+1, len(route_nodes
)):#{
2344 v0
= ni
.location
- nj
.location
2348 if ni
.SR_data
.ent_type
== 'ent_gate':
2351 if nj
.SR_data
.ent_type
== 'ent_gate':#{
2357 v1
= gate
.matrix_world
.to_3x3() @ Vector((0,-1,0))
2358 if gate
.SR_data
.ent_gate
[0].target
:
2359 if v1
.dot(v0
) > 0.0: continue
2361 if v1
.dot(v0
) < 0.0: continue
2366 if dist
> 25.0: continue
2367 graph
[route_nodes
[i
].name
][route_nodes
[j
].name
] = dist
2368 graph
[route_nodes
[j
].name
][route_nodes
[i
].name
] = dist
2375 def cv_draw_route( route
, route_nodes
):
2377 pole
= Vector((0.2,0.2,10))
2378 hat
= Vector((1,8,0.2))
2379 cc
= route
.SR_data
.ent_route
[0].colour
2381 cv_draw_ucube(route
.matrix_world
,cc
,Vector((0.5,-7.5,6)),\
2382 Vector((0,-6.5,5.5)))
2383 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5, 0.5,0)) )
2384 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5,-13.5,0)) )
2385 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5, 12)) )
2386 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5,-1)) )
2388 checkpoints
= route
.SR_data
.ent_route
[0].gates
2389 graph
= node_graph( route_nodes
)
2391 for i
in range(len(checkpoints
)):#{
2392 gi
= checkpoints
[i
].target
2393 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
2396 dest
= gi
.SR_data
.ent_gate
[0].target
2398 cv_draw_line_dotted( gi
.location
, dest
.location
, cc
)
2402 if gi
==gj
: continue # error?
2403 if not gi
or not gj
: continue
2405 path
= dijkstra( graph
, gj
.name
, gi
.name
)
2408 for sj
in range(len(path
)-1):#{
2409 o0
= bpy
.data
.objects
[ path
[sj
] ]
2410 o1
= bpy
.data
.objects
[ path
[sj
+1] ]
2411 cv_draw_arrow(o0
.location
,o1
.location
,cc
,1.5)
2415 cv_draw_line_dotted( gi
.location
, gj
.location
, cc
)
2422 global cv_view_shader
2423 global cv_view_verts
2424 global cv_view_colours
2425 global cv_view_course_i
2427 cv_view_course_i
= 0
2429 cv_view_colours
= []
2431 cv_view_shader
.bind()
2432 gpu
.state
.depth_mask_set(False)
2433 gpu
.state
.line_width_set(2.0)
2434 gpu
.state
.face_culling_set('BACK')
2435 gpu
.state
.depth_test_set('LESS')
2436 gpu
.state
.blend_set('NONE')
2441 for obj
in bpy
.context
.collection
.objects
:#{
2442 if obj
.type == 'ARMATURE':#{
2443 if obj
.data
.pose_position
== 'REST':
2444 draw_skeleton_helpers( obj
)
2447 ent_type
= obj_ent_type( obj
)
2449 if ent_type
== 'ent_gate':#{
2451 route_nodes
+= [obj
]
2453 elif ent_type
== 'ent_route_node':
2454 route_nodes
+= [obj
]
2455 elif ent_type
== 'ent_route':
2460 #cv_draw_route_map( route_nodes )
2461 for route
in routes
:#{
2462 cv_draw_route( route
, route_nodes
)
2469 classes
= [ SR_INTERFACE
, SR_MATERIAL_PANEL
,\
2470 SR_COLLECTION_SETTINGS
, SR_SCENE_SETTINGS
, \
2471 SR_COMPILE
, SR_COMPILE_THIS
, SR_MIRROR_BONE_X
,\
2473 SR_OBJECT_ENT_GATE
, SR_MESH_ENT_GATE
, SR_OBJECT_ENT_SPAWN
, \
2474 SR_OBJECT_ENT_ROUTE_ENTRY
, SR_UL_ROUTE_NODE_LIST
, \
2475 SR_OBJECT_ENT_ROUTE
, SR_OT_ROUTE_LIST_NEW_ITEM
,
2476 SR_OT_ROUTE_LIST_DEL_ITEM
,\
2478 SR_OBJECT_PROPERTIES
, SR_LIGHT_PROPERTIES
, SR_BONE_PROPERTIES
,
2479 SR_MESH_PROPERTIES
, SR_MATERIAL_PROPERTIES \
2485 bpy
.utils
.register_class(c
)
2487 bpy
.types
.Scene
.SR_data
= \
2488 bpy
.props
.PointerProperty(type=SR_SCENE_SETTINGS
)
2489 bpy
.types
.Collection
.SR_data
= \
2490 bpy
.props
.PointerProperty(type=SR_COLLECTION_SETTINGS
)
2492 bpy
.types
.Object
.SR_data
= \
2493 bpy
.props
.PointerProperty(type=SR_OBJECT_PROPERTIES
)
2494 bpy
.types
.Light
.SR_data
= \
2495 bpy
.props
.PointerProperty(type=SR_LIGHT_PROPERTIES
)
2496 bpy
.types
.Bone
.SR_data
= \
2497 bpy
.props
.PointerProperty(type=SR_BONE_PROPERTIES
)
2498 bpy
.types
.Mesh
.SR_data
= \
2499 bpy
.props
.PointerProperty(type=SR_MESH_PROPERTIES
)
2500 bpy
.types
.Material
.SR_data
= \
2501 bpy
.props
.PointerProperty(type=SR_MATERIAL_PROPERTIES
)
2503 global cv_view_draw_handler
2504 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
2505 cv_draw
,(),'WINDOW','POST_VIEW')
2511 bpy
.utils
.unregister_class(c
)
2513 global cv_view_draw_handler
2514 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
2517 # ---------------------------------------------------------------------------- #
2521 # ---------------------------------------------------------------------------- #
2523 # Transliteration of: #
2524 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
2526 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
2527 # SPDX-License-Identifier: MIT #
2528 # QOI - The "Quite OK Image" format for fast, lossless image compression #
2530 # ---------------------------------------------------------------------------- #
2532 class qoi_rgba_t(Structure
):
2535 _fields_
= [("r",c_uint8
),
2541 QOI_OP_INDEX
= 0x00 # 00xxxxxx
2542 QOI_OP_DIFF
= 0x40 # 01xxxxxx
2543 QOI_OP_LUMA
= 0x80 # 10xxxxxx
2544 QOI_OP_RUN
= 0xc0 # 11xxxxxx
2545 QOI_OP_RGB
= 0xfe # 11111110
2546 QOI_OP_RGBA
= 0xff # 11111111
2548 QOI_MASK_2
= 0xc0 # 11000000
2550 def qoi_colour_hash( c
):
2552 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
2557 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
2562 return bytearray([ (0xff000000 & v
) >> 24, \
2563 (0x00ff0000 & v
) >> 16, \
2564 (0x0000ff00 & v
) >> 8, \
2568 def qoi_encode( img
):
2572 print(F
"{' ':<30}",end
='\r')
2573 print(F
"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end
='\r')
2575 index
= [ qoi_rgba_t() for _
in range(64) ]
2579 data
.extend( bytearray(c_uint32(0x66696f71)) )
2580 data
.extend( qoi_32bit( img
.size
[0] ) )
2581 data
.extend( qoi_32bit( img
.size
[1] ) )
2582 data
.extend( bytearray(c_uint8(4)) )
2583 data
.extend( bytearray(c_uint8(0)) )
2586 px_prev
= qoi_rgba_t()
2587 px_prev
.r
= c_uint8(0)
2588 px_prev
.g
= c_uint8(0)
2589 px_prev
.b
= c_uint8(0)
2590 px_prev
.a
= c_uint8(255)
2598 px_len
= img
.size
[0] * img
.size
[1]
2599 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
2601 for px_pos
in range( px_len
): #{
2602 idx
= px_pos
* img
.channels
2605 px
.r
= paxels
[idx
+min(0,nc
)]
2606 px
.g
= paxels
[idx
+min(1,nc
)]
2607 px
.b
= paxels
[idx
+min(2,nc
)]
2608 px
.a
= paxels
[idx
+min(3,nc
)]
2610 if qoi_eq( px
, px_prev
): #{
2613 if (run
== 62) or (px_pos
== px_len
-1): #{
2614 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2620 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2624 index_pos
= qoi_colour_hash(px
) % 64
2626 if qoi_eq( index
[index_pos
], px
): #{
2627 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
2630 index
[ index_pos
].r
= px
.r
2631 index
[ index_pos
].g
= px
.g
2632 index
[ index_pos
].b
= px
.b
2633 index
[ index_pos
].a
= px
.a
2635 if px
.a
== px_prev
.a
: #{
2636 vr
= int(px
.r
) - int(px_prev
.r
)
2637 vg
= int(px
.g
) - int(px_prev
.g
)
2638 vb
= int(px
.b
) - int(px_prev
.b
)
2643 if (vr
> -3) and (vr
< 2) and\
2644 (vg
> -3) and (vg
< 2) and\
2645 (vb
> -3) and (vb
< 2):
2647 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
2648 data
.extend( bytearray( c_uint8(op
) ))
2650 elif (vg_r
> -9) and (vg_r
< 8) and\
2651 (vg
> -33) and (vg
< 32 ) and\
2652 (vg_b
> -9) and (vg_b
< 8):
2654 op
= QOI_OP_LUMA |
(vg
+32)
2655 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
2656 data
.extend( bytearray( c_uint8(op
) ) )
2657 data
.extend( bytearray( c_uint8(delta
) ))
2660 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
2661 data
.extend( bytearray( c_uint8(px
.r
) ))
2662 data
.extend( bytearray( c_uint8(px
.g
) ))
2663 data
.extend( bytearray( c_uint8(px
.b
) ))
2667 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
2668 data
.extend( bytearray( c_uint8(px
.r
) ))
2669 data
.extend( bytearray( c_uint8(px
.g
) ))
2670 data
.extend( bytearray( c_uint8(px
.b
) ))
2671 data
.extend( bytearray( c_uint8(px
.a
) ))
2684 data
.extend( bytearray( c_uint8(0) ))
2685 data
.extend( bytearray( c_uint8(1) ))
2686 bytearray_align_to( data
, 16, b
'\x00' )