Repository navigation
Expand file tree
/
Copy pathoperators.py
More file actions
1304 lines (1177 loc) · 55.1 KB
/
Copy pathoperators.py
File metadata and controls
1304 lines (1177 loc) · 55.1 KB
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This file is part of the Blender addon "Renderlayer Manager".
#
# The addon "Renderlayer Manager" is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3 of the License or
# any later version.
#
# The addon "Renderlayer Manager" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the addon. If not, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
import colorsys
import string
import os
from mathutils import Vector
from bpy.props import BoolProperty
from . import utils
from . import ui
from . import properties
if bpy in locals():
import importlib
importlib.reload(utils)
class UIListActions(bpy.types.Operator):
bl_idname = "rlm.ui_list_action"
bl_label = 'List Actions'
bl_description = 'Add, delete or move renderlayer'
bl_options = {'REGISTER', 'UNDO'}
action: bpy.props.EnumProperty(
items=(
('ADD', "Add", ""),
('REMOVE', "Remove", ""),
('UP', "Up", ""),
('DOWN', "Down", ""),
),
)
def execute(self, context):
idx = context.scene.rlm.active_renderlayer_index
rlayers = context.scene.view_layers
# add new renderlayer
if self.action == 'ADD':
# add new renderlayer respectively viewlayer
rlayer = rlayers.new(name='View Layer')
# reset renderlayer order numbers from 0-max
utils.normalize_viewlayer_order()
# set renderlayer list index to new renderlayer
context.scene.rlm.active_renderlayer_index = len(rlayers)-1
# move renderlayer to end of the list
utils.move_viewlayer_order(rlayer)
# remove selected renderlayer
elif self.action == 'REMOVE':
if len(rlayers) > 1 and idx < len(rlayers):
# get selected renderlayer order
rl_order = context.view_layer.order
# remove selected renderlayer
selected_renderlayer = rlayers[idx]
rlayers.remove(selected_renderlayer)
# reset renderlayer order numbers from 0-max
utils.normalize_viewlayer_order()
# check if saved order value is not greater than number of all renderlayers now
if rl_order >= len(rlayers):
rl_order-=1
# get new rlayer and its index with the order number
rlayer = [x for x in context.scene.view_layers if x.order == rl_order][0]
rl_idx = context.scene.view_layers[:].index(rlayer)
# set scene and renderlayer list to new renderlayer respectively viewlayer
context.scene.rlm.active_renderlayer_index = rl_idx
context.window.view_layer = rlayer
# move renderlayer up or down in list
else:
utils.move_viewlayer_order(rlayers[idx], self.action)
return {'FINISHED'}
class AutoRenameRenderlayer(bpy.types.Operator):
bl_idname = "rlm.auto_rename_renderlayer"
bl_label = 'Auto-Rename'
bl_description = 'Auto-rename renderlayer'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
rlayers = context.scene.view_layers
alpha = list(string.ascii_lowercase)
utils.normalize_viewlayer_order()
for rl in rlayers:
if rl.name.startswith('rl_'):
name = rl.name[5:]
else:
name = rl.name
x = rl.order if rl.order < 26 else 25
rl.name = 'rl_{}_{}'.format(alpha[x], name)
return {'FINISHED'}
class RandomColorRenderlayer(bpy.types.Operator):
bl_idname = "rlm.random_color_renderlayer"
bl_label = 'Random Color'
bl_description = 'Set random color per renderlayer'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
def hex_to_rgb(h):
return tuple(int(h[i:i+2], 16)/255 for i in (0, 2, 4))
addon_prefs = context.preferences.addons[__package__].preferences
view_layers = sorted(context.scene.view_layers.keys())
step = 1/(len(context.scene.view_layers))
x = 0
for rl_name in view_layers:
rl = context.scene.view_layers[rl_name]
if addon_prefs.color_palette == 'STANDARD':
h = x
s = 0.7
v = 0.36
rl.color = colorsys.hsv_to_rgb(h, s, v)
x += step
elif addon_prefs.color_palette == 'IBM':
hex_colors = ['648FFF', '785EF0', 'DC267F', 'FE6100', 'FFB000']
i = int(x%(len(hex_colors)))
rl.color = hex_to_rgb(hex_colors[i])
x += 1
elif addon_prefs.color_palette == 'WONG':
hex_colors = ['000000', 'E69F00', '56B4E9', '009E73', 'F0E442', '0072B2', 'D55E00', 'CC79A7']
i = int(x%(len(hex_colors)))
rl.color = hex_to_rgb(hex_colors[i])
x += 1
elif addon_prefs.color_palette == 'TOL':
hex_colors = ['332288', '117733', '44AA99', '88CCEE', 'DDCC77', 'CC6677', 'AA4499', '882255']
i = int(x%(len(hex_colors)))
rl.color = hex_to_rgb(hex_colors[i])
x += 1
return {'FINISHED'}
class EditRenderlayerSettings(bpy.types.Operator):
bl_idname = 'rlm.edit_renderlayer_settings'
bl_label = 'Edit Renderlayer Settings'
bl_description = 'Copy and paste renderlayer settings to other renderlayer'
bl_options = {'REGISTER', 'UNDO'}
action: bpy.props.EnumProperty(
items=(
('COPY', "Copy", ""),
('PASTE', "Paste", ""),
('COPY_TO_ALL', "Copy to All", ""),
('ADD_WP_LAYER', 'Add World Position Layer', ''),
),
)
SETTINGS = {}
CYCLES_SETTINGS = {}
EEVEE_SETTINGS = {}
FILE_FORMAT_SETTINGS = {}
EXCLUDE_SETTINGS = utils.exclude_settings()
PASSES = {}
def execute(self, context):
def copy_settings(view_layer):
self.SETTINGS.clear()
self.CYCLES_SETTINGS.clear()
self.EEVEE_SETTINGS.clear()
self.FILE_FORMAT_SETTINGS.clear()
self.PASSES.clear()
for prop in default_props:
self.SETTINGS[prop.identifier] = getattr(view_layer, prop.identifier)
for prop in cycles_props:
self.CYCLES_SETTINGS[prop.identifier] = getattr(view_layer.cycles, prop.identifier)
for prop in eevee_props:
self.EEVEE_SETTINGS[prop.identifier] = getattr(view_layer.eevee, prop.identifier)
for prop in file_format_props:
self.FILE_FORMAT_SETTINGS[prop.identifier] = getattr(view_layer.image_settings, prop.identifier)
for rl_pass in view_layer.passes:
self.PASSES[rl_pass.name] = {}
self.PASSES[rl_pass.name]['image_settings'] = {}
rl_pass_props = [x for x in rl_pass.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
rl_pass_file_format_props = [x for x in rl_pass.image_settings.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
for prop in rl_pass_props:
self.PASSES[rl_pass.name][prop.identifier] = getattr(rl_pass, prop.identifier)
for prop in rl_pass_file_format_props:
self.PASSES[rl_pass.name]['image_settings'][prop.identifier] = getattr(rl_pass.image_settings, prop.identifier)
self.PASSES['Image'] = {}
self.PASSES['Image']['image_settings'] = {}
image_props = [x for x in view_layer.image.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
image_file_format_props = [x for x in view_layer.image.image_settings.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
for prop in image_props:
self.PASSES['Image'][prop.identifier] = getattr(view_layer.image, prop.identifier)
for prop in image_file_format_props:
self.PASSES['Image']['image_settings'][prop.identifier] = getattr(view_layer.image.image_settings, prop.identifier)
def paste_settings(view_layer):
for key in [*self.SETTINGS]:
if key in self.EXCLUDE_SETTINGS:
continue
setattr(view_layer, key, self.SETTINGS[key])
for key in [*self.CYCLES_SETTINGS]:
if key in self.EXCLUDE_SETTINGS:
continue
setattr(view_layer.cycles, key, self.CYCLES_SETTINGS[key])
for key in [*self.EEVEE_SETTINGS]:
if key in self.EXCLUDE_SETTINGS:
continue
setattr(view_layer.eevee, key, self.EEVEE_SETTINGS[key])
for key in [*self.FILE_FORMAT_SETTINGS]:
if key in self.EXCLUDE_SETTINGS:
continue
setattr(view_layer.image_settings, key, self.FILE_FORMAT_SETTINGS[key])
view_layer.passes.clear()
for rl_pass in [*self.PASSES]:
if rl_pass == 'Image':
continue
i = view_layer.passes.add()
i.name = key
for key in [*self.PASSES[rl_pass]]:
if key == 'image_settings':
continue
setattr(i, key, self.PASSES[rl_pass][key])
for key in [*self.PASSES[rl_pass]['image_settings']]:
setattr(i.image_settings, key, self.PASSES[rl_pass]['image_settings'][key])
for key in [*self.PASSES['Image']]:
if key == 'image_settings':
continue
setattr(view_layer.image, key, self.PASSES['Image'][key])
for key in [*self.PASSES[rl_pass]['image_settings']]:
setattr(view_layer.image.image_settings, key, self.PASSES['Image']['image_settings'][key])
view_layers = context.scene.view_layers
active_renderlayer_idx = context.scene.rlm.active_renderlayer_index
view_layer = view_layers[active_renderlayer_idx]
default_props = [x for x in view_layer.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
cycles_props = [x for x in view_layer.cycles.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
eevee_props = [x for x in view_layer.eevee.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
file_format_props = [x for x in view_layer.image_settings.bl_rna.properties if x.identifier not in {'rna_type'} and not x.is_readonly]
if self.action == 'COPY':
utils.set_renderpasses(view_layer)
self.report({'INFO'}, 'Copy Settings!')
copy_settings(view_layer)
elif self.action == 'PASTE':
if not self.SETTINGS or not self.CYCLES_SETTINGS or not self.EEVEE_SETTINGS:
self.report({'ERROR'}, 'No Settings in Copy Buffer!')
return {'CANCELLED'}
self.report({'INFO'}, 'Paste Settings!')
paste_settings(view_layer)
elif self.action == 'COPY_TO_ALL':
self.report({'INFO'}, 'Paste Settings to all Layer!')
copy_settings(view_layer)
for layer in [layer for layer in view_layers if layer.use]:
paste_settings(layer)
return {'FINISHED'}
class EditRenderlayerPasses(bpy.types.Operator):
bl_idname = 'rlm.edit_renderlayer_passes'
bl_label = 'Edit Renderlayer Passes'
bl_description = 'Add and remove renderlayer passes'
bl_options = {'REGISTER', 'UNDO'}
action: bpy.props.EnumProperty(
items=(
('ADD', "Add", ""),
('REMOVE', "Remove", ""),
('REMOVE_MULTI', "Remove Multi", ""),
('ADD_ALL', "Add All", ""),
('REMOVE_ALL', "Remove All", ""),
),
)
pass_index: bpy.props.IntProperty(
default=0,
)
def execute(self, context):
addon_prefs = context.preferences.addons[__package__].preferences
rlayer = context.view_layer
if self.action == 'ADD':
rl_passes = [x for x in addon_prefs.output_passes if x.selected]
for rl_pass in rl_passes:
rl_pass.selected = False
if rl_pass.name in rlayer.passes:
continue
i = rlayer.passes.add()
i.name = rl_pass.name
i.custom_name = rl_pass.custom_name
if self.action == 'REMOVE':
rlayer.passes.remove(self.pass_index)
if self.action == 'REMOVE_MULTI':
rl_passes = [x.name for x in rlayer.passes if x.selected]
for rl_pass in rl_passes:
if rl_pass == 'Image':
continue
idx = rlayer.passes.keys().index(rl_pass)
rlayer.passes.remove(idx)
if self.action == 'ADD_ALL':
for rl_pass in addon_prefs.output_passes:
rl_pass.selected = False
if rl_pass.name in rlayer.passes.keys() or rl_pass.name == 'Image':
continue
i = rlayer.passes.add()
i.name = rl_pass.name
i.custom_name = rl_pass.custom_name
if self.action == 'REMOVE_ALL':
rlayer.passes.clear()
utils.set_renderpasses(context.view_layer)
return {'FINISHED'}
class AddViewlayerFromCollections(bpy.types.Operator):
bl_idname = 'rlm.add_viewlayer_from_collections'
bl_label = 'Add Viewlayer from Collections'
bl_description = 'For every collection below the Scene Collection add a viewlayer with the same name'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
cols = [x.name for x in context.scene.collection.children]
rlayers = context.scene.view_layers
for col in cols:
if col in [x.name for x in rlayers]:
continue
rlayer = rlayers.new(name=col)
context.scene.rlm.active_renderlayer_index = len(rlayers)-1
utils.move_viewlayer_order(rlayer)
return {'FINISHED'}
class RenderlayerTokens(bpy.types.Operator):
bl_idname = 'rlm.renderlayer_tokens'
bl_label = 'Tokens'
bl_description = 'Add dynamic tokens to the output file name or reset it'
bl_options = {'REGISTER', 'UNDO'}
token: bpy.props.EnumProperty(
items=utils.list_tokens(),
)
def execute(self, context):
if self.token != 'CLEAR':
context.scene.rlm.output_name += str(self.token)
else:
context.scene.rlm.output_name = ''
return {'FINISHED'}
class CreateNodes(bpy.types.Operator):
bl_idname = 'rlm.create_nodes'
bl_label = 'Create Nodes'
bl_description = 'Create renderlayer and file output nodes'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
excluded_file_formats = utils.list_excluded_file_formats()
if context.scene.render.image_settings.file_format in excluded_file_formats:
return False
return True
def execute(self, context):
def _merge_layers(input_1, input_2):
merge = nodes.new('CompositorNodeAlphaOver')
node_tree.links.new(input_1, merge.inputs[1])
node_tree.links.new(input_2, merge.inputs[2])
merge.name = merge.inputs[2].links[0].from_node.label + '_AlphaOver-RLM'
merge.hide = True
if context.scene.rlm.include_all_scenes:
merge.parent = frame
return merge
def _add_renderlayer_nodes():
rlayers = []
offset = Vector((0, 0))
for view_layer in sorted(view_layers, key=lambda x: x.order):
node_name = '{}-{}-RLM'.format(scn.name, view_layer.name)
if node_name in existing_nodes_by_name:
continue
utils.set_renderpasses(view_layer)
rlayer = nodes.new('CompositorNodeRLayers')
rlayer.name = node_name
rlayer.label = view_layer.name
rlayer.scene = scn
rlayer.layer = view_layer.name
rlayer.use_custom_color = True
rlayer.color = view_layer.color
rlayer.location = offset
# respect view layer render toggle by muting the node branch
rlayer.mute = not view_layer.use
outputs = [x for x in rlayer.outputs if x.enabled]
offset += Vector((0, -480)) + Vector((0, -(len(outputs)-1)*20))
rlayers.append(rlayer)
return rlayers
def _add_file_output_item(output, view_layer, file_output):
rl_passes = view_layer.passes
addon_prefs = bpy.context.preferences.addons[__package__].preferences
# get custom output name and image file format settings
item_name = output.name
number = None
if item_name.startswith('Crypto'):
if context.scene.rlm.use_cryptomatte_multilayer_exr:
return
item_name = output.name[:-2]
number = output.name[-2:]
if item_name == 'Image':
img_settings = utils.get_image_format_settings(view_layer.image)
item_name = view_layer.image.custom_name
elif item_name in rl_passes.keys():
img_settings = utils.get_image_format_settings(rl_passes[item_name])
item_name = rl_passes[item_name].custom_name
if number:
item_name += number
else:
img_settings = {'file_format': '---'}
if item_name.startswith('Combined_'):
item_name = item_name.replace('Combined', addon_prefs.output_passes['Lightgroup'].custom_name)
else:
item_name = output.name
# add new file output items
item = file_output.file_output_items.new("RGBA", item_name)
# change image file format for data passes if use exr for data passes is enabled
if context.scene.rlm.use_exr_for_data_pass and output.name in utils.list_data_passes():
item.override_node_format = True
item.format.file_format = 'OPEN_EXR'
item.format.color_mode = addon_prefs.exr_color_mode
item.format.color_depth = addon_prefs.exr_color_depth
item.format.exr_codec = addon_prefs.exr_codec
return
# set image file format settings
if img_settings['file_format'] != '---':
item.override_node_format = True
utils.set_image_format_settings(item, img_settings, item=True)
# do not apply display transform on data aovs
if item_name in view_layer.aovs.keys():
if view_layer.aovs[output.name].type != 'COLOR':
item.save_as_render = False
return
def _connect_file_output_node(rlayer, file_output):
view_layer = view_layers[rlayer.name.split('-')[-2]]
rl_passes = view_layer.passes
addon_prefs = context.preferences.addons[__package__].preferences
for output in [x for x in rlayer.outputs if x.enabled]:
if file_output.format.file_format in ['CINEON', 'IRIS', 'DPX'] and output.name == 'Alpha':
continue
# get custom output name
output_name = output.name
number = None
if output.name.startswith('Crypto'):
output_name = output.name[:-2]
number = output.name[-2:]
if output_name == 'Image':
custom_name = view_layer.image.custom_name
elif output_name in rl_passes.keys():
custom_name = rl_passes[output_name].custom_name
if number:
custom_name += number
else:
if output_name.startswith('Combined_'):
custom_name = output_name.replace('Combined', addon_prefs.output_passes['Lightgroup'].custom_name)
else:
custom_name = output_name
# link renderlayer and file output node and set file output item socket type
if custom_name in file_output.inputs:
socket_type = output.type
file_output.file_output_items[custom_name].socket_type = socket_type if socket_type != "VALUE" else "FLOAT"
should_denoise = False
if context.scene.render.engine == 'CYCLES':
if output_name in rl_passes.keys():
match = rl_passes[output_name]
should_denoise = getattr(match, "denoise", False)
elif output_name.startswith("Combined_"):
lightgroup_name = output_name.replace("Combined_", "")
if hasattr(view_layer, "lightgroup_settings"):
for lg in view_layer.lightgroup_settings:
if lg.name == lightgroup_name and getattr(lg, "denoise", False):
should_denoise = True
break
if should_denoise:
denoiser_node = nodes.new("CompositorNodeDenoise")
denoiser_node.location = rlayer.location + Vector((300, 0))
denoiser_node.name = f"{rlayer.scene.name}-{rlayer.layer}_Denoise-RLM"
node_tree.links.new(output, denoiser_node.inputs["Image"])
if "Denoising Normal" in rlayer.outputs:
node_tree.links.new(rlayer.outputs["Denoising Normal"], denoiser_node.inputs["Normal"])
elif "Normal" in rlayer.outputs:
node_tree.links.new(rlayer.outputs["Normal"], denoiser_node.inputs["Normal"])
if "Denoising Albedo" in rlayer.outputs:
node_tree.links.new(rlayer.outputs["Denoising Albedo"], denoiser_node.inputs["Albedo"])
elif "Albedo" in rlayer.outputs:
node_tree.links.new(rlayer.outputs["Albedo"], denoiser_node.inputs["Albedo"])
node_tree.links.new(denoiser_node.outputs["Image"], file_output.inputs[custom_name])
else:
node_tree.links.new(output, file_output.inputs[custom_name])
def _add_file_output_nodes():
file_outputs = []
for rlayer in rlayers:
file_output = nodes.new('CompositorNodeOutputFile')
file_output.name = rlayer.name.replace('-RLM', '_FileOutput-RLM')
file_output.label = rlayer.label + ' FileOutput'
file_output.use_custom_color = True
file_output.color = rlayer.color
file_output.width = 500
position = rlayer.location
file_output.location = position + Vector((400,0))
# add file output items for every renderpass
view_layer = view_layers[rlayer.name.split('-')[-2]]
# mute file output node when view layer is disabled
file_output.mute = not view_layer.use
rl_passes = view_layer.passes
for output in [x for x in rlayer.outputs if x.enabled and x.name != 'Deprecated']:
if output.name == 'Alpha' and 'Alpha' not in rl_passes.keys():
continue
if output.name == 'Noisy Image' and 'Noisy Image' not in rl_passes.keys():
continue
if output.name == 'Noisy Shadow Catcher' and 'Noisy Shadow Catcher' not in rl_passes.keys():
continue
_add_file_output_item(output, view_layer, file_output)
# connect all file output items with renderlayer node outputs
_connect_file_output_node(rlayer, file_output)
# change file format if override is enabled
if view_layer.override_file_format and view_layer.image_settings.file_format != '---':
img_settings = utils.get_image_format_settings(view_layer)
else:
img_settings = utils.get_image_format_settings(context.scene.render)
utils.set_image_format_settings(file_output, img_settings, item=False)
file_outputs.append(file_output)
return file_outputs
def _add_crypto_file_output_nodes():
addon_prefs = bpy.context.preferences.addons[__package__].preferences
file_outputs = []
for rlayer in rlayers:
# check if crypto outputs exist
crypto_outputs = [x for x in rlayer.outputs if x.enabled and x.name.startswith('Crypto')]
if not crypto_outputs:
continue
# add crypto file output node
file_output = nodes.new('CompositorNodeOutputFile')
file_output.name = rlayer.name.replace('-RLM', '_Crypto_FileOutput-RLM')
file_output.label = rlayer.label + ' Cryptomatte FileOutput'
file_output.use_custom_color = True
file_output.color = rlayer.color
file_output.width = 500
position = rlayer.location
file_output.location += Vector((1000, position[1]))
# set file format to multilayer exr
file_output.format.file_format = 'OPEN_EXR_MULTILAYER'
file_output.format.color_mode = addon_prefs.crypto_color_mode
file_output.format.color_depth = addon_prefs.crypto_color_depth
file_output.format.exr_codec = addon_prefs.crypto_codec
# add file output items and connect them with renderlayer node
view_layer = view_layers[rlayer.name.split('-')[-2]]
# mute crypto file output node when view layer is disabled
file_output.mute = not view_layer.use
beauty_output = rlayer.outputs[0]
file_output.file_output_items.new("RGBA", view_layer.image.custom_name)
node_tree.links.new(beauty_output, file_output.inputs[view_layer.image.custom_name])
for output in crypto_outputs:
inputs = [x.name for x in file_output.inputs]
custom_name = view_layer.passes[output.name[:-2]].custom_name
if custom_name not in inputs:
file_output.file_output_items.new("RGBA", custom_name)
node_tree.links.new(beauty_output, file_output.inputs[custom_name])
name = custom_name + output.name[-2:]
file_output.file_output_items.new("RGBA", name)
node_tree.links.new(output, file_output.inputs[name])
file_outputs.append(file_output)
return file_outputs
node_tree = utils.get_or_create_rlm_node_group(context.scene)
context.scene.compositing_node_group = node_tree
scenes = []
if context.scene.rlm.include_all_scenes:
scenes = bpy.data.scenes
else:
scenes.append(context.scene)
nodes = node_tree.nodes
root = Vector((0, 0))
existing_nodes_by_name = {node.name: node for node in nodes}
if nodes:
try:
max_x = max(node.location.x for node in nodes)
step = 1900 if context.scene.rlm.use_cryptomatte_multilayer_exr else 1200
root = Vector((max_x + step, 0))
except Exception:
root = Vector((0, 0))
addon_prefs = context.preferences.addons[__package__].preferences
# fix scene and viewlayer names
for scn in scenes:
scn.name = scn.name.replace('-', '_')
for view_layer in scn.view_layers:
view_layer.name = view_layer.name.replace('-', '_')
for scn in scenes:
try:
view_layers = scn.view_layers
rlayers = _add_renderlayer_nodes()
file_outputs = _add_file_output_nodes()
if context.scene.rlm.use_cryptomatte_multilayer_exr:
file_outputs.extend(_add_crypto_file_output_nodes())
for node in file_outputs:
node.select = True
utils.set_output_paths(context, selection=True)
if context.scene.rlm.include_all_scenes:
frame = nodes.new('NodeFrame')
frame.name = '{}-Frame-RLM'.format(scn.name)
frame.label = scn.name
for rlayer in rlayers:
rlayer.parent = frame
rlayer.location += root
for file_output in file_outputs:
file_output.parent = frame
file_output.location += root
comp_layers = sorted(
[rlayer for rlayer in rlayers if view_layers[rlayer.layer].composite],
key=lambda x: view_layers[x.layer].order,
)
version = 'v%02d' % context.scene.rlm.version
scene = scn.name
output_1 = None
offset = root + Vector((400, len(comp_layers) * 50))
if comp_layers:
output_1 = comp_layers[0].outputs[0]
for rlayer in comp_layers[1:]:
output_2 = rlayer.outputs[0]
merge = _merge_layers(output_1, output_2)
merge.location += offset
offset -= Vector((0, 70))
output_1 = merge.outputs[0]
if output_1:
viewer_node = nodes.new('CompositorNodeViewer')
viewer_node.name = '{}-Viewer-RLM'.format(scn.name)
viewer_node.location = root + Vector((600, 200))
node_tree.links.new(output_1, viewer_node.inputs[0])
viewer_node.hide = True
comp_file_output = nodes.new('CompositorNodeOutputFile')
comp_file_output.name = '{}-Composite_FileOutput-RLM'.format(scn.name)
comp_file_output.label = 'Composite FileOuput'
img_settings = utils.get_image_format_settings(context.scene.render)
utils.set_image_format_settings(comp_file_output, img_settings, item=False)
comp_file_output.directory = context.scene.rlm.output_dir
comp_path = f'comp/{version}/'
if context.scene.rlm.include_all_scenes:
comp_path = f'comp/{scene}/{version}/'
if context.scene.render.image_settings.media_type != 'IMAGE':
comp_path += f'Image_####'
comp_file_output.file_name = comp_path
item = comp_file_output.file_output_items.new("RGBA", 'Image')
if context.scene.render.image_settings.media_type == 'IMAGE':
item.name += '_####'
comp_file_output.location = viewer_node.location + Vector((0, 200))
comp_file_output.width = 500
node_tree.links.new(output_1, comp_file_output.inputs[0])
if context.scene.rlm.include_all_scenes:
viewer_node.parent = frame
comp_file_output.parent = frame
if context.scene.rlm.use_cryptomatte_multilayer_exr:
root += Vector((1900, 0))
else:
root += Vector((1200, 0))
except Exception:
continue
# save current viewlayer settings
utils.get_viewlayer_settings(context, utils.VIEWLAYER_SETTINGS)
return {'FINISHED'}
class UpdateOutputPaths(bpy.types.Operator):
bl_idname = 'rlm.set_output_paths'
bl_label = 'Update Selected File Outputs'
bl_description = 'Update paths for selected file output nodes'
bl_options = {'REGISTER', 'UNDO'}
selection: BoolProperty(name='Selection', default=True)
@classmethod
def poll(cls, context):
node_tree = getattr(context.scene, "compositing_node_group", None)
if node_tree not in bpy.data.node_groups.values():
groups = utils.get_rlm_node_groups()
if not groups:
return False
node_tree = groups[0]
if not context.scene.rlm.auto_update_paths:
nodes = [node for node in node_tree.nodes if node.select]
if not nodes:
return False
return True
def execute(self, context):
utils.set_output_paths(context, selection=self.selection)
return {'FINISHED'}
class SetActiveViewlayer(bpy.types.Operator):
bl_idname = 'rlm.set_active_viewlayer'
bl_label = 'Set Active Viewlayer'
bl_description = 'Set active viewlayer in UIList'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
active_viewlayer = context.view_layer
context.scene.rlm.active_renderlayer_index = context.scene.view_layers.values().index(active_viewlayer)
return {'FINISHED'}
class SetRenderpasses(bpy.types.Operator):
bl_idname = 'rlm.set_renderpasses'
bl_label = 'Set Renderpasses'
bl_description = 'Enable renderpasses from active renderlayer passes list for the active viewlayer'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
utils.set_renderpasses(context.view_layer)
return {'FINISHED'}
def sync_viewlayer_mute_nodes(scene=None):
if scene is None:
scene = bpy.context.scene
node_tree = getattr(scene, "compositing_node_group", None)
if node_tree not in bpy.data.node_groups.values():
groups = utils.get_rlm_node_groups()
if not groups:
return False
node_tree = groups[0]
for node in node_tree.nodes:
if node.type == 'R_LAYERS':
scn = node.scene
if not scn:
continue
view_layer = scn.view_layers.get(node.layer)
if not view_layer:
continue
node.mute = not view_layer.use
for node in node_tree.nodes:
if node.type == 'OUTPUT_FILE' and node.name.endswith('-RLM'):
base_name = node.name
base_name = base_name.replace('_FileOutput-RLM', '')
base_name = base_name.replace('_Crypto_FileOutput-RLM', '')
parts = base_name.split('-')
if len(parts) < 2:
continue
scene_name = '-'.join(parts[:-1])
view_layer_name = parts[-1]
scn = bpy.data.scenes.get(scene_name)
if not scn:
continue
view_layer = scn.view_layers.get(view_layer_name)
if not view_layer:
continue
node.mute = not view_layer.use
return True
class SyncViewlayerMute(bpy.types.Operator):
bl_idname = 'rlm.sync_viewlayer_mute'
bl_label = 'Sync Viewlayer Mute'
bl_description = 'Update mute state of nodes to match viewlayer camera toggles'
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
node_tree = getattr(context.scene, "compositing_node_group", None)
if node_tree in bpy.data.node_groups.values():
return True
groups = utils.get_rlm_node_groups()
return bool(groups)
def execute(self, context):
if not sync_viewlayer_mute_nodes():
return {'CANCELLED'}
return {'FINISHED'}
class ToggleViewlayerMute(bpy.types.Operator):
bl_idname = 'rlm.toggle_viewlayer_mute'
bl_label = 'Toggle Viewlayer Mute'
bl_description = 'Toggle viewlayer render and sync node mute state'
bl_options = {'REGISTER', 'UNDO'}
view_layer_name: bpy.props.StringProperty()
def execute(self, context):
scene = context.scene
view_layer = scene.view_layers.get(self.view_layer_name)
if not view_layer:
return {'CANCELLED'}
view_layer.use = not view_layer.use
sync_viewlayer_mute_nodes(scene)
return {'FINISHED'}
class AutoConnectDenoiserPasses(bpy.types.Operator):
bl_idname = 'rlm.auto_connect_denoiser_passes'
bl_label = 'Auto Connect Denoiser Passes'
bl_description = 'Automatically connect denoiser to render passes with Normal and Albedo inputs'
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
scene = context.scene
if not scene.use_nodes:
scene.use_nodes = True
node_tree = scene.node_tree
denoiser_node = None
for node in node_tree.nodes:
if node.type == 'DENOISE':
denoiser_node = node
break
if not denoiser_node:
denoiser_node = node_tree.nodes.new('CompositorNodeDenoise')
denoiser_node.location = (300, 0)
render_layers_node = None
for node in node_tree.nodes:
if node.type == 'R_LAYERS':
render_layers_node = node
break
if not render_layers_node:
render_layers_node = node_tree.nodes.new('CompositorNodeRLayers')
render_layers_node.location = (0, 0)
for input_socket in denoiser_node.inputs:
if input_socket.links:
for link in list(input_socket.links):
node_tree.links.remove(link)
view_layer = context.view_layer
denoiser_passes = getattr(view_layer, "denoiser_passes", 'DIFFDIR')
connected_passes = []
if denoiser_passes == 'DIFFDIR':
if 'DiffDir' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['DiffDir'], denoiser_node.inputs['Image'])
connected_passes.append('DiffDir')
else:
self.report({'WARNING'}, 'DiffDir pass not found in render layers')
return {'CANCELLED'}
elif denoiser_passes == 'DIFFIND':
if 'DiffInd' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['DiffInd'], denoiser_node.inputs['Image'])
connected_passes.append('DiffInd')
else:
self.report({'WARNING'}, 'DiffInd pass not found in render layers')
return {'CANCELLED'}
elif denoiser_passes == 'BOTH':
self.report({'WARNING'}, 'Cannot connect both passes simultaneously to single denoiser. Please select one pass.')
return {'CANCELLED'}
if 'Denoising Normal' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['Denoising Normal'], denoiser_node.inputs['Normal'])
elif 'Normal' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['Normal'], denoiser_node.inputs['Normal'])
else:
self.report({'WARNING'}, 'Normal pass not found - denoising quality may be reduced')
if 'Denoising Albedo' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['Denoising Albedo'], denoiser_node.inputs['Albedo'])
elif 'Albedo' in render_layers_node.outputs:
node_tree.links.new(render_layers_node.outputs['Albedo'], denoiser_node.inputs['Albedo'])
else:
self.report({'WARNING'}, 'Albedo pass not found - denoising quality may be reduced')
file_output_nodes = [node for node in node_tree.nodes if node.type == 'OUTPUT_FILE']
for file_output in file_output_nodes:
for input_socket in file_output.inputs:
if input_socket.links:
for link in list(input_socket.links):
if link.from_socket is None:
continue
socket_name = None
if hasattr(link.from_socket, 'name'):
socket_name = link.from_socket.name
elif hasattr(link.from_socket, 'identifier'):
socket_name = link.from_socket.identifier
if (
link.from_node == render_layers_node
and socket_name
and socket_name in connected_passes
):
node_tree.links.remove(link)
node_tree.links.new(denoiser_node.outputs['Image'], input_socket)
break
self.report({'INFO'}, f'Denoiser connected to {denoiser_passes} with Normal/Albedo, output to same export path')
return {'FINISHED'}
class AddLightgroupOperator(bpy.types.Operator):
bl_idname = "rlm.add_lightgroup"
bl_label = "Add Lightgroup"
bl_description = "Add a new lightgroup with denoise option"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
rlayer = context.view_layer
new_lg = rlayer.lightgroup_settings.add()
new_lg.name = f"Lightgroup_{len(rlayer.lightgroup_settings)}"
new_lg.denoise = True
new_lg.file_format = '---'
rlayer.rlm_active_lightgroup_index = len(rlayer.lightgroup_settings) - 1
try:
utils.set_cycles_lightgroups_from_settings(rlayer)
except Exception:
pass
for area in context.window.screen.areas:
if area.type in {'NODE_EDITOR', 'PROPERTIES'}:
area.tag_redraw()
for region in context.area.regions:
if region.type == 'UI':
region.tag_redraw()
return {'FINISHED'}
class RemoveLightgroupOperator(bpy.types.Operator):
bl_idname = "rlm.remove_lightgroup"
bl_label = "Remove Lightgroup"
bl_description = "Remove the selected lightgroup"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
rlayer = context.view_layer
idx = rlayer.rlm_active_lightgroup_index
if 0 <= idx < len(rlayer.lightgroup_settings):
name = rlayer.lightgroup_settings[idx].name
rlayer.lightgroup_settings.remove(idx)
rlayer.rlm_active_lightgroup_index = max(0, idx - 1)
try:
if hasattr(rlayer, 'lightgroups') and name:
if name in rlayer.lightgroups:
if hasattr(rlayer.lightgroups, 'remove'):
rlayer.lightgroups.remove(rlayer.lightgroups[name])
elif hasattr(rlayer.lightgroups, 'clear'):
rlayer.lightgroups.clear()
except Exception:
pass
try:
utils.set_cycles_lightgroups_from_settings(rlayer)
except Exception:
pass
for area in context.window.screen.areas:
if area.type in {'NODE_EDITOR', 'PROPERTIES'}:
area.tag_redraw()
for region in context.area.regions:
if region.type == 'UI':
region.tag_redraw()
return {'FINISHED'}
class SyncLightgroupsAddUsed(bpy.types.Operator):
bl_idname = "rlm.sync_lightgroups_add_used"
bl_label = "Add Used Lightgroups"
bl_description = "Add entries for lightgroups present in the ViewLayer"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
rlayer = context.view_layer
if not hasattr(rlayer, 'lightgroups'):
return {'CANCELLED'}
existing = {lg.name for lg in rlayer.lightgroup_settings}
for lg_name in rlayer.lightgroups.keys():
if lg_name not in existing:
new_lg = rlayer.lightgroup_settings.add()
new_lg.name = lg_name
new_lg.denoise = True
new_lg.file_format = '---'
for area in context.window.screen.areas:
if area.type == 'NODE_EDITOR':