render_toolbox/operators/output_management.py
pullusb b6b090d4ea Migrate Fileoutput and viewlayer management ops from gp_render
Better overall UI.
Split file output in a separate panel
2025-07-30 18:16:32 +02:00

393 lines
12 KiB
Python

import bpy
import re
from bpy.types import Operator
from bpy.props import (StringProperty,
BoolProperty,
)
from .. import fn
# 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(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(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(Operator):
bl_idname = "rt.set_output_node_format"
bl_label = "Set file output node format from active"
bl_description = "Change all selected file output node format to match active"
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"}
class RT_OT_set_active_file_output_slot_to_composite(bpy.types.Operator):
bl_idname = "rt.set_active_file_output_slot_to_composite"
bl_label = "Set Active File Output Slot To Composite"
bl_description = "Use active slot of active file output node to set scene output settings (swap connection)"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.scene.use_nodes\
and context.scene.node_tree\
and context.scene.node_tree.nodes.active\
and context.scene.node_tree.nodes.active.type == 'OUTPUT_FILE'
relink_composite : bpy.props.BoolProperty(
name='Relink Composite',
default=True,
description='In case file slot is linked, swap link to Composite file',
options={'SKIP_SAVE'},
)
def invoke(self, context, event):
self.fo = context.scene.node_tree.nodes.active
if not len(self.fo.file_slots):
self.report({'ERROR'}, 'no slots in active file output')
return {'CANCELLED'}
# check if active slot has a source
if not self.fo.inputs[self.fo.active_input_index].is_linked:
return self.execute(context)
# check if composite linked
out = context.scene.node_tree.nodes.get('Composite')
if not out or not out.inputs[0].is_linked:
self.compo_out_from_link = ''
return self.execute(context)
# compo linked, pop panel to choose replace or not
self.compo_out_from_link = out.inputs[0].links[0].from_node.name
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text=f'Composite node connected to: {self.compo_out_from_link}')
col.label(text=f'Would you like to replace by file output slot source ?')
layout.prop(self, 'relink_composite')
def execute(self, context):
# if comp
fn.set_scene_output_from_active_fileout_item()
idx = self.fo.active_input_index
sl = self.fo.file_slots[idx]
sk = self.fo.inputs[idx]
if not sk.is_linked:
self.report({'INFO'}, f'Outut changed to match {sl.path} (slot was not linked)')
return {'FINISHED'}
## If linked replace links to Composite node
if not self.relink_composite:
return {'FINISHED'}
ntree = context.scene.node_tree
links = context.scene.node_tree.links
nodes = context.scene.node_tree.nodes
out = nodes.get('Composite')
if not out:
out = fn.create_node('COMPOSITE', tree=ntree)
fo_loc = fn.real_loc(self.fo)
out.location = (fo_loc.x, fo_loc.y + 160)
# if out.inputs[0].is_linked:
# self.report({'WARNING'}, f'Outut changed to match {sl.path} (Composite node already linked)')
lnk = sk.links[0]
from_sk = sk.links[0].from_socket
links.remove(lnk)
links.new(from_sk, out.inputs[0])
return {"FINISHED"}
classes = (
RT_OT_number_outputs,
RT_OT_mute_toggle_output_nodes,
RT_OT_set_output_node_format,
RT_OT_set_active_file_output_slot_to_composite,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)