Rename Item example

From The Foundry MODO SDK wiki
Jump to: navigation, search

Renaming items

The code below creates a custom command that will rename the currently selected item if an arg is given or it will remain the same if nothing is passed to it.

note: as a custom command, the python source file needs to be placed in an 'lxserv' folder in order to be 'blessed' (registered) as a plugin at startup.

#python
import lx
import lxifc
import lxu.command
import lxu.select
 
class ItemRenameCommand(lxu.command.BasicCommand):
    def __init__(self):
        lxu.command.BasicCommand.__init__(self)
        self.dyna_Add('name', lx.symbol.sTYPE_STRING)
        self.basic_SetFlags(0, lx.symbol.fCMDARG_OPTIONAL)
 
    def cmd_Flags(self):
        return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
 
    def basic_Execute(self, msg, flags):
        newItemName = self.dyna_String(0, "none")
        if newItemName != 'none':
            currentSelection = lxu.select.ItemSelection()
            itemObject = currentSelection.current()[0]
            itemObject.SetName('%s' % newItemName)
        else:
            pass
 
lx.bless(ItemRenameCommand, "pl_item.rename")