Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get Object Name Without Unicode in Python?

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
14748 Views, 7 Replies

Get Object Name Without Unicode in Python?

Hi all,

 

Quick question. I'm using the typical cmds.ls(sl=1,o=1) command to return a list of the selected objects. I've been checking the docs and it doesn't seem like there's a flag to return just the name of the objects. For example, I don't want the whole [u'pCube1'] deal, I just want pCube1 (no qotes of unicode).

 

Is there a one-step way to get that? My workaround so far has been something like this, but I'm positive there has to be a better way to do this...

 

sel = cmds.ls(sl=1,o=1)
objNames = [] 

for obj in sel:
    objNames.append(obj)

I've also been using the string replace method to get rid of all the [u' and '] items.... Again, probably not the best workaround. I must be missing something pretty obvious. 

7 REPLIES 7
Message 2 of 8
saihtam
in reply to: Anonymous

I'd recommend going through the basic data types of Python a bit better.

Have you tried to print the items of the list? Because that returns you exactly what you want. [u'something'] is just saying that it's a list, the qoutes are just showing you that it's a string and the u indicates that it's unicode. Perfectly fine. But this doesn't get shown when you use the actual variable.

 

import maya.cmds as cmds

sl = cmds.ls(sl=1, o=1)

for s in sl:
    print s
    
cmds.select(sl)

That will print each name nicely, and select them just as you'd expect.

 

EDIT: Also, why make a new list with the exact same content? If you really need a new list you can use this format

new_list = old_list[:]

That will make a new copy of the list. If you don't include the brackets etc then you will just reference the list, not actually copy it.

 

EDIT2: Ugh. That was your workaround. Me no read good. Anyway, more info!

- Mathias
Message 3 of 8
Anonymous
in reply to: saihtam

Thanks Saihtam. I'm a self-taught programmer so some of these basic concepts get by me. I made the silly mistake of doing print(sel) instead of print(sel[0])...

Thanks for putting up with my questions haha.
-Ben
Message 4 of 8
saihtam
in reply to: Anonymous

Well, that's not really a mistake. You're just printing the entire list instead of one item in the list. Remember that a list can contain all sorts of data. Even other lists. So it's important to be able to see the difference between a normal string and a unicode string.

We all gotta start somewhere 🙂

And my favorite thing about ls is that you can search(or list, as it's short for) for objects. I use that all the time.

Get all joints in the scene by type:

cmds.ls(type='joint')



Get all joints with suffix _JNT:

cmds.ls("*_JNT", type='joint')



And so on. I often use this if I want to delete constraints and other stuff from a duplicated skeleton. I'd select hierachy on my skeleton, then I'd do something like this:

cmds.select(cmds.ls(sl=1, type='joint'), d=1)
cmds.delete() #usually just do this by hand.
- Mathias
Message 5 of 8
adamghering
in reply to: saihtam

for that last bit of code where you say you use that to delete constraints, where is the constraint called?  Im trying to do that exact thing and ...well Im struggling with python with the unicode as well as syntax compared to mel. it just seems all convoluted

Message 6 of 8
Kahylan
in reply to: adamghering

Hi!

 

The constraint in this case is not called but selected.

The delete() command will delete everything in the selection if not provided a positional argument.

#this line checks the selection for joints and then deselects the joints found
cmds.select(cmds.ls(sl=1, type='joint'), d=1)
#this line just deletes the selection, since no argument is provided
cmds.delete() #usually just do this by hand.

 

 

Now if you want to call a constraint, you could do it in the following way:

import maya.cmds as mc
#creating the constraint
const = mc.parentConstraint("parentName","childName")

#deleting the constraint using the dynamic attribute
mc.delete(const)

 

Does that answer your question?

If not, what exactly do you need to do?

Message 7 of 8
adamghering
in reply to: Kahylan

Trying to parse an objects connections,

grab the constraint driving it and delete it

 

Im a bit clumsy when it comes to coding, was trying to broaden my knowledge into python, I can get it working in mel but python sometimes eludes me just yet. as its just calling mel commands and then reformatting it in python i find I keep reverting as the mel just makes more sense to me.

Message 8 of 8
Kahylan
in reply to: adamghering

This can be achieved fairly easily, since attributes can only take one incoming connection you can simply filter the list connections so it only takes sources and take the the first element in the list.

def deleteConst(obj = "", attr = "tx"):
    
    constraint = cmds.listConnections("{0}.{1}".format(obj, attr), d = False)[0]
    cmds.delete(constraint)

deleteConst(obj = "name_of_your_object", attr = "attribute_your_constraint_is_driving") 

Now this is in a function so it is easy for you to change inputs, but if you already have your object stored in a variable, and know a channel that is driven you can simply take line 3 & 4 and switch obj for your variable and attr for your attribute in the brackets in line 3.

 

I hope it helps!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report