Python rename tool - Error


#1

Hey guys.
I am having problems in my first python script.

Anyone can help me to fix it?

Here is the code:

[I]import maya.cmds as melToPy

Função da criação da interface

def janela():

# Cria formulário e botao
    
width = [1, 135]
    
melToPy.window(title="Rename Tool", w=500, h=140)
melToPy.textFieldButtonGrp("selObj", label= 'Rename to:', text= "", columnWidth= width, h=123, buttonLabel= "Do it", buttonCommand= 'beloRenamer()')
melToPy.showWindow()

Função para capturar o node selecionado e renomeação em seguida

def beloRenamer():

selObj = melToPy.ls(sl=True)
for i in selObj:
    melToPy.rename(object, selObj)

Fecha o código

janela()[/I]

Thanks again for any help to fix it.
Glauber Belo


#2

Hi Glauber,

please use the “#” sign in the message composer to enclose code into code tags, this makes reading of code easier like this;

import maya.cmds as melToPy
def janela():        
    width = [1, 135]        
    melToPy.window(title="Rename Tool", w=500, h=140)
    melToPy.textFieldButtonGrp("selObj", label= 'Rename to:', text= "", columnWidth= width, h=123, buttonLabel= "Do it", buttonCommand= 'beloRenamer()')
    melToPy.showWindow()

def beloRenamer():   
    selObj = melToPy.ls(sl=True)
    for i in selObj:
        melToPy.rename(object, selObj)

janela()

And it would help a lot if you can tell us what error message you get.


#3

Hi haggi,
Thanks for response.

The message error is:

Error: RuntimeError: file <maya console> line 11: Syntax error: unexpected end < at position 1 while parsing:
    <type 'object'>
    ^ 


#4

Interesting, everthing works fine here if I copy your code and run it.


#5

The error tells you that some variable called object is not defined correctly.
Also in beloRenamer() it seems you are iterating though the selected objects, but not using the iterator. I have attempted to guess how you wanted this to work and corrected it as best I can…

def beloRenamer():   
    selObjects = melToPy.ls(sl=True)
    for object in selObjects:
        melToPy.rename(object, "newName")

As it is written here the selected objects would be renamed as newName, newName1, newName2, and so on. This is probably not what you want. You need a way to pass the new name into the function from your UI.
Maybe this could work, but would require you to change the button command.

def beloRenamer(newName):   
    selObjects = melToPy.ls(sl=True)
    for object in selObjects:
        melToPy.rename(object, newName)

Let me know if I’m on the right track before I go into detail.

David


#6

Big thanks djx!! :smiley:
I hope this will help me a lot to moving foward in this script

Thanks you!