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