Add limited section choice, prefix or suffix

This commit is contained in:
pullusb 2025-07-15 17:51:57 +02:00
parent 9f9284853e
commit 00712553c6

View File

@ -32,10 +32,19 @@ class RT_OT_outputs_search_and_replace(bpy.types.Operator):
default='all' default='all'
) )
prefix: BoolProperty(name="Prefix Only", description="Affect only prefix of name (skipping names without separator)", default=False) section : EnumProperty(
name="Section",
description="Search-replace only on a specific section of the text field based on a separator (if no separator exists, skip the field)",
items=(
('full', "Full", "Affect the whole string, No specific section"),
('suffix', "Suffix", "Affect only the part after the last separator"),
('prefix', "Prefix", "Affect only part before the first separator"),
),
default='full'
)
separator: StringProperty(name="Separator", description="Separator for prefix/suffix", default='/')
separator: StringProperty(name="Separator", description="Separator for prefix", default='_')
selected_node_only: BoolProperty(name="Selected Nodes Only", description="Affect only selected file output nodes", default=True) selected_node_only: BoolProperty(name="Selected Nodes Only", description="Affect only selected file output nodes", default=True)
@classmethod @classmethod
@ -48,30 +57,50 @@ class RT_OT_outputs_search_and_replace(bpy.types.Operator):
old = source old = source
if self.use_regex: if self.use_regex:
# Directly replace using regex
new = re.sub(self.find, self.replace, source) new = re.sub(self.find, self.replace, source)
if old != new: if old != new:
return new return new
## Regex usage exemple to add as hint:
# search (separate in 3 groups) : (.*/)(.*?_)(.*)
# replace (keep only group 1 and 3) : \1\3
# --
# source: "ViewLayer_DiffCol/ViewLayer_DiffCol_"
# result: "ViewLayer_DiffCol/DiffCol_"
return return
if self.prefix: if self.section == 'full':
if not self.separator in source: new = source.replace(self.find, self.replace)
# Only if separator exists if old != new:
return return new
if not self.separator in source:
# Only if separator exists
return
if self.section == 'prefix':
splited = source.split(self.separator) splited = source.split(self.separator)
prefix = splited[0] prefix = splited[0]
if not prefix:
return
new_prefix = prefix.replace(self.find, self.replace) new_prefix = prefix.replace(self.find, self.replace)
if prefix != new_prefix: if prefix != new_prefix:
splited[0] = new_prefix splited[0] = new_prefix
return self.separator.join(splited) return self.separator.join(splited)
else: elif self.section == 'suffix':
new = source.replace(self.find, self.replace) splited = source.rsplit(self.separator, 1)
if old != new: suffix = splited[-1]
return new if not suffix:
return
new_suffix = suffix.replace(self.find, self.replace)
if suffix != new_suffix:
splited[-1] = new_suffix
return self.separator.join(splited)
def invoke(self, context, event): def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=400) return context.window_manager.invoke_props_dialog(self, width=430)
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
@ -87,12 +116,14 @@ class RT_OT_outputs_search_and_replace(bpy.types.Operator):
row_c.prop(self, "use_regex", text="Use Regex") row_c.prop(self, "use_regex", text="Use Regex")
row_b= layout.row() row_b= layout.row()
row_b.prop(self, "prefix") row_b.prop(self, "section")
row_b.active = not self.use_regex row_b.active = not self.use_regex
subrow_b = row_b.row(align=True) subrow_b = row_b.row(align=True)
subrow_b.active = self.prefix subrow_b.active = self.section != 'full'
subrow_b.prop(self, "separator") subrow_b.alignment = 'RIGHT'
subrow_b.label(text='Separator:')
subrow_b.prop(self, "separator", text='')
layout.prop(self, "find") layout.prop(self, "find")
layout.prop(self, "replace") layout.prop(self, "replace")