My first steps in Python , please help :)

Hey , everybody!
I am trying to learn Python already for one week and decided to make my first tool. Very simple rename tool for maya.
I got almost everything working in UI , but I can’t get rename tool it self work.
It gives me error when I am trying to rename staff , mostly it’s because I don’t know how query name from my
“cmds.textField” . So my question what should I put in my “cmds.rename” command , so instead of my “blablabla” it would get name from textField and rename my object?

Sorry for my english , hopefully I got my question correct. I am really new to it , so if you have some advice please make it easy as possible :smiley:

Thank you :slight_smile:


import maya.cmds as cmds

def UI():
# Check to see if our window exists
if cmds.window(“FirstVladRenameToolUI”, exists = True):
cmds.deleteUI(“FirstVladRenameToolUI”)

# Create our window
window = cmds.window("FirstVladRenameToolUI", title = "Vlad Rename Tool", sizeable = False, w = 500, h = 150, mnb = False, mxb = False)

# Create a main layout
mainLayout = cmds.columnLayout(w = 500, h = 150)

# Create our own column layout
cmds.separator(h=15)
rowColumnLayout = cmds.rowColumnLayout(nc = 1, cw = [(1,460),(2,40)], columnOffset = [(1, "both", 5), (2, "both", 5 )])

# Instructions for use
cmds.text(l="Instructions: select object(s) to rename", align = "left" )


# Input field
inputField = cmds.textField("inputField", w = 500 , parent = mainLayout)



# Make button
cmds.separator(h=20, style = "none")
cmds.button(l = "Rename Me!", w = 500, h = 50, align = "center",c = RenameTool, parent = mainLayout)

# Show window
cmds.showWindow(window)

def RenameTool(*args):
selectObject = cmds.ls(sl = True)
sizeObject = len(selectObject)

for sel in range(0, sizeObject, 1):
    cmds.rename(selectObject[sel], "blablabla")

I believe this is what you are trying to do? I have re-typed some of your code, and linked the RenameTool function to your button. Finally, I extracted the text value by querying the input field and saving this as a variable, which then replaces your “blablabla” string. Hopefully this helps :slight_smile:

# Import modules
import maya.cmds as cmds

# Remove existing windows
if cmds.window("FirstVladRenameToolUI", exists = True):
    cmds.deleteUI("FirstVladRenameToolUI")

# Create window
window = cmds.window("FirstVladRenameToolUI", title = "Vlad Rename Tool", sizeable = False, w = 500, h = 150, mnb = False, mxb = False)

# Create a main layout
mainLayout = cmds.columnLayout(w = 500, h = 150)

# Create our own column layout
cmds.separator(h=15)
rowColumnLayout = cmds.rowColumnLayout(nc = 1, cw = [(1,460),(2,40)], columnOffset = [(1, "both", 5), (2, "both", 5 )])

# Instructions for use
cmds.text(l="Instructions: select object(s) to rename", align = "left" )

# Input field
inputField = cmds.textField("inputField", w = 500 , parent = mainLayout)

# Define the main function
def RenameTool(*args):
    selectObject = cmds.ls(sl = True)
    sizeObject = len(selectObject)
    newName = cmds.textField( inputField, query=True, text=True)
    for sel in range(0, sizeObject, 1):
        cmds.rename(selectObject[sel], newName)
# Make button
cmds.separator(h=20, style = "none")
cmds.button(l = "Rename Me!", w = 500, h = 50, align = "center",c = RenameTool, parent = mainLayout)

# Show window
cmds.showWindow(window)
1 Like

Thanks a lot for taking time , to do this.
I see , button supposed to be after “def Rename”. I also tried to do like you did with “newName = cmds.textField( inputField, query=True, text=True)” , but it didn’t work. Probably because of button script in wrong place.

Thanks a lot!

because inputField is not defined in “RenameTool” function..

to work this code..
change
newName = cmds.textField(inputField, ~~

to
newName = cmds.textField(“inputField”, ~~

import maya.cmds as cmds

def UI():
# Check to see if our window exists
if cmds.window(“FirstVladRenameToolUI”, exists = True):
cmds.deleteUI(“FirstVladRenameToolUI”)

# Create our window
window = cmds.window("FirstVladRenameToolUI", title = "Vlad Rename Tool", sizeable = False, w = 500, h = 150, mnb = False, mxb = False)

# Create a main layout
mainLayout = cmds.columnLayout(w = 500, h = 150)

# Create our own column layout
cmds.separator(h=15)
rowColumnLayout = cmds.rowColumnLayout(nc = 1, cw = [(1,460),(2,40)], columnOffset = [(1, "both", 5), (2, "both", 5 )])

# Instructions for use
cmds.text(l="Instructions: select object(s) to rename", align = "left" )


# Input field
inputField = cmds.textField("inputField", w = 500 , parent = mainLayout)



# Make button
cmds.separator(h=20, style = "none")
cmds.button(l = "Rename Me!", w = 500, h = 50, align = "center",c = lambda x:RenameTool(cmds.textField(inputField, q=True,tx=True) ), parent = mainLayout)

# Show window
cmds.showWindow(window)

def RenameTool(*args):
selectObject = cmds.ls(sl = True)
sizeObject = len(selectObject)

for sel in range(0, sizeObject, 1):
    cmds.rename(selectObject[sel], args)

UI()