gp_render/OP_manage_outputs.py

184 lines
6.0 KiB
Python
Raw Normal View History

import bpy
import re
from . import fn
class GPEXP_OT_mute_toggle_output_nodes(bpy.types.Operator):
bl_idname = "gp.mute_toggle_output_nodes"
bl_label = "Mute Toggle output nodes"
bl_description = "Mute / Unmute all output nodes"
bl_options = {"REGISTER"}
mute : bpy.props.BoolProperty(default=True, options={'SKIP_SAVE'})
def execute(self, context):
# scene = bpy.data.scenes.get('Render')
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 GPEXP_OT_number_outputs(bpy.types.Operator):
bl_idname = "gp.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 : bpy.props.StringProperty(default='SELECTED', options={'SKIP_SAVE'})
# ctrl : bpy.props.StringProperty(default=False, options={'SKIP_SAVE'}) # no need
def invoke(self, context, event):
self.ctrl = event.ctrl
return self.execute(context)
def execute(self, context):
render = bpy.data.scenes.get('Render')
if not render:
print('SKIP, no Render scene')
return {"CANCELLED"}
ct = 0
nodes = render.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.ctrl:
fn.delete_numbering(fo)
else:
fn.renumber_keep_existing(fo)
# fn.renumber(fo)
txt = 'de-numbered' if self.ctrl else 're-numbered'
if ct:
self.report({'INFO'}, f'{ct} output nodes {txt}')
else:
self.report({'ERROR'}, f'No output nodes {txt}')
return {"FINISHED"}
class GPEXP_OT_set_output_node_format(bpy.types.Operator):
bl_idname = "gp.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 : bpy.props.BoolProperty(default=True, options={'SKIP_SAVE'})
def execute(self, context):
# scene = bpy.data.scenes.get('Render')
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):
2021-09-23 15:18:06 +02:00
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"}
def out_norm(x):
a = x.group(1) if x.group(1) else ''
b = x.group(2) if x.group(2) else ''
c = x.group(3) if x.group(3) else ''
d = x.group(4) if x.group(4) else ''
e = x.group(5) if x.group(5) else ''
return f'{a}{b}{fn.normalize(c)}{d}{e}'
## does not match the right thing yet
class GPEXP_OT_normalize_outnames(bpy.types.Operator):
bl_idname = "gp.normalize_outnames"
bl_label = "Normalize Output names"
bl_description = "Normalize output names with lowercase and replace dash to underscore"
bl_options = {"REGISTER"}
mute : bpy.props.BoolProperty(default=True, options={'SKIP_SAVE'})
def execute(self, context):
# scene = bpy.data.scenes.get('Render')
nodes = context.scene.node_tree.nodes
# if not nodes.active or nodes.active.type != 'OUTPUT_FILE':
# self.report({"ERROR"}, f'')
# return {"CANCELLED"}
reslash = re.compile('\\/')
ct = 0
for n in nodes:
if n.type != 'OUTPUT_FILE' or not n.select:
continue
# Normalize last part of the file out names
base_path_l = reslash.split(n.base_path)
base_path_l[-1] = fn.normalize(base_path_l[-1])
n.base_path = '/'.join(base_path_l)
for fs in n.file_slots:
fp = fs.path
fp_l = reslash.split(fp)
for i, part in enumerate(fp_l):
fp_l[1] = re.sub(r'(^\d{3}_)?([A-Z]{2}_)?(.*?)(_[A-Z]{2})?(_)?', out_norm, part)
fs.path = '/'.join(fp_l)
ct += 1
# state = 'muted' if self.mute else 'unmuted'
self.report({"INFO"}, f'{ct} output nodes normalized')
return {"FINISHED"}
classes=(
GPEXP_OT_mute_toggle_output_nodes,
GPEXP_OT_set_output_node_format,
GPEXP_OT_number_outputs,
# GPEXP_OT_normalize_outnames,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)