Chrommanito
Chrommanito

Reputation: 11

how to make selected object renamed in maya using python?

I have a python function here that is supposed to rename object in maya. But when the window showed up and i click the 'rename' button, nothing changed. not even bringing new window. please help

def renameObject():
    a = cmds.ls(sl=True)
    txt = cmds.textField('txtName', q=True, tx=True)
    
    cmds.rename('a', txt)
    cmds.confirmDialog(icn='information', message='Done!')
    cmds.showWindow()
    return

cmds.window(title='Rename Object')
cmds.columnLayout(adj=1)
cmds.text(label= 'Insert Name', w=300, h=30)
cmds.separator()
cmds.textField('txtName')
cmds.button(label='Rename', width=300, c=lambda*args:'renameObject()')

cmds.showWindow()

Upvotes: 0

Views: 1756

Answers (2)

DrWeeny
DrWeeny

Reputation: 2512

def renameObject(*args):
    a = cmds.ls(sl=True)
    txt = cmds.textField(txt_field , q=True, tx=True)
    
    cmds.rename(a[0], txt)
    cmds.confirmDialog(icn='information', message='Done!')
    cmds.showWindow()

cmds.window(title='Rename Object')
cmds.columnLayout(adj=1)
cmds.text(label= 'Insert Name', w=300, h=30)
cmds.separator()
txt_field = cmds.textField('txtName')
cmds.button(label='Rename', width=300, c=renameObject)

cmds.showWindow()

I've corrected your code, it shuold work when ran but there is lots of mistakes in your code, Haggi Krey has pointed lots of them. If you want to dive in UI design, you should look at partial module from functools. There is lots of examples here in Stack

Upvotes: 0

haggi krey
haggi krey

Reputation: 1978

Two reasons:

  1. Your lambda expression has a string 'renameObject()', it should be the function name without apostrope.
  2. Even if the renameObject() function is called, it will fail because you assign the current selection to a variable called a. But in the rename function, you use again a string 'a'. So maya searches for an object called 'a' and tries to rename it what does not work unless you really have an object called 'a'.

And the confirmDialog() does not need a cmds.showWindow(), it works without.

Upvotes: 1

Related Questions