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 class ent_water(Structure
):
201 _fields_
= [("transform",mdl_transform
),
202 ("max_dist",c_float
),
203 ("reserved0",c_uint32
),
204 ("reserved1",c_uint32
)]
207 def obj_ent_type( obj
):
209 if obj
.type == 'ARMATURE': return 'mdl_armature'
210 elif obj
.type == 'LIGHT': return 'ent_light'
211 else: return obj
.SR_data
.ent_type
214 def sr_filter_ent_type( obj
, ent_type
):
216 if obj
== bpy
.context
.active_object
: return False
218 for c0
in obj
.users_collection
:#{
219 for c1
in bpy
.context
.active_object
.users_collection
:#{
221 return ent_type
== obj_ent_type( obj
)
229 def compile_obj_transform( obj
, transform
):
231 co
= obj
.matrix_world
@ Vector((0,0,0))
232 q
= obj
.matrix_local
.to_quaternion()
237 transform
.co
[0] = co
[0]
238 transform
.co
[1] = co
[2]
239 transform
.co
[2] = -co
[1]
240 transform
.q
[0] = q
[1]
241 transform
.q
[1] = q
[3]
242 transform
.q
[2] = -q
[2]
243 transform
.q
[3] = q
[0]
244 transform
.s
[0] = s
[0]
245 transform
.s
[1] = s
[2]
246 transform
.s
[2] = s
[1]
249 def int_align_to( v
, align
):
251 while(v
%align
)!=0: v
+= 1
255 def bytearray_align_to( buffer, align
, w
=b
'\xaa' ):
257 while (len(buffer) % align
) != 0: buffer.extend(w
)
261 def bytearray_print_hex( s
, w
=16 ):
263 for r
in range((len(s
)+(w
-1))//w
):#{
265 i1
=min((r
+1)*w
,len(s
))
266 print( F
'{r*w:06x}| \x1B[31m', end
='')
267 print( F
"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end
='' )
268 print( "\x1B[0m", end
='')
269 print( ''.join(chr(x
) if (x
>=33 and x
<=126) else '.' for x
in s
[i0
:i1
] ) )
273 def sr_compile_string( s
):
275 if s
in sr_compile
.string_cache
: return sr_compile
.string_cache
[s
]
277 index
= len( sr_compile
.string_data
)
278 sr_compile
.string_cache
[s
] = index
279 sr_compile
.string_data
.extend( s
.encode('utf-8') )
280 sr_compile
.string_data
.extend( b
'\0' )
282 bytearray_align_to( sr_compile
.string_data
, 4 )
286 def material_tex_image(v
):
296 cxr_graph_mapping
= \
298 # Default shader setup
305 "image": "tex_diffuse"
309 "A": material_tex_image("tex_diffuse"),
310 "B": material_tex_image("tex_decal")
317 "Color": material_tex_image("tex_normal")
323 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
325 def material_info(mat
):
329 # Using the cxr_graph_mapping as a reference, go through the shader
330 # graph and gather all $props from it.
332 def _graph_read( node_def
, node
=None, depth
=0 ):#{
339 _graph_read
.extracted
= []
341 for node_idname
in node_def
:#{
342 for n
in mat
.node_tree
.nodes
:#{
343 if n
.name
== node_idname
:#{
344 node_def
= node_def
[node_idname
]
352 for link
in node_def
:#{
353 link_def
= node_def
[link
]
355 if isinstance( link_def
, dict ):#{
357 for x
in node
.inputs
:#{
358 if isinstance( x
, bpy
.types
.NodeSocketColor
):#{
366 if node_link
and node_link
.is_linked
:#{
367 # look for definitions for the connected node type
369 from_node
= node_link
.links
[0].from_node
371 node_name
= from_node
.name
.split('.')[0]
372 if node_name
in link_def
:#{
373 from_node_def
= link_def
[ node_name
]
375 _graph_read( from_node_def
, from_node
, depth
+1 )
379 if "default" in link_def
:#{
380 prop
= link_def
['default']
381 info
[prop
] = node_link
.default_value
387 info
[prop
] = getattr( node
, link
)
392 _graph_read( cxr_graph_mapping
)
396 def sr_pack_file( file, path
, data
):
398 file.path
= sr_compile_string( path
)
399 file.pack_offset
= len( sr_compile
.pack_data
)
400 file.pack_size
= len( data
)
402 sr_compile
.pack_data
.extend( data
)
403 bytearray_align_to( sr_compile
.pack_data
, 16 )
406 def sr_compile_texture( img
):
411 name
= os
.path
.splitext( img
.name
)[0]
413 if name
in sr_compile
.texture_cache
:
414 return sr_compile
.texture_cache
[name
]
416 texture_index
= (len(sr_compile
.texture_data
)//sizeof(mdl_texture
)) +1
421 if sr_compile
.pack_textures
:#{
422 filedata
= qoi_encode( img
)
423 sr_pack_file( tex
.file, name
, filedata
)
426 sr_compile
.texture_cache
[name
] = texture_index
427 sr_compile
.texture_data
.extend( bytearray(tex
) )
431 def sr_compile_material( mat
):
435 if mat
.name
in sr_compile
.material_cache
:
436 return sr_compile
.material_cache
[mat
.name
]
438 index
= (len(sr_compile
.material_data
)//sizeof(mdl_material
))+1
439 sr_compile
.material_cache
[mat
.name
] = index
442 m
.pstr_name
= sr_compile_string( mat
.name
)
445 if mat
.SR_data
.collision
:#{
447 if mat
.SR_data
.skate_surface
: flags |
= 0x1
448 if mat
.SR_data
.grind_surface
: flags |
= (0x8|
0x1)
451 if mat
.SR_data
.grow_grass
: flags |
= 0x4
454 m
.surface_prop
= int(mat
.SR_data
.surface_prop
)
456 if mat
.SR_data
.shader
== 'standard': m
.shader
= 0
457 if mat
.SR_data
.shader
== 'standard_cutout': m
.shader
= 1
458 if mat
.SR_data
.shader
== 'terrain_blend':#{
461 m
.colour
[0] = pow( mat
.SR_data
.sand_colour
[0], 1.0/2.2 )
462 m
.colour
[1] = pow( mat
.SR_data
.sand_colour
[1], 1.0/2.2 )
463 m
.colour
[2] = pow( mat
.SR_data
.sand_colour
[2], 1.0/2.2 )
466 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
467 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
470 if mat
.SR_data
.shader
== 'vertex_blend':#{
473 m
.colour1
[0] = mat
.SR_data
.blend_offset
[0]
474 m
.colour1
[1] = mat
.SR_data
.blend_offset
[1]
477 if mat
.SR_data
.shader
== 'water':#{
480 m
.colour
[0] = pow( mat
.SR_data
.shore_colour
[0], 1.0/2.2 )
481 m
.colour
[1] = pow( mat
.SR_data
.shore_colour
[1], 1.0/2.2 )
482 m
.colour
[2] = pow( mat
.SR_data
.shore_colour
[2], 1.0/2.2 )
484 m
.colour1
[0] = pow( mat
.SR_data
.ocean_colour
[0], 1.0/2.2 )
485 m
.colour1
[1] = pow( mat
.SR_data
.ocean_colour
[1], 1.0/2.2 )
486 m
.colour1
[2] = pow( mat
.SR_data
.ocean_colour
[2], 1.0/2.2 )
490 inf
= material_info( mat
)
492 if mat
.SR_data
.shader
== 'standard' or \
493 mat
.SR_data
.shader
== 'standard_cutout' or \
494 mat
.SR_data
.shader
== 'terrain_blend' or \
495 mat
.SR_data
.shader
== 'vertex_blend':
497 if 'tex_diffuse' in inf
:
498 m
.tex_diffuse
= sr_compile_texture(inf
['tex_diffuse'])
501 sr_compile
.material_data
.extend( bytearray(m
) )
505 def sr_armature_bones( armature
):
507 def _recurse_bone( b
):
510 for c
in b
.children
: yield from _recurse_bone( c
)
513 for b
in armature
.data
.bones
:
515 yield from _recurse_bone( b
)
518 def sr_compile_mesh( obj
):
521 compile_obj_transform(obj
, node
.transform
)
522 node
.pstr_name
= sr_compile_string(obj
.name
)
528 for mod
in obj
.modifiers
:#{
529 if mod
.type == 'DATA_TRANSFER' or mod
.type == 'SHRINKWRAP' or \
530 mod
.type == 'BOOLEAN' or mod
.type == 'CURVE' or \
533 can_use_cache
= False
536 if mod
.type == 'ARMATURE': #{
538 armature
= mod
.object
539 rig_weight_groups
= \
540 ['0 [ROOT]']+[_
.name
for _
in sr_armature_bones(mod
.object)]
541 node
.armature_id
= sr_compile
.entity_ids
[armature
.name
]
543 POSE_OR_REST_CACHE
= armature
.data
.pose_position
544 armature
.data
.pose_position
= 'REST'
548 # Check the cache first
550 if can_use_cache
and (obj
.data
.name
in sr_compile
.mesh_cache
):#{
551 ref
= sr_compile
.mesh_cache
[obj
.data
.name
]
552 node
.submesh_start
= ref
[0]
553 node
.submesh_count
= ref
[1]
554 sr_compile
.mesh_data
.extend(bytearray(node
))
558 # Compile a whole new mesh
560 node
.submesh_start
= len(sr_compile
.submesh_data
)//sizeof(mdl_submesh
)
561 node
.submesh_count
= 0
563 dgraph
= bpy
.context
.evaluated_depsgraph_get()
564 data
= obj
.evaluated_get(dgraph
).data
565 data
.calc_loop_triangles()
566 data
.calc_normals_split()
568 # Mesh is split into submeshes based on their material
570 mat_list
= data
.materials
if len(data
.materials
) > 0 else [None]
571 for material_id
, mat
in enumerate(mat_list
): #{
575 sm
.indice_start
= len(sr_compile
.indice_data
)//sizeof(c_uint32
)
576 sm
.vertex_start
= len(sr_compile
.vertex_data
)//sizeof(mdl_vert
)
579 sm
.material_id
= sr_compile_material( mat
)
581 INF
=99999999.99999999
587 # Keep a reference to very very very similar vertices
588 # i have no idea how to speed it up.
590 vertex_reference
= {}
592 # Write the vertex / indice data
594 for tri_index
, tri
in enumerate(data
.loop_triangles
):#{
595 if tri
.material_index
!= material_id
:
599 vert
= data
.vertices
[tri
.vertices
[j
]]
601 vi
= data
.loops
[li
].vertex_index
603 # Gather vertex information
606 norm
= data
.loops
[li
].normal
608 colour
= (255,255,255,255)
615 uv
= data
.uv_layers
.active
.data
[li
].uv
619 if data
.vertex_colors
:#{
620 colour
= data
.vertex_colors
.active
.data
[li
].color
621 colour
= (int(colour
[0]*255.0),\
622 int(colour
[1]*255.0),\
623 int(colour
[2]*255.0),\
624 int(colour
[3]*255.0))
627 # Weight groups: truncates to the 3 with the most influence. The
628 # fourth bone ID is never used by the shader so it
632 src_groups
= [_
for _
in data
.vertices
[vi
].groups \
633 if obj
.vertex_groups
[_
.group
].name
in \
636 weight_groups
= sorted( src_groups
, key
= \
637 lambda a
: a
.weight
, reverse
=True )
639 for ml
in range(3):#{
640 if len(weight_groups
) > ml
:#{
641 g
= weight_groups
[ml
]
642 name
= obj
.vertex_groups
[g
.group
].name
645 groups
[ml
] = rig_weight_groups
.index(name
)
650 if len(weight_groups
) > 0:#{
651 inv_norm
= (1.0/tot
) * 65535.0
652 for ml
in range(3):#{
653 weights
[ml
] = int( weights
[ml
] * inv_norm
)
654 weights
[ml
] = min( weights
[ml
], 65535 )
655 weights
[ml
] = max( weights
[ml
], 0 )
660 li1
= tri
.loops
[(j
+1)%3]
661 vi1
= data
.loops
[li1
].vertex_index
662 e0
= data
.edges
[ data
.loops
[li
].edge_index
]
664 if e0
.use_freestyle_mark
and \
665 ((e0
.vertices
[0] == vi
and e0
.vertices
[1] == vi1
) or \
666 (e0
.vertices
[0] == vi1
and e0
.vertices
[1] == vi
)):
672 TOLERENCE
= float(10**4)
673 key
= (int(co
[0]*TOLERENCE
+0.5),
674 int(co
[1]*TOLERENCE
+0.5),
675 int(co
[2]*TOLERENCE
+0.5),
676 int(norm
[0]*TOLERENCE
+0.5),
677 int(norm
[1]*TOLERENCE
+0.5),
678 int(norm
[2]*TOLERENCE
+0.5),
679 int(uv
[0]*TOLERENCE
+0.5),
680 int(uv
[1]*TOLERENCE
+0.5),
681 colour
[0], # these guys are already quantized
694 if key
in vertex_reference
:
695 index
= vertex_reference
[key
]
697 index
= bytearray(c_uint32(sm
.vertex_count
))
700 vertex_reference
[key
] = index
710 v
.colour
[0] = colour
[0]
711 v
.colour
[1] = colour
[1]
712 v
.colour
[2] = colour
[2]
713 v
.colour
[3] = colour
[3]
714 v
.weights
[0] = weights
[0]
715 v
.weights
[1] = weights
[1]
716 v
.weights
[2] = weights
[2]
717 v
.weights
[3] = weights
[3]
718 v
.groups
[0] = groups
[0]
719 v
.groups
[1] = groups
[1]
720 v
.groups
[2] = groups
[2]
721 v
.groups
[3] = groups
[3]
724 sm
.bbx
[0][i
] = min( sm
.bbx
[0][i
], v
.co
[i
] )
725 sm
.bbx
[1][i
] = max( sm
.bbx
[1][i
], v
.co
[i
] )
728 sr_compile
.vertex_data
.extend(bytearray(v
))
732 sr_compile
.indice_data
.extend( index
)
736 # Make sure bounding box isn't -inf -> inf if no vertices
738 if sm
.vertex_count
== 0:
743 # Add submesh to encoder
745 sr_compile
.submesh_data
.extend( bytearray(sm
) )
746 node
.submesh_count
+= 1
750 armature
.data
.pose_position
= POSE_OR_REST_CACHE
753 # Save a reference to this node since we want to reuse the submesh indices
755 sr_compile
.mesh_cache
[obj
.data
.name
]=(node
.submesh_start
,node
.submesh_count
)
756 sr_compile
.mesh_data
.extend(bytearray(node
))
759 def sr_compile_armature( obj
):
761 node
= mdl_armature()
762 node
.bone_start
= len(sr_compile
.bone_data
)//sizeof(mdl_bone
)
764 node
.anim_start
= len(sr_compile
.anim_data
)//sizeof(mdl_animation
)
767 bones
= [_
for _
in sr_armature_bones(obj
)]
768 bones_names
= [None]+[_
.name
for _
in bones
]
772 if b
.use_deform
: bone
.flags
= 0x1
773 if b
.parent
: bone
.parent
= bones_names
.index(b
.parent
.name
)
775 bone
.collider
= int(b
.SR_data
.collider
)
777 if bone
.collider
>0:#{
778 bone
.hitbox
[0][0] = b
.SR_data
.collider_min
[0]
779 bone
.hitbox
[0][1] = b
.SR_data
.collider_min
[2]
780 bone
.hitbox
[0][2] = -b
.SR_data
.collider_max
[1]
781 bone
.hitbox
[1][0] = b
.SR_data
.collider_max
[0]
782 bone
.hitbox
[1][1] = b
.SR_data
.collider_max
[2]
783 bone
.hitbox
[1][2] = -b
.SR_data
.collider_min
[1]
786 if b
.SR_data
.cone_constraint
:#{
788 bone
.conevx
[0] = b
.SR_data
.conevx
[0]
789 bone
.conevx
[1] = b
.SR_data
.conevx
[2]
790 bone
.conevx
[2] = -b
.SR_data
.conevx
[1]
791 bone
.conevy
[0] = b
.SR_data
.conevy
[0]
792 bone
.conevy
[1] = b
.SR_data
.conevy
[2]
793 bone
.conevy
[2] = -b
.SR_data
.conevy
[1]
794 bone
.coneva
[0] = b
.SR_data
.coneva
[0]
795 bone
.coneva
[1] = b
.SR_data
.coneva
[2]
796 bone
.coneva
[2] = -b
.SR_data
.coneva
[1]
797 bone
.conet
= b
.SR_data
.conet
800 bone
.co
[0] = b
.head_local
[0]
801 bone
.co
[1] = b
.head_local
[2]
802 bone
.co
[2] = -b
.head_local
[1]
803 bone
.end
[0] = b
.tail_local
[0] - bone
.co
[0]
804 bone
.end
[1] = b
.tail_local
[2] - bone
.co
[1]
805 bone
.end
[2] = -b
.tail_local
[1] - bone
.co
[2]
806 bone
.pstr_name
= sr_compile_string( b
.name
)
808 for c
in obj
.pose
.bones
[b
.name
].constraints
:#{
811 bone
.ik_target
= bones_names
.index(c
.subtarget
)
812 bone
.ik_pole
= bones_names
.index(c
.pole_subtarget
)
817 sr_compile
.bone_data
.extend(bytearray(bone
))
822 if obj
.animation_data
and sr_compile
.pack_animations
: #{
823 # So we can restore later
825 previous_frame
= bpy
.context
.scene
.frame_current
826 previous_action
= obj
.animation_data
.action
827 POSE_OR_REST_CACHE
= obj
.data
.pose_position
828 obj
.data
.pose_position
= 'POSE'
830 for NLALayer
in obj
.animation_data
.nla_tracks
:#{
831 for NLAStrip
in NLALayer
.strips
:#{
834 for a
in bpy
.data
.actions
:#{
835 if a
.name
== NLAStrip
.name
:#{
836 obj
.animation_data
.action
= a
841 # Clip to NLA settings
843 anim_start
= int(NLAStrip
.action_frame_start
)
844 anim_end
= int(NLAStrip
.action_frame_end
)
848 anim
= mdl_animation()
849 anim
.pstr_name
= sr_compile_string( NLAStrip
.action
.name
)
851 anim
.keyframe_start
= len(sr_compile
.keyframe_data
)//\
852 sizeof(mdl_transform
)
853 anim
.length
= anim_end
-anim_start
856 # Export the keyframes
857 for frame
in range(anim_start
,anim_end
):#{
858 bpy
.context
.scene
.frame_set(frame
)
861 pb
= obj
.pose
.bones
[rb
.name
]
863 # relative bone matrix
864 if rb
.parent
is not None:#{
865 offset_mtx
= rb
.parent
.matrix_local
866 offset_mtx
= offset_mtx
.inverted_safe() @ \
869 inv_parent
= pb
.parent
.matrix
@ offset_mtx
870 inv_parent
.invert_safe()
871 fpm
= inv_parent
@ pb
.matrix
874 bone_mtx
= rb
.matrix
.to_4x4()
875 local_inv
= rb
.matrix_local
.inverted_safe()
876 fpm
= bone_mtx
@ local_inv
@ pb
.matrix
879 loc
, rot
, sca
= fpm
.decompose()
882 lc_m
= pb
.matrix_channel
.to_3x3()
883 if pb
.parent
is not None:#{
884 smtx
= pb
.parent
.matrix_channel
.to_3x3()
885 lc_m
= smtx
.inverted() @ lc_m
887 rq
= lc_m
.to_quaternion()
900 sr_compile
.keyframe_data
.extend(bytearray(kf
))
906 # Add to animation buffer
908 sr_compile
.anim_data
.extend(bytearray(anim
))
913 print( F
"[SR] | anim( {NLAStrip.action.name} )" )
917 # Restore context to how it was before
919 bpy
.context
.scene
.frame_set( previous_frame
)
920 obj
.animation_data
.action
= previous_action
921 obj
.data
.pose_position
= POSE_OR_REST_CACHE
924 sr_compile
.armature_data
.extend(bytearray(node
))
927 def sr_ent_push( struct
):
929 clase
= type(struct
).__name
__
931 if clase
not in sr_compile
.entity_data
:#{
932 sr_compile
.entity_data
[ clase
] = bytearray()
933 sr_compile
.entity_info
[ clase
] = { 'size': sizeof(struct
) }
936 index
= len(sr_compile
.entity_data
[ clase
])//sizeof(struct
)
937 sr_compile
.entity_data
[ clase
].extend( bytearray(struct
) )
941 def sr_array_title( arr
, name
, count
, size
, offset
):
943 for i
in range(len(name
)):#{
944 arr
.name
[i
] = ord(name
[i
])
946 arr
.file_offset
= offset
947 arr
.item_count
= count
951 def sr_compile( collection
):
953 print( F
"[SR] compiler begin ({collection.name}.mdl)" )
956 sr_compile
.pack_textures
= collection
.SR_data
.pack_textures
957 sr_compile
.pack_animations
= collection
.SR_data
.animations
960 sr_compile
.string_cache
= {}
961 sr_compile
.mesh_cache
= {}
962 sr_compile
.material_cache
= {}
963 sr_compile
.texture_cache
= {}
966 sr_compile
.mesh_data
= bytearray()
967 sr_compile
.submesh_data
= bytearray()
968 sr_compile
.vertex_data
= bytearray()
969 sr_compile
.indice_data
= bytearray()
970 sr_compile
.bone_data
= bytearray()
971 sr_compile
.material_data
= bytearray()
972 sr_compile
.armature_data
= bytearray()
973 sr_compile
.anim_data
= bytearray()
974 sr_compile
.keyframe_data
= bytearray()
975 sr_compile
.texture_data
= bytearray()
977 # just bytes not structures
978 sr_compile
.string_data
= bytearray()
979 sr_compile
.pack_data
= bytearray()
982 sr_compile
.entity_data
= {}
983 sr_compile
.entity_info
= {}
985 print( F
"[SR] assign entity ID's" )
986 sr_compile
.entities
= {}
987 sr_compile
.entity_ids
= {}
990 for obj
in collection
.all_objects
: #{
991 if obj
.type == 'MESH': mesh_count
+= 1
993 ent_type
= obj_ent_type( obj
)
994 if ent_type
== 'none': continue
996 if ent_type
not in sr_compile
.entities
: sr_compile
.entities
[ent_type
] = []
997 sr_compile
.entity_ids
[obj
.name
] = len( sr_compile
.entities
[ent_type
] )
998 sr_compile
.entities
[ent_type
] += [obj
]
1001 print( F
"[SR] Compiling geometry" )
1003 for obj
in collection
.all_objects
:#{
1004 if obj
.type == 'MESH':#{
1006 print( F
'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end
='\r' )
1007 sr_compile_mesh( obj
)
1011 checkpoint_count
= 0
1012 pathindice_count
= 0
1014 for ent_type
, arr
in sr_compile
.entities
.items():#{
1015 print(F
"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1017 for i
in range(len(arr
)):#{
1020 print( F
"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end
='\r' )
1022 if ent_type
== 'mdl_armature': sr_compile_armature(obj
)
1023 elif ent_type
== 'ent_light': #{
1025 compile_obj_transform( obj
, light
.transform
)
1026 light
.daytime
= obj
.data
.SR_data
.daytime
1027 if obj
.data
.type == 'POINT':#{
1030 elif obj
.data
.type == 'SPOT':#{
1032 light
.angle
= obj
.data
.spot_size
*0.5
1034 light
.range = obj
.data
.cutoff_distance
1035 light
.colour
[0] = obj
.data
.color
[0]
1036 light
.colour
[1] = obj
.data
.color
[1]
1037 light
.colour
[2] = obj
.data
.color
[2]
1038 light
.colour
[3] = obj
.data
.energy
1039 sr_ent_push( light
)
1041 elif ent_type
== 'ent_gate': #{
1044 obj_data
= obj
.SR_data
.ent_gate
[0]
1045 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
1046 if obj_data
.target
:#{
1047 gate
.target
= sr_compile
.entity_ids
[obj_data
.target
.name
]
1050 gate
.dimensions
[0] = mesh_data
.dimensions
[0]
1051 gate
.dimensions
[1] = mesh_data
.dimensions
[1]
1052 gate
.dimensions
[2] = mesh_data
.dimensions
[2]
1054 q
= [obj
.matrix_local
.to_quaternion(), (0,0,0,1)]
1055 co
= [obj
.matrix_world
@ Vector((0,0,0)), (0,0,0)]
1057 if obj_data
.target
:#{
1058 q
[1] = obj_data
.target
.matrix_local
.to_quaternion()
1059 co
[1]= obj_data
.target
.matrix_world
@ Vector((0,0,0))
1064 for x
in range(2):#{
1065 gate
.co
[x
][0] = co
[x
][0]
1066 gate
.co
[x
][1] = co
[x
][2]
1067 gate
.co
[x
][2] = -co
[x
][1]
1068 gate
.q
[x
][0] = q
[x
][1]
1069 gate
.q
[x
][1] = q
[x
][3]
1070 gate
.q
[x
][2] = -q
[x
][2]
1071 gate
.q
[x
][3] = q
[x
][0]
1076 elif ent_type
== 'ent_spawn': #{
1078 compile_obj_transform( obj
, spawn
.transform
)
1079 obj_data
= obj
.SR_data
.ent_spawn
[0]
1080 spawn
.pstr_name
= sr_compile_string( obj_data
.name
)
1081 sr_ent_push( spawn
)
1083 elif ent_type
== 'ent_route': #{
1084 obj_data
= obj
.SR_data
.ent_route
[0]
1086 route
.pstr_name
= sr_compile_string( obj_data
.alias
)
1087 route
.checkpoints_start
= checkpoint_count
1088 route
.checkpoints_count
= 0
1091 route
.colour
[ci
] = obj_data
.colour
[ci
]
1092 route
.colour
[3] = 1.0
1094 compile_obj_transform( obj
, route
.transform
)
1096 checkpoints
= obj_data
.gates
1099 for uc
in obj
.users_collection
[0].objects
:#{
1100 uc_type
= obj_ent_type( uc
)
1101 if uc_type
== 'ent_gate' or uc_type
== 'ent_route_node':
1104 graph
= node_graph( route_nodes
)
1106 for i
in range(len(checkpoints
)):#{
1107 gi
= checkpoints
[i
].target
1108 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
1112 dest
= gi
.SR_data
.ent_gate
[0].target
1116 if gi
==gj
: continue # error?
1117 if not gi
or not gj
: continue
1119 checkpoint
= ent_checkpoint()
1120 checkpoint
.gate_index
= sr_compile
.entity_ids
[gate
.name
]
1121 checkpoint
.path_start
= pathindice_count
1122 checkpoint
.path_count
= 0
1124 path
= dijkstra( graph
, gj
.name
, gi
.name
)
1126 for pi
in range(1,len(path
)-1):#{
1127 pathindice
= ent_path_index()
1128 pathindice
.index
= sr_compile
.entity_ids
[path
[pi
]]
1129 sr_ent_push( pathindice
)
1131 checkpoint
.path_count
+= 1
1132 pathindice_count
+= 1
1136 sr_ent_push( checkpoint
)
1137 route
.checkpoints_count
+= 1
1138 checkpoint_count
+= 1
1141 sr_ent_push( route
)
1143 elif ent_type
== 'ent_route_node':#{
1144 rn
= ent_route_node()
1145 rn
.co
[0] = obj
.location
[0]
1146 rn
.co
[1] = obj
.location
[2]
1147 rn
.co
[2] = -obj
.location
[1]
1150 elif ent_type
== 'ent_water':#{
1152 compile_obj_transform( obj
, water
.transform
)
1153 water
.max_dist
= 0.0
1154 sr_ent_push( water
)
1159 print( F
"[SR] Writing file" )
1161 file_array_instructions
= {}
1164 def _write_array( name
, item_size
, data
):#{
1165 nonlocal file_array_instructions
, file_offset
1167 count
= len(data
)//item_size
1168 file_array_instructions
[name
] = {'count':count
, 'size':item_size
,\
1169 'data':data
, 'offset': file_offset
}
1170 file_offset
+= len(data
)
1171 file_offset
= int_align_to( file_offset
, 8 )
1174 _write_array( 'strings', 1, sr_compile
.string_data
)
1175 _write_array( 'mdl_mesh', sizeof(mdl_mesh
), sr_compile
.mesh_data
)
1176 _write_array( 'mdl_submesh', sizeof(mdl_submesh
), sr_compile
.submesh_data
)
1177 _write_array( 'mdl_material', sizeof(mdl_material
), sr_compile
.material_data
)
1178 _write_array( 'mdl_texture', sizeof(mdl_texture
), sr_compile
.texture_data
)
1179 _write_array( 'mdl_armature', sizeof(mdl_armature
), sr_compile
.armature_data
)
1180 _write_array( 'mdl_bone', sizeof(mdl_bone
), sr_compile
.bone_data
)
1182 for name
, buffer in sr_compile
.entity_data
.items():#{
1183 _write_array( name
, sr_compile
.entity_info
[name
]['size'], buffer )
1186 _write_array( 'mdl_animation', sizeof(mdl_animation
), sr_compile
.anim_data
)
1187 _write_array( 'mdl_keyframe', sizeof(mdl_transform
),sr_compile
.keyframe_data
)
1188 _write_array( 'mdl_vert', sizeof(mdl_vert
), sr_compile
.vertex_data
)
1189 _write_array( 'mdl_indice', sizeof(c_uint32
), sr_compile
.indice_data
)
1190 _write_array( 'pack', 1, sr_compile
.pack_data
)
1192 header_size
= int_align_to( sizeof(mdl_header
), 8 )
1193 index_size
= int_align_to( sizeof(mdl_array
)*len(file_array_instructions
),8 )
1195 folder
= bpy
.path
.abspath(bpy
.context
.scene
.SR_data
.export_dir
)
1196 path
= F
"{folder}{collection.name}.mdl"
1199 fp
= open( path
, "wb" )
1200 header
= mdl_header()
1202 sr_array_title( header
.arrays
, \
1203 'index', len(file_array_instructions
), \
1204 sizeof(mdl_array
), header_size
)
1206 fp
.write( bytearray_align_to( bytearray(header
), 8 ) )
1208 print( F
'[SR] {"name":>16}| count | offset' )
1210 for name
,info
in file_array_instructions
.items():#{
1212 offset
= info
['offset'] + header_size
+ index_size
1213 sr_array_title( arr
, name
, info
['count'], info
['size'], offset
)
1214 index
.extend( bytearray(arr
) )
1216 print( F
'[SR] {name:>16}| {info["count"]: 8} '+\
1217 F
' 0x{info["offset"]:02x}' )
1219 fp
.write( bytearray_align_to( index
, 8 ) )
1220 #bytearray_print_hex( index )
1222 for name
,info
in file_array_instructions
.items():#{
1223 fp
.write( bytearray_align_to( info
['data'], 8 ) )
1228 print( '[SR] done' )
1231 class SR_SCENE_SETTINGS(bpy
.types
.PropertyGroup
):
1233 use_hidden
: bpy
.props
.BoolProperty( name
="use hidden", default
=False )
1234 export_dir
: bpy
.props
.StringProperty( name
="Export Dir", subtype
='DIR_PATH' )
1235 gizmos
: bpy
.props
.BoolProperty( name
="Draw Gizmos", default
=True )
1237 panel
: bpy
.props
.EnumProperty(
1241 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
1242 ('ENTITY', 'Entity', '', 'MONKEY',1),
1243 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
1248 class SR_COLLECTION_SETTINGS(bpy
.types
.PropertyGroup
):
1250 pack_textures
: bpy
.props
.BoolProperty( name
="Pack Textures", default
=False )
1251 animations
: bpy
.props
.BoolProperty( name
="Export animation", default
=True)
1254 def sr_get_mirror_bone( bones
):
1256 side
= bones
.active
.name
[-1:]
1257 other_name
= bones
.active
.name
[:-1]
1258 if side
== 'L': other_name
+= 'R'
1259 elif side
== 'R': other_name
+= 'L'
1263 if b
.name
== other_name
:
1270 class SR_MIRROR_BONE_X(bpy
.types
.Operator
):
1272 bl_idname
="skaterift.mirror_bone"
1273 bl_label
="Mirror bone attributes - SkateRift"
1275 def execute(_
,context
):
1277 active_object
= context
.active_object
1278 bones
= active_object
.data
.bones
1280 b
= sr_get_mirror_bone( bones
)
1282 if not b
: return {'FINISHED'}
1284 b
.SR_data
.collider
= a
.SR_data
.collider
1286 def _v3copyflipy( a
, b
):#{
1292 _v3copyflipy( a
.SR_data
.collider_min
, b
.SR_data
.collider_min
)
1293 _v3copyflipy( a
.SR_data
.collider_max
, b
.SR_data
.collider_max
)
1294 b
.SR_data
.collider_min
[1] = -a
.SR_data
.collider_max
[1]
1295 b
.SR_data
.collider_max
[1] = -a
.SR_data
.collider_min
[1]
1297 b
.SR_data
.cone_constraint
= a
.SR_data
.cone_constraint
1299 _v3copyflipy( a
.SR_data
.conevx
, b
.SR_data
.conevy
)
1300 _v3copyflipy( a
.SR_data
.conevy
, b
.SR_data
.conevx
)
1301 _v3copyflipy( a
.SR_data
.coneva
, b
.SR_data
.coneva
)
1303 b
.SR_data
.conet
= a
.SR_data
.conet
1306 ob
= bpy
.context
.scene
.objects
[0]
1307 ob
.hide_render
= ob
.hide_render
1312 class SR_COMPILE(bpy
.types
.Operator
):
1314 bl_idname
="skaterift.compile_all"
1315 bl_label
="Compile All"
1317 def execute(_
,context
):
1319 view_layer
= bpy
.context
.view_layer
1320 for col
in view_layer
.layer_collection
.children
["export"].children
:
1321 if not col
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1322 sr_compile( bpy
.data
.collections
[col
.name
] )
1328 class SR_COMPILE_THIS(bpy
.types
.Operator
):
1330 bl_idname
="skaterift.compile_this"
1331 bl_label
="Compile This collection"
1333 def execute(_
,context
):
1335 col
= bpy
.context
.collection
1342 class SR_INTERFACE(bpy
.types
.Panel
):
1344 bl_idname
= "VIEW3D_PT_skate_rift"
1345 bl_label
= "Skate Rift"
1346 bl_space_type
= 'VIEW_3D'
1347 bl_region_type
= 'UI'
1348 bl_category
= "Skate Rift"
1350 def draw(_
, context
):
1354 row
= _
.layout
.row()
1356 row
.prop( context
.scene
.SR_data
, 'panel', expand
=True )
1358 if context
.scene
.SR_data
.panel
== 'SETTINGS': #{
1359 _
.layout
.prop( context
.scene
.SR_data
, 'gizmos' )
1361 elif context
.scene
.SR_data
.panel
== 'EXPORT': #{
1362 _
.layout
.prop( context
.scene
.SR_data
, "export_dir" )
1363 col
= bpy
.context
.collection
1365 found_in_export
= False
1367 view_layer
= bpy
.context
.view_layer
1368 for c1
in view_layer
.layer_collection
.children
["export"].children
: #{
1369 if not c1
.hide_viewport
or bpy
.context
.scene
.SR_data
.use_hidden
:
1372 if c1
.name
== col
.name
: #{
1373 found_in_export
= True
1377 box
= _
.layout
.box()
1379 row
.alignment
= 'CENTER'
1382 if found_in_export
: #{
1383 row
.label( text
=col
.name
+ ".mdl" )
1384 box
.prop( col
.SR_data
, "pack_textures" )
1385 box
.prop( col
.SR_data
, "animations" )
1386 box
.operator( "skaterift.compile_this" )
1390 row
.label( text
=col
.name
)
1394 row
.alignment
= 'CENTER'
1396 row
.label( text
="This collection is not in the export group" )
1399 box
= _
.layout
.box()
1402 split
= row
.split( factor
=0.3, align
=True )
1403 split
.prop( context
.scene
.SR_data
, "use_hidden", text
="hidden" )
1406 if export_count
== 0:
1408 row1
.operator( "skaterift.compile_all", \
1409 text
=F
"Compile all ({export_count} collections)" )
1411 elif context
.scene
.SR_data
.panel
== 'ENTITY': #{
1412 active_object
= context
.active_object
1413 if not active_object
: return
1415 box
= _
.layout
.box()
1417 row
.alignment
= 'CENTER'
1418 row
.label( text
=active_object
.name
)
1421 def _draw_prop_collection( data
): #{
1424 row
.alignment
= 'CENTER'
1427 row
.label( text
=F
'{data[0]}' )
1429 if hasattr(type(data
[0]),'sr_inspector'):#{
1430 type(data
[0]).sr_inspector( box
, data
)
1433 for a
in data
[0].__annotations
__:
1434 box
.prop( data
[0], a
)
1438 if active_object
.type == 'ARMATURE': #{
1439 if active_object
.mode
== 'POSE': #{
1440 bones
= active_object
.data
.bones
1441 mb
= sr_get_mirror_bone( bones
)
1443 box
.operator( "skaterift.mirror_bone", \
1444 text
=F
'Mirror attributes to {mb.name}' )
1447 _draw_prop_collection( [bones
.active
.SR_data
] )
1451 row
.alignment
='CENTER'
1454 row
.label( text
="Enter pose mode to modify bone properties" )
1457 elif active_object
.type == 'LIGHT': #{
1458 _draw_prop_collection( [active_object
.data
.SR_data
] )
1460 elif active_object
.type == 'EMPTY' or active_object
.type == 'MESH': #{
1461 box
.prop( active_object
.SR_data
, "ent_type" )
1462 ent_type
= active_object
.SR_data
.ent_type
1464 col
= getattr( active_object
.SR_data
, ent_type
, None )
1465 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1467 if active_object
.type == 'MESH':#{
1468 col
= getattr( active_object
.data
.SR_data
, ent_type
, None )
1469 if col
!= None and len(col
)!=0: _draw_prop_collection( col
)
1476 class SR_MATERIAL_PANEL(bpy
.types
.Panel
):
1478 bl_label
="Skate Rift material"
1479 bl_idname
="MATERIAL_PT_sr_material"
1480 bl_space_type
='PROPERTIES'
1481 bl_region_type
='WINDOW'
1482 bl_context
="material"
1484 def draw(_
,context
):
1486 active_object
= bpy
.context
.active_object
1487 if active_object
== None: return
1488 active_mat
= active_object
.active_material
1489 if active_mat
== None: return
1491 info
= material_info( active_mat
)
1493 if 'tex_diffuse' in info
:#{
1494 _
.layout
.label( icon
='INFO', \
1495 text
=F
"{info['tex_diffuse'].name} will be compiled" )
1498 _
.layout
.prop( active_mat
.SR_data
, "shader" )
1499 _
.layout
.prop( active_mat
.SR_data
, "surface_prop" )
1500 _
.layout
.prop( active_mat
.SR_data
, "collision" )
1502 if active_mat
.SR_data
.collision
:#{
1503 _
.layout
.prop( active_mat
.SR_data
, "skate_surface" )
1504 _
.layout
.prop( active_mat
.SR_data
, "grind_surface" )
1505 _
.layout
.prop( active_mat
.SR_data
, "grow_grass" )
1508 if active_mat
.SR_data
.shader
== "terrain_blend":#{
1509 box
= _
.layout
.box()
1510 box
.prop( active_mat
.SR_data
, "blend_offset" )
1511 box
.prop( active_mat
.SR_data
, "sand_colour" )
1513 elif active_mat
.SR_data
.shader
== "vertex_blend":#{
1514 box
= _
.layout
.box()
1515 box
.label( icon
='INFO', text
="Uses vertex colours, the R channel" )
1516 box
.prop( active_mat
.SR_data
, "blend_offset" )
1518 elif active_mat
.SR_data
.shader
== "water":#{
1519 box
= _
.layout
.box()
1520 box
.label( icon
='INFO', text
="Depth scale of 16 meters" )
1521 box
.prop( active_mat
.SR_data
, "shore_colour" )
1522 box
.prop( active_mat
.SR_data
, "ocean_colour" )
1527 def sr_get_type_enum( scene
, context
):
1529 items
= [('none','None',"")]
1530 mesh_entities
=['ent_gate','ent_water']
1531 point_entities
=['ent_spawn','ent_route_node','ent_route']
1533 for e
in point_entities
: items
+= [(e
,e
,'')]
1535 if context
.scene
.SR_data
.panel
== 'ENTITY': #{
1536 if context
.active_object
.type == 'MESH': #{
1537 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1541 for e
in mesh_entities
: items
+= [(e
,e
,'')]
1547 def sr_on_type_change( _
, context
):
1549 obj
= context
.active_object
1550 ent_type
= obj
.SR_data
.ent_type
1551 if ent_type
== 'none': return
1552 if obj
.type == 'MESH':#{
1553 col
= getattr( obj
.data
.SR_data
, ent_type
, None )
1554 if col
!= None and len(col
)==0: col
.add()
1557 col
= getattr( obj
.SR_data
, ent_type
, None )
1558 if col
!= None and len(col
)==0: col
.add()
1561 class SR_OBJECT_ENT_SPAWN(bpy
.types
.PropertyGroup
):
1563 alias
: bpy
.props
.StringProperty( name
='alias' )
1566 class SR_OBJECT_ENT_GATE(bpy
.types
.PropertyGroup
):
1568 target
: bpy
.props
.PointerProperty( \
1569 type=bpy
.types
.Object
, name
="destination", \
1570 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,'ent_gate'))
1573 class SR_MESH_ENT_GATE(bpy
.types
.PropertyGroup
):
1575 dimensions
: bpy
.props
.FloatVectorProperty(name
="dimensions",size
=3)
1578 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy
.types
.PropertyGroup
):
1580 target
: bpy
.props
.PointerProperty( \
1581 type=bpy
.types
.Object
, name
='target', \
1582 poll
=lambda self
,obj
: sr_filter_ent_type(obj
,'ent_gate'))
1585 class SR_UL_ROUTE_NODE_LIST(bpy
.types
.UIList
):
1587 bl_idname
= 'SR_UL_ROUTE_NODE_LIST'
1589 def draw_item(_
,context
,layout
,data
,item
,icon
,active_data
,active_propname
):
1591 layout
.prop( item
, 'target', text
='', emboss
=False )
1595 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy
.types
.Operator
):
1597 bl_idname
= "skaterift.new_entry"
1598 bl_label
= "Add gate"
1600 def execute(self
, context
):#{
1601 active_object
= context
.active_object
1602 active_object
.SR_data
.ent_route
[0].gates
.add()
1607 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy
.types
.Operator
):
1609 bl_idname
= "skaterift.del_entry"
1610 bl_label
= "Remove gate"
1613 def poll(cls
, context
):#{
1614 active_object
= context
.active_object
1615 if obj_ent_type
== 'ent_gate':#{
1616 return active_object
.SR_data
.ent_route
[0].gates
1621 def execute(self
, context
):#{
1622 active_object
= context
.active_object
1623 lista
= active_object
.SR_data
.ent_route
[0].gates
1624 index
= active_object
.SR_data
.ent_route
[0].gates_index
1626 active_object
.SR_data
.ent_route
[0].gates_index
= \
1627 min(max(0, index
-1), len(lista
) - 1)
1632 class SR_OBJECT_ENT_ROUTE(bpy
.types
.PropertyGroup
):
1634 gates
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY
)
1635 gates_index
: bpy
.props
.IntProperty()
1637 colour
: bpy
.props
.FloatVectorProperty( \
1641 default
=Vector((0.79,0.63,0.48)),\
1642 description
="Route colour"\
1645 alias
: bpy
.props
.StringProperty(\
1647 default
="Untitled Course")
1650 def sr_inspector( layout
, data
):
1652 layout
.prop( data
[0], 'alias' )
1653 layout
.prop( data
[0], 'colour' )
1655 layout
.label( text
='Checkpoints' )
1656 layout
.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
1657 data
[0], 'gates', data
[0], 'gates_index', rows
=5)
1660 row
.operator( 'skaterift.new_entry', text
='Add' )
1661 row
.operator( 'skaterift.del_entry', text
='Remove' )
1665 class SR_OBJECT_PROPERTIES(bpy
.types
.PropertyGroup
):
1667 ent_gate
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_GATE
)
1668 ent_spawn
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_SPAWN
)
1669 ent_route
: bpy
.props
.CollectionProperty(type=SR_OBJECT_ENT_ROUTE
)
1671 ent_type
: bpy
.props
.EnumProperty(
1673 items
=[('none', 'None', '', 0),
1674 ('ent_gate','Gate','', 1),
1675 ('ent_spawn','Spawn','', 2),
1676 ('ent_route_node', 'Route Node', '', 3 ),
1677 ('ent_route', 'Route', '', 4),
1678 ('ent_water', 'Water Surface', '', 5)],
1679 update
=sr_on_type_change
1683 class SR_MESH_PROPERTIES(bpy
.types
.PropertyGroup
):
1685 ent_gate
: bpy
.props
.CollectionProperty(type=SR_MESH_ENT_GATE
)
1688 class SR_LIGHT_PROPERTIES(bpy
.types
.PropertyGroup
):
1690 daytime
: bpy
.props
.BoolProperty( name
='Daytime' )
1693 class SR_BONE_PROPERTIES(bpy
.types
.PropertyGroup
):
1695 collider
: bpy
.props
.EnumProperty( name
='Collider Type',
1696 items
=[('0','none',''),
1698 ('2','capsule','')])
1700 collider_min
: bpy
.props
.FloatVectorProperty( name
='Collider Min', size
=3 )
1701 collider_max
: bpy
.props
.FloatVectorProperty( name
='Collider Max', size
=3 )
1703 cone_constraint
: bpy
.props
.BoolProperty( name
='Cone constraint' )
1705 conevx
: bpy
.props
.FloatVectorProperty( name
='vx' )
1706 conevy
: bpy
.props
.FloatVectorProperty( name
='vy' )
1707 coneva
: bpy
.props
.FloatVectorProperty( name
='va' )
1708 conet
: bpy
.props
.FloatProperty( name
='t' )
1711 def sr_inspector( layout
, data
):
1715 box
.prop( data
, 'collider' )
1717 if int(data
.collider
)>0:#{
1719 row
.prop( data
, 'collider_min' )
1721 row
.prop( data
, 'collider_max' )
1725 box
.prop( data
, 'cone_constraint' )
1726 if data
.cone_constraint
:#{
1728 row
.prop( data
, 'conevx' )
1730 row
.prop( data
, 'conevy' )
1732 row
.prop( data
, 'coneva' )
1733 box
.prop( data
, 'conet' )
1738 class SR_MATERIAL_PROPERTIES(bpy
.types
.PropertyGroup
):
1740 shader
: bpy
.props
.EnumProperty(
1743 ('standard',"standard",''),
1744 ('standard_cutout', "standard_cutout", ''),
1745 ('terrain_blend', "terrain_blend", ''),
1746 ('vertex_blend', "vertex_blend", ''),
1747 ('water',"water",'')
1750 surface_prop
: bpy
.props
.EnumProperty(
1751 name
="Surface Property",
1753 ('0','concrete',''),
1760 collision
: bpy
.props
.BoolProperty( \
1761 name
="Collisions Enabled",\
1763 description
= "Can the player collide with this material"\
1765 skate_surface
: bpy
.props
.BoolProperty( \
1766 name
="Skate Surface", \
1768 description
= "Should the game try to target this surface?" \
1770 grind_surface
: bpy
.props
.BoolProperty( \
1771 name
="Grind Surface", \
1773 description
= "Grind face?" \
1775 grow_grass
: bpy
.props
.BoolProperty( \
1776 name
="Grow Grass", \
1778 description
= "Spawn grass sprites on this surface?" \
1780 blend_offset
: bpy
.props
.FloatVectorProperty( \
1781 name
="Blend Offset", \
1783 default
=Vector((0.5,0.0)),\
1784 description
="When surface is more than 45 degrees, add this vector " +\
1787 sand_colour
: bpy
.props
.FloatVectorProperty( \
1788 name
="Sand Colour",\
1791 default
=Vector((0.79,0.63,0.48)),\
1792 description
="Blend to this colour near the 0 coordinate on UP axis"\
1794 shore_colour
: bpy
.props
.FloatVectorProperty( \
1795 name
="Shore Colour",\
1798 default
=Vector((0.03,0.32,0.61)),\
1799 description
="Water colour at the shoreline"\
1801 ocean_colour
: bpy
.props
.FloatVectorProperty( \
1802 name
="Ocean Colour",\
1805 default
=Vector((0.0,0.006,0.03)),\
1806 description
="Water colour in the deep bits"\
1810 # ---------------------------------------------------------------------------- #
1814 # ---------------------------------------------------------------------------- #
1816 cv_view_draw_handler
= None
1817 cv_view_shader
= gpu
.shader
.from_builtin('3D_SMOOTH_COLOR')
1819 cv_view_colours
= []
1820 cv_view_course_i
= 0
1822 # Draw axis alligned sphere at position with radius
1824 def cv_draw_sphere( pos
, radius
, colour
):
1826 global cv_view_verts
, cv_view_colours
1828 ly
= pos
+ Vector((0,0,radius
))
1829 lx
= pos
+ Vector((0,radius
,0))
1830 lz
= pos
+ Vector((0,0,radius
))
1832 pi
= 3.14159265358979323846264
1836 t
= ((i
+1.0) * 1.0/16.0) * pi
* 2.0
1840 py
= pos
+ Vector((s
*radius
,0.0,c
*radius
))
1841 px
= pos
+ Vector((s
*radius
,c
*radius
,0.0))
1842 pz
= pos
+ Vector((0.0,s
*radius
,c
*radius
))
1844 cv_view_verts
+= [ px
, lx
]
1845 cv_view_verts
+= [ py
, ly
]
1846 cv_view_verts
+= [ pz
, lz
]
1848 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
1857 # Draw axis alligned sphere at position with radius
1859 def cv_draw_halfsphere( pos
, tx
, ty
, tz
, radius
, colour
):
1861 global cv_view_verts
, cv_view_colours
1863 ly
= pos
+ tz
*radius
1864 lx
= pos
+ ty
*radius
1865 lz
= pos
+ tz
*radius
1867 pi
= 3.14159265358979323846264
1871 t
= ((i
+1.0) * 1.0/16.0) * pi
1875 s1
= math
.sin(t
*2.0)
1876 c1
= math
.cos(t
*2.0)
1878 py
= pos
+ s
*tx
*radius
+ c
*tz
*radius
1879 px
= pos
+ s
*tx
*radius
+ c
*ty
*radius
1880 pz
= pos
+ s1
*ty
*radius
+ c1
*tz
*radius
1882 cv_view_verts
+= [ px
, lx
]
1883 cv_view_verts
+= [ py
, ly
]
1884 cv_view_verts
+= [ pz
, lz
]
1886 cv_view_colours
+= [ colour
, colour
, colour
, colour
, colour
, colour
]
1895 # Draw transformed -1 -> 1 cube
1897 def cv_draw_ucube( transform
, colour
, s
=Vector((1,1,1)), o
=Vector((0,0,0)) ):
1899 global cv_view_verts
, cv_view_colours
1905 vs
[0] = transform
@ Vector((a
[0], a
[1], a
[2]))
1906 vs
[1] = transform
@ Vector((a
[0], b
[1], a
[2]))
1907 vs
[2] = transform
@ Vector((b
[0], b
[1], a
[2]))
1908 vs
[3] = transform
@ Vector((b
[0], a
[1], a
[2]))
1909 vs
[4] = transform
@ Vector((a
[0], a
[1], b
[2]))
1910 vs
[5] = transform
@ Vector((a
[0], b
[1], b
[2]))
1911 vs
[6] = transform
@ Vector((b
[0], b
[1], b
[2]))
1912 vs
[7] = transform
@ Vector((b
[0], a
[1], b
[2]))
1914 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
1915 (0,4),(1,5),(2,6),(3,7)]
1921 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
1922 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
1923 cv_view_colours
+= [colour
, colour
]
1928 # Draw line with colour
1930 def cv_draw_line( p0
, p1
, colour
):
1932 global cv_view_verts
, cv_view_colours
1934 cv_view_verts
+= [p0
,p1
]
1935 cv_view_colours
+= [colour
, colour
]
1939 # Draw line with colour(s)
1941 def cv_draw_line2( p0
, p1
, c0
, c1
):
1943 global cv_view_verts
, cv_view_colours
1945 cv_view_verts
+= [p0
,p1
]
1946 cv_view_colours
+= [c0
,c1
]
1952 def cv_tangent_basis( n
, tx
, ty
):
1954 if abs( n
[0] ) >= 0.57735027:
1975 # Draw coloured arrow
1977 def cv_draw_arrow( p0
, p1
, c0
, size
=0.15 ):
1979 global cv_view_verts
, cv_view_colours
1985 tx
= Vector((1,0,0))
1986 ty
= Vector((1,0,0))
1987 cv_tangent_basis( n
, tx
, ty
)
1989 cv_view_verts
+= [p0
,p1
, midpt
+(tx
-n
)*size
,midpt
, midpt
+(-tx
-n
)*size
,midpt
]
1990 cv_view_colours
+= [c0
,c0
,c0
,c0
,c0
,c0
]
1994 def cv_draw_line_dotted( p0
, p1
, c0
, dots
=10 ):
1996 global cv_view_verts
, cv_view_colours
1998 for i
in range(dots
):#{
2002 p2
= p0
*(1.0-t0
)+p1
*t0
2003 p3
= p0
*(1.0-t1
)+p1
*t1
2005 cv_view_verts
+= [p2
,p3
]
2006 cv_view_colours
+= [c0
,c0
]
2011 # Drawhandles of a bezier control point
2013 def cv_draw_bhandle( obj
, direction
, colour
):
2015 global cv_view_verts
, cv_view_colours
2018 h0
= obj
.matrix_world
@ Vector((0,direction
,0))
2020 cv_view_verts
+= [p0
]
2021 cv_view_verts
+= [h0
]
2022 cv_view_colours
+= [colour
,colour
]
2026 # Draw a bezier curve (at fixed resolution 10)
2028 def cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
):
2030 global cv_view_verts
, cv_view_colours
2040 p
=ttt
*p1
+(3*tt
-3*ttt
)*h1
+(3*ttt
-6*tt
+3*t
)*h0
+(3*tt
-ttt
-3*t
+1)*p0
2042 cv_view_verts
+= [(last
[0],last
[1],last
[2])]
2043 cv_view_verts
+= [(p
[0],p
[1],p
[2])]
2044 cv_view_colours
+= [c0
*a0
+c1
*(1-a0
),c0
*a0
+c1
*(1-a0
)]
2051 # I think this one extends the handles of the bezier otwards......
2053 def cv_draw_sbpath( o0
,o1
,c0
,c1
,s0
,s1
):
2055 global cv_view_course_i
2057 offs
= ((cv_view_course_i
% 2)*2-1) * cv_view_course_i
* 0.02
2059 p0
= o0
.matrix_world
@ Vector((offs
, 0,0))
2060 h0
= o0
.matrix_world
@ Vector((offs
, s0
,0))
2061 p1
= o1
.matrix_world
@ Vector((offs
, 0,0))
2062 h1
= o1
.matrix_world
@ Vector((offs
,-s1
,0))
2064 cv_draw_bezier( p0
,h0
,p1
,h1
,c0
,c1
)
2068 # Flush the lines buffers. This is called often because god help you if you want
2069 # to do fixed, fast buffers in this catastrophic programming language.
2071 def cv_draw_lines():
2073 global cv_view_shader
, cv_view_verts
, cv_view_colours
2075 if len(cv_view_verts
) < 2:
2078 lines
= batch_for_shader(\
2079 cv_view_shader
, 'LINES', \
2080 { "pos":cv_view_verts
, "color":cv_view_colours
})
2082 lines
.draw( cv_view_shader
)
2085 cv_view_colours
= []
2088 # I dont remember what this does exactly
2090 def cv_draw_bpath( o0
,o1
,c0
,c1
):
2092 cv_draw_sbpath( o0
,o1
,c0
,c1
,1.0,1.0 )
2095 # Semi circle to show the limit. and some lines
2097 def draw_limit( obj
, center
, major
, minor
, amin
, amax
, colour
):
2099 global cv_view_verts
, cv_view_colours
2104 for x
in range(16):#{
2107 a0
= amin
*(1.0-t0
)+amax
*t0
2108 a1
= amin
*(1.0-t1
)+amax
*t1
2110 p0
= center
+ major
*f
*math
.cos(a0
) + minor
*f
*math
.sin(a0
)
2111 p1
= center
+ major
*f
*math
.cos(a1
) + minor
*f
*math
.sin(a1
)
2113 p0
=obj
.matrix_world
@ p0
2114 p1
=obj
.matrix_world
@ p1
2115 cv_view_verts
+= [p0
,p1
]
2116 cv_view_colours
+= [colour
,colour
]
2119 cv_view_verts
+= [p0
,center
]
2120 cv_view_colours
+= [colour
,colour
]
2123 cv_view_verts
+= [p1
,center
]
2124 cv_view_colours
+= [colour
,colour
]
2128 cv_view_verts
+= [center
+major
*1.2*f
,center
+major
*f
*0.8]
2129 cv_view_colours
+= [colour
,colour
]
2134 # Cone and twist limit
2136 def draw_cone_twist( center
, vx
, vy
, va
):
2138 global cv_view_verts
, cv_view_colours
2139 axis
= vy
.cross( vx
)
2144 cv_view_verts
+= [center
, center
+va
*size
]
2145 cv_view_colours
+= [ (1,1,1,1), (1,1,1,1) ]
2147 for x
in range(32):#{
2148 t0
= (x
/32) * math
.tau
2149 t1
= ((x
+1)/32) * math
.tau
2156 p0
= center
+ (axis
+ vx
*c0
+ vy
*s0
).normalized() * size
2157 p1
= center
+ (axis
+ vx
*c1
+ vy
*s1
).normalized() * size
2159 col0
= ( abs(c0
), abs(s0
), 0.0, 1.0 )
2160 col1
= ( abs(c1
), abs(s1
), 0.0, 1.0 )
2162 cv_view_verts
+= [center
, p0
, p0
, p1
]
2163 cv_view_colours
+= [ (0,0,0,0), col0
, col0
, col1
]
2169 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
2171 def draw_skeleton_helpers( obj
):
2173 global cv_view_verts
, cv_view_colours
2175 if obj
.data
.pose_position
!= 'REST':#{
2179 for bone
in obj
.data
.bones
:#{
2181 a
= Vector((bone
.SR_data
.collider_min
[0],
2182 bone
.SR_data
.collider_min
[1],
2183 bone
.SR_data
.collider_min
[2]))
2184 b
= Vector((bone
.SR_data
.collider_max
[0],
2185 bone
.SR_data
.collider_max
[1],
2186 bone
.SR_data
.collider_max
[2]))
2188 if bone
.SR_data
.collider
== '1':#{
2190 vs
[0]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+a
[2]))
2191 vs
[1]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+a
[2]))
2192 vs
[2]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+a
[2]))
2193 vs
[3]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+a
[2]))
2194 vs
[4]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+a
[1],c
[2]+b
[2]))
2195 vs
[5]=obj
.matrix_world
@Vector((c
[0]+a
[0],c
[1]+b
[1],c
[2]+b
[2]))
2196 vs
[6]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+b
[1],c
[2]+b
[2]))
2197 vs
[7]=obj
.matrix_world
@Vector((c
[0]+b
[0],c
[1]+a
[1],c
[2]+b
[2]))
2199 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
2200 (0,4),(1,5),(2,6),(3,7)]
2206 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2207 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2208 cv_view_colours
+= [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
2211 elif bone
.SR_data
.collider
== '2':#{
2216 for i
in range(3):#{
2217 if abs(v0
[i
]) > largest
:#{
2218 largest
= abs(v0
[i
])
2223 v1
= Vector((0,0,0))
2224 v1
[major_axis
] = 1.0
2226 tx
= Vector((0,0,0))
2227 ty
= Vector((0,0,0))
2229 cv_tangent_basis( v1
, tx
, ty
)
2230 r
= (abs(tx
.dot( v0
)) + abs(ty
.dot( v0
))) * 0.25
2231 l
= v0
[ major_axis
] - r
*2
2233 p0
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
*-0.5 )
2234 p1
= obj
.matrix_world
@Vector( c
+ (a
+b
)*0.5 + v1
*l
* 0.5 )
2236 colour
= [0.2,0.2,0.2,1.0]
2237 colour
[major_axis
] = 0.5
2239 cv_draw_halfsphere( p0
, -v1
, ty
, tx
, r
, colour
)
2240 cv_draw_halfsphere( p1
, v1
, ty
, tx
, r
, colour
)
2241 cv_draw_line( p0
+tx
* r
, p1
+tx
* r
, colour
)
2242 cv_draw_line( p0
+tx
*-r
, p1
+tx
*-r
, colour
)
2243 cv_draw_line( p0
+ty
* r
, p1
+ty
* r
, colour
)
2244 cv_draw_line( p0
+ty
*-r
, p1
+ty
*-r
, colour
)
2250 center
= obj
.matrix_world
@ c
2251 if bone
.SR_data
.cone_constraint
:#{
2252 vx
= Vector([bone
.SR_data
.conevx
[_
] for _
in range(3)])
2253 vy
= Vector([bone
.SR_data
.conevy
[_
] for _
in range(3)])
2254 va
= Vector([bone
.SR_data
.coneva
[_
] for _
in range(3)])
2255 draw_cone_twist( center
, vx
, vy
, va
)
2260 def cv_ent_gate( obj
):
2262 global cv_view_verts
, cv_view_colours
2264 if obj
.type != 'MESH': return
2266 mesh_data
= obj
.data
.SR_data
.ent_gate
[0]
2267 data
= obj
.SR_data
.ent_gate
[0]
2268 dims
= mesh_data
.dimensions
2271 c
= Vector((0,0,dims
[2]))
2273 vs
[0] = obj
.matrix_world
@ Vector((-dims
[0],0.0,-dims
[1]+dims
[2]))
2274 vs
[1] = obj
.matrix_world
@ Vector((-dims
[0],0.0, dims
[1]+dims
[2]))
2275 vs
[2] = obj
.matrix_world
@ Vector(( dims
[0],0.0, dims
[1]+dims
[2]))
2276 vs
[3] = obj
.matrix_world
@ Vector(( dims
[0],0.0,-dims
[1]+dims
[2]))
2277 vs
[4] = obj
.matrix_world
@ (c
+Vector((-1,0,-2)))
2278 vs
[5] = obj
.matrix_world
@ (c
+Vector((-1,0, 2)))
2279 vs
[6] = obj
.matrix_world
@ (c
+Vector(( 1,0, 2)))
2280 vs
[7] = obj
.matrix_world
@ (c
+Vector((-1,0, 0)))
2281 vs
[8] = obj
.matrix_world
@ (c
+Vector(( 1,0, 0)))
2283 indices
= [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
2288 cv_view_verts
+= [(v0
[0],v0
[1],v0
[2])]
2289 cv_view_verts
+= [(v1
[0],v1
[1],v1
[2])]
2290 cv_view_colours
+= [(1,1,0,1),(1,1,0,1)]
2293 sw
= (0.4,0.4,0.4,0.2)
2294 if data
.target
!= None:
2295 cv_draw_arrow( obj
.location
, data
.target
.location
, sw
)
2298 def dijkstra( graph
, start_node
, target_node
):
2300 unvisited
= [_
for _
in graph
]
2305 shortest_path
[n
] = 9999999.999999
2306 shortest_path
[start_node
] = 0
2309 current_min_node
= None
2310 for n
in unvisited
:#{
2311 if current_min_node
== None:
2312 current_min_node
= n
2313 elif shortest_path
[n
] < shortest_path
[current_min_node
]:
2314 current_min_node
= n
2317 for branch
in graph
[current_min_node
]:#{
2318 tentative_value
= shortest_path
[current_min_node
]
2319 tentative_value
+= graph
[current_min_node
][branch
]
2320 if tentative_value
< shortest_path
[branch
]:#{
2321 shortest_path
[branch
] = tentative_value
2322 previous_nodes
[branch
] = current_min_node
2326 unvisited
.remove(current_min_node
)
2331 while node
!= start_node
:#{
2334 if node
not in previous_nodes
: return None
2335 node
= previous_nodes
[node
]
2338 # Add the start node manually
2339 path
.append(start_node
)
2343 def node_graph( route_nodes
):
2346 for n
in route_nodes
:
2349 for i
in range(len(route_nodes
)-1):#{
2350 for j
in range(i
+1, len(route_nodes
)):#{
2354 v0
= ni
.location
- nj
.location
2358 if ni
.SR_data
.ent_type
== 'ent_gate':
2361 if nj
.SR_data
.ent_type
== 'ent_gate':#{
2367 v1
= gate
.matrix_world
.to_3x3() @ Vector((0,-1,0))
2368 if gate
.SR_data
.ent_gate
[0].target
:
2369 if v1
.dot(v0
) > 0.0: continue
2371 if v1
.dot(v0
) < 0.0: continue
2376 if dist
> 25.0: continue
2377 graph
[route_nodes
[i
].name
][route_nodes
[j
].name
] = dist
2378 graph
[route_nodes
[j
].name
][route_nodes
[i
].name
] = dist
2385 def cv_draw_route( route
, route_nodes
):
2387 pole
= Vector((0.2,0.2,10))
2388 hat
= Vector((1,8,0.2))
2389 cc
= route
.SR_data
.ent_route
[0].colour
2391 cv_draw_ucube(route
.matrix_world
,cc
,Vector((0.5,-7.5,6)),\
2392 Vector((0,-6.5,5.5)))
2393 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5, 0.5,0)) )
2394 cv_draw_ucube(route
.matrix_world
,cc
,pole
, Vector(( 0.5,-13.5,0)) )
2395 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5, 12)) )
2396 cv_draw_ucube(route
.matrix_world
,cc
,hat
, Vector((-0.5,-6.5,-1)) )
2398 checkpoints
= route
.SR_data
.ent_route
[0].gates
2399 graph
= node_graph( route_nodes
)
2401 for i
in range(len(checkpoints
)):#{
2402 gi
= checkpoints
[i
].target
2403 gj
= checkpoints
[(i
+1)%len(checkpoints
)].target
2406 dest
= gi
.SR_data
.ent_gate
[0].target
2408 cv_draw_line_dotted( gi
.location
, dest
.location
, cc
)
2412 if gi
==gj
: continue # error?
2413 if not gi
or not gj
: continue
2415 path
= dijkstra( graph
, gj
.name
, gi
.name
)
2418 for sj
in range(len(path
)-1):#{
2419 o0
= bpy
.data
.objects
[ path
[sj
] ]
2420 o1
= bpy
.data
.objects
[ path
[sj
+1] ]
2421 cv_draw_arrow(o0
.location
,o1
.location
,cc
,1.5)
2425 cv_draw_line_dotted( gi
.location
, gj
.location
, cc
)
2432 global cv_view_shader
2433 global cv_view_verts
2434 global cv_view_colours
2435 global cv_view_course_i
2437 cv_view_course_i
= 0
2439 cv_view_colours
= []
2441 cv_view_shader
.bind()
2442 gpu
.state
.depth_mask_set(False)
2443 gpu
.state
.line_width_set(2.0)
2444 gpu
.state
.face_culling_set('BACK')
2445 gpu
.state
.depth_test_set('LESS')
2446 gpu
.state
.blend_set('NONE')
2451 for obj
in bpy
.context
.collection
.objects
:#{
2452 if obj
.type == 'ARMATURE':#{
2453 if obj
.data
.pose_position
== 'REST':
2454 draw_skeleton_helpers( obj
)
2457 ent_type
= obj_ent_type( obj
)
2459 if ent_type
== 'ent_gate':#{
2461 route_nodes
+= [obj
]
2463 elif ent_type
== 'ent_route_node':
2464 route_nodes
+= [obj
]
2465 elif ent_type
== 'ent_route':
2470 #cv_draw_route_map( route_nodes )
2471 for route
in routes
:#{
2472 cv_draw_route( route
, route_nodes
)
2479 classes
= [ SR_INTERFACE
, SR_MATERIAL_PANEL
,\
2480 SR_COLLECTION_SETTINGS
, SR_SCENE_SETTINGS
, \
2481 SR_COMPILE
, SR_COMPILE_THIS
, SR_MIRROR_BONE_X
,\
2483 SR_OBJECT_ENT_GATE
, SR_MESH_ENT_GATE
, SR_OBJECT_ENT_SPAWN
, \
2484 SR_OBJECT_ENT_ROUTE_ENTRY
, SR_UL_ROUTE_NODE_LIST
, \
2485 SR_OBJECT_ENT_ROUTE
, SR_OT_ROUTE_LIST_NEW_ITEM
,
2486 SR_OT_ROUTE_LIST_DEL_ITEM
,\
2488 SR_OBJECT_PROPERTIES
, SR_LIGHT_PROPERTIES
, SR_BONE_PROPERTIES
,
2489 SR_MESH_PROPERTIES
, SR_MATERIAL_PROPERTIES \
2495 bpy
.utils
.register_class(c
)
2497 bpy
.types
.Scene
.SR_data
= \
2498 bpy
.props
.PointerProperty(type=SR_SCENE_SETTINGS
)
2499 bpy
.types
.Collection
.SR_data
= \
2500 bpy
.props
.PointerProperty(type=SR_COLLECTION_SETTINGS
)
2502 bpy
.types
.Object
.SR_data
= \
2503 bpy
.props
.PointerProperty(type=SR_OBJECT_PROPERTIES
)
2504 bpy
.types
.Light
.SR_data
= \
2505 bpy
.props
.PointerProperty(type=SR_LIGHT_PROPERTIES
)
2506 bpy
.types
.Bone
.SR_data
= \
2507 bpy
.props
.PointerProperty(type=SR_BONE_PROPERTIES
)
2508 bpy
.types
.Mesh
.SR_data
= \
2509 bpy
.props
.PointerProperty(type=SR_MESH_PROPERTIES
)
2510 bpy
.types
.Material
.SR_data
= \
2511 bpy
.props
.PointerProperty(type=SR_MATERIAL_PROPERTIES
)
2513 global cv_view_draw_handler
2514 cv_view_draw_handler
= bpy
.types
.SpaceView3D
.draw_handler_add(\
2515 cv_draw
,(),'WINDOW','POST_VIEW')
2521 bpy
.utils
.unregister_class(c
)
2523 global cv_view_draw_handler
2524 bpy
.types
.SpaceView3D
.draw_handler_remove(cv_view_draw_handler
,'WINDOW')
2527 # ---------------------------------------------------------------------------- #
2531 # ---------------------------------------------------------------------------- #
2533 # Transliteration of: #
2534 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
2536 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
2537 # SPDX-License-Identifier: MIT #
2538 # QOI - The "Quite OK Image" format for fast, lossless image compression #
2540 # ---------------------------------------------------------------------------- #
2542 class qoi_rgba_t(Structure
):
2545 _fields_
= [("r",c_uint8
),
2551 QOI_OP_INDEX
= 0x00 # 00xxxxxx
2552 QOI_OP_DIFF
= 0x40 # 01xxxxxx
2553 QOI_OP_LUMA
= 0x80 # 10xxxxxx
2554 QOI_OP_RUN
= 0xc0 # 11xxxxxx
2555 QOI_OP_RGB
= 0xfe # 11111110
2556 QOI_OP_RGBA
= 0xff # 11111111
2558 QOI_MASK_2
= 0xc0 # 11000000
2560 def qoi_colour_hash( c
):
2562 return c
.r
*3 + c
.g
*5 + c
.b
*7 + c
.a
*11
2567 return (a
.r
==b
.r
) and (a
.g
==b
.g
) and (a
.b
==b
.b
) and (a
.a
==b
.a
)
2572 return bytearray([ (0xff000000 & v
) >> 24, \
2573 (0x00ff0000 & v
) >> 16, \
2574 (0x0000ff00 & v
) >> 8, \
2578 def qoi_encode( img
):
2582 print(F
"{' ':<30}",end
='\r')
2583 print(F
"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end
='\r')
2585 index
= [ qoi_rgba_t() for _
in range(64) ]
2589 data
.extend( bytearray(c_uint32(0x66696f71)) )
2590 data
.extend( qoi_32bit( img
.size
[0] ) )
2591 data
.extend( qoi_32bit( img
.size
[1] ) )
2592 data
.extend( bytearray(c_uint8(4)) )
2593 data
.extend( bytearray(c_uint8(0)) )
2596 px_prev
= qoi_rgba_t()
2597 px_prev
.r
= c_uint8(0)
2598 px_prev
.g
= c_uint8(0)
2599 px_prev
.b
= c_uint8(0)
2600 px_prev
.a
= c_uint8(255)
2608 px_len
= img
.size
[0] * img
.size
[1]
2609 paxels
= [ int(min(max(_
,0),1)*255) for _
in img
.pixels
]
2611 for px_pos
in range( px_len
): #{
2612 idx
= px_pos
* img
.channels
2615 px
.r
= paxels
[idx
+min(0,nc
)]
2616 px
.g
= paxels
[idx
+min(1,nc
)]
2617 px
.b
= paxels
[idx
+min(2,nc
)]
2618 px
.a
= paxels
[idx
+min(3,nc
)]
2620 if qoi_eq( px
, px_prev
): #{
2623 if (run
== 62) or (px_pos
== px_len
-1): #{
2624 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2630 data
.extend( bytearray( c_uint8(QOI_OP_RUN |
(run
-1))) )
2634 index_pos
= qoi_colour_hash(px
) % 64
2636 if qoi_eq( index
[index_pos
], px
): #{
2637 data
.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos
)) )
2640 index
[ index_pos
].r
= px
.r
2641 index
[ index_pos
].g
= px
.g
2642 index
[ index_pos
].b
= px
.b
2643 index
[ index_pos
].a
= px
.a
2645 if px
.a
== px_prev
.a
: #{
2646 vr
= int(px
.r
) - int(px_prev
.r
)
2647 vg
= int(px
.g
) - int(px_prev
.g
)
2648 vb
= int(px
.b
) - int(px_prev
.b
)
2653 if (vr
> -3) and (vr
< 2) and\
2654 (vg
> -3) and (vg
< 2) and\
2655 (vb
> -3) and (vb
< 2):
2657 op
= QOI_OP_DIFF |
(vr
+2) << 4 |
(vg
+2) << 2 |
(vb
+2)
2658 data
.extend( bytearray( c_uint8(op
) ))
2660 elif (vg_r
> -9) and (vg_r
< 8) and\
2661 (vg
> -33) and (vg
< 32 ) and\
2662 (vg_b
> -9) and (vg_b
< 8):
2664 op
= QOI_OP_LUMA |
(vg
+32)
2665 delta
= (vg_r
+8) << 4 |
(vg_b
+ 8)
2666 data
.extend( bytearray( c_uint8(op
) ) )
2667 data
.extend( bytearray( c_uint8(delta
) ))
2670 data
.extend( bytearray( c_uint8(QOI_OP_RGB
) ) )
2671 data
.extend( bytearray( c_uint8(px
.r
) ))
2672 data
.extend( bytearray( c_uint8(px
.g
) ))
2673 data
.extend( bytearray( c_uint8(px
.b
) ))
2677 data
.extend( bytearray( c_uint8(QOI_OP_RGBA
) ) )
2678 data
.extend( bytearray( c_uint8(px
.r
) ))
2679 data
.extend( bytearray( c_uint8(px
.g
) ))
2680 data
.extend( bytearray( c_uint8(px
.b
) ))
2681 data
.extend( bytearray( c_uint8(px
.a
) ))
2694 data
.extend( bytearray( c_uint8(0) ))
2695 data
.extend( bytearray( c_uint8(1) ))
2696 bytearray_align_to( data
, 16, b
'\x00' )