-
Notifications
You must be signed in to change notification settings - Fork 15
/
nodes.py
1584 lines (1256 loc) · 54.9 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
from bpy.props import PointerProperty, StringProperty
from bpy.types import Node
from io_hubs_addon.io.utils import gather_property
from .utils import gather_object_property, gather_socket_value, filter_on_components, filter_entity_type, get_prefs, object_exists, createSocketForComponentProperty, get_input_entity, get_variable_value
from .consts import MATERIAL_PROPERTIES_ENUM, MATERIAL_PROPERTIES_TO_TYPES, SUPPORTED_COMPONENTS, SUPPORTED_PROPERTY_COMPONENTS
from .sockets import BGFlowSocket
def update_networked_color(self, context):
if hasattr(self, "networked") and self.networked or self.category == "Networking":
self.color = get_prefs().network_node_color
else:
self.color = (0.2, 0.6, 0.2)
class BGNode():
bl_label = "Behavior Graph Node"
bl_icon = "NODE"
category: bpy.props.StringProperty()
def init(self, context):
self.use_custom_color = True
def refresh(self):
if self.category == "Networking":
update_networked_color(self, bpy.context)
@classmethod
def poll(cls, ntree):
# return True
return ntree.bl_idname == 'BGTree'
class BGEventNode():
def init(self, context):
super().init(context)
self.color = (0.6, 0.2, 0.2)
BGFlowSocket.create(self.outputs)
class BGActionNode():
def init(self, context):
super().init(context)
self.color = (0.2, 0.2, 0.6)
BGFlowSocket.create(self.inputs)
BGFlowSocket.create(self.outputs)
class BGNetworked():
networked: bpy.props.BoolProperty(default=True, update=update_networked_color)
def init(self, context):
super().init(context)
update_networked_color(self, context)
def refresh(self):
if hasattr(super(), "refresh") and callable(getattr(super(), "refresh")):
super().refresh()
update_networked_color(self, bpy.context)
def gather_configuration(self, ob, variables, events, export_settings):
return {
"networked": self.networked
}
def on_entity_property_update(node, context):
if "entity" in node.outputs:
node.outputs.get("entity").target = node.target
class BGEntityPropertyNode():
entity_type: bpy.props.EnumProperty(
name="",
description="Target Entity",
items=filter_entity_type,
update=on_entity_property_update,
default=0
)
target: PointerProperty(
name="Target",
type=bpy.types.Object,
poll=filter_on_components,
update=on_entity_property_update
)
def draw_buttons(self, context, layout):
layout.prop(self, "entity_type")
if self.entity_type != "self":
layout.prop(self, "target")
def gather_configuration(self, ob, variables, events, export_settings):
from .behavior_graph import gather_object_property
target = ob if self.entity_type == "self" else self.target
if not self.target and type(ob) is bpy.types.Scene:
raise Exception('Empty entity cannot be used for Scene objects in this context')
elif not self.target and self.entity_type == "other":
raise Exception('Entity not set')
else:
return {
"target": gather_object_property(export_settings, target)
}
def gather_parameters(self, ob, input_socket, export_settings):
if not self.target:
if type(ob) is bpy.types.Scene:
raise Exception('Empty entity cannot be used for Scene objects in this context')
else:
return {
"value": gather_object_property(export_settings, ob)
}
else:
return {
"value": gather_object_property(export_settings, input_socket.target)
}
entity_property_settings = {
"name": ("NodeSocketString", ""),
"visible": ("NodeSocketBool", False),
"position": ("NodeSocketVectorXYZ", [0.0, 0.0, 0.0]),
"rotation": ("NodeSocketVectorEuler", [0.0, 0.0, 0.0]),
"scale": ("NodeSocketVectorXYZ", [1.0, 1.0, 1.0]),
}
def update_set_entity_property(self, context):
if self.inputs and len(self.inputs) > 2:
self.inputs.remove(self.inputs[2])
target = get_input_entity(self, bpy.context)
if not target:
return
setattr(self, "node_type", "hubs/entity/set/" + self.targetProperty)
(socket_type,
default_value) = entity_property_settings[self.targetProperty]
socket = self.inputs.new(socket_type, self.targetProperty)
socket.default_value = default_value
class BGHubsSetEntityProperty(BGNetworked, BGActionNode, BGNode, Node):
bl_label = "Set Entity Property"
node_type: bpy.props.StringProperty()
targetProperty: bpy.props.EnumProperty(
name="",
items=[
("visible", "visible", ""),
("position", "position", ""),
("rotation", "rotation", ""),
("scale", "scale", "")
],
default="visible",
update=update_set_entity_property
)
def init(self, context):
super().init(context)
self.inputs.new("BGHubsEntitySocket", "entity")
update_set_entity_property(self, context)
def draw_buttons(self, context, layout):
layout.prop(self, "networked")
entity_type = self.inputs.get("entity").entity_type
if (entity_type == "self" and context.scene.bg_node_type != 'SCENE') or entity_type != "self":
layout.prop(self, "targetProperty")
def update_network_dependencies(self, ob, export_settings):
if self.networked:
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
from .utils import update_gltf_network_dependencies
from .components import networked_object_properties
if self.targetProperty == "position" or self.targetProperty == "rotation" or self.targetProperty == "scale":
value = {
"transform": True
}
elif self.targetProperty == "visible":
value = {
self.targetProperty: True
}
update_gltf_network_dependencies(self, export_settings, target,
networked_object_properties.NetworkedObjectProperties, value)
class BGNode_hubs_onInteract(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Interact"
node_type = "hubs/onInteract"
def init(self, context):
super().init(context)
self.outputs.new("BGHubsEntitySocket", "entity")
class BGNode_hubs_onCollisionEnter(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Collision Enter"
node_type = "hubs/onCollisionEnter"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsEntitySocket", "entity")
class BGNode_hubs_onCollisionStay(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Collision Stay"
node_type = "hubs/onCollisionStay"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsEntitySocket", "entity")
class BGNode_hubs_onCollisionExit(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Collision Exit"
node_type = "hubs/onCollisionExit"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsEntitySocket", "entity")
class BGNode_hubs_onPlayerCollisionEnter(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Player Collision Enter"
node_type = "hubs/onPlayerCollisionEnter"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsPlayerSocket", "player")
class BGNode_hubs_onPlayerCollisionStay(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Player Collision Stay"
node_type = "hubs/onPlayerCollisionStay"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsPlayerSocket", "player")
class BGNode_hubs_onPlayerCollisionExit(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Player Collision Exit"
node_type = "hubs/onPlayerCollisionExit"
poll_components: StringProperty(default="physics-shape")
def init(self, context):
super().init(context)
self.outputs.new("BGHubsPlayerSocket", "player")
class BGNode_media_onMediaEvent(BGEventNode, BGEntityPropertyNode, BGNode, Node):
bl_label = "On Media Event"
node_type = "media/onMediaEvent"
poll_components: StringProperty(default="video,audio")
def init(self, context):
self.use_custom_color = True
self.color = (0.6, 0.2, 0.2)
BGFlowSocket.create(self.outputs, name="create")
BGFlowSocket.create(self.outputs, name="play")
BGFlowSocket.create(self.outputs, name="pause")
BGFlowSocket.create(self.outputs, name="end")
BGFlowSocket.create(self.outputs, name="destroy")
self.outputs.new("BGHubsEntitySocket", "entity")
def draw_buttons(self, context, layout):
super().draw_buttons(context, layout)
def update_output_sockets(self, context):
existing_outputs = len(self.outputs)
print("existing", existing_outputs, "desired", self.numOutputs)
if (existing_outputs < self.numOutputs):
for i in range(existing_outputs, self.numOutputs):
BGFlowSocket.create(self.outputs, f"{i+1}")
elif existing_outputs > self.numOutputs:
for i in range(self.numOutputs, existing_outputs):
self.outputs.remove(self.outputs[f"{i+1}"])
class BGNode_flow_sequence(BGNode, Node):
bl_label = "Sequence"
node_type = "flow/sequence"
numOutputs: bpy.props.IntProperty(
name="Outputs",
default=2,
min=1,
update=update_output_sockets
)
def init(self, context):
super().init(context)
self.color = (0.2, 0.2, 0.2)
BGFlowSocket.create(self.inputs)
update_output_sockets(self, context)
def draw_buttons(self, context, layout):
layout.prop(self, "numOutputs")
def get_available_variables(self, context):
get_available_variables.cached_enum = [("None", "None", "None")]
target = get_input_entity(self, context)
if target:
for var in target.bg_global_variables:
get_available_variables.cached_enum.append((var.name, var.name, var.name))
return get_available_variables.cached_enum
# Bug: https://docs.blender.org/api/current/bpy.props.html#bpy.props.EnumProperty
# Workaround: https://blender.stackexchange.com/a/216233
get_available_variables.cached_enum = []
def update_selected_variable(self, context):
isSetter = self.bl_idname == "BGNode_variable_set"
has_socket = False
old_type = "None"
if isSetter:
self.color = (0.2, 0.6, 0.2)
if "value" in self.inputs:
has_socket = True
from .utils import socket_to_type
old_type = socket_to_type[self.inputs.get("value").bl_idname]
else:
self.color = (0.6, 0.6, 0.2)
if "value" in self.outputs:
has_socket = True
from .utils import socket_to_type
old_type = socket_to_type[self.outputs.get("value").bl_idname]
if not context:
return
remove_sockets = True
target = get_input_entity(self, context)
if target:
has_var = self.variableName in target.bg_global_variables
var = target.bg_global_variables.get(self.variableName)
var_type = var.type if has_var else "None"
if var_type == old_type:
remove_sockets = False
if remove_sockets and has_socket:
if isSetter:
if "value" in self.inputs:
from .utils import socket_to_type
self.inputs.remove(self.inputs.get("value"))
elif "value" in self.outputs:
from .utils import socket_to_type
self.outputs.remove(self.outputs.get("value"))
if not target:
return
# Create a new socket based on the selected variable type
from .utils import type_to_socket
if var_type != "None" and old_type != var_type:
if isSetter:
socket = self.inputs.new(type_to_socket[var_type], "value")
else:
socket = self.outputs.new(type_to_socket[var_type], "value")
if var_type == "entity":
self.outputs.get("value").target = target.bg_global_variables.get(self.variableName).defaultEntity
if var_type == "entity":
socket.refresh = False
if isSetter:
if var and hasattr(var, "networked") and var.networked and var_type != "entity":
self.color = get_prefs().network_node_color
self.prop_type = var_type
def getVariableId(self):
target = get_input_entity(self, bpy.context)
if target and self.variableName in target.bg_global_variables:
return target.bg_global_variables.find(self.variableName) + 1
return 0
def setVariableId(self, value):
target = get_input_entity(self, bpy.context)
if not target or value == 0:
self.variableName = 'None'
elif value <= len(target.bg_global_variables):
self.variableName = target.bg_global_variables[value - 1].name
else:
self.variableName = 'None'
class BGNode_variable_get(BGNode, Node):
bl_label = "Get Variable"
node_type = "variable/get"
variableName: bpy.props.StringProperty(
name="Variable Name",
description="Variable Name",
default="None"
)
variableId: bpy.props.EnumProperty(
name="Variable",
description="Variable",
items=get_available_variables,
update=update_selected_variable,
get=getVariableId,
set=setVariableId,
)
def init(self, context):
super().init(context)
self.color = (0.6, 0.6, 0.2)
self.inputs.new("BGHubsEntitySocket", "entity")
entity = self.inputs.get("entity")
entity.export = False
update_selected_variable(self, context)
def draw_buttons(self, context, layout):
layout.prop(self, "variableId")
def refresh(self):
update_selected_variable(self, bpy.context)
def gather_configuration(self, ob, variables, events, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
var_name = f"{target.name}_{self.variableName}"
if self.variableName == 'None' or var_name not in variables:
raise Exception(f'variable node: {self.variableName}[{var_name}], id: "None"')
else:
print(f'variable node: {self.variableName}, id: {variables[var_name]["id"]}')
return {
"variableId": variables[var_name]["id"]
}
class BGNode_variable_set(BGActionNode, BGNode, Node):
bl_label = "Set Variable"
node_type = "variable/set"
variableName: bpy.props.StringProperty(
name="Variable Name",
description="Variable Name",
default="None"
)
variableId: bpy.props.EnumProperty(
name="Value",
description="Variable Value",
items=get_available_variables,
update=update_selected_variable,
get=getVariableId,
set=setVariableId,
)
def init(self, context):
super().init(context)
self.color = (0.2, 0.6, 0.2)
self.inputs.new("BGHubsEntitySocket", "entity")
entity = self.inputs.get("entity")
entity.export = False
update_selected_variable(self, context)
def draw_buttons(self, context, layout):
layout.prop(self, "variableId")
def refresh(self):
update_selected_variable(self, bpy.context)
def gather_configuration(self, ob, variables, events, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
var_name = f"{target.name}_{self.variableName}"
if self.variableName == 'None' or var_name not in variables:
raise Exception(f'variable node: {self.variableName}[{var_name}], id: "None"')
else:
print(f'variable node: {self.variableName}, id: {variables[var_name]["id"]}')
return {
"variableId": variables[var_name]["id"]
}
def get_available_custom_events(self, context):
get_available_custom_events.cached_enum = [("None", "None", "None")]
target = get_input_entity(self, context)
if target:
for var in target.bg_custom_events:
get_available_custom_events.cached_enum.append((var.name, var.name, var.name))
return get_available_custom_events.cached_enum
# Bug: https://docs.blender.org/api/current/bpy.props.html#bpy.props.EnumProperty
# Workaround: https://blender.stackexchange.com/a/216233
get_available_custom_events.cached_enum = []
def getCustomEventId(self):
target = get_input_entity(self, bpy.context)
if target and self.customEventName in target.bg_custom_events:
return target.bg_custom_events.find(self.customEventName) + 1
return 0
def setCustomEventId(self, value):
target = get_input_entity(self, bpy.context)
if not target or value == 0:
self.customEventName = 'None'
elif value <= len(target.bg_custom_events):
self.customEventName = target.bg_custom_events[value - 1].name
else:
self.customEventName = 'None'
def update_selected_custom_event(self, context):
isInput = self.bl_idname == "BGNode_customEvent_trigger"
if not context:
return
target = get_input_entity(self, context)
if not target:
return
params = None
recreate_sockets = True
if self.customEventName != "None":
params = target.bg_custom_events.get(self.customEventName).parameters
if isInput:
if len(params) != len(self.inputs) - 2 and not len(params) == 0:
recreate_sockets = True
else:
sockets_equal = True
for idx, param in enumerate(params):
if len(self.inputs) > idx + 2:
param_name = param.name
param_type = param.type
from .utils import socket_to_type
socket_name = self.inputs[idx + 2].name
socket_type = socket_to_type[self.inputs[idx + 2].bl_idname]
sockets_equal = sockets_equal and socket_type == param_type and socket_name == param_name
recreate_sockets = not sockets_equal
else:
if len(params) != len(self.outputs) - 1:
recreate_sockets = True
else:
sockets_equal = True
for idx, param in enumerate(params):
if len(self.outputs) > idx + 1:
param_name = param.name
param_type = param.type
from .utils import socket_to_type
socket_name = self.outputs[idx + 1].name
socket_type = socket_to_type[self.outputs[idx + 1].bl_idname]
sockets_equal = sockets_equal and socket_type == param_type and socket_name == param_name
recreate_sockets = not sockets_equal
if recreate_sockets:
if isInput:
for i in range(len(self.inputs) - 1, 1, -1):
self.inputs.remove(self.inputs[i])
else:
for i in range(len(self.outputs) - 1, 0, -1):
self.outputs.remove(self.outputs[i])
if params:
for param in params:
param_name = param.name
param_type = param.type
from .utils import type_to_socket
if isInput:
socket = self.inputs.new(type_to_socket[param_type], param_name)
else:
socket = self.outputs.new(type_to_socket[param_type], param_name)
if param_type == "entity":
socket.refresh = False
socket.default_value = get_variable_value(param)
class BGNode_customEvent_trigger(BGActionNode, BGNode, Node):
bl_label = "Trigger"
node_type = "customEvent/trigger"
customEventName: bpy.props.StringProperty(
name="Custom Event Name",
description="Custom Event Name",
default="None"
)
customEventId: bpy.props.EnumProperty(
name="Custom Event",
description="Custom Event",
items=get_available_custom_events,
update=update_selected_custom_event,
get=getCustomEventId,
set=setCustomEventId,
)
def init(self, context):
super().init(context)
self.inputs.new("BGHubsEntitySocket", "entity")
entity = self.inputs.get("entity")
entity.export = False
def draw_buttons(self, context, layout):
layout.prop(self, "customEventId")
def refresh(self):
self.customEventId = self.customEventId
def gather_parameters(self, ob, input_socket, export_settings):
target = get_input_entity(self, bpy.context)
if not target or self.customEventName == "None":
return
from .utils import socket_to_type
params = target.bg_custom_events.get(self.customEventName).parameters
if len(params) > 0:
target_param = None
for param in params:
if param.name == input_socket.name and param.type == socket_to_type[input_socket.bl_idname]:
target_param = param
break
if target_param:
return {
"value": gather_socket_value(ob, export_settings, input_socket)
}
def gather_configuration(self, ob, variables, events, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
event_name = f"{target.name}_{self.customEventName}"
if self.customEventName == 'None' or event_name not in events:
raise Exception(f' custom event node: {self.customEventName}[{event_name}], id: "None"')
else:
print(f'custom event node: {self.customEventName}, id: {events[event_name]["id"]}')
return {
"customEventId": events[event_name]["id"]
}
class BGNode_customEvent_onTriggered(BGEventNode, BGNode, Node):
bl_label = "On Trigger"
node_type = "customEvent/onTriggered"
customEventName: bpy.props.StringProperty(
name="Custom Event Name",
description="Custom Event Name",
default="None"
)
customEventId: bpy.props.EnumProperty(
name="Custom Event",
description="Custom Event",
items=get_available_custom_events,
update=update_selected_custom_event,
get=getCustomEventId,
set=setCustomEventId,
)
def init(self, context):
super().init(context)
self.inputs.new("BGHubsEntitySocket", "entity")
entity = self.inputs.get("entity")
entity.export = False
def draw_buttons(self, context, layout):
layout.prop(self, "customEventId")
def refresh(self):
self.customEventId = self.customEventId
def gather_configuration(self, ob, variables, events, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
event_name = f"{target.name}_{self.customEventName}"
if self.customEventName == 'None' or event_name not in events:
raise Exception(f' custom event node: {self.customEventName}[{event_name}], id: "None"')
else:
print(f'custom event node: {self.customEventName}, id: {events[event_name]["id"]}')
return {
"customEventId": events[event_name]["id"]
}
def get_available_networkedBehavior_properties(self, context):
get_available_networkedBehavior_properties.cached_enum = [("None", "None", "None")]
target = get_input_entity(self, context)
if target:
for var in target.bg_global_variables:
get_available_networkedBehavior_properties.cached_enum.append((var.name, var.name, var.name))
return get_available_networkedBehavior_properties.cached_enum
# Bug: https://docs.blender.org/api/current/bpy.props.html#bpy.props.EnumProperty
# Workaround: https://blender.stackexchange.com/a/216233
get_available_networkedBehavior_properties.cached_enum = []
def update_selected_networked_variable(self, context):
isSetter = self.bl_idname == "BGNode_networkedVariable_set"
has_socket = False
old_type = "None"
if isSetter:
self.color = (0.2, 0.6, 0.2)
if "value" in self.inputs:
has_socket = True
from .utils import socket_to_type
old_type = socket_to_type[self.inputs.get("value").bl_idname]
else:
self.color = (0.6, 0.6, 0.2)
if "value" in self.outputs:
has_socket = True
from .utils import socket_to_type
old_type = socket_to_type[self.outputs.get("value").bl_idname]
if not context:
return
remove_sockets = True
target = get_input_entity(self, context)
if target:
has_var = self.prop_name in target.bg_global_variables
var = target.bg_global_variables.get(self.prop_name)
var_type = var.type if has_var else "None"
if var_type == old_type:
remove_sockets = False
if remove_sockets:
if has_socket:
if isSetter:
if "value" in self.inputs:
from .utils import socket_to_type
self.inputs.remove(self.inputs.get("value"))
elif "value" in self.outputs:
from .utils import socket_to_type
self.outputs.remove(self.outputs.get("value"))
else:
# Path for migrating the old networked variable get/set nodes
if isSetter:
for i in range(len(self.inputs) - 1, 1, -1):
self.inputs.remove(self.inputs[i])
elif len(self.outputs) > 1:
for i in range(len(self.outputs) - 1, -1, -1):
self.outputs.remove(self.outputs[i])
if not target:
return
# Create a new socket based on the selected variable type
from .utils import type_to_socket
if var_type != "None" and old_type != var_type:
if isSetter:
socket = self.inputs.new(type_to_socket[var_type], "value")
else:
socket = self.outputs.new(type_to_socket[var_type], "value")
if var_type == "entity":
self.outputs.get("value").target = target.bg_global_variables.get(self.prop_name).defaultEntity
if var_type == "entity":
socket.refresh = False
if isSetter:
if var and hasattr(var, "networked") and var.networked and var_type != "entity":
self.color = get_prefs().network_node_color
self.networked = True
else:
self.networked = False
self.prop_type = var_type
def getNetworkedVariableId(self):
target = get_input_entity(self, bpy.context)
if target and self.prop_name in target.bg_global_variables:
return target.bg_global_variables.find(self.prop_name) + 1
return 0
def setNetworkedVariableId(self, value):
target = get_input_entity(self, bpy.context)
if not target or value == 0:
self.prop_name = 'None'
elif value <= len(target.bg_global_variables):
self.prop_name = target.bg_global_variables[value - 1].name
else:
self.prop_name = 'None'
class BGNode_networkedVariable_set(BGNetworked, BGActionNode, BGNode, Node):
bl_label = "Variable Set"
node_type = "networkedVariable/set"
prop_id: bpy.props.EnumProperty(
name="Property",
description="Property",
items=get_available_networkedBehavior_properties,
update=update_selected_networked_variable,
get=getNetworkedVariableId,
set=setNetworkedVariableId,
default=0
)
prop_name: bpy.props.StringProperty(
name="Property Name",
description="Property Name",
default="None"
)
prop_type: bpy.props.StringProperty(default="")
networked: bpy.props.BoolProperty(default=True, update=update_networked_color)
def init(self, context):
super().init(context)
self.inputs.new("BGHubsEntitySocket", "entity")
self.inputs.get("entity")
update_selected_networked_variable(self, context)
def draw_buttons(self, context, layout):
row = layout.row()
row.prop(self, "networked")
row.enabled = False
layout.prop(self, "prop_id")
def refresh(self):
update_selected_networked_variable(self, bpy.context)
def gather_parameters(self, ob, input_socket, export_settings):
return {
"value": gather_socket_value(ob, export_settings, input_socket)
}
def gather_configuration(self, ob, variables, events, export_settings):
config = super().gather_configuration(ob, variables, events, export_settings)
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
has_prop = self.prop_name in target.bg_global_variables
if not has_prop:
raise Exception(f"Property {self.prop_name} does not exist")
prop = target.bg_global_variables.get(self.prop_name)
export_prop_name = self.prop_name if prop.networked else f"{target.name}_{self.prop_name}"
config.update({
"variableId": variables[export_prop_name]["id"] if not prop.networked else -1,
"name": export_prop_name,
"valueTypeName": self.prop_type
})
return config
def update_network_dependencies(self, ob, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
if not object_exists(target):
raise Exception(f"Entity {target.name} does not exist")
has_prop = self.prop_name in target.bg_global_variables
if not has_prop:
raise Exception(f"Property {self.prop_name} does not exist")
prop = target.bg_global_variables.get(self.prop_name)
if prop.networked:
from .utils import update_gltf_network_dependencies, gather_variable_value
from .components.networked_behavior import NetworkedBehavior
value = {
self.prop_name: {
"type": prop.type,
"value": gather_variable_value(target, prop, export_settings)
}
}
update_gltf_network_dependencies(self, export_settings, target, NetworkedBehavior, value)
class BGNode_networkedVariable_get(BGNode, Node):
bl_label = "Variable Get"
node_type = "networkedVariable/get"
poll_components: StringProperty(default="networked-behavior")
prop_id: bpy.props.EnumProperty(
name="Property",
description="Property",
items=get_available_networkedBehavior_properties,
update=update_selected_networked_variable,
get=getNetworkedVariableId,
set=setNetworkedVariableId,
default=0
)
prop_name: bpy.props.StringProperty(
name="Property Name",
description="Property Name",
default="None"
)
prop_type: bpy.props.StringProperty(default="")
def init(self, context):
super().init(context)
self.color = (0.6, 0.6, 0.2)
self.inputs.new("BGHubsEntitySocket", "entity")
self.inputs.get("entity")
update_selected_networked_variable(self, context)
def draw_buttons(self, context, layout):
layout.prop(self, "prop_id")
def refresh(self):
update_selected_networked_variable(self, bpy.context)
def gather_parameters(self, ob, input_socket, export_settings):
return {
"value": gather_socket_value(ob, export_settings, input_socket)
}
def gather_configuration(self, ob, variables, events, export_settings):
target = get_input_entity(self, bpy.context, ob)
if not target:
raise Exception("Entity not set")
has_prop = self.prop_name in target.bg_global_variables
if not has_prop:
raise Exception(f"Property {self.prop_name} does not exist")
prop = target.bg_global_variables.get(self.prop_name)
export_prop_name = self.prop_name if prop.networked else f"{target.name}_{self.prop_name}"
return {
"networked": prop.networked,
"variableId": variables[export_prop_name]["id"] if not prop.networked else -1,
"name": export_prop_name,
"valueTypeName": self.prop_type
}
def get_children(ob):
children = []
for ob in bpy.context.view_layer.objects:
if ob.parent == ob:
children.append(ob)
return children
def get_object_components(ob):
items = []
items.extend(ob.hubs_component_list.items.keys())
for child in get_children(ob):
items.extend(child.hubs_component_list.items.keys())
return items
def getAvailableComponents(self, context):
from io_hubs_addon.components.components_registry import get_components_registry
registry = get_components_registry()
getAvailableComponents.cached_enum = [("None", "None", "None")]
target = get_input_entity(self, context)
if target: