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