PDA

View Full Version : python- ls without unique names


JaredTaylor
11-26-2012, 10:55 PM
Hi,

I have a rename script that works fine except for one thing - if an object name is not unique the name that comes into the rename box has the full path which is not what I want for convenience's sake.

I need a way to cmds.ls(sl = True) and have it return a non-unique name.

import maya.cmds as cmds
import sys

'''
Rename popup box for outliner - copy/paste this script into a hotkey field in the hotkey editor
'''


try:
item = cmds.ls(os = True)[0]
except:
item = 'null'

if item == 'null':
print "Nothing Selected!",
sys.exit()



txtA = cmds.promptDialog(t = "Rename", tx = item, button = ['Rename', 'Cancel'], db = 'Rename')

if txtA == 'Rename':
tval = cmds.promptDialog(tx = True, q = True)
cmds.rename(item, tval)



And before anyone asks, I assign this to a hotkey so I don't have to grab the mouse just to rename an item, I can pickwalk so much faster.

Nyro
11-27-2012, 01:24 AM
I don't know Python, as I only script in MEL, but here's my attempt at the solution:

item = cmds.ls(os = True, l = True)[0]
shortName = item.rsplit('|', 1)[1]

Basically, you first ensure that the ls command returns full path strings. Then you separate the path and the object's name. Because you use the -l flag on ls, every object will have a prefix of "|", at least one.

Now just use shortName in the promptDialog, but continue to use item for the rename command.

As stated, I just scanned the standard python library for that function, so I can't guarantee it works 100% or that there might not be a more efficient way to do this :)

Panupat
11-27-2012, 03:24 AM
replace

tx = item

with

tx = item.split("|")[-1]

CGTalk Moderation
11-27-2012, 03:24 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.