dissidia
dissidia

Reputation: 1581

What's happening in my object renaming script?

please bear with me, with my questions especially... I apologize in advance should my thread title misleads you in some ways but this is the best I can come up with

I am doing a basic renaming application as shown in the code below. Basically I took this from an example that I have came across, slap it into my code and viola, it works. Sadly there are a few parts in which I do not understand and as I tried searching online for answers, I came up with nothing and it sure is frustrating.

import pymel.core as pm
objects = pm.ls(selection=True)
# Result: [nt.Transform(u'my_pCube1')] # 
pfx = 'test'

for item in objects:
    item.rename(pfx + "_" + item.name().split('|')[-1])
    print 'Prefix added: %s_' %pfx
  1. Is [nt.Transform(u'my_pCube1')] considered a long name or short name in this instance?

  2. where does .name() came from? Is it a part of the rename hidden functions or something which I am not seeing from the documentation?

Then, as I am using cmds almost throughout my coding, I thought I might as well standardized it and changed pm to cmds... However instead of running as I thought it would, it gives me the error as shown below.

objects = cmds.ls(selection=True)
# AttributeError: 'unicode' object has no attribute 'rename' # 
  1. Why is it so?
  2. Additionally, as I tried to print out the output of objects, it prints out objects instead of the selection.. Any ideas?

Upvotes: 1

Views: 1869

Answers (2)

theodox
theodox

Reputation: 12218

your sample is using PyMel, so the objects in your lists are PyNodes as @vaibhaw points out. They are useful in this situation because they retain a connection to the original scene objects.

The long name of a maya object is the name in the form |path|to|object where the names off all the objects above it in the hierarchy are the included. To get this from a pynode use the fullPath() function on your nodes

The short name is usually the name displayed in the outliner or channel box for an object. When you have more than one object with the same short name, maya will give you enough of the long name to differentiate the objects. For a hierarchy like:

group1
  box
group2 
  box

the shortNames will be |group1|box and |group2|box. Get this from a pynode with shortName(); it's what you would get using plain old maya or mel.

To get the name you see in the channel box from a pynode, use nodeName(). That gives you only the object's 'personal' name.

If you use pynodes, you should be able to just call rename() on each object. However you'll need to keep the objects around to check their new names -- the new names may not be exactly what you want, since Maya won't allow sibling objects to have the same name; it will rename siblings with a trailing number to keep them unique.

In the second bit of code you're using maya.cmds. It does not operate on objects with their own functions, it treats all objects as strings. So

pynode = pm.PyNode("pCube1")
pynode.rename("fred")
print pynode
# Result: [nt.Transform(u'fred')] # 

in pymel, but in maya.cmds, the obects are just strings:

cube = "pCube1"
renamed_cube = cmds.rename(cube, "barney")
print renamed_cube:
#  Result: barney # 

Upvotes: 2

vaibhaw
vaibhaw

Reputation: 151

I am assuming that pm is pymel.core module. See the documentation of this module here. ls method in pymel.core module returns a list of PyNode objects.

Read about PyNodes here. Specially read the sections PyNodes Are Not Strings and Mutability and You which mentions that rename method can be used for PyNode objects.

Look into documentation of ls and rename methods in cmds. ls returns an array of string variables(names of objects), not the objects themselves. And strings have no method called rename, hence you are getting the AttributeError.

To rename using cmds, this may work:

objects = cmds.ls(selection=True)
pfx = 'test'

for item in objects:
    cmds.rename(item, pfx + "_" + item)
    # cmds.rename(old_filename, new_filename)
    print 'Prefix added: %s_' %pfx

Code not tested as I do not have Maya installed.

.name() is an attribute of PyNode object.

Upvotes: 1

Related Questions