import bpy import re from bpy.types import Operator from bpy.props import (StringProperty, BoolProperty, ) # region renumber outputs def delete_numbering(fo): # padding=3 '''Delete prefix numbering on all slots on passed file output''' if fo.type != 'OUTPUT_FILE': return if fo.format.file_format == 'OPEN_EXR_MULTILAYER': slots = fo.layer_slots field_attr = 'name' else: slots = fo.file_slots field_attr = 'path' for fs in slots: elems = getattr(fs, field_attr).split('/') for i, e in enumerate(elems): elems[i] = re.sub(r'^\d{3}_', '', e) new = '/'.join(elems) setattr(fs, field_attr, new) ## Add - Insert prefix incrementation in fileoutput def get_numbered_output(out, slot_name): '''Return output slot name without looking for numbering ???_ ''' pattern = r'^(?:\d{3}_)?' # optional non capture group of 3 digits + _ pattern = f'{pattern}{slot_name}' for inp in out.inputs: if re.match(pattern, inp.name): return inp def add_fileslot_number(fs, number): field_attr = 'name' if hasattr(fs, 'name') else 'path' elems = getattr(fs, field_attr).split('/') for i, e in enumerate(elems): if re.match(r'^\d{3}_', e): elems[i] = re.sub(r'^(\d{3})', lambda x: str(number).zfill(3), e) else: elems[i] = f'{str(number).zfill(3)}_{e}' new = '/'.join(elems) setattr(fs, field_attr, new) return new def renumber(fo, offset=10): '''Force renumber all the slots with a 3''' if fo.type != 'OUTPUT_FILE': return ct = 10 # start at 10 slots = fo.layer_slots if fo.format.file_format == 'OPEN_EXR_MULTILAYER' else fo.file_slots for fs in slots: add_fileslot_number(fs, ct) ct += offset def get_num(string) -> int: '''get a tring or a file_slot object return leading number or None ''' if not isinstance(string, str): if hasattr(string, 'path'): string = string.path else: string = string.name num = re.search(r'^(\d{3})_', string) if num: return int(num.group(1)) def reverse_fileout_inputs(fo): count = len(fo.inputs) for i in range(count): fo.inputs.move(count-1, i) def renumber_keep_existing(fo, offset=10, invert=True): '''Renumber by keeping existing numbers and inserting new one whenever possible Big and ugly function that do the trick nonetheless... ''' if fo.type != 'OUTPUT_FILE': return ct = 10 if invert: reverse_fileout_inputs(fo) fsl = fo.layer_slots if fo.format.file_format == 'OPEN_EXR_MULTILAYER' else fo.file_slots last_idx = len(fsl) - 1 prev = None prev_num = None for idx, fs in enumerate(fsl): # print('-->', idx, fs.path) if idx == last_idx: # handle last if get_num(fs) is not None: break if idx > 0: prev = fsl[idx-1] num = get_num(prev) if num is not None: add_fileslot_number(fs, num + offset) else: add_fileslot_number(fs, ct) else: add_fileslot_number(fs, 10) # there is only one slot (maybe don't number ?) break # update the ct with the current taken number if any number = get_num(fs) if number is not None: prev = fs ct = number + offset continue # skip already numbered # analyse all next slots until there is numbered divider = 0 # print(f'range(1, {len(fsl) - idx}') for i in range(1, len(fsl) - idx): next_num = get_num(fsl[idx + i]) if next_num is not None: divider = i+1 break if idx == 0: # handle first prev_num = 0 prev = None if next_num is None: add_fileslot_number(fs, 0) elif next_num == 0: print(f'Cannot insert value before 0 to {fsl.path}') continue else: add_fileslot_number(fs, int(next_num / 2)) else: prev = fsl[idx-1] test_prev = get_num(prev) if test_prev is not None: prev_num = test_prev if not divider: if prev_num is not None: add_fileslot_number(fs, prev_num + offset) else: add_fileslot_number(fs, ct) else: if prev_num is not None: # iterate rename gap_inc = int((next_num - prev_num) / divider) if gap_inc < 1: # same values ! print(f'cannot insert a median value at {fs.path} between {prev_num} and {next_num}') continue ct = prev_num for temp_id in range(idx, idx+i): ct += gap_inc add_fileslot_number(fsl[temp_id], ct) else: print("what's going on ?\n") # first check if it has a number (if not bas) prev = fs ct += offset if invert: reverse_fileout_inputs(fo) class RT_OT_number_outputs(bpy.types.Operator): bl_idname = "rt.number_outputs" bl_label = "Number Outputs" bl_description = "(Re)Number the outputs to have ordered file by name in export directories\ \nCtrl+Clic : Delete numbering" bl_options = {"REGISTER"} @classmethod def poll(cls, context): return True mode : StringProperty(default='SELECTED', options={'SKIP_SAVE'}) clear : BoolProperty(default=False, options={'SKIP_SAVE'}) def invoke(self, context, event): # use clear with Ctrl + Click if event.ctrl: self.clear = True return self.execute(context) def execute(self, context): scn = context.scene ct = 0 nodes = scn.node_tree.nodes for fo in nodes: if fo.type != 'OUTPUT_FILE': continue if self.mode == 'SELECTED' and not fo.select: continue # print(f'numbering {fo.name}') ct += 1 if self.clear: delete_numbering(fo) else: renumber_keep_existing(fo) txt = 'de-numbered' if self.clear else 're-numbered' if ct: self.report({'INFO'}, f'{ct} output nodes {txt}') else: self.report({'ERROR'}, f'No output nodes {txt}') return {"FINISHED"} # region file output class RT_OT_mute_toggle_output_nodes(bpy.types.Operator): bl_idname = "rt.mute_toggle_output_nodes" bl_label = "Mute Toggle output nodes" bl_description = "Mute / Unmute all output nodes" bl_options = {"REGISTER"} mute : BoolProperty(default=True, options={'SKIP_SAVE'}) def execute(self, context): ct = 0 for n in context.scene.node_tree.nodes: if n.type != 'OUTPUT_FILE': continue n.mute = self.mute ct += 1 state = 'muted' if self.mute else 'unmuted' self.report({"INFO"}, f'{ct} nodes {state}') return {"FINISHED"} class RT_OT_set_output_node_format(bpy.types.Operator): bl_idname = "rt.set_output_node_format" bl_label = "Set output format from active" bl_description = "Change all selected output node to match active output node format" bl_options = {"REGISTER"} mute : BoolProperty(default=True, options={'SKIP_SAVE'}) def execute(self, context): nodes = context.scene.node_tree.nodes if not nodes.active or nodes.active.type != 'OUTPUT_FILE': self.report({"ERROR"}, f'Active node should be an output file to use as reference for output format') return {"CANCELLED"} ref = nodes.active # file_format = ref.format.file_format # color_mode = ref.format.color_mode # color_depth = ref.format.color_depth # compression = ref.format.compression ct = 0 for n in nodes: if n.type != 'OUTPUT_FILE' or n == ref or not n.select: continue for attr in dir(ref.format): if attr.startswith('__') or attr in {'rna_type','bl_rna', 'view_settings', 'display_settings','stereo_3d_format'}: # views_format continue try: setattr(n.format, attr, getattr(ref.format, attr)) except Exception as e: print(f"can't set attribute : {attr}") # n.format.file_format = file_format # n.format.color_mode = color_mode # n.format.color_depth = color_depth # n.format.compression = compression ct += 1 # state = 'muted' if self.mute else 'unmuted' self.report({"INFO"}, f'{ct} output format copied from {ref.name}') return {"FINISHED"} # region view layers class RT_OT_enable_all_viewlayers(bpy.types.Operator): bl_idname = "rt.enable_all_viewlayers" bl_label = "Enable All Viewlayers" bl_description = "Enable all View layers except those named 'exclude' 'View Layer'" bl_options = {"REGISTER"} def execute(self, context): scn = context.scene vl_list = [vl for vl in scn.view_layers if not vl.use and vl.name not in {'View Layer', 'exclude'}] for v in vl_list: v.use = True self.report({"INFO"}, f'{len(vl_list)} ViewLayers Reactivated') return {"FINISHED"} class RT_OT_activate_only_selected_layers(bpy.types.Operator): bl_idname = "rt.activate_only_selected_layers" bl_label = "Activate Only Selected Layers" bl_description = "Activate only selected node view layer , excluding all others" bl_options = {"REGISTER"} def execute(self, context): scn = context.scene nodes = scn.node_tree.nodes rlayers_nodes = [n for n in nodes if n.select and n.type == 'R_LAYERS'] vls = [scn.view_layers.get(n.layer) for n in rlayers_nodes if scn.view_layers.get(n.layer)] for v in scn.view_layers: v.use = v in vls self.report({"INFO"}, f'Now only {len(vls)} viewlayer active (/{len(scn.view_layers)})') return {"FINISHED"} classes = ( RT_OT_number_outputs, RT_OT_mute_toggle_output_nodes, RT_OT_set_output_node_format, RT_OT_enable_all_viewlayers, RT_OT_activate_only_selected_layers ) def register(): for cls in classes: bpy.utils.register_class(cls) def unregister(): for cls in reversed(classes): bpy.utils.unregister_class(cls)