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