some niceer ui stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / blender_export.py
1 import bpy, blf, math, gpu, os
2 import cProfile
3 from ctypes import *
4 from mathutils import *
5 from gpu_extras.batch import batch_for_shader
6 from bpy_extras import mesh_utils
7 from bpy_extras import view3d_utils
8
9 bl_info = {
10 "name":"Skaterift .mdl exporter",
11 "author": "Harry Godden (hgn)",
12 "version": (0,2),
13 "blender":(3,1,0),
14 "location":"Export",
15 "description":"",
16 "warning":"",
17 "wiki_url":"",
18 "category":"Import/Export",
19 }
20
21 sr_entity_list = [
22 ('none', 'None', '', 0 ),
23 ('ent_gate', 'Gate', '', 1 ),
24 ('ent_spawn', 'Spawn Point', '', 2 ),
25 ('ent_route_node', 'Routing Path', '', 3 ),
26 ('ent_route', 'Skate Course', '', 4 ),
27 ('ent_water', 'Water Surface', '', 5 ),
28 ('ent_volume', 'Volume/Trigger', '', 6 ),
29 ('ent_audio', 'Audio', '', 7 ),
30 ('ent_marker', 'Marker', '', 8 ),
31 ('ent_font', 'Font', '', 9 ),
32 ('ent_font_variant', 'Font:Variant', '', 10 ),
33 ('ent_traffic', 'Traffic Model', '', 11 ),
34 ('ent_skateshop', 'Skate Shop', '', 12 ),
35 ('ent_camera', 'Camera', '', 13 ),
36 ('ent_swspreview', 'Workshop Preview', '', 14 ),
37 ('ent_menuitem', 'Menu Item', '', 15 ),
38 ('ent_worldinfo', 'World Info', '', 16 ),
39 ('ent_ccmd', 'CCmd', '', 17 ),
40 ('ent_challenge', 'Challenge', '', 18 ),
41 ('ent_unlock', 'Unlockable', '', 19 ),
42 ('ent_relay', 'Relay', '', 20 )
43 ]
44
45 MDL_VERSION_NR = 102
46 SR_TRIGGERABLE = [ 'ent_audio', 'ent_ccmd', 'ent_gate', 'ent_unlock', \
47 'ent_relay', 'ent_skateshop', 'ent_challenge' ]
48
49 def get_entity_enum_id( alias ):
50 #{
51 for et in sr_entity_list:#{
52 if et[0] == alias:#{
53 return et[3]
54 #}
55 #}
56
57 return 0
58 #}
59
60 class mdl_vert(Structure): # 48 bytes. Quite large. Could compress
61 #{ # the normals and uvs to i16s. Not an
62 _pack_ = 1 # real issue, yet.
63 _fields_ = [("co",c_float*3),
64 ("norm",c_float*3),
65 ("uv",c_float*2),
66 ("colour",c_uint8*4),
67 ("weights",c_uint16*4),
68 ("groups",c_uint8*4)]
69 #}
70
71 class mdl_transform(Structure):
72 #{
73 _fields_ = [("co",c_float*3),
74 ( "s",c_float*3),
75 ( "q",c_float*4)]
76 #}
77
78 class mdl_submesh(Structure):
79 #{
80 _fields_ = [("indice_start",c_uint32),
81 ("indice_count",c_uint32),
82 ("vertex_start",c_uint32),
83 ("vertex_count",c_uint32),
84 ("bbx",(c_float*3)*2),
85 ("material_id",c_uint16), # index into the material array
86 ("flags",c_uint16)]
87 #}
88
89 class mdl_material(Structure):
90 #{
91 _fields_ = [("pstr_name",c_uint32),
92 ("shader",c_uint32),
93 ("flags",c_uint32),
94 ("surface_prop",c_uint32),
95 ("colour",c_float*4),
96 ("colour1",c_float*4),
97 ("tex_diffuse",c_uint32),
98 ("tex_none0",c_uint32),
99 ("tex_none1",c_uint32)]
100 #}
101
102 class mdl_bone(Structure):
103 #{
104 _fields_ = [("co",c_float*3),("end",c_float*3),
105 ("parent",c_uint32),
106 ("collider",c_uint32),
107 ("ik_target",c_uint32),
108 ("ik_pole",c_uint32),
109 ("flags",c_uint32),
110 ("pstr_name",c_uint32),
111 ("hitbox",(c_float*3)*2),
112 ("conevx",c_float*3),("conevy",c_float*3),("coneva",c_float*3),
113 ("conet",c_float)]
114 #}
115
116 class mdl_armature(Structure):
117 #{
118 _fields_ = [("transform",mdl_transform),
119 ("bone_start",c_uint32),
120 ("bone_count",c_uint32),
121 ("anim_start",c_uint32),
122 ("anim_count",c_uint32)]
123 #}
124
125 class mdl_animation(Structure):
126 #{
127 _fields_ = [("pstr_name",c_uint32),
128 ("length",c_uint32),
129 ("rate",c_float),
130 ("keyframe_start",c_uint32)]
131 #}
132
133 class mdl_mesh(Structure):
134 #{
135 _fields_ = [("transform",mdl_transform),
136 ("submesh_start",c_uint32),
137 ("submesh_count",c_uint32),
138 ("pstr_name",c_uint32),
139 ("entity_id",c_uint32),
140 ("armature_id",c_uint32)]
141 #}
142
143 class mdl_file(Structure):
144 #{
145 _fields_ = [("path",c_uint32),
146 ("pack_offset",c_uint32),
147 ("pack_size",c_uint32)]
148 #}
149
150 class mdl_texture(Structure):
151 #{
152 _fields_ = [("file",mdl_file),
153 ("glname",c_uint32)]
154 #}
155
156 class mdl_array(Structure):
157 #{
158 _fields_ = [("file_offset",c_uint32),
159 ("item_count",c_uint32),
160 ("item_size",c_uint32),
161 ("name",c_byte*16)]
162 #}
163
164 class mdl_header(Structure):
165 #{
166 _fields_ = [("version",c_uint32),
167 ("arrays",mdl_array)]
168 #}
169
170 class ent_spawn(Structure):
171 #{
172 _fields_ = [("transform",mdl_transform),
173 ("pstr_name",c_uint32)]
174 #}
175
176 class ent_light(Structure):
177 #{
178 _fields_ = [("transform",mdl_transform),
179 ("daytime",c_uint32),
180 ("type",c_uint32),
181 ("colour",c_float*4),
182 ("angle",c_float),
183 ("range",c_float),
184 ("inverse_world",(c_float*3)*4), # Runtime
185 ("angle_sin_cos",(c_float*2))] # Runtime
186 #}
187
188 class version_refcount_union(Union):
189 #{
190 _fields_ = [("timing_version",c_uint32),
191 ("ref_count",c_uint8)]
192 #}
193
194 class ent_gate(Structure):
195 #{
196 _fields_ = [("flags",c_uint32),
197 ("target", c_uint32),
198 ("key",c_uint32),
199 ("dimensions", c_float*3),
200 ("co", (c_float*3)*2),
201 ("q", (c_float*4)*2),
202 ("to_world",(c_float*3)*4),
203 ("transport",(c_float*3)*4),
204 ("_anonymous_union",version_refcount_union),
205 ("timing_time",c_double),
206 ("routes",c_uint16*4),
207 ("route_count",c_uint8),
208 ("submesh_start",c_uint32), # v102+
209 ("submesh_count",c_uint32), # v102+ (can be 0)
210 ]
211 sr_functions = { 0: 'unlock' }
212 #}
213
214 class ent_route_node(Structure):
215 #{
216 _fields_ = [("co",c_float*3),
217 ("ref_count",c_uint8),
218 ("ref_total",c_uint8)]
219 #}
220
221 class ent_path_index(Structure):
222 #{
223 _fields_ = [("index",c_uint16)]
224 #}
225
226 class vg_audio_clip(Structure):
227 #{
228 _fields_ = [("path",c_uint64),
229 ("flags",c_uint32),
230 ("size",c_uint32),
231 ("data",c_uint64)]
232 #}
233
234 class union_file_audio_clip(Union):
235 #{
236 _fields_ = [("file",mdl_file),
237 ("reserved",vg_audio_clip)]
238 #}
239
240 class ent_audio_clip(Structure):
241 #{
242 _fields_ = [("_anon",union_file_audio_clip),
243 ("probability",c_float)]
244 #}
245
246 class ent_checkpoint(Structure):
247 #{
248 _fields_ = [("gate_index",c_uint16),
249 ("path_start",c_uint16),
250 ("path_count",c_uint16)]
251 #}
252
253 class ent_route(Structure):
254 #{
255 _fields_ = [("transform",mdl_transform),
256 ("pstr_name",c_uint32),
257 ("checkpoints_start",c_uint16),
258 ("checkpoints_count",c_uint16),
259 ("colour",c_float*4),
260 ("active",c_uint32), #runtime
261 ("factive",c_float),
262 ("board_transform",(c_float*3)*4),
263 ("sm",mdl_submesh),
264 ("latest_pass",c_double)]
265 #}
266
267 class ent_water(Structure):
268 #{
269 _fields_ = [("transform",mdl_transform),
270 ("max_dist",c_float),
271 ("reserved0",c_uint32),
272 ("reserved1",c_uint32)]
273 #}
274
275 class volume_trigger(Structure):
276 #{
277 _fields_ = [("event",c_uint32),
278 ("blank",c_uint32)]
279 #}
280
281 class volume_particles(Structure):
282 #{
283 _fields_ = [("blank",c_uint32),
284 ("blank2",c_uint32)]
285 #}
286
287 class volume_union(Union):
288 #{
289 _fields_ = [("trigger",volume_trigger),
290 ("particles",volume_particles)]
291 #}
292
293 class ent_volume(Structure):
294 #{
295 _fields_ = [("transform",mdl_transform),
296 ("to_world",(c_float*3)*4),
297 ("to_local",(c_float*3)*4),
298 ("type",c_uint32),
299 ("target",c_uint32),
300 ("_anon",volume_union)]
301 #}
302
303 class ent_audio(Structure):
304 #{
305 _fields_ = [("transform",mdl_transform),
306 ("flags",c_uint32),
307 ("clip_start",c_uint32),
308 ("clip_count",c_uint32),
309 ("volume",c_float),
310 ("crossfade",c_float),
311 ("channel_behaviour",c_uint32),
312 ("group",c_uint32),
313 ("probability_curve",c_uint32),
314 ("max_channels",c_uint32)]
315 #}
316
317 class ent_marker(Structure):
318 #{
319 _fields_ = [("transform",mdl_transform),
320 ("name",c_uint32)]
321 #}
322
323 class ent_glyph(Structure):
324 #{
325 _fields_ = [("size",c_float*2),
326 ("indice_start",c_uint32),
327 ("indice_count",c_uint32)]
328 #}
329
330 class ent_font_variant(Structure):
331 #{
332 _fields_ = [("name",c_uint32),
333 ("material_id",c_uint32)]
334 #}
335
336 class ent_font(Structure):
337 #{
338 _fields_ = [("alias",c_uint32),
339 ("variant_start",c_uint32),
340 ("variant_count",c_uint32),
341 ("glyph_start",c_uint32),
342 ("glyph_count",c_uint32),
343 ("glyph_utf32_base",c_uint32)]
344 #}
345
346 class ent_traffic(Structure):
347 #{
348 _fields_ = [("transform",mdl_transform),
349 ("submesh_start",c_uint32),
350 ("submesh_count",c_uint32),
351 ("start_node",c_uint32),
352 ("node_count",c_uint32),
353 ("speed",c_float),
354 ("t",c_float),
355 ("index",c_uint32)]
356 #}
357
358 # Skateshop
359 # ---------------------------------------------------------------
360 class ent_skateshop_characters(Structure):
361 #{
362 _fields_ = [("id_display",c_uint32),
363 ("id_info",c_uint32)]
364 #}
365 class ent_skateshop_boards(Structure):
366 #{
367 _fields_ = [("id_display",c_uint32),
368 ("id_info",c_uint32),
369 ("id_rack",c_uint32)]
370 #}
371 class ent_skateshop_worlds(Structure):
372 #{
373 _fields_ = [("id_display",c_uint32),
374 ("id_info",c_uint32)]
375 #}
376 class ent_skateshop_anon_union(Union):
377 #{
378 _fields_ = [("boards",ent_skateshop_boards),
379 ("character",ent_skateshop_characters),
380 ("worlds",ent_skateshop_worlds)]
381 #}
382 class ent_skateshop(Structure):
383 #{
384 _fields_ = [("transform",mdl_transform), ("type",c_uint32),
385 ("id_camera",c_uint32),
386 ("_anonymous_union",ent_skateshop_anon_union)]
387 #}
388
389 class ent_swspreview(Structure):
390 #{
391 _fields_ = [("id_camera",c_uint32),
392 ("id_display",c_uint32),
393 ("id_display1",c_uint32)]
394 #}
395
396 # Menu
397 # -----------------------------------------------------------------
398 class ent_menuitem_slider(Structure):
399 #{
400 _fields_ = [("id_min",c_uint32),
401 ("id_max",c_uint32),
402 ("id_handle",c_uint32),
403 ("pstr_data",c_uint32)]
404 #}
405 class ent_menuitem_button(Structure):
406 #{
407 _fields_ = [("pstr",c_uint32)]
408 #}
409 class ent_menuitem_checkmark(Structure):
410 #{
411 _fields_ = [("id_check",c_uint32),
412 ("pstr_data",c_uint32),
413 ("offset",c_float*3)]
414 #}
415 class ent_menuitem_page(Structure):
416 #{
417 _fields_ = [("pstr_name",c_uint32),
418 ("id_entrypoint",c_uint32),
419 ("id_viewpoint",c_uint32)]
420 #}
421 class ent_menuitem_anon_union(Union):
422 #{
423 _fields_ = [("slider",ent_menuitem_slider),
424 ("button",ent_menuitem_button),
425 ("checkmark",ent_menuitem_checkmark),
426 ("page",ent_menuitem_page)]
427 #}
428 class ent_menuitem(Structure):
429 #{
430 _fields_ = [("type",c_uint32), ("groups",c_uint32),
431 ("id_links",c_uint32*4),
432 ("factive",c_float), ("fvisible",c_float),
433 #-- TODO: Refactor this into a simple mesh structure
434 ("transform",mdl_transform),
435 ("submesh_start",c_uint32),("submesh_count",c_uint32),
436 ("_u64",c_uint64),
437 #-- end
438 ("_anonymous_union", ent_menuitem_anon_union)]
439 #}
440
441 class ent_camera(Structure):
442 #{
443 _fields_ = [("transform",mdl_transform),
444 ("fov",c_float)]
445 #}
446
447 class ent_worldinfo(Structure):
448 #{
449 _fields_ = [("pstr_name",c_uint32),
450 ("pstr_author",c_uint32), # unused
451 ("pstr_desc",c_uint32), # unused
452 ("timezone",c_float)]
453 #}
454
455 class ent_ccmd(Structure):
456 #{
457 _fields_ = [("pstr_command",c_uint32)]
458 #}
459
460 class ent_challenge(Structure):#{
461 _fields_ = [("transform",mdl_transform),
462 ("submesh_start",c_uint32), ("submesh_count",c_uint32),
463 ("id_next",c_uint32),
464 ("filter",c_uint32),
465 ("id_win",c_uint32),
466 ("win_event",c_uint32),
467 ("time_limit",c_float)]
468
469 sr_functions = { 0: 'trigger',
470 1: 'start_challenge' }
471 #}
472
473 class ent_unlock(Structure):#{
474 _fields_ = [("pstr_alias",c_uint32),
475 ("target",c_uint32),
476 ("target_event",c_uint32),
477 ("status",c_uint32)]
478 sr_functions = { 0: 'unlock' }
479 #}
480
481 class ent_relay(Structure):#{
482 _fields_ = [("targets",(c_uint32*2)*4),
483 ("targets_events",c_uint32*4)]
484 sr_functions = { 0: 'trigger' }
485 #}
486
487 def obj_ent_type( obj ):
488 #{
489 if obj.type == 'ARMATURE': return 'mdl_armature'
490 elif obj.type == 'LIGHT': return 'ent_light'
491 elif obj.type == 'CAMERA': return 'ent_camera'
492 else: return obj.SR_data.ent_type
493 #}
494
495 def sr_filter_ent_type( obj, ent_types ):
496 #{
497 if obj == bpy.context.active_object: return False
498
499 for c0 in obj.users_collection:#{
500 for c1 in bpy.context.active_object.users_collection:#{
501 if c0 == c1:#{
502 return obj_ent_type( obj ) in ent_types
503 #}
504 #}
505 #}
506
507 return False
508 #}
509
510 def v4_dot( a, b ):#{
511 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3]
512 #}
513
514 def q_identity( q ):#{
515 q[0] = 0.0
516 q[1] = 0.0
517 q[2] = 0.0
518 q[3] = 1.0
519 #}
520
521 def q_normalize( q ):#{
522 l2 = v4_dot(q,q)
523 if( l2 < 0.00001 ):#{
524 q_identity( q )
525 #}
526 else:#{
527 s = 1.0/math.sqrt(l2)
528 q[0] *= s
529 q[1] *= s
530 q[2] *= s
531 q[3] *= s
532 #}
533 #}
534
535 def compile_obj_transform( obj, transform ):
536 #{
537 co = obj.matrix_world @ Vector((0,0,0))
538
539 # This was changed from matrix_local on 09.05.23
540 q = obj.matrix_world.to_quaternion()
541 s = obj.scale
542 q_normalize( q )
543
544 # Setup transform
545 #
546 transform.co[0] = co[0]
547 transform.co[1] = co[2]
548 transform.co[2] = -co[1]
549 transform.q[0] = q[1]
550 transform.q[1] = q[3]
551 transform.q[2] = -q[2]
552 transform.q[3] = q[0]
553 transform.s[0] = s[0]
554 transform.s[1] = s[2]
555 transform.s[2] = s[1]
556 #}
557
558 def int_align_to( v, align ):
559 #{
560 while(v%align)!=0: v += 1
561 return v
562 #}
563
564 def bytearray_align_to( buffer, align, w=b'\xaa' ):
565 #{
566 while (len(buffer) % align) != 0: buffer.extend(w)
567 return buffer
568 #}
569
570 def bytearray_print_hex( s, w=16 ):
571 #{
572 for r in range((len(s)+(w-1))//w):#{
573 i0=(r+0)*w
574 i1=min((r+1)*w,len(s))
575 print( F'{r*w:06x}| \x1B[31m', end='')
576 print( F"{' '.join('{:02x}'.format(x) for x in s[i0:i1]):<48}",end='' )
577 print( "\x1B[0m", end='')
578 print( ''.join(chr(x) if (x>=33 and x<=126) else '.' for x in s[i0:i1] ) )
579 #}
580 #}
581
582 def sr_compile_string( s ):
583 #{
584 if s in sr_compile.string_cache: return sr_compile.string_cache[s]
585
586 index = len( sr_compile.string_data )
587 sr_compile.string_cache[s] = index
588 sr_compile.string_data.extend( c_uint32(hash_djb2(s)) )
589 sr_compile.string_data.extend( s.encode('utf-8') )
590 sr_compile.string_data.extend( b'\0' )
591
592 bytearray_align_to( sr_compile.string_data, 4 )
593 return index
594 #}
595
596 def material_tex_image(v):
597 #{
598 return {
599 "Image Texture":
600 {
601 "image": F"{v}"
602 }
603 }
604 #}
605
606 cxr_graph_mapping = \
607 {
608 # Default shader setup
609 "Principled BSDF":
610 {
611 "Base Color":
612 {
613 "Image Texture":
614 {
615 "image": "tex_diffuse"
616 },
617 "Mix":
618 {
619 "A": material_tex_image("tex_diffuse"),
620 "B": material_tex_image("tex_decal")
621 },
622 },
623 "Normal":
624 {
625 "Normal Map":
626 {
627 "Color": material_tex_image("tex_normal")
628 }
629 }
630 },
631 "Emission":
632 {
633 "Color": material_tex_image("tex_diffuse")
634 }
635 }
636
637 # https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
638 #
639 def material_info(mat):
640 #{
641 info = {}
642
643 # Using the cxr_graph_mapping as a reference, go through the shader
644 # graph and gather all $props from it.
645 #
646 def _graph_read( node_def, node=None, depth=0 ):#{
647 nonlocal mat
648 nonlocal info
649
650 # Find rootnodes
651 #
652 if node == None:#{
653 _graph_read.extracted = []
654
655 for node_idname in node_def:#{
656 for n in mat.node_tree.nodes:#{
657 if n.name == node_idname:#{
658 node_def = node_def[node_idname]
659 node = n
660 break
661 #}
662 #}
663 #}
664 #}
665
666 for link in node_def:#{
667 link_def = node_def[link]
668
669 if isinstance( link_def, dict ):#{
670 node_link = None
671 for x in node.inputs:#{
672 if isinstance( x, bpy.types.NodeSocketColor ):#{
673 if link == x.name:#{
674 node_link = x
675 break
676 #}
677 #}
678 #}
679
680 if node_link and node_link.is_linked:#{
681 # look for definitions for the connected node type
682 #
683 from_node = node_link.links[0].from_node
684
685 node_name = from_node.name.split('.')[0]
686 if node_name in link_def:#{
687 from_node_def = link_def[ node_name ]
688
689 _graph_read( from_node_def, from_node, depth+1 )
690 #}
691 #}
692 else:#{
693 if "default" in link_def:#{
694 prop = link_def['default']
695 info[prop] = node_link.default_value
696 #}
697 #}
698 #}
699 else:#{
700 prop = link_def
701 info[prop] = getattr( node, link )
702 #}
703 #}
704 #}
705
706 _graph_read( cxr_graph_mapping )
707 return info
708 #}
709
710 def vg_str_bin( s ):
711 #{
712 decoded = bytearray()
713 for i in range(len(s)//2):#{
714 c = (ord(s[i*2+0])-0x41)
715 c |= (ord(s[i*2+1])-0x41)<<4
716 decoded.extend(bytearray(c_uint8(c))) #??
717 #}
718 return decoded
719 #}
720
721 def sr_pack_file( file, path, data ):
722 #{
723 file.path = sr_compile_string( path )
724 file.pack_offset = len( sr_compile.pack_data )
725 file.pack_size = len( data )
726
727 sr_compile.pack_data.extend( data )
728 bytearray_align_to( sr_compile.pack_data, 16 )
729 #}
730
731 def sr_compile_texture( img ):
732 #{
733 if img == None:
734 return 0
735
736 name = os.path.splitext( img.name )[0]
737
738 if name in sr_compile.texture_cache:
739 return sr_compile.texture_cache[name]
740
741 texture_index = (len(sr_compile.texture_data)//sizeof(mdl_texture)) +1
742
743 tex = mdl_texture()
744 tex.glname = 0
745
746 if sr_compile.pack_textures:#{
747 filedata = qoi_encode( img )
748 sr_pack_file( tex.file, name, filedata )
749 #}
750
751 sr_compile.texture_cache[name] = texture_index
752 sr_compile.texture_data.extend( bytearray(tex) )
753 return texture_index
754 #}
755
756 def sr_compile_material( mat ):
757 #{
758 if mat == None:
759 return 0
760 if mat.name in sr_compile.material_cache:
761 return sr_compile.material_cache[mat.name]
762
763 index = (len(sr_compile.material_data)//sizeof(mdl_material))+1
764 sr_compile.material_cache[mat.name] = index
765
766 m = mdl_material()
767 m.pstr_name = sr_compile_string( mat.name )
768
769 flags = 0x00
770 if mat.SR_data.collision:#{
771 flags |= 0x2 # collision flag
772 if (mat.SR_data.shader != 'invisible') and \
773 (mat.SR_data.shader != 'boundary'):#{
774 if mat.SR_data.skate_surface: flags |= 0x1
775 if mat.SR_data.grow_grass: flags |= 0x4
776 if mat.SR_data.grind_surface: flags |= 0x8
777 if mat.SR_data.preview_visibile: flags |= 0x40
778 #}
779 if mat.SR_data.shader == 'invisible': flags |= 0x10
780 if mat.SR_data.shader == 'boundary': flags |= (0x10|0x20)
781 #}
782
783 m.flags = flags
784
785 m.surface_prop = int(mat.SR_data.surface_prop)
786
787 if mat.SR_data.shader == 'standard': m.shader = 0
788 if mat.SR_data.shader == 'standard_cutout': m.shader = 1
789 if mat.SR_data.shader == 'terrain_blend':#{
790 m.shader = 2
791
792 m.colour[0] = pow( mat.SR_data.sand_colour[0], 1.0/2.2 )
793 m.colour[1] = pow( mat.SR_data.sand_colour[1], 1.0/2.2 )
794 m.colour[2] = pow( mat.SR_data.sand_colour[2], 1.0/2.2 )
795 m.colour[3] = 1.0
796
797 m.colour1[0] = mat.SR_data.blend_offset[0]
798 m.colour1[1] = mat.SR_data.blend_offset[1]
799 #}
800
801 if mat.SR_data.shader == 'vertex_blend':#{
802 m.shader = 3
803
804 m.colour1[0] = mat.SR_data.blend_offset[0]
805 m.colour1[1] = mat.SR_data.blend_offset[1]
806 #}
807
808 if mat.SR_data.shader == 'water':#{
809 m.shader = 4
810
811 m.colour[0] = pow( mat.SR_data.shore_colour[0], 1.0/2.2 )
812 m.colour[1] = pow( mat.SR_data.shore_colour[1], 1.0/2.2 )
813 m.colour[2] = pow( mat.SR_data.shore_colour[2], 1.0/2.2 )
814 m.colour[3] = 1.0
815 m.colour1[0] = pow( mat.SR_data.ocean_colour[0], 1.0/2.2 )
816 m.colour1[1] = pow( mat.SR_data.ocean_colour[1], 1.0/2.2 )
817 m.colour1[2] = pow( mat.SR_data.ocean_colour[2], 1.0/2.2 )
818 m.colour1[3] = 1.0
819 #}
820
821 if mat.SR_data.shader == 'invisible':#{
822 m.shader = 5
823 #}
824
825 if mat.SR_data.shader == 'boundary':#{
826 m.shader = 6
827 #}
828
829 if mat.SR_data.shader == 'fxglow':#{
830 m.shader = 7
831 #}
832
833 inf = material_info( mat )
834
835 if mat.SR_data.shader == 'standard' or \
836 mat.SR_data.shader == 'standard_cutout' or \
837 mat.SR_data.shader == 'terrain_blend' or \
838 mat.SR_data.shader == 'vertex_blend' or \
839 mat.SR_data.shader == 'fxglow': #{
840 if 'tex_diffuse' in inf:
841 m.tex_diffuse = sr_compile_texture(inf['tex_diffuse'])
842 #}
843
844 sr_compile.material_data.extend( bytearray(m) )
845 return index
846 #}
847
848 def sr_armature_bones( armature ):
849 #{
850 def _recurse_bone( b ):
851 #{
852 yield b
853 for c in b.children: yield from _recurse_bone( c )
854 #}
855
856 for b in armature.data.bones:
857 if not b.parent:
858 yield from _recurse_bone( b )
859 #}
860
861 def sr_entity_id( obj ):#{
862 if not obj: return 0
863
864 tipo = get_entity_enum_id( obj_ent_type(obj) )
865 index = sr_compile.entity_ids[ obj.name ]
866
867 return (tipo&0xffff)<<16 | (index&0xffff)
868 #}
869
870 # Returns submesh_start,count and armature_id
871 def sr_compile_mesh_internal( obj ):
872 #{
873 can_use_cache = True
874 armature = None
875
876 submesh_start = 0
877 submesh_count = 0
878 armature_id = 0
879
880 for mod in obj.modifiers:#{
881 if mod.type == 'DATA_TRANSFER' or mod.type == 'SHRINKWRAP' or \
882 mod.type == 'BOOLEAN' or mod.type == 'CURVE' or \
883 mod.type == 'ARRAY':
884 #{
885 can_use_cache = False
886 #}
887
888 if mod.type == 'ARMATURE': #{
889 armature = mod.object
890 rig_weight_groups = \
891 ['0 [ROOT]']+[_.name for _ in sr_armature_bones(mod.object)]
892 armature_id = sr_compile.entity_ids[armature.name]
893
894 POSE_OR_REST_CACHE = armature.data.pose_position
895 armature.data.pose_position = 'REST'
896 #}
897 #}
898
899 # Check the cache first
900 #
901 if can_use_cache and (obj.data.name in sr_compile.mesh_cache):#{
902 ref = sr_compile.mesh_cache[obj.data.name]
903 submesh_start = ref[0]
904 submesh_count = ref[1]
905 return (submesh_start,submesh_count,armature_id)
906 #}
907
908 # Compile a whole new mesh
909 #
910 submesh_start = len(sr_compile.submesh_data)//sizeof(mdl_submesh)
911 submesh_count = 0
912
913 dgraph = bpy.context.evaluated_depsgraph_get()
914 data = obj.evaluated_get(dgraph).data
915 data.calc_loop_triangles()
916 data.calc_normals_split()
917
918 # Mesh is split into submeshes based on their material
919 #
920 mat_list = data.materials if len(data.materials) > 0 else [None]
921 for material_id, mat in enumerate(mat_list): #{
922 mref = {}
923
924 sm = mdl_submesh()
925 sm.indice_start = len(sr_compile.indice_data)//sizeof(c_uint32)
926 sm.vertex_start = len(sr_compile.vertex_data)//sizeof(mdl_vert)
927 sm.vertex_count = 0
928 sm.indice_count = 0
929 sm.material_id = sr_compile_material( mat )
930
931 INF=99999999.99999999
932 for i in range(3):#{
933 sm.bbx[0][i] = INF
934 sm.bbx[1][i] = -INF
935 #}
936
937 # Keep a reference to very very very similar vertices
938 # i have no idea how to speed it up.
939 #
940 vertex_reference = {}
941
942 # Write the vertex / indice data
943 #
944 for tri_index, tri in enumerate(data.loop_triangles):#{
945 if tri.material_index != material_id: continue
946
947 for j in range(3):#{
948 vert = data.vertices[tri.vertices[j]]
949 li = tri.loops[j]
950 vi = data.loops[li].vertex_index
951
952 # Gather vertex information
953 #
954 co = vert.co
955 norm = data.loops[li].normal
956 uv = (0,0)
957 colour = (255,255,255,255)
958 groups = [0,0,0,0]
959 weights = [0,0,0,0]
960
961 # Uvs
962 #
963 if data.uv_layers:
964 uv = data.uv_layers.active.data[li].uv
965
966 # Vertex Colours
967 #
968 if data.vertex_colors:#{
969 colour = data.vertex_colors.active.data[li].color
970 colour = (int(colour[0]*255.0),\
971 int(colour[1]*255.0),\
972 int(colour[2]*255.0),\
973 int(colour[3]*255.0))
974 #}
975
976 # Weight groups: truncates to the 3 with the most influence. The
977 # fourth bone ID is never used by the shader so it
978 # is always 0
979 #
980 if armature:#{
981 src_groups = [_ for _ in data.vertices[vi].groups \
982 if obj.vertex_groups[_.group].name in \
983 rig_weight_groups ]
984
985 weight_groups = sorted( src_groups, key = \
986 lambda a: a.weight, reverse=True )
987 tot = 0.0
988 for ml in range(3):#{
989 if len(weight_groups) > ml:#{
990 g = weight_groups[ml]
991 name = obj.vertex_groups[g.group].name
992 weight = g.weight
993 weights[ml] = weight
994 groups[ml] = rig_weight_groups.index(name)
995 tot += weight
996 #}
997 #}
998
999 if len(weight_groups) > 0:#{
1000 inv_norm = (1.0/tot) * 65535.0
1001 for ml in range(3):#{
1002 weights[ml] = int( weights[ml] * inv_norm )
1003 weights[ml] = min( weights[ml], 65535 )
1004 weights[ml] = max( weights[ml], 0 )
1005 #}
1006 #}
1007 #}
1008 else:#{
1009 li1 = tri.loops[(j+1)%3]
1010 vi1 = data.loops[li1].vertex_index
1011 e0 = data.edges[ data.loops[li].edge_index ]
1012
1013 if e0.use_freestyle_mark and \
1014 ((e0.vertices[0] == vi and e0.vertices[1] == vi1) or \
1015 (e0.vertices[0] == vi1 and e0.vertices[1] == vi)):
1016 #{
1017 weights[0] = 1
1018 #}
1019 #}
1020
1021 TOLERENCE = float(10**4)
1022 key = (int(co[0]*TOLERENCE+0.5),
1023 int(co[1]*TOLERENCE+0.5),
1024 int(co[2]*TOLERENCE+0.5),
1025 int(norm[0]*TOLERENCE+0.5),
1026 int(norm[1]*TOLERENCE+0.5),
1027 int(norm[2]*TOLERENCE+0.5),
1028 int(uv[0]*TOLERENCE+0.5),
1029 int(uv[1]*TOLERENCE+0.5),
1030 colour[0], # these guys are already quantized
1031 colour[1], # .
1032 colour[2], # .
1033 colour[3], # .
1034 weights[0], # v
1035 weights[1],
1036 weights[2],
1037 weights[3],
1038 groups[0],
1039 groups[1],
1040 groups[2],
1041 groups[3])
1042
1043 if key in vertex_reference:
1044 index = vertex_reference[key]
1045 else:#{
1046 index = bytearray(c_uint32(sm.vertex_count))
1047 sm.vertex_count+=1
1048
1049 vertex_reference[key] = index
1050 v = mdl_vert()
1051 v.co[0] = co[0]
1052 v.co[1] = co[2]
1053 v.co[2] = -co[1]
1054 v.norm[0] = norm[0]
1055 v.norm[1] = norm[2]
1056 v.norm[2] = -norm[1]
1057 v.uv[0] = uv[0]
1058 v.uv[1] = uv[1]
1059 v.colour[0] = colour[0]
1060 v.colour[1] = colour[1]
1061 v.colour[2] = colour[2]
1062 v.colour[3] = colour[3]
1063 v.weights[0] = weights[0]
1064 v.weights[1] = weights[1]
1065 v.weights[2] = weights[2]
1066 v.weights[3] = weights[3]
1067 v.groups[0] = groups[0]
1068 v.groups[1] = groups[1]
1069 v.groups[2] = groups[2]
1070 v.groups[3] = groups[3]
1071
1072 for i in range(3):#{
1073 sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
1074 sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
1075 #}
1076
1077 sr_compile.vertex_data.extend(bytearray(v))
1078 #}
1079
1080 sm.indice_count += 1
1081 sr_compile.indice_data.extend( index )
1082 #}
1083 #}
1084
1085 # Make sure bounding box isn't -inf -> inf if no vertices
1086 #
1087 if sm.vertex_count == 0:
1088 for j in range(2):
1089 for i in range(3):
1090 sm.bbx[j][i] = 0
1091
1092 # Add submesh to encoder
1093 #
1094 sr_compile.submesh_data.extend( bytearray(sm) )
1095 submesh_count += 1
1096 #}
1097
1098 if armature:#{
1099 armature.data.pose_position = POSE_OR_REST_CACHE
1100 #}
1101
1102 # Save a reference to this mesh since we want to reuse the submesh indices
1103 # later.
1104 sr_compile.mesh_cache[obj.data.name]=(submesh_start,submesh_count)
1105 return (submesh_start,submesh_count,armature_id)
1106 #}
1107
1108 def sr_compile_mesh( obj ):
1109 #{
1110 node=mdl_mesh()
1111 compile_obj_transform(obj, node.transform)
1112 node.pstr_name = sr_compile_string(obj.name)
1113 ent_type = obj_ent_type( obj )
1114
1115 node.entity_id = 0
1116
1117 if ent_type != 'none':#{
1118 ent_id_lwr = sr_compile.entity_ids[obj.name]
1119 ent_id_upr = get_entity_enum_id( obj_ent_type(obj) )
1120 node.entity_id = (ent_id_upr << 16) | ent_id_lwr
1121 #}
1122
1123 node.submesh_start, node.submesh_count, node.armature_id = \
1124 sr_compile_mesh_internal( obj )
1125
1126 sr_compile.mesh_data.extend(bytearray(node))
1127 #}
1128
1129 def sr_compile_fonts( collection ):
1130 #{
1131 print( F"[SR] Compiling fonts" )
1132
1133 glyph_count = 0
1134 variant_count = 0
1135
1136 for obj in collection.all_objects:#{
1137 if obj_ent_type(obj) != 'ent_font': continue
1138
1139 data = obj.SR_data.ent_font[0]
1140
1141 font=ent_font()
1142 font.alias = sr_compile_string( data.alias )
1143 font.variant_start = variant_count
1144 font.variant_count = 0
1145 font.glyph_start = glyph_count
1146
1147 glyph_base = data.glyphs[0].utf32
1148 glyph_range = data.glyphs[-1].utf32+1 - glyph_base
1149
1150 font.glyph_utf32_base = glyph_base
1151 font.glyph_count = glyph_range
1152
1153 for i in range(len(data.variants)):#{
1154 data_var = data.variants[i]
1155 if not data_var.mesh: continue
1156
1157 mesh = data_var.mesh.data
1158
1159 variant = ent_font_variant()
1160 variant.name = sr_compile_string( data_var.tipo )
1161
1162 # fonts (variants) only support one material each
1163 mat = None
1164 if len(mesh.materials) != 0:
1165 mat = mesh.materials[0]
1166 variant.material_id = sr_compile_material( mat )
1167
1168 font.variant_count += 1
1169
1170 islands = mesh_utils.mesh_linked_triangles(mesh)
1171 centroids = [Vector((0,0)) for _ in range(len(islands))]
1172
1173 for j in range(len(islands)):#{
1174 for tri in islands[j]:#{
1175 centroids[j].x += tri.center[0]
1176 centroids[j].y += tri.center[2]
1177 #}
1178
1179 centroids[j] /= len(islands[j])
1180 #}
1181
1182 for j in range(glyph_range):#{
1183 data_glyph = data.glyphs[j]
1184 glyph = ent_glyph()
1185 glyph.indice_start = len(sr_compile.indice_data)//sizeof(c_uint32)
1186 glyph.indice_count = 0
1187 glyph.size[0] = data_glyph.bounds[2]
1188 glyph.size[1] = data_glyph.bounds[3]
1189
1190 vertex_reference = {}
1191
1192 for k in range(len(islands)):#{
1193 if centroids[k].x < data_glyph.bounds[0] or \
1194 centroids[k].x > data_glyph.bounds[0]+data_glyph.bounds[2] or\
1195 centroids[k].y < data_glyph.bounds[1] or \
1196 centroids[k].y > data_glyph.bounds[1]+data_glyph.bounds[3]:
1197 #{
1198 continue
1199 #}
1200
1201 for l in range(len(islands[k])):#{
1202 tri = islands[k][l]
1203 for m in range(3):#{
1204 vert = mesh.vertices[tri.vertices[m]]
1205 li = tri.loops[m]
1206 vi = mesh.loops[li].vertex_index
1207
1208 # Gather vertex information
1209 #
1210 co = [vert.co[_] for _ in range(3)]
1211 co[0] -= data_glyph.bounds[0]
1212 co[2] -= data_glyph.bounds[1]
1213 norm = mesh.loops[li].normal
1214 uv = (0,0)
1215 if mesh.uv_layers: uv = mesh.uv_layers.active.data[li].uv
1216
1217 TOLERENCE = float(10**4)
1218 key = (int(co[0]*TOLERENCE+0.5),
1219 int(co[1]*TOLERENCE+0.5),
1220 int(co[2]*TOLERENCE+0.5),
1221 int(norm[0]*TOLERENCE+0.5),
1222 int(norm[1]*TOLERENCE+0.5),
1223 int(norm[2]*TOLERENCE+0.5),
1224 int(uv[0]*TOLERENCE+0.5),
1225 int(uv[1]*TOLERENCE+0.5))
1226
1227 if key in vertex_reference:
1228 index = vertex_reference[key]
1229 else:#{
1230 vindex = len(sr_compile.vertex_data)//sizeof(mdl_vert)
1231 index = bytearray(c_uint32(vindex))
1232 vertex_reference[key] = index
1233 v = mdl_vert()
1234 v.co[0] = co[0]
1235 v.co[1] = co[2]
1236 v.co[2] = -co[1]
1237 v.norm[0] = norm[0]
1238 v.norm[1] = norm[2]
1239 v.norm[2] = -norm[1]
1240 v.uv[0] = uv[0]
1241 v.uv[1] = uv[1]
1242
1243 sr_compile.vertex_data.extend(bytearray(v))
1244 #}
1245
1246 glyph.indice_count += 1
1247 sr_compile.indice_data.extend( index )
1248 #}
1249 #}
1250 #}
1251 sr_ent_push( glyph )
1252 #}
1253 sr_ent_push( variant )
1254 #}
1255 sr_ent_push( font )
1256 #}
1257 #}
1258
1259 def sr_compile_menus( collection ):
1260 #{
1261 print( "[SR1] Compiling menus" )
1262 groups = []
1263
1264 for obj in collection.all_objects:#{
1265 if obj_ent_type(obj) != 'ent_menuitem': continue
1266 obj_data = obj.SR_data.ent_menuitem[0]
1267
1268 bitmask = 0x00000000
1269
1270 for col in obj.users_collection:#{
1271 name = col.name
1272 if name not in groups: groups.append( name )
1273 bitmask |= (0x1 << groups.index(name))
1274 #}
1275
1276 item = ent_menuitem()
1277 item.type = int( obj_data.tipo )
1278 item.groups = bitmask
1279
1280 compile_obj_transform( obj, item.transform )
1281 if obj.type == 'MESH':#{
1282 item.submesh_start, item.submesh_count, _ = \
1283 sr_compile_mesh_internal( obj )
1284 #}
1285
1286 if item.type == 1 or item.type == 2:#{
1287 item_button = item._anonymous_union.button
1288 item_button.pstr = sr_compile_string( obj_data.string )
1289 #}
1290 elif item.type == 3:#{
1291 item_checkmark = item._anonymous_union.checkmark
1292 item_checkmark.pstr_data = sr_compile_string( obj_data.string )
1293 item_checkmark.id_check = sr_entity_id( obj_data.checkmark )
1294 delta = obj_data.checkmark.location - obj.location
1295 item_checkmark.offset[0] = delta[0]
1296 item_checkmark.offset[1] = delta[2]
1297 item_checkmark.offset[2] = -delta[1]
1298 #}
1299 elif item.type == 4:#{
1300 item_slider = item._anonymous_union.slider
1301 item_slider.id_min = sr_entity_id( obj_data.slider_minloc )
1302 item_slider.id_max = sr_entity_id( obj_data.slider_maxloc )
1303 item_slider.id_handle = sr_entity_id( obj_data.slider_handle )
1304 item_slider.pstr_data = sr_compile_string( obj_data.string )
1305 #}
1306 elif item.type == 5:#{
1307 item_page = item._anonymous_union.page
1308 item_page.pstr_name = sr_compile_string( obj_data.string )
1309 item_page.id_entrypoint = sr_entity_id( obj_data.newloc )
1310 item_page.id_viewpoint = sr_entity_id( obj_data.camera )
1311 #}
1312
1313 if obj_data.link0:
1314 item.id_links[0] = sr_entity_id( obj_data.link0 )
1315 if obj_data.link1:
1316 item.id_links[1] = sr_entity_id( obj_data.link1 )
1317 if item.type != 4:#{
1318 if obj_data.link2:
1319 item.id_links[2] = sr_entity_id( obj_data.link2 )
1320 if obj_data.link3:
1321 item.id_links[3] = sr_entity_id( obj_data.link3 )
1322 #}
1323
1324 sr_ent_push( item )
1325 #}
1326 #}
1327
1328 def sr_compile_armature( obj ):
1329 #{
1330 node = mdl_armature()
1331 node.bone_start = len(sr_compile.bone_data)//sizeof(mdl_bone)
1332 node.bone_count = 0
1333 node.anim_start = len(sr_compile.anim_data)//sizeof(mdl_animation)
1334 node.anim_count = 0
1335
1336 bones = [_ for _ in sr_armature_bones(obj)]
1337 bones_names = [None]+[_.name for _ in bones]
1338
1339 for b in bones:#{
1340 bone = mdl_bone()
1341 if b.use_deform: bone.flags = 0x1
1342 if b.parent: bone.parent = bones_names.index(b.parent.name)
1343
1344 bone.collider = int(b.SR_data.collider)
1345
1346 if bone.collider>0:#{
1347 bone.hitbox[0][0] = b.SR_data.collider_min[0]
1348 bone.hitbox[0][1] = b.SR_data.collider_min[2]
1349 bone.hitbox[0][2] = -b.SR_data.collider_max[1]
1350 bone.hitbox[1][0] = b.SR_data.collider_max[0]
1351 bone.hitbox[1][1] = b.SR_data.collider_max[2]
1352 bone.hitbox[1][2] = -b.SR_data.collider_min[1]
1353 #}
1354
1355 if b.SR_data.cone_constraint:#{
1356 bone.flags |= 0x4
1357 bone.conevx[0] = b.SR_data.conevx[0]
1358 bone.conevx[1] = b.SR_data.conevx[2]
1359 bone.conevx[2] = -b.SR_data.conevx[1]
1360 bone.conevy[0] = b.SR_data.conevy[0]
1361 bone.conevy[1] = b.SR_data.conevy[2]
1362 bone.conevy[2] = -b.SR_data.conevy[1]
1363 bone.coneva[0] = b.SR_data.coneva[0]
1364 bone.coneva[1] = b.SR_data.coneva[2]
1365 bone.coneva[2] = -b.SR_data.coneva[1]
1366 bone.conet = b.SR_data.conet
1367 #}
1368
1369 bone.co[0] = b.head_local[0]
1370 bone.co[1] = b.head_local[2]
1371 bone.co[2] = -b.head_local[1]
1372 bone.end[0] = b.tail_local[0] - bone.co[0]
1373 bone.end[1] = b.tail_local[2] - bone.co[1]
1374 bone.end[2] = -b.tail_local[1] - bone.co[2]
1375 bone.pstr_name = sr_compile_string( b.name )
1376
1377 for c in obj.pose.bones[b.name].constraints:#{
1378 if c.type == 'IK':#{
1379 bone.flags |= 0x2
1380 bone.ik_target = bones_names.index(c.subtarget)
1381 bone.ik_pole = bones_names.index(c.pole_subtarget)
1382 #}
1383 #}
1384
1385 node.bone_count += 1
1386 sr_compile.bone_data.extend(bytearray(bone))
1387 #}
1388
1389 # Compile anims
1390 #
1391 if obj.animation_data and sr_compile.pack_animations: #{
1392 # So we can restore later
1393 #
1394 previous_frame = bpy.context.scene.frame_current
1395 previous_action = obj.animation_data.action
1396 POSE_OR_REST_CACHE = obj.data.pose_position
1397 obj.data.pose_position = 'POSE'
1398
1399 for NLALayer in obj.animation_data.nla_tracks:#{
1400 for NLAStrip in NLALayer.strips:#{
1401 # set active
1402 #
1403 for a in bpy.data.actions:#{
1404 if a.name == NLAStrip.name:#{
1405 obj.animation_data.action = a
1406 break
1407 #}
1408 #}
1409
1410 # Clip to NLA settings
1411 #
1412 anim_start = int(NLAStrip.action_frame_start)
1413 anim_end = int(NLAStrip.action_frame_end)
1414
1415 # Export strips
1416 #
1417 anim = mdl_animation()
1418 anim.pstr_name = sr_compile_string( NLAStrip.action.name )
1419 anim.rate = 30.0
1420 anim.keyframe_start = len(sr_compile.keyframe_data)//\
1421 sizeof(mdl_transform)
1422 anim.length = anim_end-anim_start
1423
1424 i = 0
1425 # Export the keyframes
1426 for frame in range(anim_start,anim_end):#{
1427 bpy.context.scene.frame_set(frame)
1428
1429 for rb in bones:#{
1430 pb = obj.pose.bones[rb.name]
1431
1432 # relative bone matrix
1433 if rb.parent is not None:#{
1434 offset_mtx = rb.parent.matrix_local
1435 offset_mtx = offset_mtx.inverted_safe() @ \
1436 rb.matrix_local
1437
1438 inv_parent = pb.parent.matrix @ offset_mtx
1439 inv_parent.invert_safe()
1440 fpm = inv_parent @ pb.matrix
1441 #}
1442 else:#{
1443 bone_mtx = rb.matrix.to_4x4()
1444 local_inv = rb.matrix_local.inverted_safe()
1445 fpm = bone_mtx @ local_inv @ pb.matrix
1446 #}
1447
1448 loc, rot, sca = fpm.decompose()
1449
1450 # rotation
1451 lc_m = pb.matrix_channel.to_3x3()
1452 if pb.parent is not None:#{
1453 smtx = pb.parent.matrix_channel.to_3x3()
1454 lc_m = smtx.inverted() @ lc_m
1455 #}
1456 rq = lc_m.to_quaternion()
1457 q_normalize( rq )
1458
1459 kf = mdl_transform()
1460 kf.co[0] = loc[0]
1461 kf.co[1] = loc[2]
1462 kf.co[2] = -loc[1]
1463 kf.q[0] = rq[1]
1464 kf.q[1] = rq[3]
1465 kf.q[2] = -rq[2]
1466 kf.q[3] = rq[0]
1467 kf.s[0] = sca[0]
1468 kf.s[1] = sca[1]
1469 kf.s[2] = sca[2]
1470 sr_compile.keyframe_data.extend(bytearray(kf))
1471
1472 i+=1
1473 #}
1474 #}
1475
1476 # Add to animation buffer
1477 #
1478 sr_compile.anim_data.extend(bytearray(anim))
1479 node.anim_count += 1
1480
1481 # Report progress
1482 #
1483 print( F"[SR] | anim( {NLAStrip.action.name} )" )
1484 #}
1485 #}
1486
1487 # Restore context to how it was before
1488 #
1489 bpy.context.scene.frame_set( previous_frame )
1490 obj.animation_data.action = previous_action
1491 obj.data.pose_position = POSE_OR_REST_CACHE
1492 #}
1493
1494 sr_compile.armature_data.extend(bytearray(node))
1495 #}
1496
1497 def sr_ent_push( struct ):
1498 #{
1499 clase = type(struct).__name__
1500
1501 if clase not in sr_compile.entity_data:#{
1502 sr_compile.entity_data[ clase ] = bytearray()
1503 sr_compile.entity_info[ clase ] = { 'size': sizeof(struct) }
1504 #}
1505
1506 index = len(sr_compile.entity_data[ clase ])//sizeof(struct)
1507 sr_compile.entity_data[ clase ].extend( bytearray(struct) )
1508 return index
1509 #}
1510
1511 def sr_array_title( arr, name, count, size, offset ):
1512 #{
1513 for i in range(len(name)):#{
1514 arr.name[i] = ord(name[i])
1515 #}
1516 arr.file_offset = offset
1517 arr.item_count = count
1518 arr.item_size = size
1519 #}
1520
1521 def hash_djb2(s):
1522 #{
1523 picadillo = 5381
1524 for x in s:#{
1525 picadillo = (((picadillo << 5) + picadillo) + ord(x)) & 0xFFFFFFFF
1526 #}
1527 return picadillo
1528 #}
1529
1530 def sr_compile( collection ):
1531 #{
1532 print( F"[SR] compiler begin ({collection.name}.mdl)" )
1533
1534 #settings
1535 sr_compile.pack_textures = collection.SR_data.pack_textures
1536 sr_compile.pack_animations = collection.SR_data.animations
1537
1538 # caches
1539 sr_compile.string_cache = {}
1540 sr_compile.mesh_cache = {}
1541 sr_compile.material_cache = {}
1542 sr_compile.texture_cache = {}
1543
1544 # compiled data
1545 sr_compile.mesh_data = bytearray()
1546 sr_compile.submesh_data = bytearray()
1547 sr_compile.vertex_data = bytearray()
1548 sr_compile.indice_data = bytearray()
1549 sr_compile.bone_data = bytearray()
1550 sr_compile.material_data = bytearray()
1551 sr_compile.armature_data = bytearray()
1552 sr_compile.anim_data = bytearray()
1553 sr_compile.keyframe_data = bytearray()
1554 sr_compile.texture_data = bytearray()
1555
1556 # just bytes not structures
1557 sr_compile.string_data = bytearray()
1558 sr_compile.pack_data = bytearray()
1559
1560 # variable
1561 sr_compile.entity_data = {}
1562 sr_compile.entity_info = {}
1563
1564 print( F"[SR] assign entity ID's" )
1565 sr_compile.entities = {}
1566 sr_compile.entity_ids = {}
1567
1568 # begin
1569 # -------------------------------------------------------
1570
1571 sr_compile_string( "null" )
1572
1573 mesh_count = 0
1574 for obj in collection.all_objects: #{
1575 if obj.type == 'MESH':#{
1576 mesh_count += 1
1577 #}
1578
1579 ent_type = obj_ent_type( obj )
1580 if ent_type == 'none': continue
1581
1582 if ent_type not in sr_compile.entities: sr_compile.entities[ent_type] = []
1583 sr_compile.entity_ids[obj.name] = len( sr_compile.entities[ent_type] )
1584 sr_compile.entities[ent_type] += [obj]
1585 #}
1586
1587 print( F"[SR] Compiling geometry" )
1588 i=0
1589 for obj in collection.all_objects:#{
1590 if obj.type == 'MESH':#{
1591 i+=1
1592
1593 ent_type = obj_ent_type( obj )
1594
1595 # entity ignore mesh list
1596 #
1597 if ent_type == 'ent_traffic': continue
1598 if ent_type == 'ent_font': continue
1599 if ent_type == 'ent_font_variant': continue
1600 if ent_type == 'ent_menuitem': continue
1601 if ent_type == 'ent_challenge': continue
1602
1603 #TODO: This is messy.
1604 if ent_type == 'ent_gate':#{
1605 obj_data = obj.SR_data.ent_gate[0]
1606 if obj_data.custom: continue
1607 #}
1608 #--------------------------
1609
1610 print( F'[SR] {i: 3}/{mesh_count} {obj.name:<40}', end='\r' )
1611 sr_compile_mesh( obj )
1612 #}
1613 #}
1614
1615 audio_clip_count = 0
1616
1617 for ent_type, arr in sr_compile.entities.items():#{
1618 print(F"[SR] Compiling {len(arr)} {ent_type}{'s' if len(arr)>1 else ''}")
1619
1620 for i in range(len(arr)):#{
1621 obj = arr[i]
1622
1623 print( F"[SR] {i+1: 3}/{len(arr)} {obj.name:<40} ",end='\r' )
1624
1625 if ent_type == 'mdl_armature': sr_compile_armature(obj)
1626 elif ent_type == 'ent_light': #{
1627 light = ent_light()
1628 compile_obj_transform( obj, light.transform )
1629 light.daytime = obj.data.SR_data.daytime
1630 if obj.data.type == 'POINT':#{
1631 light.type = 0
1632 #}
1633 elif obj.data.type == 'SPOT':#{
1634 light.type = 1
1635 light.angle = obj.data.spot_size*0.5
1636 #}
1637 light.range = obj.data.cutoff_distance
1638 light.colour[0] = obj.data.color[0]
1639 light.colour[1] = obj.data.color[1]
1640 light.colour[2] = obj.data.color[2]
1641 light.colour[3] = obj.data.energy
1642 sr_ent_push( light )
1643 #}
1644 elif ent_type == 'ent_camera': #{
1645 cam = ent_camera()
1646 compile_obj_transform( obj, cam.transform )
1647 cam.fov = obj.data.angle * 45.0
1648 sr_ent_push(cam)
1649 #}
1650 elif ent_type == 'ent_gate': #{
1651 gate = ent_gate()
1652 obj_data = obj.SR_data.ent_gate[0]
1653 mesh_data = obj.data.SR_data.ent_gate[0]
1654
1655 flags = 0x0000
1656
1657 if obj_data.tipo == 'default':#{
1658 if obj_data.target:#{
1659 gate.target = sr_compile.entity_ids[obj_data.target.name]
1660 flags |= 0x0001
1661 #}
1662 #}
1663 elif obj_data.tipo == 'nonlocal':#{
1664 gate.target = 0
1665 gate.key = sr_compile_string(obj_data.key)
1666 flags |= 0x0002
1667 #}
1668
1669 if obj_data.flip: flags |= 0x0004
1670 if obj_data.custom:#{
1671 flags |= 0x0008
1672 gate.submesh_start, gate.submesh_count, _ = \
1673 sr_compile_mesh_internal( obj )
1674 #}
1675 if obj_data.locked: flags |= 0x0010
1676 gate.flags = flags
1677
1678 gate.dimensions[0] = mesh_data.dimensions[0]
1679 gate.dimensions[1] = mesh_data.dimensions[1]
1680 gate.dimensions[2] = mesh_data.dimensions[2]
1681
1682 q = [obj.matrix_local.to_quaternion(), (0,0,0,1)]
1683 co = [obj.matrix_world @ Vector((0,0,0)), (0,0,0)]
1684
1685 if obj_data.target:#{
1686 q[1] = obj_data.target.matrix_local.to_quaternion()
1687 co[1]= obj_data.target.matrix_world @ Vector((0,0,0))
1688 #}
1689
1690 # Setup transform
1691 #
1692 for x in range(2):#{
1693 gate.co[x][0] = co[x][0]
1694 gate.co[x][1] = co[x][2]
1695 gate.co[x][2] = -co[x][1]
1696 gate.q[x][0] = q[x][1]
1697 gate.q[x][1] = q[x][3]
1698 gate.q[x][2] = -q[x][2]
1699 gate.q[x][3] = q[x][0]
1700 #}
1701
1702 sr_ent_push( gate )
1703 #}
1704 elif ent_type == 'ent_spawn': #{
1705 spawn = ent_spawn()
1706 compile_obj_transform( obj, spawn.transform )
1707 obj_data = obj.SR_data.ent_spawn[0]
1708 spawn.pstr_name = sr_compile_string( obj_data.alias )
1709 sr_ent_push( spawn )
1710 #}
1711 elif ent_type == 'ent_water':#{
1712 water = ent_water()
1713 compile_obj_transform( obj, water.transform )
1714 water.max_dist = 0.0
1715 sr_ent_push( water )
1716 #}
1717 elif ent_type == 'ent_audio':#{
1718 obj_data = obj.SR_data.ent_audio[0]
1719 audio = ent_audio()
1720 compile_obj_transform( obj, audio.transform )
1721 audio.clip_start = audio_clip_count
1722 audio.clip_count = len(obj_data.files)
1723 audio_clip_count += audio.clip_count
1724 audio.max_channels = obj_data.max_channels
1725 audio.volume = obj_data.volume
1726
1727 # TODO flags:
1728 # - allow/disable doppler
1729 # - channel group tags with random colours
1730 # - transition properties
1731
1732 if obj_data.flag_loop: audio.flags |= 0x1
1733 if obj_data.flag_nodoppler: audio.flags |= 0x2
1734 if obj_data.flag_3d: audio.flags |= 0x4
1735 if obj_data.flag_auto: audio.flags |= 0x8
1736 if obj_data.formato == '0': audio.flags |= 0x000
1737 elif obj_data.formato == '1': audio.flags |= 0x400
1738 elif obj_data.formato == '2': audio.flags |= 0x1000
1739
1740 audio.channel_behaviour = int(obj_data.channel_behaviour)
1741 if audio.channel_behaviour >= 1:#{
1742 audio.group = obj_data.group
1743 #}
1744 if audio.channel_behaviour == 2:#{
1745 audio.crossfade = obj_data.transition_duration
1746 #}
1747 audio.probability_curve = int(obj_data.probability_curve)
1748
1749 for ci in range(audio.clip_count):#{
1750 entry = obj_data.files[ci]
1751 clip = ent_audio_clip()
1752 clip.probability = entry.probability
1753 if obj_data.formato == '2':#{
1754 sr_pack_file( clip._anon.file, '', vg_str_bin(entry.path) )
1755 #}
1756 else:#{
1757 clip._anon.file.path = sr_compile_string( entry.path )
1758 clip._anon.file.pack_offset = 0
1759 clip._anon.file.pack_size = 0
1760 #}
1761 sr_ent_push( clip )
1762 #}
1763 sr_ent_push( audio )
1764 #}
1765 elif ent_type == 'ent_volume':#{
1766 obj_data = obj.SR_data.ent_volume[0]
1767 volume = ent_volume()
1768 volume.type = int(obj_data.subtype)
1769 compile_obj_transform( obj, volume.transform )
1770
1771 if obj_data.target:#{
1772 volume.target = sr_entity_id( obj_data.target )
1773 volume._anon.trigger.event = obj_data.target_event
1774 #}
1775
1776 sr_ent_push(volume)
1777 #}
1778 elif ent_type == 'ent_marker':#{
1779 marker = ent_marker()
1780 marker.name = sr_compile_string( obj.SR_data.ent_marker[0].alias )
1781 compile_obj_transform( obj, marker.transform )
1782 sr_ent_push(marker)
1783 #}
1784 elif ent_type == 'ent_skateshop':#{
1785 skateshop = ent_skateshop()
1786 obj_data = obj.SR_data.ent_skateshop[0]
1787 skateshop.type = int(obj_data.tipo)
1788 if skateshop.type == 0:#{
1789 boardshop = skateshop._anonymous_union.boards
1790 boardshop.id_display = sr_entity_id( obj_data.mark_display )
1791 boardshop.id_info = sr_entity_id( obj_data.mark_info )
1792 boardshop.id_rack = sr_entity_id( obj_data.mark_rack )
1793 #}
1794 elif skateshop.type == 1:#{
1795 charshop = skateshop._anonymous_union.character
1796 charshop.id_display = sr_entity_id( obj_data.mark_display )
1797 charshop.id_info = sr_entity_id( obj_data.mark_info )
1798 #}
1799 elif skateshop.type == 2:#{
1800 worldshop = skateshop._anonymous_union.worlds
1801 worldshop.id_display = sr_entity_id( obj_data.mark_display )
1802 worldshop.id_info = sr_entity_id( obj_data.mark_info )
1803 #}
1804 skateshop.id_camera = sr_entity_id( obj_data.cam )
1805 compile_obj_transform( obj, skateshop.transform )
1806 sr_ent_push(skateshop)
1807 #}
1808 elif ent_type == 'ent_swspreview':#{
1809 workshop_preview = ent_swspreview()
1810 obj_data = obj.SR_data.ent_swspreview[0]
1811 workshop_preview.id_display = sr_entity_id( obj_data.mark_display )
1812 workshop_preview.id_display1 = sr_entity_id( obj_data.mark_display1)
1813 workshop_preview.id_camera = sr_entity_id( obj_data.cam )
1814 sr_ent_push( workshop_preview )
1815 #}
1816 elif ent_type == 'ent_worldinfo':#{
1817 worldinfo = ent_worldinfo()
1818 obj_data = obj.SR_data.ent_worldinfo[0]
1819 worldinfo.pstr_name = sr_compile_string( obj_data.name )
1820 worldinfo.pstr_author = sr_compile_string( obj_data.author )
1821 worldinfo.pstr_desc = sr_compile_string( obj_data.desc )
1822 worldinfo.timezone = obj_data.timezone
1823 sr_ent_push( worldinfo )
1824 #}
1825 elif ent_type == 'ent_ccmd':#{
1826 ccmd = ent_ccmd()
1827 obj_data = obj.SR_data.ent_ccmd[0]
1828 ccmd.pstr_command = sr_compile_string( obj_data.command )
1829 sr_ent_push( ccmd )
1830 #}
1831 elif ent_type == 'ent_challenge':#{
1832 challenge = ent_challenge()
1833 obj_data = obj.SR_data.ent_challenge[0]
1834 challenge.id_next = sr_entity_id( obj_data.proxima )
1835 challenge.id_win = sr_entity_id( obj_data.target )
1836 challenge.win_event = obj_data.target_event
1837 challenge.filter = 0
1838 challenge.time_limit = obj_data.time_limit
1839
1840 compile_obj_transform( obj, challenge.transform )
1841 challenge.submesh_start, challenge.submesh_count, _ = \
1842 sr_compile_mesh_internal( obj )
1843
1844 sr_ent_push( challenge )
1845 #}
1846 elif ent_type == 'ent_unlock':#{
1847 unlock = ent_unlock()
1848 obj_data = obj.SR_data.ent_unlock[0]
1849 unlock.pstr_alias = sr_compile_string( obj_data.alias )
1850 unlock.target = sr_entity_id( obj_data.target )
1851 unlock.target_event = obj_data.target_event
1852 unlock.status = 0
1853 sr_ent_push( unlock )
1854 #}
1855 elif ent_type == 'ent_relay':#{
1856 relay = ent_relay()
1857 obj_data = obj.SR_data.ent_relay[0]
1858 relay.targets[0][0] = sr_entity_id( obj_data.target0 )
1859 relay.targets[1][0] = sr_entity_id( obj_data.target1 )
1860 relay.targets[2][0] = sr_entity_id( obj_data.target2 )
1861 relay.targets[3][0] = sr_entity_id( obj_data.target3 )
1862 relay.targets[0][1] = obj_data.target0_event
1863 relay.targets[1][1] = obj_data.target1_event
1864 relay.targets[2][1] = obj_data.target2_event
1865 relay.targets[3][1] = obj_data.target3_event
1866 sr_ent_push( relay )
1867 #}
1868 #}
1869 #}
1870
1871 sr_compile_menus( collection )
1872 sr_compile_fonts( collection )
1873
1874 def _children( col ):#{
1875 yield col
1876 for c in col.children:#{
1877 yield from _children(c)
1878 #}
1879 #}
1880
1881 checkpoint_count = 0
1882 pathindice_count = 0
1883 routenode_count = 0
1884
1885 for col in _children(collection):#{
1886 print( F"Adding routes for subcollection: {col.name}" )
1887 route_gates = []
1888 route_curves = []
1889 routes = []
1890 traffics = []
1891
1892 for obj in col.objects:#{
1893 if obj.type == 'ARMATURE': pass
1894 else:#{
1895 ent_type = obj_ent_type( obj )
1896
1897 if ent_type == 'ent_gate':
1898 route_gates += [obj]
1899 elif ent_type == 'ent_route_node':#{
1900 if obj.type == 'CURVE':#{
1901 route_curves += [obj]
1902 #}
1903 #}
1904 elif ent_type == 'ent_route':
1905 routes += [obj]
1906 elif ent_type == 'ent_traffic':
1907 traffics += [obj]
1908 #}
1909 #}
1910
1911 dij = create_node_graph( route_curves, route_gates )
1912
1913 for obj in routes:#{
1914 obj_data = obj.SR_data.ent_route[0]
1915 route = ent_route()
1916 route.pstr_name = sr_compile_string( obj_data.alias )
1917 route.checkpoints_start = checkpoint_count
1918 route.checkpoints_count = 0
1919
1920 for ci in range(3):
1921 route.colour[ci] = obj_data.colour[ci]
1922 route.colour[3] = 1.0
1923
1924 compile_obj_transform( obj, route.transform )
1925 checkpoints = obj_data.gates
1926
1927 for i in range(len(checkpoints)):#{
1928 gi = checkpoints[i].target
1929 gj = checkpoints[(i+1)%len(checkpoints)].target
1930 gate = gi
1931
1932 if gi:#{
1933 dest = gi.SR_data.ent_gate[0].target
1934 gi = dest
1935 #}
1936
1937 if gi==gj: continue # error?
1938 if not gi or not gj: continue
1939
1940 checkpoint = ent_checkpoint()
1941 checkpoint.gate_index = sr_compile.entity_ids[gate.name]
1942 checkpoint.path_start = pathindice_count
1943 checkpoint.path_count = 0
1944
1945 path = solve_graph( dij, gi.name, gj.name )
1946
1947 if path:#{
1948 for pi in range(len(path)):#{
1949 pathindice = ent_path_index()
1950 pathindice.index = routenode_count + path[pi]
1951 sr_ent_push( pathindice )
1952
1953 checkpoint.path_count += 1
1954 pathindice_count += 1
1955 #}
1956 #}
1957
1958 sr_ent_push( checkpoint )
1959 route.checkpoints_count += 1
1960 checkpoint_count += 1
1961 #}
1962
1963 sr_ent_push( route )
1964 #}
1965
1966 for obj in traffics:#{
1967 traffic = ent_traffic()
1968 compile_obj_transform( obj, traffic.transform )
1969 traffic.submesh_start, traffic.submesh_count, _ = \
1970 sr_compile_mesh_internal( obj )
1971
1972 # find best subsection
1973
1974 graph_keys = list(dij.graph)
1975 min_dist = 100.0
1976 best_point = 0
1977
1978 for j in range(len(dij.points)):#{
1979 point = dij.points[j]
1980 dist = (point-obj.location).magnitude
1981
1982 if dist < min_dist:#{
1983 min_dist = dist
1984 best_point = j
1985 #}
1986 #}
1987
1988 # scan to each edge
1989 best_begin = best_point
1990 best_end = best_point
1991
1992 while True:#{
1993 map0 = dij.subsections[best_begin]
1994 if map0[1] == -1: break
1995 best_begin = map0[1]
1996 #}
1997 while True:#{
1998 map1 = dij.subsections[best_end]
1999 if map1[2] == -1: break
2000 best_end = map1[2]
2001 #}
2002
2003 traffic.start_node = routenode_count + best_begin
2004 traffic.node_count = best_end - best_begin
2005 traffic.index = best_point - best_begin
2006 traffic.speed = obj.SR_data.ent_traffic[0].speed
2007 traffic.t = 0.0
2008
2009 sr_ent_push(traffic)
2010 #}
2011
2012 for point in dij.points:#{
2013 rn = ent_route_node()
2014 rn.co[0] = point[0]
2015 rn.co[1] = point[2]
2016 rn.co[2] = -point[1]
2017 sr_ent_push( rn )
2018 #}
2019
2020 routenode_count += len(dij.points)
2021 #}
2022
2023 print( F"[SR] Writing file" )
2024
2025 file_array_instructions = {}
2026 file_offset = 0
2027
2028 def _write_array( name, item_size, data ):#{
2029 nonlocal file_array_instructions, file_offset
2030
2031 count = len(data)//item_size
2032 file_array_instructions[name] = {'count':count, 'size':item_size,\
2033 'data':data, 'offset': file_offset}
2034 file_offset += len(data)
2035 file_offset = int_align_to( file_offset, 8 )
2036 #}
2037
2038 _write_array( 'strings', 1, sr_compile.string_data )
2039 _write_array( 'mdl_mesh', sizeof(mdl_mesh), sr_compile.mesh_data )
2040 _write_array( 'mdl_submesh', sizeof(mdl_submesh), sr_compile.submesh_data )
2041 _write_array( 'mdl_material', sizeof(mdl_material), sr_compile.material_data)
2042 _write_array( 'mdl_texture', sizeof(mdl_texture), sr_compile.texture_data)
2043 _write_array( 'mdl_armature', sizeof(mdl_armature), sr_compile.armature_data)
2044 _write_array( 'mdl_bone', sizeof(mdl_bone), sr_compile.bone_data )
2045
2046 for name, buffer in sr_compile.entity_data.items():#{
2047 _write_array( name, sr_compile.entity_info[name]['size'], buffer )
2048 #}
2049
2050 _write_array( 'mdl_animation', sizeof(mdl_animation), sr_compile.anim_data)
2051 _write_array( 'mdl_keyframe', sizeof(mdl_transform),sr_compile.keyframe_data)
2052 _write_array( 'mdl_vert', sizeof(mdl_vert), sr_compile.vertex_data )
2053 _write_array( 'mdl_indice', sizeof(c_uint32), sr_compile.indice_data )
2054 _write_array( 'pack', 1, sr_compile.pack_data )
2055
2056 header_size = int_align_to( sizeof(mdl_header), 8 )
2057 index_size = int_align_to( sizeof(mdl_array)*len(file_array_instructions),8 )
2058
2059 folder = bpy.path.abspath(bpy.context.scene.SR_data.export_dir)
2060 path = F"{folder}{collection.name}.mdl"
2061 print( path )
2062
2063 os.makedirs(os.path.dirname(path),exist_ok=True)
2064 fp = open( path, "wb" )
2065 header = mdl_header()
2066 header.version = MDL_VERSION_NR
2067 sr_array_title( header.arrays, \
2068 'index', len(file_array_instructions), \
2069 sizeof(mdl_array), header_size )
2070
2071 fp.write( bytearray_align_to( bytearray(header), 8 ) )
2072
2073 print( F'[SR] {"name":>16}| count | offset' )
2074 index = bytearray()
2075 for name,info in file_array_instructions.items():#{
2076 arr = mdl_array()
2077 offset = info['offset'] + header_size + index_size
2078 sr_array_title( arr, name, info['count'], info['size'], offset )
2079 index.extend( bytearray(arr) )
2080
2081 print( F'[SR] {name:>16}| {info["count"]: 8} '+\
2082 F' 0x{info["offset"]:02x}' )
2083 #}
2084 fp.write( bytearray_align_to( index, 8 ) )
2085 #bytearray_print_hex( index )
2086
2087 for name,info in file_array_instructions.items():#{
2088 fp.write( bytearray_align_to( info['data'], 8 ) )
2089 #}
2090
2091 fp.close()
2092
2093 print( '[SR] done' )
2094 #}
2095
2096 class SR_SCENE_SETTINGS(bpy.types.PropertyGroup):
2097 #{
2098 use_hidden: bpy.props.BoolProperty( name="use hidden", default=False )
2099 export_dir: bpy.props.StringProperty( name="Export Dir", subtype='DIR_PATH' )
2100 gizmos: bpy.props.BoolProperty( name="Draw Gizmos", default=True )
2101
2102 panel: bpy.props.EnumProperty(
2103 name='Panel',
2104 description='',
2105 items=[
2106 ('EXPORT', 'Export', '', 'MOD_BUILD',0),
2107 ('ENTITY', 'Entity', '', 'MONKEY',1),
2108 ('SETTINGS', 'Settings', 'Settings', 'PREFERENCES',2),
2109 ],
2110 )
2111 #}
2112
2113 class SR_COLLECTION_SETTINGS(bpy.types.PropertyGroup):
2114 #{
2115 pack_textures: bpy.props.BoolProperty( name="Pack Textures", default=False )
2116 animations: bpy.props.BoolProperty( name="Export animation", default=True)
2117 #}
2118
2119 def sr_get_mirror_bone( bones ):
2120 #{
2121 side = bones.active.name[-1:]
2122 other_name = bones.active.name[:-1]
2123 if side == 'L': other_name += 'R'
2124 elif side == 'R': other_name += 'L'
2125 else: return None
2126
2127 for b in bones:#{
2128 if b.name == other_name:
2129 return b
2130 #}
2131
2132 return None
2133 #}
2134
2135 class SR_MIRROR_BONE_X(bpy.types.Operator):
2136 #{
2137 bl_idname="skaterift.mirror_bone"
2138 bl_label="Mirror bone attributes - SkateRift"
2139
2140 def execute(_,context):
2141 #{
2142 active_object = context.active_object
2143 bones = active_object.data.bones
2144 a = bones.active
2145 b = sr_get_mirror_bone( bones )
2146
2147 if not b: return {'FINISHED'}
2148
2149 b.SR_data.collider = a.SR_data.collider
2150
2151 def _v3copyflipy( a, b ):#{
2152 b[0] = a[0]
2153 b[1] = -a[1]
2154 b[2] = a[2]
2155 #}
2156
2157 _v3copyflipy( a.SR_data.collider_min, b.SR_data.collider_min )
2158 _v3copyflipy( a.SR_data.collider_max, b.SR_data.collider_max )
2159 b.SR_data.collider_min[1] = -a.SR_data.collider_max[1]
2160 b.SR_data.collider_max[1] = -a.SR_data.collider_min[1]
2161
2162 b.SR_data.cone_constraint = a.SR_data.cone_constraint
2163
2164 _v3copyflipy( a.SR_data.conevx, b.SR_data.conevy )
2165 _v3copyflipy( a.SR_data.conevy, b.SR_data.conevx )
2166 _v3copyflipy( a.SR_data.coneva, b.SR_data.coneva )
2167
2168 b.SR_data.conet = a.SR_data.conet
2169
2170 # redraw
2171 ob = bpy.context.scene.objects[0]
2172 ob.hide_render = ob.hide_render
2173 return {'FINISHED'}
2174 #}
2175 #}
2176
2177 class SR_COMPILE(bpy.types.Operator):
2178 #{
2179 bl_idname="skaterift.compile_all"
2180 bl_label="Compile All"
2181
2182 def execute(_,context):
2183 #{
2184 view_layer = bpy.context.view_layer
2185 for col in view_layer.layer_collection.children["export"].children:
2186 if not col.hide_viewport or bpy.context.scene.SR_data.use_hidden:
2187 sr_compile( bpy.data.collections[col.name] )
2188
2189 return {'FINISHED'}
2190 #}
2191 #}
2192
2193 class SR_COMPILE_THIS(bpy.types.Operator):
2194 #{
2195 bl_idname="skaterift.compile_this"
2196 bl_label="Compile This collection"
2197
2198 def execute(_,context):
2199 #{
2200 col = bpy.context.collection
2201 sr_compile( col )
2202
2203 return {'FINISHED'}
2204 #}
2205 #}
2206
2207 class SR_INTERFACE(bpy.types.Panel):
2208 #{
2209 bl_idname = "VIEW3D_PT_skate_rift"
2210 bl_label = "Skate Rift"
2211 bl_space_type = 'VIEW_3D'
2212 bl_region_type = 'UI'
2213 bl_category = "Skate Rift"
2214
2215 def draw(_, context):
2216 #{
2217 # Compiler section
2218
2219 row = _.layout.row()
2220 row.scale_y = 1.75
2221 row.prop( context.scene.SR_data, 'panel', expand=True )
2222
2223 if context.scene.SR_data.panel == 'SETTINGS': #{
2224 _.layout.prop( context.scene.SR_data, 'gizmos' )
2225 #}
2226 elif context.scene.SR_data.panel == 'EXPORT': #{
2227 _.layout.prop( context.scene.SR_data, "export_dir" )
2228 col = bpy.context.collection
2229
2230 found_in_export = False
2231 export_count = 0
2232 view_layer = bpy.context.view_layer
2233 for c1 in view_layer.layer_collection.children["export"].children: #{
2234 if not c1.hide_viewport or bpy.context.scene.SR_data.use_hidden:
2235 export_count += 1
2236
2237 if c1.name == col.name: #{
2238 found_in_export = True
2239 #}
2240 #}
2241
2242 box = _.layout.box()
2243 row = box.row()
2244 row.alignment = 'CENTER'
2245 row.scale_y = 1.5
2246
2247 if found_in_export: #{
2248 row.label( text=col.name + ".mdl" )
2249 box.prop( col.SR_data, "pack_textures" )
2250 box.prop( col.SR_data, "animations" )
2251 box.operator( "skaterift.compile_this" )
2252 #}
2253 else: #{
2254 row.enabled=False
2255 row.label( text=col.name )
2256
2257 row = box.row()
2258 row.enabled=False
2259 row.alignment = 'CENTER'
2260 row.scale_y = 1.5
2261 row.label( text="This collection is not in the export group" )
2262 #}
2263
2264 box = _.layout.box()
2265 row = box.row()
2266
2267 split = row.split( factor=0.3, align=True )
2268 split.prop( context.scene.SR_data, "use_hidden", text="hidden" )
2269
2270 row1 = split.row()
2271 if export_count == 0:
2272 row1.enabled=False
2273 row1.operator( "skaterift.compile_all", \
2274 text=F"Compile all ({export_count} collections)" )
2275 #}
2276 elif context.scene.SR_data.panel == 'ENTITY': #{
2277 active_object = context.active_object
2278 if not active_object: return
2279
2280 amount = max( 0, len(context.selected_objects)-1 )
2281
2282 row = _.layout.row()
2283 row.operator( 'skaterift.copy_entity_data', \
2284 text=F'Copy entity data to {amount} other objects' )
2285 if amount == 0: row.enabled=False
2286
2287 box = _.layout.box()
2288 row = box.row()
2289 row.alignment = 'CENTER'
2290 row.label( text=active_object.name )
2291 row.scale_y = 1.5
2292
2293 def _draw_prop_collection( source, data ): #{
2294 nonlocal box
2295 row = box.row()
2296 row.alignment = 'CENTER'
2297 row.enabled = False
2298 row.scale_y = 1.5
2299 row.label( text=F'{source}' )
2300
2301 if hasattr(type(data[0]),'sr_inspector'):#{
2302 type(data[0]).sr_inspector( box, data )
2303 #}
2304 else:#{
2305 for a in data[0].__annotations__:
2306 box.prop( data[0], a )
2307 #}
2308 #}
2309
2310 if active_object.type == 'ARMATURE': #{
2311 if active_object.mode == 'POSE': #{
2312 bones = active_object.data.bones
2313 mb = sr_get_mirror_bone( bones )
2314 if mb:#{
2315 box.operator( "skaterift.mirror_bone", \
2316 text=F'Mirror attributes to {mb.name}' )
2317 #}
2318
2319 _draw_prop_collection( \
2320 F'bpy.types.Bone["{bones.active.name}"].SR_data',\
2321 [bones.active.SR_data ] )
2322 #}
2323 else: #{
2324 row = box.row()
2325 row.alignment='CENTER'
2326 row.scale_y=2.0
2327 row.enabled=False
2328 row.label( text="Enter pose mode to modify bone properties" )
2329 #}
2330 #}
2331 elif active_object.type == 'LIGHT': #{
2332 _draw_prop_collection( \
2333 F'bpy.types.Light["{active_object.data.name}"].SR_data', \
2334 [active_object.data.SR_data] )
2335 #}
2336 elif active_object.type in ['EMPTY','CURVE','MESH']:#{
2337 box.prop( active_object.SR_data, "ent_type" )
2338 ent_type = active_object.SR_data.ent_type
2339
2340 col = getattr( active_object.SR_data, ent_type, None )
2341 if col != None and len(col)!=0:
2342 _draw_prop_collection( \
2343 F'bpy.types.Object["{active_object.name}"].SR_data.{ent_type}[0]', \
2344 col )
2345
2346 if active_object.type == 'MESH':#{
2347 col = getattr( active_object.data.SR_data, ent_type, None )
2348 if col != None and len(col)!=0:
2349 _draw_prop_collection( \
2350 F'bpy.types.Mesh["{active_object.data.name}"].SR_data.{ent_type}[0]', \
2351 col )
2352 #}
2353 #}
2354 #}
2355 #}
2356 #}
2357
2358 class SR_MATERIAL_PANEL(bpy.types.Panel):
2359 #{
2360 bl_label="Skate Rift material"
2361 bl_idname="MATERIAL_PT_sr_material"
2362 bl_space_type='PROPERTIES'
2363 bl_region_type='WINDOW'
2364 bl_context="material"
2365
2366 def draw(_,context):
2367 #{
2368 active_object = bpy.context.active_object
2369 if active_object == None: return
2370 active_mat = active_object.active_material
2371 if active_mat == None: return
2372
2373 info = material_info( active_mat )
2374
2375 if 'tex_diffuse' in info:#{
2376 _.layout.label( icon='INFO', \
2377 text=F"{info['tex_diffuse'].name} will be compiled" )
2378 #}
2379
2380 _.layout.prop( active_mat.SR_data, "shader" )
2381 _.layout.prop( active_mat.SR_data, "surface_prop" )
2382 _.layout.prop( active_mat.SR_data, "collision" )
2383
2384 if active_mat.SR_data.collision:#{
2385 box = _.layout.box()
2386 row = box.row()
2387
2388 if (active_mat.SR_data.shader != 'invisible') and \
2389 (active_mat.SR_data.shader != 'boundary'):#{
2390 row.prop( active_mat.SR_data, "skate_surface" )
2391 row.prop( active_mat.SR_data, "grind_surface" )
2392 row.prop( active_mat.SR_data, "grow_grass" )
2393 row.prop( active_mat.SR_data, "preview_visibile" )
2394 #}
2395 #}
2396
2397 if active_mat.SR_data.shader == "terrain_blend":#{
2398 box = _.layout.box()
2399 box.prop( active_mat.SR_data, "blend_offset" )
2400 box.prop( active_mat.SR_data, "sand_colour" )
2401 #}
2402 elif active_mat.SR_data.shader == "vertex_blend":#{
2403 box = _.layout.box()
2404 box.label( icon='INFO', text="Uses vertex colours, the R channel" )
2405 box.prop( active_mat.SR_data, "blend_offset" )
2406 #}
2407 elif active_mat.SR_data.shader == "water":#{
2408 box = _.layout.box()
2409 box.label( icon='INFO', text="Depth scale of 16 meters" )
2410 box.prop( active_mat.SR_data, "shore_colour" )
2411 box.prop( active_mat.SR_data, "ocean_colour" )
2412 #}
2413 #}
2414 #}
2415
2416 def sr_get_type_enum( scene, context ):
2417 #{
2418 items = [('none','None',"")]
2419 mesh_entities=['ent_gate','ent_water']
2420 point_entities=['ent_spawn','ent_route_node','ent_route']
2421
2422 for e in point_entities: items += [(e,e,'')]
2423
2424 if context.scene.SR_data.panel == 'ENTITY': #{
2425 if context.active_object.type == 'MESH': #{
2426 for e in mesh_entities: items += [(e,e,'')]
2427 #}
2428 #}
2429 else: #{
2430 for e in mesh_entities: items += [(e,e,'')]
2431 #}
2432
2433 return items
2434 #}
2435
2436 def sr_on_type_change( _, context ):
2437 #{
2438 obj = context.active_object
2439 ent_type = obj.SR_data.ent_type
2440 if ent_type == 'none': return
2441 if obj.type == 'MESH':#{
2442 col = getattr( obj.data.SR_data, ent_type, None )
2443 if col != None and len(col)==0: col.add()
2444 #}
2445
2446 col = getattr( obj.SR_data, ent_type, None )
2447 if col != None and len(col)==0: col.add()
2448 #}
2449
2450 class SR_OBJECT_ENT_SPAWN(bpy.types.PropertyGroup):
2451 #{
2452 alias: bpy.props.StringProperty( name='alias' )
2453 #}
2454
2455 class SR_OBJECT_ENT_GATE(bpy.types.PropertyGroup):
2456 #{
2457 target: bpy.props.PointerProperty( \
2458 type=bpy.types.Object, name="destination", \
2459 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_gate']))
2460
2461 key: bpy.props.StringProperty()
2462 tipo: bpy.props.EnumProperty(items=(('default', 'Default', ""),
2463 ('nonlocal', 'Non-Local', "")))
2464
2465 flip: bpy.props.BoolProperty( name="Flip exit", default=False )
2466 custom: bpy.props.BoolProperty( name="Mesh is surface", default=False )
2467 locked: bpy.props.BoolProperty( name="Start Locked", default=False )
2468
2469 @staticmethod
2470 def sr_inspector( layout, data ):
2471 #{
2472 box = layout.box()
2473 box.prop( data[0], 'tipo', text="subtype" )
2474
2475 if data[0].tipo == 'default': box.prop( data[0], 'target' )
2476 elif data[0].tipo == 'nonlocal': box.prop( data[0], 'key' )
2477
2478 flags = box.box()
2479 flags.prop( data[0], 'flip' )
2480 flags.prop( data[0], 'custom' )
2481 flags.prop( data[0], 'locked' )
2482 #}
2483 #}
2484
2485 class SR_MESH_ENT_GATE(bpy.types.PropertyGroup):
2486 #{
2487 dimensions: bpy.props.FloatVectorProperty(name="dimensions",size=3)
2488 #}
2489
2490 class SR_OBJECT_ENT_ROUTE_ENTRY(bpy.types.PropertyGroup):
2491 #{
2492 target: bpy.props.PointerProperty( \
2493 type=bpy.types.Object, name='target', \
2494 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_gate']))
2495 #}
2496
2497 class SR_UL_ROUTE_NODE_LIST(bpy.types.UIList):
2498 #{
2499 bl_idname = 'SR_UL_ROUTE_NODE_LIST'
2500
2501 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2502 #{
2503 layout.prop( item, 'target', text='', emboss=False )
2504 #}
2505 #}
2506
2507 def internal_listdel_execute(self,context,ent_name,collection_name):
2508 #{
2509 active_object = context.active_object
2510 data = getattr(active_object.SR_data,ent_name)[0]
2511 lista = getattr(data,collection_name)
2512 index = getattr(data,F'{collection_name}_index')
2513
2514 lista.remove(index)
2515
2516 setattr(data,F'{collection_name}_index', min(max(0,index-1), len(lista)-1))
2517 return{'FINISHED'}
2518 #}
2519
2520 def internal_listadd_execute(self,context,ent_name,collection_name):
2521 #{
2522 active_object = context.active_object
2523 getattr(getattr(active_object.SR_data,ent_name)[0],collection_name).add()
2524 return{'FINISHED'}
2525 #}
2526
2527 def copy_propgroup( de, to ):
2528 #{
2529 for a in de.__annotations__:#{
2530 if isinstance(getattr(de,a), bpy.types.bpy_prop_collection):#{
2531 ca = getattr(de,a)
2532 cb = getattr(to,a)
2533
2534 while len(cb) != len(ca):#{
2535 if len(cb) < len(ca): cb.add()
2536 else: cb.remove(0)
2537 #}
2538 for i in range(len(ca)):#{
2539 copy_propgroup(ca[i],cb[i])
2540 #}
2541 #}
2542 else:#{
2543 setattr(to,a,getattr(de,a))
2544 #}
2545 #}
2546 #}
2547
2548 class SR_OT_COPY_ENTITY_DATA(bpy.types.Operator):
2549 #{
2550 bl_idname = "skaterift.copy_entity_data"
2551 bl_label = "Copy entity data"
2552
2553 def execute(self, context):#{
2554 data = context.active_object.SR_data
2555 new_type = data.ent_type
2556 print( F"Copy entity data from: {context.active_object.name}" )
2557
2558 for obj in context.selected_objects:#{
2559 if obj != context.active_object:#{
2560 print( F" To: {obj.name}" )
2561
2562 obj.SR_data.ent_type = new_type
2563
2564 if active_object.type == 'MESH':#{
2565 col = getattr( obj.data.SR_data, new_type, None )
2566 if col != None and len(col)==0: col.add()
2567 mdata = context.active_object.data.SR_data
2568 copy_propgroup( getattr(mdata,new_type)[0], col[0] )
2569 #}
2570
2571 col = getattr( obj.SR_data, new_type, None )
2572 if col != None and len(col)==0: col.add()
2573 copy_propgroup( getattr(data,new_type)[0], col[0] )
2574 #}
2575 #}
2576 return{'FINISHED'}
2577 #}
2578 #}
2579
2580 class SR_OT_ROUTE_LIST_NEW_ITEM(bpy.types.Operator):
2581 #{
2582 bl_idname = "skaterift.new_entry"
2583 bl_label = "Add gate"
2584
2585 def execute(self, context):#{
2586 return internal_listadd_execute(self,context,'ent_route','gates')
2587 #}
2588 #}
2589
2590 class SR_OT_ROUTE_LIST_DEL_ITEM(bpy.types.Operator):
2591 #{
2592 bl_idname = "skaterift.del_entry"
2593 bl_label = "Remove gate"
2594
2595 @classmethod
2596 def poll(cls, context):#{
2597 active_object = context.active_object
2598 if obj_ent_type(active_object) == 'ent_route':#{
2599 return active_object.SR_data.ent_route[0].gates
2600 #}
2601 else: return False
2602 #}
2603
2604 def execute(self, context):#{
2605 return internal_listdel_execute(self,context,'ent_route','gates')
2606 #}
2607 #}
2608
2609 class SR_OT_AUDIO_LIST_NEW_ITEM(bpy.types.Operator):
2610 #{
2611 bl_idname = "skaterift.al_new_entry"
2612 bl_label = "Add file"
2613
2614 def execute(self, context):#{
2615 return internal_listadd_execute(self,context,'ent_audio','files')
2616 #}
2617 #}
2618
2619 class SR_OT_AUDIO_LIST_DEL_ITEM(bpy.types.Operator):
2620 #{
2621 bl_idname = "skaterift.al_del_entry"
2622 bl_label = "Remove file"
2623
2624 @classmethod
2625 def poll(cls, context):#{
2626 active_object = context.active_object
2627 if obj_ent_type(active_object) == 'ent_audio':#{
2628 return active_object.SR_data.ent_audio[0].files
2629 #}
2630 else: return False
2631 #}
2632
2633 def execute(self, context):#{
2634 return internal_listdel_execute(self,context,'ent_audio','files')
2635 return{'FINISHED'}
2636 #}
2637 #}
2638
2639 class SR_OT_GLYPH_LIST_NEW_ITEM(bpy.types.Operator):
2640 #{
2641 bl_idname = "skaterift.gl_new_entry"
2642 bl_label = "Add glyph"
2643
2644 def execute(self, context):#{
2645 active_object = context.active_object
2646
2647 font = active_object.SR_data.ent_font[0]
2648 font.glyphs.add()
2649
2650 if len(font.glyphs) > 1:#{
2651 prev = font.glyphs[-2]
2652 cur = font.glyphs[-1]
2653
2654 cur.bounds = prev.bounds
2655 cur.utf32 = prev.utf32+1
2656 #}
2657
2658 return{'FINISHED'}
2659 #}
2660 #}
2661
2662 class SR_OT_GLYPH_LIST_DEL_ITEM(bpy.types.Operator):
2663 #{
2664 bl_idname = "skaterift.gl_del_entry"
2665 bl_label = "Remove Glyph"
2666
2667 @classmethod
2668 def poll(cls, context):#{
2669 active_object = context.active_object
2670 if obj_ent_type(active_object) == 'ent_font':#{
2671 return active_object.SR_data.ent_font[0].glyphs
2672 #}
2673 else: return False
2674 #}
2675
2676 def execute(self, context):#{
2677 return internal_listdel_execute(self,context,'ent_font','glyphs')
2678 #}
2679 #}
2680
2681 class SR_OT_GLYPH_LIST_MOVE_ITEM(bpy.types.Operator):
2682 #{
2683 bl_idname = "skaterift.gl_move_item"
2684 bl_label = "aa"
2685 direction: bpy.props.EnumProperty(items=(('UP', 'Up', ""),
2686 ('DOWN', 'Down', ""),))
2687
2688 @classmethod
2689 def poll(cls, context):#{
2690 active_object = context.active_object
2691 if obj_ent_type(active_object) == 'ent_font':#{
2692 return active_object.SR_data.ent_font[0].glyphs
2693 #}
2694 else: return False
2695 #}
2696
2697 def execute(_, context):#{
2698 active_object = context.active_object
2699 data = active_object.SR_data.ent_font[0]
2700
2701 index = data.glyphs_index
2702 neighbor = index + (-1 if _.direction == 'UP' else 1)
2703 data.glyphs.move( neighbor, index )
2704
2705 list_length = len(data.glyphs) - 1
2706 new_index = index + (-1 if _.direction == 'UP' else 1)
2707
2708 data.glyphs_index = max(0, min(new_index, list_length))
2709
2710 return{'FINISHED'}
2711 #}
2712 #}
2713
2714 class SR_OT_FONT_VARIANT_LIST_NEW_ITEM(bpy.types.Operator):
2715 #{
2716 bl_idname = "skaterift.fv_new_entry"
2717 bl_label = "Add variant"
2718
2719 def execute(self, context):#{
2720 return internal_listadd_execute(self,context,'ent_font','variants')
2721 #}
2722 #}
2723
2724 class SR_OT_FONT_VARIANT_LIST_DEL_ITEM(bpy.types.Operator):
2725 #{
2726 bl_idname = "skaterift.fv_del_entry"
2727 bl_label = "Remove variant"
2728
2729 @classmethod
2730 def poll(cls, context):#{
2731 active_object = context.active_object
2732 if obj_ent_type(active_object) == 'ent_font':#{
2733 return active_object.SR_data.ent_font[0].variants
2734 #}
2735 else: return False
2736 #}
2737
2738 def execute(self, context):#{
2739 return internal_listdel_execute(self,context,'ent_font','variants')
2740 #}
2741 #}
2742
2743 class SR_OBJECT_ENT_AUDIO_FILE_ENTRY(bpy.types.PropertyGroup):
2744 #{
2745 path: bpy.props.StringProperty( name="Path" )
2746 probability: bpy.props.FloatProperty( name="Probability",default=100.0 )
2747 #}
2748
2749 class SR_UL_AUDIO_LIST(bpy.types.UIList):
2750 #{
2751 bl_idname = 'SR_UL_AUDIO_LIST'
2752
2753 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2754 #{
2755 split = layout.split(factor=0.7)
2756 c = split.column()
2757 c.prop( item, 'path', text='', emboss=False )
2758 c = split.column()
2759 c.prop( item, 'probability', text='%', emboss=True )
2760 #}
2761 #}
2762
2763 class SR_UL_FONT_VARIANT_LIST(bpy.types.UIList):
2764 #{
2765 bl_idname = 'SR_UL_FONT_VARIANT_LIST'
2766
2767 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2768 #{
2769 layout.prop( item, 'mesh', emboss=False )
2770 layout.prop( item, 'tipo' )
2771 #}
2772 #}
2773
2774 class SR_UL_FONT_GLYPH_LIST(bpy.types.UIList):
2775 #{
2776 bl_idname = 'SR_UL_FONT_GLYPH_LIST'
2777
2778 def draw_item(_,context,layout,data,item,icon,active_data,active_propname):
2779 #{
2780 s0 = layout.split(factor=0.3)
2781 c = s0.column()
2782 s1 = c.split(factor=0.3)
2783 c = s1.column()
2784 row = c.row()
2785 lbl = chr(item.utf32) if item.utf32 >= 32 and item.utf32 <= 126 else \
2786 f'x{item.utf32:x}'
2787 row.label(text=lbl)
2788 c = s1.column()
2789 c.prop( item, 'utf32', text='', emboss=True )
2790 c = s0.column()
2791 row = c.row()
2792 row.prop( item, 'bounds', text='', emboss=False )
2793 #}
2794 #}
2795
2796 class SR_OBJECT_ENT_ROUTE(bpy.types.PropertyGroup):
2797 #{
2798 gates: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_ROUTE_ENTRY)
2799 gates_index: bpy.props.IntProperty()
2800
2801 colour: bpy.props.FloatVectorProperty( \
2802 name="Colour",\
2803 subtype='COLOR',\
2804 min=0.0,max=1.0,\
2805 default=Vector((0.79,0.63,0.48)),\
2806 description="Route colour"\
2807 )
2808
2809 alias: bpy.props.StringProperty(\
2810 name="Alias",\
2811 default="Untitled Course")
2812
2813 @staticmethod
2814 def sr_inspector( layout, data ):
2815 #{
2816 layout.prop( data[0], 'alias' )
2817 layout.prop( data[0], 'colour' )
2818
2819 layout.label( text='Checkpoints' )
2820 layout.template_list('SR_UL_ROUTE_NODE_LIST', 'Checkpoints', \
2821 data[0], 'gates', data[0], 'gates_index', rows=5)
2822
2823 row = layout.row()
2824 row.operator( 'skaterift.new_entry', text='Add' )
2825 row.operator( 'skaterift.del_entry', text='Remove' )
2826 #}
2827 #}
2828
2829 class SR_OBJECT_ENT_VOLUME(bpy.types.PropertyGroup):#{
2830 subtype: bpy.props.EnumProperty(
2831 name="Subtype",
2832 items=[('0','Trigger',''),
2833 ('1','Particles (0.1s)','')]
2834 )
2835
2836 target: bpy.props.PointerProperty( \
2837 type=bpy.types.Object, name="Target", \
2838 poll=lambda self,obj: sr_filter_ent_type(obj,\
2839 ['ent_audio','ent_skateshop','ent_ccmd',\
2840 'ent_challenge']))
2841 target_event: bpy.props.IntProperty( name="Event/Method" )
2842
2843 @staticmethod
2844 def inspect_target( layout, data, propname ):#{
2845 box = layout.box()
2846 box.prop( data[0], propname )
2847
2848 row = box.row()
2849 row.prop( data[0], propname + '_event')
2850
2851 target = getattr( data[0], propname )
2852 if target:#{
2853 tipo = target.SR_data.ent_type
2854 cls = globals()[ tipo ]
2855
2856 table = getattr( cls, 'sr_functions', None )
2857 if table:#{
2858 index = getattr( data[0], propname+'_event')
2859 if index in table:
2860 row.label( text=table[index] )
2861 else:
2862 row.label( text="undefined function" )
2863 #}
2864 #}
2865 else:#{
2866 row.label( text="..." )
2867 row.enabled=False
2868 #}
2869 #}
2870
2871 @staticmethod
2872 def sr_inspector( layout, data ):#{
2873 layout.prop( data[0], 'subtype' )
2874 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target' )
2875 #}
2876 #}
2877
2878 class SR_OBJECT_ENT_AUDIO(bpy.types.PropertyGroup):
2879 #{
2880 files: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_AUDIO_FILE_ENTRY)
2881 files_index: bpy.props.IntProperty()
2882
2883 flag_3d: bpy.props.BoolProperty( name="3D audio",default=True )
2884 flag_loop: bpy.props.BoolProperty( name="Loop",default=False )
2885 flag_auto: bpy.props.BoolProperty( name="Play at start",default=False )
2886 flag_nodoppler: bpy.props.BoolProperty( name="No Doppler",default=False )
2887
2888 group: bpy.props.IntProperty( name="Group ID", default=0 )
2889 formato: bpy.props.EnumProperty(
2890 name="Format",
2891 items=[('0','Uncompressed Mono',''),
2892 ('1','Compressed Vorbis',''),
2893 ('2','[vg] Bird Synthesis','')]
2894 )
2895 probability_curve: bpy.props.EnumProperty(
2896 name="Probability Curve",
2897 items=[('0','Constant',''),
2898 ('1','Wildlife Daytime',''),
2899 ('2','Wildlife Nighttime','')])
2900 channel_behaviour: bpy.props.EnumProperty(
2901 name="Channel Behaviour",
2902 items=[('0','Unlimited',''),
2903 ('1','Discard if group full', ''),
2904 ('2','Crossfade if group full','')])
2905
2906 transition_duration: bpy.props.FloatProperty(name="Transition Time",\
2907 default=0.2)
2908
2909 max_channels: bpy.props.IntProperty( name="Max Channels", default=1 )
2910 volume: bpy.props.FloatProperty( name="Volume",default=1.0 )
2911
2912 @staticmethod
2913 def sr_inspector( layout, data ):
2914 #{
2915 layout.prop( data[0], 'formato' )
2916 layout.prop( data[0], 'volume' )
2917
2918 box = layout.box()
2919 box.label( text='Channels' )
2920 split = box.split(factor=0.3)
2921 c = split.column()
2922 c.prop( data[0], 'max_channels' )
2923 c = split.column()
2924 c.prop( data[0], 'channel_behaviour', text='Behaviour' )
2925 if data[0].channel_behaviour >= '1':
2926 box.prop( data[0], 'group' )
2927 if data[0].channel_behaviour == '2':
2928 box.prop( data[0], 'transition_duration' )
2929
2930 box = layout.box()
2931 box.label( text='Flags' )
2932 box.prop( data[0], 'flag_3d' )
2933 if data[0].flag_3d: box.prop( data[0], 'flag_nodoppler' )
2934
2935 box.prop( data[0], 'flag_loop' )
2936 box.prop( data[0], 'flag_auto' )
2937
2938 layout.prop( data[0], 'probability_curve' )
2939
2940 split = layout.split(factor=0.7)
2941 c = split.column()
2942 c.label( text='Filepath' )
2943 c = split.column()
2944 c.label( text='Chance' )
2945 layout.template_list('SR_UL_AUDIO_LIST', 'Files', \
2946 data[0], 'files', data[0], 'files_index', rows=5)
2947
2948 row = layout.row()
2949 row.operator( 'skaterift.al_new_entry', text='Add' )
2950 row.operator( 'skaterift.al_del_entry', text='Remove' )
2951 #}
2952 #}
2953
2954 class SR_OBJECT_ENT_MARKER(bpy.types.PropertyGroup):
2955 #{
2956 alias: bpy.props.StringProperty()
2957 #}
2958
2959 class SR_OBJECT_ENT_GLYPH(bpy.types.PropertyGroup):
2960 #{
2961 mini: bpy.props.FloatVectorProperty(size=2)
2962 maxi: bpy.props.FloatVectorProperty(size=2)
2963 utf32: bpy.props.IntProperty()
2964 #}
2965
2966 class SR_OBJECT_ENT_GLYPH_ENTRY(bpy.types.PropertyGroup):
2967 #{
2968 bounds: bpy.props.FloatVectorProperty(size=4,subtype='NONE')
2969 utf32: bpy.props.IntProperty()
2970 #}
2971
2972 class SR_OBJECT_ENT_FONT_VARIANT(bpy.types.PropertyGroup):
2973 #{
2974 mesh: bpy.props.PointerProperty(type=bpy.types.Object)
2975 tipo: bpy.props.StringProperty()
2976 #}
2977
2978 class SR_OBJECT_ENT_FONT(bpy.types.PropertyGroup):
2979 #{
2980 variants: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_FONT_VARIANT)
2981 glyphs: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GLYPH_ENTRY)
2982 alias: bpy.props.StringProperty()
2983
2984 glyphs_index: bpy.props.IntProperty()
2985 variants_index: bpy.props.IntProperty()
2986
2987 @staticmethod
2988 def sr_inspector( layout, data ):
2989 #{
2990 layout.prop( data[0], 'alias' )
2991
2992 layout.label( text='Variants' )
2993 layout.template_list('SR_UL_FONT_VARIANT_LIST', 'Variants', \
2994 data[0], 'variants', data[0], 'variants_index',\
2995 rows=5 )
2996 row = layout.row()
2997 row.operator( 'skaterift.fv_new_entry', text='Add' )
2998 row.operator( 'skaterift.fv_del_entry', text='Remove' )
2999
3000 layout.label( text='ASCII Glyphs' )
3001 layout.template_list('SR_UL_FONT_GLYPH_LIST', 'Glyphs', \
3002 data[0], 'glyphs', data[0], 'glyphs_index', rows=5)
3003
3004 row = layout.row()
3005 row.operator( 'skaterift.gl_new_entry', text='Add' )
3006 row.operator( 'skaterift.gl_del_entry', text='Remove' )
3007 row.operator( 'skaterift.gl_move_item', text='^' ).direction='UP'
3008 row.operator( 'skaterift.gl_move_item', text='v' ).direction='DOWN'
3009 #}
3010 #}
3011
3012 class SR_OBJECT_ENT_TRAFFIC(bpy.types.PropertyGroup):
3013 #{
3014 speed: bpy.props.FloatProperty(default=1.0)
3015 #}
3016
3017 class SR_OBJECT_ENT_SKATESHOP(bpy.types.PropertyGroup):
3018 #{
3019 tipo: bpy.props.EnumProperty( name='Type',
3020 items=[('0','boards',''),
3021 ('1','character',''),
3022 ('2','world','')] )
3023 mark_rack: bpy.props.PointerProperty( \
3024 type=bpy.types.Object, name="Board Rack", \
3025 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3026 mark_display: bpy.props.PointerProperty( \
3027 type=bpy.types.Object, name="Selected Board Display", \
3028 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3029 mark_info: bpy.props.PointerProperty( \
3030 type=bpy.types.Object, name="Selected Board Info", \
3031 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3032 cam: bpy.props.PointerProperty( \
3033 type=bpy.types.Object, name="Viewpoint", \
3034 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3035 #}
3036
3037 class SR_OBJECT_ENT_WORKSHOP_PREVIEW(bpy.types.PropertyGroup):
3038 #{
3039 mark_display: bpy.props.PointerProperty( \
3040 type=bpy.types.Object, name="Board Display", \
3041 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3042 mark_display1: bpy.props.PointerProperty( \
3043 type=bpy.types.Object, name="Board Display (other side)", \
3044 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3045 cam: bpy.props.PointerProperty( \
3046 type=bpy.types.Object, name="Viewpoint", \
3047 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3048 #}
3049
3050 class SR_OBJECT_ENT_MENU_ITEM(bpy.types.PropertyGroup):
3051 #{
3052 link0: bpy.props.PointerProperty( \
3053 type=bpy.types.Object, name="Link 0", \
3054 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3055 link1: bpy.props.PointerProperty( \
3056 type=bpy.types.Object, name="Link 1", \
3057 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3058 link2: bpy.props.PointerProperty( \
3059 type=bpy.types.Object, name="Link 2", \
3060 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3061 link3: bpy.props.PointerProperty( \
3062 type=bpy.types.Object, name="Link 3", \
3063 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3064
3065 newloc: bpy.props.PointerProperty( \
3066 type=bpy.types.Object, name="New location", \
3067 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3068 camera: bpy.props.PointerProperty( \
3069 type=bpy.types.Object, name="Camera", \
3070 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_camera']))
3071
3072 slider_minloc: bpy.props.PointerProperty( \
3073 type=bpy.types.Object, name="Slider min", \
3074 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3075 slider_maxloc: bpy.props.PointerProperty( \
3076 type=bpy.types.Object, name="Slider max", \
3077 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_marker']))
3078 slider_handle: bpy.props.PointerProperty( \
3079 type=bpy.types.Object, name="Slider handle", \
3080 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3081
3082 checkmark: bpy.props.PointerProperty( \
3083 type=bpy.types.Object, name="Checked", \
3084 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_menuitem']))
3085
3086 string: bpy.props.StringProperty( name="String" )
3087 tipo: bpy.props.EnumProperty( name='Type',
3088 items=[('0','visual',''),
3089 ('1','event button',''),
3090 ('2','page button',''),
3091 ('3','toggle', ''),
3092 ('4','slider',''),
3093 ('5','page','')])
3094
3095 @staticmethod
3096 def sr_inspector( layout, data ):
3097 #{
3098 data = data[0]
3099 box = layout.box()
3100 box.prop( data, 'tipo' )
3101
3102 if data.tipo == '0':#{
3103 return
3104 #}
3105 elif data.tipo == '1':#{
3106 box.prop( data, 'string', text='Event' )
3107 #}
3108 elif data.tipo == '2':#{
3109 box.prop( data, 'string', text='Page' )
3110 box.prop( data, 'newloc' )
3111 #}
3112 elif data.tipo == '3':#{
3113 box.prop( data, 'string', text='Data (i32)' )
3114 box.prop( data, 'checkmark' )
3115 #}
3116 elif data.tipo == '4':#{
3117 box.prop( data, 'string', text='Data (f32)' )
3118 box.prop( data, 'slider_minloc' )
3119 box.prop( data, 'slider_maxloc' )
3120 box.prop( data, 'slider_handle' )
3121 box = box.box()
3122 box.label( text="Links" )
3123 box.prop( data, 'link0', text='v0' )
3124 box.prop( data, 'link1', text='v1' )
3125 return
3126 #}
3127 elif data.tipo == '5':#{
3128 box.prop( data, 'string', text='Page Name' )
3129 box.prop( data, 'newloc', text='Entry Point' )
3130 box.prop( data, 'camera', text='Viewpoint' )
3131 return
3132 #}
3133
3134 box = box.box()
3135 box.label( text="Links" )
3136 box.prop( data, 'link0' )
3137 box.prop( data, 'link1' )
3138 box.prop( data, 'link2' )
3139 box.prop( data, 'link3' )
3140 #}
3141 #}
3142
3143 class SR_OBJECT_ENT_WORLD_INFO(bpy.types.PropertyGroup):
3144 #{
3145 name: bpy.props.StringProperty(name="Name")
3146 desc: bpy.props.StringProperty(name="Description")
3147 author: bpy.props.StringProperty(name="Author")
3148 timezone: bpy.props.FloatProperty(name="Timezone(hrs) (UTC0 +hrs)")
3149 #}
3150
3151 class SR_OBJECT_ENT_CCMD(bpy.types.PropertyGroup):
3152 #{
3153 command: bpy.props.StringProperty(name="Command Line")
3154 #}
3155
3156 class SR_OBJECT_ENT_CHALLENGE(bpy.types.PropertyGroup):#{
3157 proxima: bpy.props.PointerProperty( \
3158 type=bpy.types.Object, name="Next", \
3159 poll=lambda self,obj: sr_filter_ent_type(obj,['ent_challenge']))
3160 target: bpy.props.PointerProperty( \
3161 type=bpy.types.Object, name="Win", \
3162 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3163 target_event: bpy.props.IntProperty( name="Event/Method" )
3164 time_limit: bpy.props.FloatProperty( name="Time Limit", default=1.0 )
3165
3166 @staticmethod
3167 def sr_inspector( layout, data ):#{
3168 layout.prop( data[0], 'proxima' )
3169 layout.prop( data[0], 'time_limit' )
3170 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target' )
3171 #}
3172 #}
3173
3174 class SR_OBJECT_ENT_UNLOCK(bpy.types.PropertyGroup):#{
3175 alias: bpy.props.StringProperty( name="Alias" )
3176 target: bpy.props.PointerProperty( \
3177 type=bpy.types.Object, name="Target", \
3178 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3179 target_event: bpy.props.IntProperty( name="Event/Method" )
3180
3181 @staticmethod
3182 def sr_inspector( layout, data ):#{
3183 layout.prop( data[0], 'alias' )
3184 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target' )
3185 #}
3186 #}
3187
3188 class SR_OBJECT_ENT_RELAY(bpy.types.PropertyGroup):#{
3189 target0: bpy.props.PointerProperty( \
3190 type=bpy.types.Object, name="Target 0", \
3191 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3192 target1: bpy.props.PointerProperty( \
3193 type=bpy.types.Object, name="Target 1", \
3194 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3195 target2: bpy.props.PointerProperty( \
3196 type=bpy.types.Object, name="Target 2", \
3197 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3198 target3: bpy.props.PointerProperty( \
3199 type=bpy.types.Object, name="Target 3", \
3200 poll=lambda self,obj: sr_filter_ent_type(obj,SR_TRIGGERABLE))
3201
3202 target0_event: bpy.props.IntProperty( name="Event" )
3203 target1_event: bpy.props.IntProperty( name="Event" )
3204 target2_event: bpy.props.IntProperty( name="Event" )
3205 target3_event: bpy.props.IntProperty( name="Event" )
3206
3207 @staticmethod
3208 def sr_inspector( layout, data ):#{
3209 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target0' )
3210 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target1' )
3211 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target2' )
3212 SR_OBJECT_ENT_VOLUME.inspect_target( layout, data, 'target3' )
3213 #}
3214 #}
3215
3216 class SR_OBJECT_PROPERTIES(bpy.types.PropertyGroup):
3217 #{
3218 ent_gate: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GATE)
3219 ent_spawn: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_SPAWN)
3220 ent_route: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_ROUTE)
3221 ent_volume: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_VOLUME)
3222 ent_audio: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_AUDIO)
3223 ent_marker: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MARKER)
3224 ent_glyph: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_GLYPH)
3225 ent_font: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_FONT)
3226 ent_traffic: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_TRAFFIC)
3227 ent_skateshop: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_SKATESHOP)
3228 ent_swspreview: \
3229 bpy.props.CollectionProperty(type=SR_OBJECT_ENT_WORKSHOP_PREVIEW)
3230 ent_menuitem: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_MENU_ITEM)
3231 ent_worldinfo: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_WORLD_INFO)
3232 ent_ccmd: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_CCMD)
3233 ent_challenge: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_CHALLENGE)
3234 ent_unlock: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_UNLOCK)
3235 ent_relay: bpy.props.CollectionProperty(type=SR_OBJECT_ENT_RELAY)
3236
3237 ent_type: bpy.props.EnumProperty(
3238 name="Type",
3239 items=sr_entity_list,
3240 update=sr_on_type_change
3241 )
3242 #}
3243
3244 class SR_MESH_PROPERTIES(bpy.types.PropertyGroup):
3245 #{
3246 ent_gate: bpy.props.CollectionProperty(type=SR_MESH_ENT_GATE)
3247 #}
3248
3249 class SR_LIGHT_PROPERTIES(bpy.types.PropertyGroup):
3250 #{
3251 daytime: bpy.props.BoolProperty( name='Daytime' )
3252 #}
3253
3254 class SR_BONE_PROPERTIES(bpy.types.PropertyGroup):
3255 #{
3256 collider: bpy.props.EnumProperty( name='Collider Type',
3257 items=[('0','none',''),
3258 ('1','box',''),
3259 ('2','capsule','')])
3260
3261 collider_min: bpy.props.FloatVectorProperty( name='Collider Min', size=3 )
3262 collider_max: bpy.props.FloatVectorProperty( name='Collider Max', size=3 )
3263
3264 cone_constraint: bpy.props.BoolProperty( name='Cone constraint' )
3265
3266 conevx: bpy.props.FloatVectorProperty( name='vx' )
3267 conevy: bpy.props.FloatVectorProperty( name='vy' )
3268 coneva: bpy.props.FloatVectorProperty( name='va' )
3269 conet: bpy.props.FloatProperty( name='t' )
3270
3271 @staticmethod
3272 def sr_inspector( layout, data ):
3273 #{
3274 data = data[0]
3275 box = layout.box()
3276 box.prop( data, 'collider' )
3277
3278 if int(data.collider)>0:#{
3279 row = box.row()
3280 row.prop( data, 'collider_min' )
3281 row = box.row()
3282 row.prop( data, 'collider_max' )
3283 #}
3284
3285 box = layout.box()
3286 box.prop( data, 'cone_constraint' )
3287 if data.cone_constraint:#{
3288 row = box.row()
3289 row.prop( data, 'conevx' )
3290 row = box.row()
3291 row.prop( data, 'conevy' )
3292 row = box.row()
3293 row.prop( data, 'coneva' )
3294 box.prop( data, 'conet' )
3295 #}
3296 #}
3297 #}
3298
3299 class SR_MATERIAL_PROPERTIES(bpy.types.PropertyGroup):
3300 #{
3301 shader: bpy.props.EnumProperty(
3302 name="Format",
3303 items = [
3304 ('standard',"standard",''),
3305 ('standard_cutout', "standard_cutout", ''),
3306 ('terrain_blend', "terrain_blend", ''),
3307 ('vertex_blend', "vertex_blend", ''),
3308 ('water',"water",''),
3309 ('invisible','Invisible',''),
3310 ('boundary','Boundary',''),
3311 ('fxglow','FX Glow',''),
3312 ])
3313
3314 surface_prop: bpy.props.EnumProperty(
3315 name="Surface Property",
3316 items = [
3317 ('0','concrete',''),
3318 ('1','wood',''),
3319 ('2','grass',''),
3320 ('3','tiles',''),
3321 ('4','metal','')
3322 ])
3323
3324 collision: bpy.props.BoolProperty( \
3325 name="Collisions Enabled",\
3326 default=True,\
3327 description = "Can the player collide with this material?"\
3328 )
3329 skate_surface: bpy.props.BoolProperty( \
3330 name="Skate Target", \
3331 default=True,\
3332 description = "Should the game try to target this surface?" \
3333 )
3334 grind_surface: bpy.props.BoolProperty( \
3335 name="Grindable", \
3336 default=True,\
3337 description = "Can you grind on this surface?" \
3338 )
3339 grow_grass: bpy.props.BoolProperty( \
3340 name="Grow Grass", \
3341 default=False,\
3342 description = "Spawn grass sprites on this surface?" \
3343 )
3344 preview_visibile: bpy.props.BoolProperty( \
3345 name="Preview visibile", \
3346 default=True,\
3347 description = "Show this material in preview models?" \
3348 )
3349 blend_offset: bpy.props.FloatVectorProperty( \
3350 name="Blend Offset", \
3351 size=2, \
3352 default=Vector((0.5,0.0)),\
3353 description="When surface is more than 45 degrees, add this vector " +\
3354 "to the UVs" \
3355 )
3356 sand_colour: bpy.props.FloatVectorProperty( \
3357 name="Sand Colour",\
3358 subtype='COLOR',\
3359 min=0.0,max=1.0,\
3360 default=Vector((0.79,0.63,0.48)),\
3361 description="Blend to this colour near the 0 coordinate on UP axis"\
3362 )
3363 shore_colour: bpy.props.FloatVectorProperty( \
3364 name="Shore Colour",\
3365 subtype='COLOR',\
3366 min=0.0,max=1.0,\
3367 default=Vector((0.03,0.32,0.61)),\
3368 description="Water colour at the shoreline"\
3369 )
3370 ocean_colour: bpy.props.FloatVectorProperty( \
3371 name="Ocean Colour",\
3372 subtype='COLOR',\
3373 min=0.0,max=1.0,\
3374 default=Vector((0.0,0.006,0.03)),\
3375 description="Water colour in the deep bits"\
3376 )
3377 #}
3378
3379 # ---------------------------------------------------------------------------- #
3380 # #
3381 # GUI section #
3382 # #
3383 # ---------------------------------------------------------------------------- #
3384
3385 cv_view_draw_handler = None
3386 cv_view_pixel_handler = None
3387 cv_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
3388 cv_view_verts = []
3389 cv_view_colours = []
3390 cv_view_course_i = 0
3391
3392 # Draw axis alligned sphere at position with radius
3393 #
3394 def cv_draw_sphere( pos, radius, colour ):
3395 #{
3396 global cv_view_verts, cv_view_colours
3397
3398 ly = pos + Vector((0,0,radius))
3399 lx = pos + Vector((0,radius,0))
3400 lz = pos + Vector((0,0,radius))
3401
3402 pi = 3.14159265358979323846264
3403
3404 for i in range(16):#{
3405 t = ((i+1.0) * 1.0/16.0) * pi * 2.0
3406 s = math.sin(t)
3407 c = math.cos(t)
3408
3409 py = pos + Vector((s*radius,0.0,c*radius))
3410 px = pos + Vector((s*radius,c*radius,0.0))
3411 pz = pos + Vector((0.0,s*radius,c*radius))
3412
3413 cv_view_verts += [ px, lx ]
3414 cv_view_verts += [ py, ly ]
3415 cv_view_verts += [ pz, lz ]
3416
3417 cv_view_colours += [ colour, colour, colour, colour, colour, colour ]
3418
3419 ly = py
3420 lx = px
3421 lz = pz
3422 #}
3423 cv_draw_lines()
3424 #}
3425
3426 # Draw axis alligned sphere at position with radius
3427 #
3428 def cv_draw_halfsphere( pos, tx, ty, tz, radius, colour ):
3429 #{
3430 global cv_view_verts, cv_view_colours
3431
3432 ly = pos + tz*radius
3433 lx = pos + ty*radius
3434 lz = pos + tz*radius
3435
3436 pi = 3.14159265358979323846264
3437
3438 for i in range(16):#{
3439 t = ((i+1.0) * 1.0/16.0) * pi
3440 s = math.sin(t)
3441 c = math.cos(t)
3442
3443 s1 = math.sin(t*2.0)
3444 c1 = math.cos(t*2.0)
3445
3446 py = pos + s*tx*radius + c *tz*radius
3447 px = pos + s*tx*radius + c *ty*radius
3448 pz = pos + s1*ty*radius + c1*tz*radius
3449
3450 cv_view_verts += [ px, lx ]
3451 cv_view_verts += [ py, ly ]
3452 cv_view_verts += [ pz, lz ]
3453
3454 cv_view_colours += [ colour, colour, colour, colour, colour, colour ]
3455
3456 ly = py
3457 lx = px
3458 lz = pz
3459 #}
3460 cv_draw_lines()
3461 #}
3462
3463 # Draw transformed -1 -> 1 cube
3464 #
3465 def cv_draw_ucube( transform, colour, s=Vector((1,1,1)), o=Vector((0,0,0)) ):
3466 #{
3467 global cv_view_verts, cv_view_colours
3468
3469 a = o + -1.0 * s
3470 b = o + 1.0 * s
3471
3472 vs = [None]*8
3473 vs[0] = transform @ Vector((a[0], a[1], a[2]))
3474 vs[1] = transform @ Vector((a[0], b[1], a[2]))
3475 vs[2] = transform @ Vector((b[0], b[1], a[2]))
3476 vs[3] = transform @ Vector((b[0], a[1], a[2]))
3477 vs[4] = transform @ Vector((a[0], a[1], b[2]))
3478 vs[5] = transform @ Vector((a[0], b[1], b[2]))
3479 vs[6] = transform @ Vector((b[0], b[1], b[2]))
3480 vs[7] = transform @ Vector((b[0], a[1], b[2]))
3481
3482 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
3483 (0,4),(1,5),(2,6),(3,7)]
3484
3485 for l in indices:#{
3486 v0 = vs[l[0]]
3487 v1 = vs[l[1]]
3488 cv_view_verts += [(v0[0],v0[1],v0[2])]
3489 cv_view_verts += [(v1[0],v1[1],v1[2])]
3490 cv_view_colours += [colour, colour]
3491 #}
3492 cv_draw_lines()
3493 #}
3494
3495 # Draw line with colour
3496 #
3497 def cv_draw_line( p0, p1, colour ):
3498 #{
3499 global cv_view_verts, cv_view_colours
3500
3501 cv_view_verts += [p0,p1]
3502 cv_view_colours += [colour, colour]
3503 cv_draw_lines()
3504 #}
3505
3506 # Draw line with colour(s)
3507 #
3508 def cv_draw_line2( p0, p1, c0, c1 ):
3509 #{
3510 global cv_view_verts, cv_view_colours
3511
3512 cv_view_verts += [p0,p1]
3513 cv_view_colours += [c0,c1]
3514 cv_draw_lines()
3515 #}
3516
3517 #
3518 #
3519 def cv_tangent_basis( n, tx, ty ):
3520 #{
3521 if abs( n[0] ) >= 0.57735027:#{
3522 tx[0] = n[1]
3523 tx[1] = -n[0]
3524 tx[2] = 0.0
3525 #}
3526 else:#{
3527 tx[0] = 0.0
3528 tx[1] = n[2]
3529 tx[2] = -n[1]
3530 #}
3531
3532 tx.normalize()
3533 _ty = n.cross( tx )
3534
3535 ty[0] = _ty[0]
3536 ty[1] = _ty[1]
3537 ty[2] = _ty[2]
3538 #}
3539
3540 # Draw coloured arrow
3541 #
3542 def cv_draw_arrow( p0, p1, c0, size=0.25 ):
3543 #{
3544 global cv_view_verts, cv_view_colours
3545
3546 n = p1-p0
3547 midpt = p0 + n*0.5
3548 n.normalize()
3549
3550 tx = Vector((1,0,0))
3551 ty = Vector((1,0,0))
3552 cv_tangent_basis( n, tx, ty )
3553
3554 cv_view_verts += [p0,p1, midpt+(tx-n)*size,midpt, midpt+(-tx-n)*size,midpt ]
3555 cv_view_colours += [c0,c0,c0,c0,c0,c0]
3556 #cv_draw_lines()
3557 #}
3558
3559 def cv_draw_line_dotted( p0, p1, c0, dots=10 ):
3560 #{
3561 global cv_view_verts, cv_view_colours
3562
3563 for i in range(dots):#{
3564 t0 = i/dots
3565 t1 = (i+0.25)/dots
3566
3567 p2 = p0*(1.0-t0)+p1*t0
3568 p3 = p0*(1.0-t1)+p1*t1
3569
3570 cv_view_verts += [p2,p3]
3571 cv_view_colours += [c0,c0]
3572 #}
3573 #cv_draw_lines()
3574 #}
3575
3576 # Drawhandles of a bezier control point
3577 #
3578 def cv_draw_bhandle( obj, direction, colour ):
3579 #{
3580 global cv_view_verts, cv_view_colours
3581
3582 p0 = obj.location
3583 h0 = obj.matrix_world @ Vector((0,direction,0))
3584
3585 cv_view_verts += [p0]
3586 cv_view_verts += [h0]
3587 cv_view_colours += [colour,colour]
3588 cv_draw_lines()
3589 #}
3590
3591 # Draw a bezier curve (at fixed resolution 10)
3592 #
3593 def cv_draw_bezier( p0,h0,p1,h1,c0,c1 ):
3594 #{
3595 global cv_view_verts, cv_view_colours
3596
3597 last = p0
3598 for i in range(10):#{
3599 t = (i+1)/10
3600 a0 = 1-t
3601
3602 tt = t*t
3603 ttt = tt*t
3604 p=ttt*p1+(3*tt-3*ttt)*h1+(3*ttt-6*tt+3*t)*h0+(3*tt-ttt-3*t+1)*p0
3605
3606 cv_view_verts += [(last[0],last[1],last[2])]
3607 cv_view_verts += [(p[0],p[1],p[2])]
3608 cv_view_colours += [c0*a0+c1*(1-a0),c0*a0+c1*(1-a0)]
3609
3610 last = p
3611 #}
3612 cv_draw_lines()
3613 #}
3614
3615 # I think this one extends the handles of the bezier otwards......
3616 #
3617 def cv_draw_sbpath( o0,o1,c0,c1,s0,s1 ):
3618 #{
3619 global cv_view_course_i
3620
3621 offs = ((cv_view_course_i % 2)*2-1) * cv_view_course_i * 0.02
3622
3623 p0 = o0.matrix_world @ Vector((offs, 0,0))
3624 h0 = o0.matrix_world @ Vector((offs, s0,0))
3625 p1 = o1.matrix_world @ Vector((offs, 0,0))
3626 h1 = o1.matrix_world @ Vector((offs,-s1,0))
3627
3628 cv_draw_bezier( p0,h0,p1,h1,c0,c1 )
3629 cv_draw_lines()
3630 #}
3631
3632 # Flush the lines buffers. This is called often because god help you if you want
3633 # to do fixed, fast buffers in this catastrophic programming language.
3634 #
3635 def cv_draw_lines():
3636 #{
3637 global cv_view_shader, cv_view_verts, cv_view_colours
3638
3639 if len(cv_view_verts) < 2:
3640 return
3641
3642 lines = batch_for_shader(\
3643 cv_view_shader, 'LINES', \
3644 { "pos":cv_view_verts, "color":cv_view_colours })
3645
3646 if bpy.context.scene.SR_data.gizmos:
3647 lines.draw( cv_view_shader )
3648
3649 cv_view_verts = []
3650 cv_view_colours = []
3651 #}
3652
3653 # I dont remember what this does exactly
3654 #
3655 def cv_draw_bpath( o0,o1,c0,c1 ):
3656 #{
3657 cv_draw_sbpath( o0,o1,c0,c1,1.0,1.0 )
3658 #}
3659
3660 # Semi circle to show the limit. and some lines
3661 #
3662 def draw_limit( obj, center, major, minor, amin, amax, colour ):
3663 #{
3664 global cv_view_verts, cv_view_colours
3665 f = 0.05
3666 ay = major*f
3667 ax = minor*f
3668
3669 for x in range(16):#{
3670 t0 = x/16
3671 t1 = (x+1)/16
3672 a0 = amin*(1.0-t0)+amax*t0
3673 a1 = amin*(1.0-t1)+amax*t1
3674
3675 p0 = center + major*f*math.cos(a0) + minor*f*math.sin(a0)
3676 p1 = center + major*f*math.cos(a1) + minor*f*math.sin(a1)
3677
3678 p0=obj.matrix_world @ p0
3679 p1=obj.matrix_world @ p1
3680 cv_view_verts += [p0,p1]
3681 cv_view_colours += [colour,colour]
3682
3683 if x == 0:#{
3684 cv_view_verts += [p0,center]
3685 cv_view_colours += [colour,colour]
3686 #}
3687 if x == 15:#{
3688 cv_view_verts += [p1,center]
3689 cv_view_colours += [colour,colour]
3690 #}
3691 #}
3692
3693 cv_view_verts += [center+major*1.2*f,center+major*f*0.8]
3694 cv_view_colours += [colour,colour]
3695
3696 cv_draw_lines()
3697 #}
3698
3699 # Cone and twist limit
3700 #
3701 def draw_cone_twist( center, vx, vy, va ):
3702 #{
3703 global cv_view_verts, cv_view_colours
3704 axis = vy.cross( vx )
3705 axis.normalize()
3706
3707 size = 0.12
3708
3709 cv_view_verts += [center, center+va*size]
3710 cv_view_colours += [ (1,1,1), (1,1,1) ]
3711
3712 for x in range(32):#{
3713 t0 = (x/32) * math.tau
3714 t1 = ((x+1)/32) * math.tau
3715
3716 c0 = math.cos(t0)
3717 s0 = math.sin(t0)
3718 c1 = math.cos(t1)
3719 s1 = math.sin(t1)
3720
3721 p0 = center + (axis + vx*c0 + vy*s0).normalized() * size
3722 p1 = center + (axis + vx*c1 + vy*s1).normalized() * size
3723
3724 col0 = ( abs(c0), abs(s0), 0.0 )
3725 col1 = ( abs(c1), abs(s1), 0.0 )
3726
3727 cv_view_verts += [center, p0, p0, p1]
3728 cv_view_colours += [ (0,0,0), col0, col0, col1 ]
3729 #}
3730
3731 cv_draw_lines()
3732 #}
3733
3734 # Draws constraints and stuff for the skeleton. This isnt documented and wont be
3735 #
3736 def draw_skeleton_helpers( obj ):
3737 #{
3738 global cv_view_verts, cv_view_colours
3739
3740 if obj.data.pose_position != 'REST':#{
3741 return
3742 #}
3743
3744 for bone in obj.data.bones:#{
3745 c = bone.head_local
3746 a = Vector((bone.SR_data.collider_min[0],
3747 bone.SR_data.collider_min[1],
3748 bone.SR_data.collider_min[2]))
3749 b = Vector((bone.SR_data.collider_max[0],
3750 bone.SR_data.collider_max[1],
3751 bone.SR_data.collider_max[2]))
3752
3753 if bone.SR_data.collider == '1':#{
3754 vs = [None]*8
3755 vs[0]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+a[2]))
3756 vs[1]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+a[2]))
3757 vs[2]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+a[2]))
3758 vs[3]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+a[2]))
3759 vs[4]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+b[2]))
3760 vs[5]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+b[2]))
3761 vs[6]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+b[2]))
3762 vs[7]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+b[2]))
3763
3764 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
3765 (0,4),(1,5),(2,6),(3,7)]
3766
3767 for l in indices:#{
3768 v0 = vs[l[0]]
3769 v1 = vs[l[1]]
3770
3771 cv_view_verts += [(v0[0],v0[1],v0[2])]
3772 cv_view_verts += [(v1[0],v1[1],v1[2])]
3773 cv_view_colours += [(0.5,0.5,0.5),(0.5,0.5,0.5)]
3774 #}
3775 #}
3776 elif bone.SR_data.collider == '2':#{
3777 v0 = b-a
3778 major_axis = 0
3779 largest = -1.0
3780
3781 for i in range(3):#{
3782 if abs(v0[i]) > largest:#{
3783 largest = abs(v0[i])
3784 major_axis = i
3785 #}
3786 #}
3787
3788 v1 = Vector((0,0,0))
3789 v1[major_axis] = 1.0
3790
3791 tx = Vector((0,0,0))
3792 ty = Vector((0,0,0))
3793
3794 cv_tangent_basis( v1, tx, ty )
3795 r = (abs(tx.dot( v0 )) + abs(ty.dot( v0 ))) * 0.25
3796 l = v0[ major_axis ] - r*2
3797
3798 p0 = obj.matrix_world@Vector( c + (a+b)*0.5 + v1*l*-0.5 )
3799 p1 = obj.matrix_world@Vector( c + (a+b)*0.5 + v1*l* 0.5 )
3800
3801 colour = [0.2,0.2,0.2]
3802 colour[major_axis] = 0.5
3803
3804 cv_draw_halfsphere( p0, -v1, ty, tx, r, colour )
3805 cv_draw_halfsphere( p1, v1, ty, tx, r, colour )
3806 cv_draw_line( p0+tx* r, p1+tx* r, colour )
3807 cv_draw_line( p0+tx*-r, p1+tx*-r, colour )
3808 cv_draw_line( p0+ty* r, p1+ty* r, colour )
3809 cv_draw_line( p0+ty*-r, p1+ty*-r, colour )
3810 #}
3811 else:#{
3812 continue
3813 #}
3814
3815 center = obj.matrix_world @ c
3816 if bone.SR_data.cone_constraint:#{
3817 vx = Vector([bone.SR_data.conevx[_] for _ in range(3)])
3818 vy = Vector([bone.SR_data.conevy[_] for _ in range(3)])
3819 va = Vector([bone.SR_data.coneva[_] for _ in range(3)])
3820 draw_cone_twist( center, vx, vy, va )
3821 #}
3822 #}
3823 #}
3824
3825 def cv_ent_gate( obj ):
3826 #{
3827 global cv_view_verts, cv_view_colours
3828
3829 if obj.type != 'MESH': return
3830
3831 mesh_data = obj.data.SR_data.ent_gate[0]
3832 data = obj.SR_data.ent_gate[0]
3833 dims = mesh_data.dimensions
3834
3835 vs = [None]*9
3836 c = Vector((0,0,dims[2]))
3837
3838 vs[0] = obj.matrix_world @ Vector((-dims[0],0.0,-dims[1]+dims[2]))
3839 vs[1] = obj.matrix_world @ Vector((-dims[0],0.0, dims[1]+dims[2]))
3840 vs[2] = obj.matrix_world @ Vector(( dims[0],0.0, dims[1]+dims[2]))
3841 vs[3] = obj.matrix_world @ Vector(( dims[0],0.0,-dims[1]+dims[2]))
3842 vs[4] = obj.matrix_world @ (c+Vector((-1,0,-2)))
3843 vs[5] = obj.matrix_world @ (c+Vector((-1,0, 2)))
3844 vs[6] = obj.matrix_world @ (c+Vector(( 1,0, 2)))
3845 vs[7] = obj.matrix_world @ (c+Vector((-1,0, 0)))
3846 vs[8] = obj.matrix_world @ (c+Vector(( 1,0, 0)))
3847
3848 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(7,8)]
3849
3850 r3d = bpy.context.area.spaces.active.region_3d
3851
3852 p0 = r3d.view_matrix.inverted().translation
3853 v0 = (obj.matrix_world@Vector((0,0,0))) - p0
3854 v1 = obj.matrix_world.to_3x3() @ Vector((0,1,0))
3855
3856 if v0.dot(v1) > 0.0: cc = (0,1,0)
3857 else: cc = (1,0,0)
3858
3859 for l in indices:#{
3860 v0 = vs[l[0]]
3861 v1 = vs[l[1]]
3862 cv_view_verts += [(v0[0],v0[1],v0[2])]
3863 cv_view_verts += [(v1[0],v1[1],v1[2])]
3864 cv_view_colours += [cc,cc]
3865 #}
3866
3867 sw = (0.4,0.4,0.4)
3868 if data.target != None:
3869 cv_draw_arrow( obj.location, data.target.location, sw )
3870 #}
3871
3872 def cv_ent_volume( obj ):
3873 #{
3874 global cv_view_verts, cv_view_colours
3875
3876 data = obj.SR_data.ent_volume[0]
3877
3878 if data.subtype == '0':#{
3879 cv_draw_ucube( obj.matrix_world, (0,1,0) )
3880
3881 if data.target:#{
3882 cv_draw_arrow( obj.location, data.target.location, (0,1,0) )
3883 #}
3884 #}
3885 elif data.subtype == '1':#{
3886 cv_draw_ucube( obj.matrix_world, (1,1,0) )
3887
3888 if data.target:#{
3889 cv_draw_arrow( obj.location, data.target.location, (1,1,0) )
3890 #}
3891 #}
3892 #}
3893
3894 def dijkstra( graph, start_node, target_node ):
3895 #{
3896 unvisited = [_ for _ in graph]
3897 shortest_path = {}
3898 previous_nodes = {}
3899
3900 for n in unvisited:
3901 shortest_path[n] = 9999999.999999
3902 shortest_path[start_node] = 0
3903
3904 while unvisited:#{
3905 current_min_node = None
3906 for n in unvisited:#{
3907 if current_min_node == None:
3908 current_min_node = n
3909 elif shortest_path[n] < shortest_path[current_min_node]:
3910 current_min_node = n
3911 #}
3912
3913 for branch in graph[current_min_node]:#{
3914 tentative_value = shortest_path[current_min_node]
3915 tentative_value += graph[current_min_node][branch]
3916 if tentative_value < shortest_path[branch]:#{
3917 shortest_path[branch] = tentative_value
3918 previous_nodes[branch] = current_min_node
3919 #}
3920 #}
3921
3922 unvisited.remove(current_min_node)
3923 #}
3924
3925 path = []
3926 node = target_node
3927 while node != start_node:#{
3928 path.append(node)
3929
3930 if node not in previous_nodes: return None
3931 node = previous_nodes[node]
3932 #}
3933
3934 # Add the start node manually
3935 path.append(start_node)
3936 return path
3937 #}
3938
3939 class dij_graph():
3940 #{
3941 def __init__(_,points,graph,subsections):#{
3942 _.points = points
3943 _.graph = graph
3944 _.subsections = subsections
3945 #}
3946 #}
3947
3948 def create_node_graph( curves, gates ):
3949 #{
3950 # add endpoints of curves
3951 graph = {}
3952 route_points = []
3953 subsections = []
3954 point_count = 0
3955 spline_count = 0
3956
3957 for c in range(len(curves)):#{
3958 for s in range(len(curves[c].data.splines)):#{
3959 spline = curves[c].data.splines[s]
3960 l = len(spline.points)
3961 if l < 2: continue
3962
3963 dist = round(spline.calc_length(),2)
3964
3965 ia = point_count
3966 ib = point_count+l-1
3967
3968 graph[ia] = { ib: dist }
3969 graph[ib] = { ia: dist }
3970
3971 for i in range(len(spline.points)):#{
3972 wco = curves[c].matrix_world @ spline.points[i].co
3973 route_points.append(Vector((wco[0],wco[1],wco[2]+0.5)))
3974
3975 previous = ia+i-1
3976 proxima = ia+i+1
3977
3978 if i == 0: previous = -1
3979 if i == len(spline.points)-1: proxima = -1
3980
3981 subsections.append((spline_count,previous,proxima))
3982 point_count += 1
3983 #}
3984
3985 spline_count += 1
3986 #}
3987 #}
3988
3989 # link endpoints
3990 graph_keys = list(graph)
3991 for i in range(len(graph_keys)-1):#{
3992 for j in range(i+1, len(graph_keys)):#{
3993 if i%2==0 and i+1==j: continue
3994
3995 ni = graph_keys[i]
3996 nj = graph_keys[j]
3997 pi = route_points[ni]
3998 pj = route_points[nj]
3999
4000 dist = round((pj-pi).magnitude,2)
4001
4002 if dist < 10.0:#{
4003 graph[ni][nj] = dist
4004 graph[nj][ni] = dist
4005 #}
4006 #}
4007 #}
4008
4009 # add and link gates( by name )
4010 for gate in gates:#{
4011 v1 = gate.matrix_world.to_3x3() @ Vector((0,1,0))
4012 if gate.SR_data.ent_gate[0].target:
4013 v1 = v1 * -1.0
4014
4015 graph[ gate.name ] = {}
4016
4017 for i in range(len(graph_keys)):#{
4018 ni = graph_keys[i]
4019 pi = route_points[ni]
4020
4021 v0 = pi-gate.location
4022 if v0.dot(v1) < 0.0: continue
4023
4024 dist = round(v0.magnitude,2)
4025
4026 if dist < 10.0:#{
4027 graph[ gate.name ][ ni ] = dist
4028 graph[ ni ][ gate.name ] = dist
4029 #}
4030 #}
4031 #}
4032
4033 return dij_graph(route_points,graph,subsections)
4034 #}
4035
4036 def solve_graph( dij, start, end ):
4037 #{
4038 path = dijkstra( dij.graph, end, start )
4039 full = []
4040
4041 if path:#{
4042 for sj in range(1,len(path)-2):#{
4043 i0 = path[sj]
4044 i1 = path[sj+1]
4045 map0 = dij.subsections[i0]
4046 map1 = dij.subsections[i1]
4047
4048 if map0[0] == map1[0]:#{
4049 if map0[1] == -1: direction = 2
4050 else: direction = 1
4051 sent = 0
4052
4053 while True:#{
4054 map0 = dij.subsections[i0]
4055 i1 = map0[direction]
4056 if i1 == -1: break
4057
4058 full.append( i0 )
4059 sent += 1
4060 i0 = i1
4061 if sent > 50: break
4062 #}
4063 #}
4064 else:#{
4065 full.append( i0 )
4066 #}
4067 #}
4068
4069 full.append( path[-2] )
4070 #}
4071 return full
4072 #}
4073
4074 def cv_draw_route( route, dij ):
4075 #{
4076 pole = Vector((0.2,0.2,10))
4077 hat = Vector((1,8,0.2))
4078 cc = (route.SR_data.ent_route[0].colour[0],
4079 route.SR_data.ent_route[0].colour[1],
4080 route.SR_data.ent_route[0].colour[2])
4081
4082 cv_draw_ucube(route.matrix_world,cc,Vector((0.5,-7.5,6)),\
4083 Vector((0,-6.5,5.5)))
4084 cv_draw_ucube(route.matrix_world,cc,pole, Vector(( 0.5, 0.5,0)) )
4085 cv_draw_ucube(route.matrix_world,cc,pole, Vector(( 0.5,-13.5,0)) )
4086 cv_draw_ucube(route.matrix_world,cc,hat, Vector((-0.5,-6.5, 12)) )
4087 cv_draw_ucube(route.matrix_world,cc,hat, Vector((-0.5,-6.5,-1)) )
4088
4089 checkpoints = route.SR_data.ent_route[0].gates
4090
4091 for i in range(len(checkpoints)):#{
4092 gi = checkpoints[i].target
4093 gj = checkpoints[(i+1)%len(checkpoints)].target
4094
4095 if gi:#{
4096 dest = gi.SR_data.ent_gate[0].target
4097 if dest:
4098 cv_draw_line_dotted( gi.location, dest.location, cc )
4099 gi = dest
4100 #}
4101
4102 if gi==gj: continue # error?
4103 if not gi or not gj: continue
4104
4105 path = solve_graph( dij, gi.name, gj.name )
4106
4107 if path:#{
4108 cv_draw_arrow(gi.location,dij.points[path[0]],cc,1.5)
4109 cv_draw_arrow(dij.points[path[len(path)-1]],gj.location,cc,1.5)
4110 for j in range(len(path)-1):#{
4111 i0 = path[j]
4112 i1 = path[j+1]
4113 o0 = dij.points[ i0 ]
4114 o1 = dij.points[ i1 ]
4115 cv_draw_arrow(o0,o1,cc,1.5)
4116 #}
4117 #}
4118 else:#{
4119 cv_draw_line_dotted( gi.location, gj.location, cc )
4120 #}
4121 #}
4122 #}
4123
4124 def cv_draw():#{
4125 global cv_view_shader
4126 global cv_view_verts
4127 global cv_view_colours
4128 global cv_view_course_i
4129
4130 cv_view_course_i = 0
4131 cv_view_verts = []
4132 cv_view_colours = []
4133
4134 cv_view_shader.bind()
4135 gpu.state.depth_mask_set(False)
4136 gpu.state.line_width_set(2.0)
4137 gpu.state.face_culling_set('BACK')
4138 gpu.state.depth_test_set('LESS')
4139 gpu.state.blend_set('NONE')
4140
4141 route_gates = []
4142 route_curves = []
4143 routes = []
4144
4145 for obj in bpy.context.collection.objects:#{
4146 if obj.type == 'ARMATURE':#{
4147 if obj.data.pose_position == 'REST':
4148 draw_skeleton_helpers( obj )
4149 #}
4150 else:#{
4151 ent_type = obj_ent_type( obj )
4152
4153 if ent_type == 'ent_gate':#{
4154 cv_ent_gate( obj )
4155 route_gates += [obj]
4156 #}
4157 elif ent_type == 'ent_route_node':#{
4158 if obj.type == 'CURVE':#{
4159 route_curves += [obj]
4160 #}
4161 #}
4162 elif ent_type == 'ent_route':
4163 routes += [obj]
4164 elif ent_type == 'ent_volume':#{
4165 cv_ent_volume( obj )
4166 #}
4167 elif ent_type == 'ent_challenge':#{
4168 data = obj.SR_data.ent_challenge[0]
4169 if data.proxima:#{
4170 cv_draw_arrow( obj.location, data.proxima.location, (0,0.2,1.0) )
4171 #}
4172 if data.target:
4173 cv_draw_arrow( obj.location, data.target.location, (0,1.0,0.0) )
4174 #}
4175 elif ent_type == 'ent_relay':#{
4176 data = obj.SR_data.ent_relay[0]
4177 if data.target0:
4178 cv_draw_arrow( obj.location, data.target0.location, (0,1,0) )
4179 if data.target1:
4180 cv_draw_arrow( obj.location, data.target1.location, (0,1,0) )
4181 if data.target2:
4182 cv_draw_arrow( obj.location, data.target2.location, (0,1,0) )
4183 if data.target3:
4184 cv_draw_arrow( obj.location, data.target3.location, (0,1,0) )
4185 #}
4186 elif ent_type == 'ent_unlock':#{
4187 data = obj.SR_data.ent_unlock[0]
4188 if data.target:
4189 cv_draw_arrow( obj.location, data.target.location, (0,1.0,0.0) )
4190 #}
4191 elif ent_type == 'ent_audio':#{
4192 if obj.SR_data.ent_audio[0].flag_3d:
4193 cv_draw_sphere( obj.location, obj.scale[0], (1,1,0) )
4194 #}
4195 elif ent_type == 'ent_font':#{
4196 data = obj.SR_data.ent_font[0]
4197
4198 for i in range(len(data.variants)):#{
4199 sub = data.variants[i].mesh
4200 if not sub: continue
4201
4202 for ch in data.glyphs:#{
4203 mini = (ch.bounds[0],ch.bounds[1])
4204 maxi = (ch.bounds[2]+mini[0],ch.bounds[3]+mini[1])
4205 p0 = sub.matrix_world @ Vector((mini[0],0.0,mini[1]))
4206 p1 = sub.matrix_world @ Vector((maxi[0],0.0,mini[1]))
4207 p2 = sub.matrix_world @ Vector((maxi[0],0.0,maxi[1]))
4208 p3 = sub.matrix_world @ Vector((mini[0],0.0,maxi[1]))
4209
4210 if i == data.variants_index: cc = (0.5,0.5,0.5)
4211 else: cc = (0,0,0)
4212
4213 cv_view_verts += [p0,p1,p1,p2,p2,p3,p3,p0]
4214 cv_view_colours += [cc,cc,cc,cc,cc,cc,cc,cc]
4215 #}
4216 #}
4217 #}
4218 elif ent_type == 'ent_skateshop':#{
4219 data = obj.SR_data.ent_skateshop[0]
4220 display = data.mark_display
4221 info = data.mark_info
4222
4223 if data.tipo == '0':#{
4224 cc = (0.0,0.9,0.6)
4225 cc1 = (0.4,0.9,0.2)
4226 cc2 = (0.9,0.6,0.1)
4227
4228 rack = data.mark_rack
4229
4230 rack_cu = Vector((3.15,2.0,0.1))*0.5
4231 rack_co = Vector((0.0,0.0,0.0))
4232 display_cu = Vector((0.3,1.2,0.1))*0.5
4233 display_co = Vector((0.0,0.0,0.1))*0.5
4234 info_cu = Vector((1.2,0.01,0.3))*0.5
4235 info_co = Vector((0.0,0.0,0.0))*0.5
4236 #}
4237 elif data.tipo == '1':#{
4238 rack = None
4239 cc1 = (1.0,0.0,0.0)
4240 cc2 = (1.0,0.5,0.0)
4241 display_cu = Vector((0.4,0.4,2.0))*0.5
4242 display_co = Vector((0.0,0.0,1.0))*0.5
4243 info_cu = Vector((1.2,0.01,0.3))*0.5
4244 info_co = Vector((0.0,0.0,0.0))*0.5
4245 #}
4246 elif data.tipo == '2':#{
4247 rack = None
4248 cc1 = (1.0,0.0,0.0)
4249 cc2 = (1.0,0.5,0.0)
4250 display_cu = Vector((1.0,1.0,0.5))*0.5
4251 display_co = Vector((0.0,0.0,0.5))*0.5
4252 info_cu = Vector((1.2,0.01,0.3))*0.5
4253 info_co = Vector((0.0,0.0,0.0))*0.5
4254 #}
4255
4256 if rack:
4257 cv_draw_ucube( rack.matrix_world, cc, rack_cu, rack_co )
4258 if display:
4259 cv_draw_ucube( display.matrix_world, cc1, display_cu, display_co)
4260 if info:
4261 cv_draw_ucube( info.matrix_world, cc2, info_cu, info_co )
4262 #}
4263 elif ent_type == 'ent_swspreview':#{
4264 cc1 = (0.4,0.9,0.2)
4265 data = obj.SR_data.ent_swspreview[0]
4266 display = data.mark_display
4267 display1 = data.mark_display1
4268 display_cu = Vector((0.3,1.2,0.1))*0.5
4269 display_co = Vector((0.0,0.0,0.1))*0.5
4270 if display:
4271 cv_draw_ucube( display.matrix_world, cc1, display_cu, display_co)
4272 if display1:
4273 cv_draw_ucube(display1.matrix_world, cc1, display_cu, display_co)
4274 #}
4275 elif ent_type == 'ent_menuitem':#{
4276 for i,col in enumerate(obj.users_collection):#{
4277 colour32 = hash_djb2( col.name )
4278 r = pow(((colour32 ) & 0xff) / 255.0, 2.2 )
4279 g = pow(((colour32>>8 ) & 0xff) / 255.0, 2.2 )
4280 b = pow(((colour32>>16) & 0xff) / 255.0, 2.2 )
4281 cc = (r,g,b)
4282 vs = [None for _ in range(8)]
4283 scale = i*0.02
4284 for j in range(8):#{
4285 v0 = Vector([(obj.bound_box[j][z]+\
4286 ((-1.0 if obj.bound_box[j][z]<0.0 else 1.0)*scale)) \
4287 for z in range(3)])
4288 vs[j] = obj.matrix_world @ v0
4289 #}
4290 indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
4291 (0,4),(1,5),(2,6),(3,7)]
4292 for l in indices:#{
4293 v0 = vs[l[0]]
4294 v1 = vs[l[1]]
4295 cv_view_verts += [(v0[0],v0[1],v0[2])]
4296 cv_view_verts += [(v1[0],v1[1],v1[2])]
4297 cv_view_colours += [cc,cc]
4298 #}
4299 #}
4300 cv_draw_lines()
4301 cc = (1.0,1.0,1.0)
4302 data = obj.SR_data.ent_menuitem[0]
4303 if data.tipo == '4':#{
4304 if data.slider_minloc and data.slider_maxloc:#{
4305 v0 = data.slider_minloc.location
4306 v1 = data.slider_maxloc.location
4307 cv_draw_line( v0, v1, cc )
4308 #}
4309 #}
4310
4311 colour32 = hash_djb2(obj.name)
4312 r = ((colour32 ) & 0xff) / 255.0
4313 g = ((colour32>>8 ) & 0xff) / 255.0
4314 b = ((colour32>>16) & 0xff) / 255.0
4315 cc = (r,g,b)
4316 origin = obj.location + (Vector((r,g,b))*2.0-Vector((1.0,1.0,1.0)))\
4317 * 0.04
4318
4319 size = 0.01
4320
4321 if data.tipo != '0':#{
4322 if data.tipo == '4':#{
4323 if data.link0:#{
4324 cv_draw_arrow( origin, data.link0.location, cc, size )
4325 #}
4326 if data.link1:#{
4327 cv_draw_arrow( origin, data.link1.location, cc, size )
4328 #}
4329 #}
4330 else:#{
4331 if data.link0:#{
4332 cv_draw_arrow( origin, data.link0.location, cc, size )
4333 #}
4334 if data.link1:#{
4335 cv_draw_arrow( origin, data.link1.location, cc, size )
4336 #}
4337 if data.link2:#{
4338 cv_draw_arrow( origin, data.link2.location, cc, size )
4339 #}
4340 if data.link3:#{
4341 cv_draw_arrow( origin, data.link3.location, cc, size )
4342 #}
4343 #}
4344 #}
4345 #}
4346 #}
4347 #}
4348
4349 dij = create_node_graph( route_curves, route_gates )
4350
4351 #cv_draw_route_map( route_nodes )
4352 for route in routes:#{
4353 cv_draw_route( route, dij )
4354 #}
4355
4356 cv_draw_lines()
4357 #}
4358
4359 def pos3d_to_2d( pos ):#{
4360 return view3d_utils.location_3d_to_region_2d( \
4361 bpy.context.region, \
4362 bpy.context.space_data.region_3d, pos )
4363 #}
4364
4365 def cv_draw_pixel():#{
4366 if not bpy.context.scene.SR_data.gizmos: return
4367 blf.size(0,10)
4368 blf.color(0, 1.0,1.0,1.0,0.9)
4369 blf.enable(0,blf.SHADOW)
4370 blf.shadow(0,3,0.0,0.0,0.0,1.0)
4371 for obj in bpy.context.collection.objects:#{
4372 ent_type = obj_ent_type( obj )
4373
4374 if ent_type != 'none':#{
4375 co = pos3d_to_2d( obj.location )
4376
4377 if not co: continue
4378 blf.position(0,co[0],co[1],0)
4379 blf.draw(0,ent_type)
4380 #}
4381 #}
4382 #}
4383
4384 classes = [ SR_INTERFACE, SR_MATERIAL_PANEL,\
4385 SR_COLLECTION_SETTINGS, SR_SCENE_SETTINGS, \
4386 SR_COMPILE, SR_COMPILE_THIS, SR_MIRROR_BONE_X,\
4387 \
4388 SR_OBJECT_ENT_GATE, SR_MESH_ENT_GATE, SR_OBJECT_ENT_SPAWN, \
4389 SR_OBJECT_ENT_ROUTE_ENTRY, SR_UL_ROUTE_NODE_LIST, \
4390 SR_OBJECT_ENT_ROUTE, SR_OT_ROUTE_LIST_NEW_ITEM,\
4391 SR_OT_GLYPH_LIST_NEW_ITEM, SR_OT_GLYPH_LIST_DEL_ITEM,\
4392 SR_OT_GLYPH_LIST_MOVE_ITEM,\
4393 SR_OT_AUDIO_LIST_NEW_ITEM,SR_OT_AUDIO_LIST_DEL_ITEM,\
4394 SR_OT_FONT_VARIANT_LIST_NEW_ITEM,SR_OT_FONT_VARIANT_LIST_DEL_ITEM,\
4395 SR_OT_COPY_ENTITY_DATA, \
4396 SR_OBJECT_ENT_VOLUME, \
4397 SR_UL_AUDIO_LIST, SR_OBJECT_ENT_AUDIO_FILE_ENTRY,\
4398 SR_OT_ROUTE_LIST_DEL_ITEM,\
4399 SR_OBJECT_ENT_AUDIO,SR_OBJECT_ENT_MARKER,SR_OBJECT_ENT_GLYPH,\
4400 SR_OBJECT_ENT_FONT_VARIANT,
4401 SR_OBJECT_ENT_GLYPH_ENTRY,\
4402 SR_UL_FONT_VARIANT_LIST,SR_UL_FONT_GLYPH_LIST,\
4403 SR_OBJECT_ENT_FONT,SR_OBJECT_ENT_TRAFFIC,SR_OBJECT_ENT_SKATESHOP,\
4404 SR_OBJECT_ENT_WORKSHOP_PREVIEW,SR_OBJECT_ENT_MENU_ITEM,\
4405 SR_OBJECT_ENT_WORLD_INFO,SR_OBJECT_ENT_CCMD,\
4406 SR_OBJECT_ENT_CHALLENGE,SR_OBJECT_ENT_UNLOCK,SR_OBJECT_ENT_RELAY,\
4407 \
4408 SR_OBJECT_PROPERTIES, SR_LIGHT_PROPERTIES, SR_BONE_PROPERTIES,
4409 SR_MESH_PROPERTIES, SR_MATERIAL_PROPERTIES \
4410 ]
4411
4412 def register():
4413 #{
4414 for c in classes:
4415 bpy.utils.register_class(c)
4416
4417 bpy.types.Scene.SR_data = \
4418 bpy.props.PointerProperty(type=SR_SCENE_SETTINGS)
4419 bpy.types.Collection.SR_data = \
4420 bpy.props.PointerProperty(type=SR_COLLECTION_SETTINGS)
4421
4422 bpy.types.Object.SR_data = \
4423 bpy.props.PointerProperty(type=SR_OBJECT_PROPERTIES)
4424 bpy.types.Light.SR_data = \
4425 bpy.props.PointerProperty(type=SR_LIGHT_PROPERTIES)
4426 bpy.types.Bone.SR_data = \
4427 bpy.props.PointerProperty(type=SR_BONE_PROPERTIES)
4428 bpy.types.Mesh.SR_data = \
4429 bpy.props.PointerProperty(type=SR_MESH_PROPERTIES)
4430 bpy.types.Material.SR_data = \
4431 bpy.props.PointerProperty(type=SR_MATERIAL_PROPERTIES)
4432
4433 global cv_view_draw_handler, cv_view_pixel_handler
4434 cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
4435 cv_draw,(),'WINDOW','POST_VIEW')
4436 cv_view_pixel_handler = bpy.types.SpaceView3D.draw_handler_add(\
4437 cv_draw_pixel,(),'WINDOW','POST_PIXEL')
4438 #}
4439
4440 def unregister():
4441 #{
4442 for c in classes:
4443 bpy.utils.unregister_class(c)
4444
4445 global cv_view_draw_handler, cv_view_pixel_handler
4446 bpy.types.SpaceView3D.draw_handler_remove(cv_view_draw_handler,'WINDOW')
4447 bpy.types.SpaceView3D.draw_handler_remove(cv_view_pixel_handler,'WINDOW')
4448 #}
4449
4450 # ---------------------------------------------------------------------------- #
4451 # #
4452 # QOI encoder #
4453 # #
4454 # ---------------------------------------------------------------------------- #
4455 # #
4456 # Transliteration of: #
4457 # https://github.com/phoboslab/qoi/blob/master/qoi.h #
4458 # #
4459 # Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org #
4460 # SPDX-License-Identifier: MIT #
4461 # QOI - The "Quite OK Image" format for fast, lossless image compression #
4462 # #
4463 # ---------------------------------------------------------------------------- #
4464
4465 class qoi_rgba_t(Structure):
4466 #{
4467 _pack_ = 1
4468 _fields_ = [("r",c_uint8),
4469 ("g",c_uint8),
4470 ("b",c_uint8),
4471 ("a",c_uint8)]
4472 #}
4473
4474 QOI_OP_INDEX = 0x00 # 00xxxxxx
4475 QOI_OP_DIFF = 0x40 # 01xxxxxx
4476 QOI_OP_LUMA = 0x80 # 10xxxxxx
4477 QOI_OP_RUN = 0xc0 # 11xxxxxx
4478 QOI_OP_RGB = 0xfe # 11111110
4479 QOI_OP_RGBA = 0xff # 11111111
4480
4481 QOI_MASK_2 = 0xc0 # 11000000
4482
4483 def qoi_colour_hash( c ):
4484 #{
4485 return c.r*3 + c.g*5 + c.b*7 + c.a*11
4486 #}
4487
4488 def qoi_eq( a, b ):
4489 #{
4490 return (a.r==b.r) and (a.g==b.g) and (a.b==b.b) and (a.a==b.a)
4491 #}
4492
4493 def qoi_32bit( v ):
4494 #{
4495 return bytearray([ (0xff000000 & v) >> 24, \
4496 (0x00ff0000 & v) >> 16, \
4497 (0x0000ff00 & v) >> 8, \
4498 (0x000000ff & v) ])
4499 #}
4500
4501 def qoi_encode( img ):
4502 #{
4503 data = bytearray()
4504
4505 print(F"{' ':<30}",end='\r')
4506 print(F"[QOI] Encoding {img.name}.qoi[{img.size[0]},{img.size[1]}]",end='\r')
4507
4508 index = [ qoi_rgba_t() for _ in range(64) ]
4509
4510 # Header
4511 #
4512 data.extend( bytearray(c_uint32(0x66696f71)) )
4513 data.extend( qoi_32bit( img.size[0] ) )
4514 data.extend( qoi_32bit( img.size[1] ) )
4515 data.extend( bytearray(c_uint8(4)) )
4516 data.extend( bytearray(c_uint8(0)) )
4517
4518 run = 0
4519 px_prev = qoi_rgba_t()
4520 px_prev.r = c_uint8(0)
4521 px_prev.g = c_uint8(0)
4522 px_prev.b = c_uint8(0)
4523 px_prev.a = c_uint8(255)
4524
4525 px = qoi_rgba_t()
4526 px.r = c_uint8(0)
4527 px.g = c_uint8(0)
4528 px.b = c_uint8(0)
4529 px.a = c_uint8(255)
4530
4531 px_len = img.size[0] * img.size[1]
4532 paxels = [ int(min(max(_,0),1)*255) for _ in img.pixels ]
4533
4534 for px_pos in range( px_len ): #{
4535 idx = px_pos * img.channels
4536 nc = img.channels-1
4537
4538 px.r = paxels[idx+min(0,nc)]
4539 px.g = paxels[idx+min(1,nc)]
4540 px.b = paxels[idx+min(2,nc)]
4541 px.a = paxels[idx+min(3,nc)]
4542
4543 if qoi_eq( px, px_prev ): #{
4544 run += 1
4545
4546 if (run == 62) or (px_pos == px_len-1): #{
4547 data.extend( bytearray( c_uint8(QOI_OP_RUN | (run-1))) )
4548 run = 0
4549 #}
4550 #}
4551 else: #{
4552 if run > 0: #{
4553 data.extend( bytearray( c_uint8(QOI_OP_RUN | (run-1))) )
4554 run = 0
4555 #}
4556
4557 index_pos = qoi_colour_hash(px) % 64
4558
4559 if qoi_eq( index[index_pos], px ): #{
4560 data.extend( bytearray( c_uint8(QOI_OP_INDEX | index_pos)) )
4561 #}
4562 else: #{
4563 index[ index_pos ].r = px.r
4564 index[ index_pos ].g = px.g
4565 index[ index_pos ].b = px.b
4566 index[ index_pos ].a = px.a
4567
4568 if px.a == px_prev.a: #{
4569 vr = int(px.r) - int(px_prev.r)
4570 vg = int(px.g) - int(px_prev.g)
4571 vb = int(px.b) - int(px_prev.b)
4572
4573 vg_r = vr - vg
4574 vg_b = vb - vg
4575
4576 if (vr > -3) and (vr < 2) and\
4577 (vg > -3) and (vg < 2) and\
4578 (vb > -3) and (vb < 2):
4579 #{
4580 op = QOI_OP_DIFF | (vr+2) << 4 | (vg+2) << 2 | (vb+2)
4581 data.extend( bytearray( c_uint8(op) ))
4582 #}
4583 elif (vg_r > -9) and (vg_r < 8) and\
4584 (vg > -33) and (vg < 32 ) and\
4585 (vg_b > -9) and (vg_b < 8):
4586 #{
4587 op = QOI_OP_LUMA | (vg+32)
4588 delta = (vg_r+8) << 4 | (vg_b + 8)
4589 data.extend( bytearray( c_uint8(op) ) )
4590 data.extend( bytearray( c_uint8(delta) ))
4591 #}
4592 else: #{
4593 data.extend( bytearray( c_uint8(QOI_OP_RGB) ) )
4594 data.extend( bytearray( c_uint8(px.r) ))
4595 data.extend( bytearray( c_uint8(px.g) ))
4596 data.extend( bytearray( c_uint8(px.b) ))
4597 #}
4598 #}
4599 else: #{
4600 data.extend( bytearray( c_uint8(QOI_OP_RGBA) ) )
4601 data.extend( bytearray( c_uint8(px.r) ))
4602 data.extend( bytearray( c_uint8(px.g) ))
4603 data.extend( bytearray( c_uint8(px.b) ))
4604 data.extend( bytearray( c_uint8(px.a) ))
4605 #}
4606 #}
4607 #}
4608
4609 px_prev.r = px.r
4610 px_prev.g = px.g
4611 px_prev.b = px.b
4612 px_prev.a = px.a
4613 #}
4614
4615 # Padding
4616 for i in range(7):
4617 data.extend( bytearray( c_uint8(0) ))
4618 data.extend( bytearray( c_uint8(1) ))
4619 bytearray_align_to( data, 16, b'\x00' )
4620
4621 return data
4622 #}