Hi,
Yes that is correct. Here is a simplified version that wraps all the RenameItem commands within a batch so that you can undo them if needed.
python code
ix.begin_command_batch("Rename items")
for item in ix.selection:
ix.cmds.RenameItem(item.get_full_name(), "mtl_" + item.get_name())
print("Renamed {} items.".format(ix.selection.get_count()))
ix.end_command_batch()
Here is a simple UI in Python that allows renaming manually a list of selected items. It's very basic but it can give you ideas.
It was posted some time ago in the forums.
python code
tool_name = 'Renamer Tool'
log_tag = '[{}] '.format(tool_name)
class RenameButton(ix.api.GuiPushButton):
line_edits = []
window = None
def __init__(self, parent, x, y, w, h, label, line_edits, window):
ix.api.GuiPushButton.__init__(self, parent, x, y, w, h, label)
self.connect(self, 'EVT_ID_PUSH_BUTTON_CLICK', self.on_click)
self.line_edits = line_edits
self.window = window
def on_click(self, sender, event_id):
for edit in self.line_edits:
path = edit.get_label()
new_name = edit.get_text()
item = ix.get_item(path)
if item:
ix.cmds.RenameItem(path, new_name)
ix.log_info(log_tag + 'Done!')
if self.window:
self.window.hide()
def create_gui(selection):
window = ix.api.GuiWindow(ix.application, 0, 0, 600, 400, tool_name)
panel = ix.api.GuiPanel(window, 0, 0, window.get_width(), window.get_height())
panel.set_constraints(ix.api.GuiWidget.CONSTRAINT_LEFT,
ix.api.GuiWidget.CONSTRAINT_TOP,
ix.api.GuiWidget.CONSTRAINT_RIGHT,
ix.api.GuiWidget.CONSTRAINT_BOTTOM);
pad = 4
w = window.get_width() - pad
h = 22
x = 0
y = 0
# title
label = ix.api.GuiLabel(panel, x + pad, y, w, h, 'Old Name -> New Name')
y += h + pad
# line edits for each object
edits = []
for item in selection:
old_name = item.get_full_name()
new_name = item.get_name()
edit = ix.api.GuiLineEdit(panel, x + pad, y, w, h, old_name)
edits.append(edit)
edits[-1].set_text(new_name)
y += h + pad
# rename button
rename_btn = RenameButton(panel, x + pad, y, 60, h, 'Rename', edits, window)
y += h + pad
window.show()
while window.is_shown(): ix.application.check_for_events()
# Run it
try:
if ix.selection.get_count() > 0:
create_gui(ix.selection)
else:
ix.log_warning(log_tag + 'No objects selects.')
except Exception as e:
ix.log_error('Script error:\n{}'.format(e))
Don't hesitate to read the SDK documentation and search the Scripting forums, or join the Discord server.
Cheers!