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