forked from mattebb/3delightblender
/
shadergraph_utils.py
1132 lines (924 loc) · 37.4 KB
/
shadergraph_utils.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
from . import color_utils
from . import filepath_utils
from . import string_utils
from . import object_utils
from .prefs_utils import get_pref
from ..rman_constants import RMAN_STYLIZED_FILTERS, RMAN_STYLIZED_PATTERNS, RMAN_UTILITY_PATTERN_NAMES, RFB_FLOAT3
import math
import bpy
class BlNodeInfo:
def __init__(self, sg_node, group_node=None, is_cycles_node=False):
self.sg_node = sg_node
self.group_node = group_node
self.is_cycles_node = is_cycles_node
class RmanConvertNode:
def __init__(self, node_type, from_node, from_socket):
self.node_type = node_type
self.from_node = from_node
self.from_socket = from_socket
def __eq__(self, other):
if type(other) != RmanConvertNode:
return False
return (self.node_type == other.node_type and self.from_node == other.from_node and self.from_socket == other.from_socket)
def is_renderman_nodetree(material):
return find_node(material, 'RendermanOutputNode')
def find_rman_output_node(nt):
nodetype = 'RendermanOutputNode'
ntree = None
for mat in bpy.data.materials:
if mat.node_tree is None:
continue
if mat.node_tree == nt:
ntree = mat.node_tree
break
for node in mat.node_tree.nodes:
# check if the node belongs to a group node
node_tree = getattr(node, 'node_tree', None)
if node_tree is None:
continue
if node_tree == nt:
ntree = mat.node_tree
if ntree is None:
return None
for node in ntree.nodes:
if getattr(node, "bl_idname", None) == nodetype:
if getattr(node, "is_active_output", True):
return node
if not active_output_node:
active_output_node = node
return active_output_node
def is_mesh_light(ob):
'''Checks to see if ob is a RenderMan mesh light
Args:
ob (bpy.types.Object) - Object caller wants to check.
Returns:
(bpy.types.Node) - the PxrMeshLight node if this is a mesh light. Else, returns None.
'''
#mat = getattr(ob, 'active_material', None)
mat = object_utils.get_active_material(ob)
if not mat:
return None
output = is_renderman_nodetree(mat)
if not output:
return None
if len(output.inputs) > 1:
socket = output.inputs[1]
if socket.is_linked:
node = socket.links[0].from_node
if node.bl_label == 'PxrMeshLight':
return node
return None
def is_rman_light(ob, include_light_filters=True):
'''Checks to see if ob is a RenderMan light
Args:
ob (bpy.types.Object) - Object caller wants to check.
include_light_filters (bool) - whether or not light filters should be included
Returns:
(bpy.types.Node) - the shading node, else returns None.
'''
return get_light_node(ob, include_light_filters=include_light_filters)
def get_rman_light_properties_group(ob):
'''Return the RendermanLightSettings properties
for this object.
Args:
ob (bpy.types.Object) - Object caller wants to get the RendermanLightSettings for.
Returns:
(RendermanLightSettings) - RendermanLightSettings object
'''
if ob is None:
return None
if ob.type == 'LIGHT':
return ob.data.renderman
else:
#mat = ob.active_material
mat = object_utils.get_active_material(ob)
if mat:
return mat.renderman_light
return None
def get_light_node(ob, include_light_filters=True):
'''Return the shading node for this light object.
Args:
ob (bpy.types.Object) - Object caller is interested in.
include_light_filters (bool) - whether or not light filters should be included
Returns:
(bpy.types.Node) - The associated shading node for ob
'''
if ob.type == 'LIGHT':
if hasattr(ob.data, 'renderman'):
if include_light_filters:
return ob.data.renderman.get_light_node()
elif ob.data.renderman.renderman_light_role == 'RMAN_LIGHT':
return ob.data.renderman.get_light_node()
else:
return is_mesh_light(ob)
def socket_node_input(nt, socket):
return next((l.from_node for l in nt.links if l.to_socket == socket), None)
def socket_socket_input(nt, socket):
return next((l.from_socket for l in nt.links if l.to_socket == socket and socket.is_linked),
None)
def get_socket_name(node, socket):
if type(socket) == dict:
return socket['name'].replace(' ', '')
# if this is a renderman node we can just use the socket name,
else:
if not hasattr('node', 'plugin_name'):
from .. import rman_bl_nodes
# cycles node?
mapping, node_desc = rman_bl_nodes.get_cycles_node_desc(node)
if node_desc:
idx = -1
is_output = socket.is_output
if is_output:
for i, output in enumerate(node.outputs):
if socket.name == output.name:
idx = i
break
else:
for i, input in enumerate(node.inputs):
if socket.name == input.name:
idx = i
break
if idx == -1:
return socket.identifier.replace(' ', '')
if is_output:
node_desc_param = node_desc.outputs[idx]
else:
node_desc_param = node_desc.params[idx]
return node_desc_param.name
else:
if socket.name in node.inputs and socket.name in node.outputs:
suffix = 'Out' if socket.is_output else 'In'
return socket.name.replace(' ', '') + suffix
return socket.identifier.replace(' ', '')
def has_lobe_enable_props(node):
if node.bl_idname in {"PxrSurfaceBxdfNode", "PxrLayerPatternOSLNode", "PxrLayerPatternNode"}:
return True
return False
def get_socket_type(node, socket):
sock_type = socket.type.lower()
if sock_type == 'rgba':
return 'color'
elif sock_type == 'value':
return 'float'
elif sock_type == 'vector':
return 'point'
else:
return sock_type
def get_node_name(node, mat_name):
node_name = string_utils.sanitize_node_name('%s_%s' % (mat_name, node.name))
return node_name
def linked_sockets(sockets):
if sockets is None:
return []
return [i for i in sockets if i.is_linked]
def is_socket_same_type(socket1, socket2):
'''Compare two NodeSockets to see if they are of the same type. Types that
are float3 like are considered the same.
Arguments:
socket1 (bpy.types.NodeSocket) - first socket to compare
socket2 (bpy.types.NodeSocket) - second socket to compare
Returns:
(bool) - return True if both sockets are the same type
'''
return (type(socket1) == type(socket2)) or (is_socket_float_type(socket1) and is_socket_float_type(socket2)) or \
(is_socket_float3_type(socket1) and is_socket_float3_type(socket2))
def is_socket_float_type(socket):
'''Check if socket is of float type
Arguments:
socket (bpy.types.NodeSocket) - socket to check
Returns:
(bool) - return True if socket are float type
'''
renderman_type = getattr(socket, 'renderman_type', None)
if renderman_type:
return renderman_type in ['int', 'float']
else:
return socket.type in ['INT', 'VALUE']
def is_socket_float3_type(socket):
'''Check if socket is of float3 type
Arguments:
socket (bpy.types.NodeSocket) - socket to check
Returns:
(bool) - return True if socket is float3 type
'''
renderman_type = getattr(socket, 'renderman_type', None)
if renderman_type:
return renderman_type in RFB_FLOAT3
else:
return socket.type in ['RGBA', 'VECTOR']
def set_solo_node(node, nt, solo_node_name, refresh_solo=False, solo_node_output=''):
def hide_all(nt, node):
if not get_pref('rman_solo_collapse_nodes'):
return
for n in nt.nodes:
hide = (n != node)
if hasattr(n, 'prev_hidden'):
setattr(n, 'prev_hidden', n.hide)
n.hide = hide
for input in n.inputs:
if not input.is_linked:
if hasattr(input, 'prev_hidden'):
setattr(input, 'prev_hidden', input.hide)
input.hide = hide
for output in n.outputs:
if not output.is_linked:
if hasattr(output, 'prev_hidden'):
setattr(output, 'prev_hidden', output.hide)
output.hide = hide
def unhide_all(nt):
if not get_pref('rman_solo_collapse_nodes'):
return
for n in nt.nodes:
hide = getattr(n, 'prev_hidden', False)
n.hide = hide
for input in n.inputs:
if not input.is_linked:
hide = getattr(input, 'prev_hidden', False)
input.hide = hide
for output in n.outputs:
if not output.is_linked:
hide = getattr(output, 'prev_hidden', False)
output.hide = hide
if refresh_solo:
node.solo_nodetree = None
node.solo_node_name = ''
node.solo_node_output = ''
unhide_all(nt)
return
if solo_node_name:
node.solo_nodetree = nt
node.solo_node_name = solo_node_name
node.solo_node_output = solo_node_output
solo_node = nt.nodes[solo_node_name]
hide_all(nt, solo_node)
# do we need to convert this socket?
def do_convert_socket(from_socket, to_socket):
if not to_socket:
return False
return (is_socket_float_type(from_socket) and is_socket_float3_type(to_socket)) or \
(is_socket_float3_type(from_socket) and is_socket_float_type(to_socket))
def find_node_input(node, name):
for input in node.inputs:
if input.name == name:
return input
return None
def find_node(material, nodetype):
if material and material.node_tree:
ntree = material.node_tree
active_output_node = None
for node in ntree.nodes:
if getattr(node, "bl_idname", None) == nodetype:
if getattr(node, "is_active_output", True):
return node
if not active_output_node:
active_output_node = node
return active_output_node
return None
def find_node_from_nodetree(ntree, nodetype):
active_output_node = None
for node in ntree.nodes:
if getattr(node, "bl_idname", None) == nodetype:
if getattr(node, "is_active_output", True):
return node
if not active_output_node:
active_output_node = node
return active_output_node
def find_material_from_nodetree(ntree):
mat = None
for m in bpy.data.materials:
if m.node_tree == ntree.id_data:
mat = m
break
return mat
def is_soloable_node(node):
is_soloable = False
node_type = getattr(node, 'renderman_node_type', '')
if node_type in ('pattern', 'bxdf'):
if node.bl_label in ['PxrLayer', 'PxrLayerMixer']:
is_soloable = False
else:
is_soloable = True
return is_soloable
def find_soloable_node(ntree):
selected_node = None
for n in ntree.nodes:
node_type = getattr(n, 'renderman_node_type', '')
if n.select and node_type in ('pattern', 'bxdf'):
if n.bl_label in ['PxrLayer', 'PxrLayerMixer']:
continue
selected_node = n
break
return selected_node
def find_selected_pattern_node(ntree):
selected_node = None
for n in ntree.nodes:
node_type = getattr(n, 'renderman_node_type', '')
if n.select and node_type == 'pattern':
if n.bl_label in ['PxrLayer', 'PxrLayerMixer']:
continue
selected_node = n
break
return selected_node
def find_node_input(node, name):
for input in node.inputs:
if input.name == name:
return input
return None
# walk the tree for nodes to export
def gather_nodes(node):
nodes = []
for socket in node.inputs:
if socket.is_linked:
link = socket.links[0]
for sub_node in gather_nodes(socket.links[0].from_node):
if sub_node not in nodes:
nodes.append(sub_node)
if link.from_node.bl_idname == 'NodeReroute':
continue
if node.bl_idname == 'NodeReroute':
continue
# if this is a float->float3 type or float3->float connections, insert
# either PxrToFloat3 or PxrToFloat conversion nodes
if is_socket_float_type(link.from_socket) and is_socket_float3_type(socket):
convert_node = RmanConvertNode('PxrToFloat3', link.from_node, link.from_socket)
if convert_node not in nodes:
nodes.append(convert_node)
elif is_socket_float3_type(link.from_socket) and is_socket_float_type(socket):
convert_node = RmanConvertNode('PxrToFloat', link.from_node, link.from_socket)
if convert_node not in nodes:
nodes.append(convert_node)
if hasattr(node, 'renderman_node_type') and node.renderman_node_type != 'output':
nodes.append(node)
elif not hasattr(node, 'renderman_node_type') and node.bl_idname not in ['ShaderNodeOutputMaterial', 'NodeGroupInput', 'NodeGroupOutput']:
nodes.append(node)
return nodes
def gather_all_nodes_for_material(ob, nodes_list):
for node in ob.node_tree.nodes:
if node not in nodes_list:
if isinstance(ob, bpy.types.ShaderNodeGroup):
nodes_list.insert(0, node)
else:
nodes_list.append(node)
if node.bl_idname == 'ShaderNodeGroup':
gather_all_nodes_for_material(node, nodes_list)
def gather_all_textured_nodes(ob, nodes_list):
nt = None
if isinstance(ob, bpy.types.Object):
if ob.type == 'LIGHT':
nt = ob.data.node_tree
elif isinstance(ob, bpy.types.Material):
nt = ob.node_tree
elif isinstance(ob, bpy.types.ShaderNodeGroup):
nt = ob.node_tree
if nt is None:
return
for node in nt.nodes:
has_textured_params = getattr(node, 'rman_has_textured_params', False)
if node not in nodes_list and has_textured_params:
nodes_list.append(node)
if node.bl_idname == 'ShaderNodeGroup':
gather_all_textured_nodes(node, nodes_list)
def get_nodetree_name(node):
nt = node.id_data.original
for nm, ng in bpy.data.node_groups.items():
if nt == ng.original:
return nm
for mat in bpy.data.materials:
if mat.node_tree is None:
continue
if mat.node_tree.original == nt:
return mat.name
for world in bpy.data.worlds:
if world.node_tree.original == nt:
return world.name
for ob in bpy.data.objects:
if ob.type == 'LIGHT':
light = ob.data
if light.node_tree is None:
continue
if light.node_tree.original == nt:
return ob.name
elif ob.type == 'CAMERA':
if find_projection_node(ob) == node:
return ob.name
return None
def get_group_node(node):
'''
Find the group node that this NodeGroupOutput or
NodeGroupInput belongs to
Returns
(bpy.types.NodeGroup)
'''
current_group_node = None
users = bpy.context.blend_data.user_map(subset={node.id_data})
for group_nt in users[node.id_data]:
nodes = []
if isinstance(group_nt, bpy.types.Material):
nodes = group_nt.node_tree.nodes
elif isinstance(group_nt, bpy.types.NodeGroup):
nodes = group_nt.nodes
for n in nodes:
if n.bl_idname == 'ShaderNodeGroup':
for n2 in n.node_tree.nodes:
if n2 == node:
current_group_node = n
break
return current_group_node
def get_all_shading_nodes(scene=None):
'''Find all shading nodes in the scene
Arguments:
scene (bpy.types.Scene) - (optional) the scene we want to find the shading nodes in
Returns:
(list) - list of all the shading nodes
'''
from . import scene_utils
nodes = list()
if not scene:
context = bpy.context
scene = context.scene
world = scene.world
integrator = find_integrator_node(world)
if integrator:
nodes.append(integrator)
nodes.extend(find_displayfilter_nodes(world))
nodes.extend(find_samplefilter_nodes(world))
for cam in bpy.data.cameras:
n = find_projection_node(cam)
if n:
nodes.append(n)
for light in scene_utils.get_all_lights(scene):
n = get_light_node(light)
if n:
nodes.append(n)
def get_group_nodes(group_node, nodes):
for n in group_node.node_tree.nodes:
if n.bl_idname == 'ShaderNodeGroup':
get_group_nodes(n, nodes)
else:
rman_type = getattr(n, 'renderman_node_type', None)
if not rman_type:
continue
if hasattr(n, 'prop_meta'):
nodes.append(n)
for mat in bpy.data.materials:
if not mat.use_nodes:
continue
for n in mat.node_tree.nodes:
if n.bl_idname == 'ShaderNodeGroup':
get_group_nodes(n, nodes)
continue
rman_type = getattr(n, 'renderman_node_type', None)
if not rman_type:
continue
if hasattr(n, 'prop_meta'):
nodes.append(n)
return nodes
def save_bl_ramps(bl_scene):
'''
Save all ramps to our custom collection properties
'''
for node in get_all_shading_nodes():
if not hasattr(node, 'rman_fake_node_group'):
continue
for prop_name, meta in node.prop_meta.items():
param_widget = meta.get('widget', 'default')
param_type = meta['renderman_type']
if param_type == 'colorramp':
nt = bpy.data.node_groups.get(node.rman_fake_node_group, None)
if nt:
prop = getattr(node, prop_name)
ramp_name = prop
color_ramp_node = nt.nodes[ramp_name]
colors = []
positions = []
bl_ramp = '%s_bl_ramp' % prop_name
bl_ramp_prop = getattr(node, bl_ramp)
bl_ramp_prop.clear()
for e in color_ramp_node.color_ramp.elements:
r = bl_ramp_prop.add()
r.position = e.position
r.rman_value = e.color
elif param_type == 'floatramp':
nt = bpy.data.node_groups.get(node.rman_fake_node_group, None)
if nt:
prop = getattr(node, prop_name)
ramp_name = prop
float_ramp_node = nt.nodes[ramp_name]
curve = float_ramp_node.mapping.curves[0]
knots = []
vals = []
bl_ramp = '%s_bl_ramp' % prop_name
bl_ramp_prop = getattr(node, bl_ramp)
bl_ramp_prop.clear()
for p in curve.points:
r = bl_ramp_prop.add()
r.position = p.location[0]
r.rman_value = p.location[1]
def reload_bl_ramps(bl_scene, check_library=True):
'''
Reload all ramps from our custom collection properties. We only
do this if the NodeTree is from a library.
'''
for node in get_all_shading_nodes():
nt = node.id_data
if check_library and not nt.library:
continue
if not hasattr(node, 'rman_fake_node_group'):
continue
color_rman_ramps = node.__annotations__.get('__COLOR_RAMPS__', [])
float_rman_ramps = node.__annotations__.get('__FLOAT_RAMPS__', [])
node_group = bpy.data.node_groups.get(node.rman_fake_node_group, None)
if not node_group:
node_group = bpy.data.node_groups.new(
node.rman_fake_node_group, 'ShaderNodeTree')
node_group.use_fake_user = True
node.rman_fake_node_group_ptr = node_group
for prop_name in color_rman_ramps:
prop = getattr(node, prop_name)
ramp_name = prop
n = node_group.nodes.get(ramp_name, None)
if not n:
n = node_group.nodes.new('ShaderNodeValToRGB')
n.name = ramp_name
bl_ramp_prop = getattr(node, '%s_bl_ramp' % prop_name)
if len(bl_ramp_prop) < 1:
continue
elements = n.color_ramp.elements
for i in range(0, len(bl_ramp_prop)):
r = bl_ramp_prop[i]
if i < len(elements):
elem = elements[i]
elem.position = r.position
else:
elem = elements.new(r.position)
elem.color = r.rman_value
if len(bl_ramp_prop) < len(elements):
for elem in [elements[i] for i in range(len(bl_ramp_prop), len(elements)-1)]:
elements.remove(elem)
# we cannot remove the last element, so
# just copy the values and remove the second to last
# element
last_elem = elements[-1]
prev_elem = elements[-2]
last_elem.color = prev_elem.color
last_elem.position = prev_elem.position
elements.remove(prev_elem)
for prop_name in float_rman_ramps:
prop = getattr(node, prop_name)
ramp_name = prop
n = node_group.nodes.get(ramp_name, None)
if not n:
n = node_group.nodes.new('ShaderNodeVectorCurve')
n.name = ramp_name
bl_ramp_prop = getattr(node, '%s_bl_ramp' % prop_name)
if len(bl_ramp_prop) < 1:
continue
curve = n.mapping.curves[0]
points = curve.points
for i in range(0, len(bl_ramp_prop)):
r = bl_ramp_prop[i]
if i < len(points):
point = points[i]
point.location[0] = r.position
point.location[1] = r.rman_value
else:
points.new(r.position, r.rman_value)
if len(bl_ramp_prop) < len(points):
for elem in [points[i] for i in range(len(bl_ramp_prop), len(points)-1)]:
points.remove(elem)
last_elem = points[-1]
prev_elem = points[-2]
last_elem.location[0] = prev_elem.location[0]
last_elem.location[1] = prev_elem.location[1]
points.remove(prev_elem)
def is_texture_property(prop_name, meta):
param_type = meta['renderman_type']
if param_type != 'string':
return False
options = meta['options']
param_widget = meta.get('widget', 'default')
if param_widget in ['fileinput', 'assetidinput']:
if 'ies' in options:
return False
elif ('texture' in options) or ('env' in options) or ('imageplane' in options):
return True
return False
def get_rerouted_node(node):
'''Find and return the rerouted node and socket, given
a NodeReroute node
Arguments:
node (bpy.types.Node) - A shader node of type NodeReroute
Returns:
(bpy.types.Node) - the rerouted node
(bpy.types.NodeSocket) - the socket that should be connected from the rerouted node
'''
if not node.inputs[0].is_linked:
return (None, None)
from_node = node.inputs[0].links[0].from_node
if from_node.bl_idname == 'NodeReroute':
return get_rerouted_node(from_node)
socket = node.inputs[0].links[0].from_socket
return (from_node, socket)
def find_integrator_node(world):
'''Find and return the integrator node from the world nodetree
Arguments:
world (bpy.types.World) - Blender world object
Returns:
(RendermanIntegratorNode) - the integrator ShadingNode
'''
rm = world.renderman
if not world.renderman.use_renderman_node:
return None
output = find_node(world, 'RendermanIntegratorsOutputNode')
if output:
socket = output.inputs[0]
if socket.is_linked:
return socket.links[0].from_node
return None
def find_displayfilter_nodes(world):
'''Find and return all display filter nodes from the world nodetree
Arguments:
world (bpy.types.World) - Blender world object
Returns:
(list) - list of display filter nodes
'''
df_nodes = []
if not world.renderman.use_renderman_node:
return df_nodes
output = find_node(world, 'RendermanDisplayfiltersOutputNode')
if output:
for i, socket in enumerate(output.inputs):
if socket.is_linked:
bl_df_node = socket.links[0].from_node
df_nodes.append(bl_df_node)
return df_nodes
def find_samplefilter_nodes(world):
'''Find and return all sample filter nodes from the world nodetree
Arguments:
world (bpy.types.World) - Blender world object
Returns:
(list) - list of sample filter nodes
'''
sf_nodes = []
if not world.renderman.use_renderman_node:
return sf_nodes
output = find_node(world, 'RendermanSamplefiltersOutputNode')
if output:
for i, socket in enumerate(output.inputs):
if socket.is_linked:
bl_sf_node = socket.links[0].from_node
sf_nodes.append(bl_sf_node)
return sf_nodes
def find_projection_node(camera):
'''Find the projection node, if any
Arguments:
camera (bpy.types.Camera) - Camera object
Returns:
(bpy.types.ShaderNode) - projection node
'''
projection_node = None
if isinstance(camera, bpy.types.Camera):
nt = camera.renderman.rman_nodetree
else:
nt = camera.data.renderman.rman_nodetree
if nt:
output = find_node_from_nodetree(nt, 'RendermanProjectionsOutputNode')
socket = output.inputs[0]
if socket.is_linked:
projection_node = socket.links[0].from_node
return projection_node
def find_all_stylized_filters(world):
nodes = list()
output = find_node(world, 'RendermanDisplayfiltersOutputNode')
if not output:
return nodes
for i, socket in enumerate(output.inputs):
if socket.is_linked:
link = socket.links[0]
node = link.from_node
if node.bl_label in RMAN_STYLIZED_FILTERS:
nodes.append(node)
return nodes
def has_stylized_pattern_node(ob, node=None):
prop_name = ''
if not node:
mat = object_utils.get_active_material(ob)
if not mat:
return False
nt = mat.node_tree
output = is_renderman_nodetree(mat)
if not output:
return False
socket = output.inputs[0]
if not socket.is_linked:
return False
link = socket.links[0]
node = link.from_node
for nm in RMAN_UTILITY_PATTERN_NAMES:
if hasattr(node, nm):
prop_name = nm
prop_meta = node.prop_meta[prop_name]
if prop_meta['renderman_type'] == 'array':
coll_nm = '%s_collection' % prop_name
collection = getattr(node, coll_nm)
for i in range(len(collection)):
nm = '%s[%d]' % (prop_name, i)
if hasattr(node, 'inputs') and nm in node.inputs and \
node.inputs[nm].is_linked:
to_socket = node.inputs[nm]
from_node = to_socket.links[0].from_node
if from_node.bl_label in RMAN_STYLIZED_PATTERNS:
return from_node
elif node.inputs[prop_name].is_linked:
to_socket = node.inputs[prop_name]
from_node = to_socket.links[0].from_node
if from_node.bl_label in RMAN_STYLIZED_PATTERNS:
return from_node
return False
def hide_cycles_nodes(id):
cycles_output_node = None
if isinstance(id, bpy.types.Material):
cycles_output_node = find_node(id, 'ShaderNodeOutputMaterial')
elif isinstance(id, bpy.types.Light):
cycles_output_node = find_node(id, 'ShaderNodeOutputLight')
elif isinstance(id, bpy.types.World):
cycles_output_node = find_node(id, 'ShaderNodeOutputWorld')
if not cycles_output_node:
return
cycles_output_node.hide = True
for i in cycles_output_node.inputs:
if i.is_linked:
i.links[0].from_node.hide = True
def create_bxdf(bxdf):
mat = bpy.data.materials.new(bxdf)
mat.use_nodes = True
nt = mat.node_tree
hide_cycles_nodes(mat)
output = nt.nodes.new('RendermanOutputNode')
default = nt.nodes.new('%sBxdfNode' % bxdf)
default.location = output.location
default.location[0] -= 300
nt.links.new(default.outputs[0], output.inputs[0])
output.inputs[1].hide = True
output.inputs[3].hide = True
default.update_mat(mat)
if bxdf == 'PxrLayerSurface':
create_pxrlayer_nodes(nt, default)
return mat
def create_pxrlayer_nodes(nt, bxdf):
from .. import rman_bl_nodes
mixer = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__["PxrLayerMixer"])
layer1 = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__["PxrLayer"])
layer2 = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__["PxrLayer"])
mixer.location = bxdf.location
mixer.location[0] -= 300
layer1.location = mixer.location
layer1.location[0] -= 300
layer1.location[1] += 300
layer2.location = mixer.location
layer2.location[0] -= 300
layer2.location[1] -= 300
nt.links.new(mixer.outputs[0], bxdf.inputs[0])
nt.links.new(layer1.outputs[0], mixer.inputs['baselayer'])
nt.links.new(layer2.outputs[0], mixer.inputs['layer1'])
def _convert_grease_pencil_stroke_texture(mat, nt, output):
from .. import rman_bl_nodes
gp_mat = mat.grease_pencil
col = gp_mat.color[:3]
# col = color_utils.linearizeSRGB(col)
alpha = gp_mat.color[3]
bl_image = gp_mat.stroke_image
bxdf = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__['PxrConstant'])
bxdf.location = output.location
bxdf.location[0] -= 300
bxdf.emitColor = col
bxdf.presence = alpha
nt.links.new(bxdf.outputs[0], output.inputs[0])
if not bl_image:
bxdf.emitColor = [0.0, 0.0, 0.0, 1.0]
else:
real_file = filepath_utils.get_real_path(bl_image.filepath)
manifold = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__['PxrManifold2D'])
manifold.angle = -math.degrees(gp_mat.pattern_angle)
manifold.scaleS = gp_mat.pattern_scale[0]
manifold.scaleT = gp_mat.pattern_scale[1]
manifold.offsetS = gp_mat.texture_offset[0]
manifold.offsetT = gp_mat.texture_offset[1]
manifold.invertT = 1
texture = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__['PxrTexture'])
texture.filename = real_file
texture.linearize = 1
nt.links.new(manifold.outputs[0], texture.inputs[3])
mix = nt.nodes.new(rman_bl_nodes.__BL_NODES_MAP__['PxrMix'])
mix.color2 = col
mix.mix = gp_mat.mix_stroke_factor
nt.links.new(texture.outputs[0], mix.inputs[0])
nt.links.new(mix.outputs[0], bxdf.inputs[0])