diff --git a/outputs_search_and_replace.py b/outputs_search_and_replace.py index 634e033..980e970 100644 --- a/outputs_search_and_replace.py +++ b/outputs_search_and_replace.py @@ -31,11 +31,20 @@ class RT_OT_outputs_search_and_replace(bpy.types.Operator): ), 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) @classmethod @@ -48,30 +57,50 @@ class RT_OT_outputs_search_and_replace(bpy.types.Operator): old = source if self.use_regex: + # Directly replace using regex new = re.sub(self.find, self.replace, source) if old != 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 - if self.prefix: - if not self.separator in source: - # Only if separator exists - return - splited = source.split(self.separator) - prefix = splited[0] - new_prefix = prefix.replace(self.find, self.replace) - if prefix != new_prefix: - splited[0] = new_prefix - return self.separator.join(splited) - - else: + if self.section == 'full': new = source.replace(self.find, self.replace) if old != new: return new + if not self.separator in source: + # Only if separator exists + return + + if self.section == 'prefix': + splited = source.split(self.separator) + prefix = splited[0] + if not prefix: + return + new_prefix = prefix.replace(self.find, self.replace) + if prefix != new_prefix: + splited[0] = new_prefix + return self.separator.join(splited) + + elif self.section == 'suffix': + splited = source.rsplit(self.separator, 1) + suffix = splited[-1] + 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): - return context.window_manager.invoke_props_dialog(self, width=400) + return context.window_manager.invoke_props_dialog(self, width=430) def draw(self, context): 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_b= layout.row() - row_b.prop(self, "prefix") + row_b.prop(self, "section") row_b.active = not self.use_regex subrow_b = row_b.row(align=True) - subrow_b.active = self.prefix - subrow_b.prop(self, "separator") + subrow_b.active = self.section != 'full' + subrow_b.alignment = 'RIGHT' + subrow_b.label(text='Separator:') + subrow_b.prop(self, "separator", text='') layout.prop(self, "find") layout.prop(self, "replace")