Mambo4
June 7, 2012, 11:44am
1
If I have unwantedNameSpace:myObject selected and use this python snippet:
selection = cmds.ls(sl=True)
myObjectName =selection[0]
print (myObjectName)
I get “unwantedNameSpace:myObject”
how can I get a myObjetcName without “unwantedNameSpace:”?
This is what I came up with, but it feels like an inelegant hack:
selection = cmds.ls(sl=True)
objectWithNameSpace=selection[0]
objectSplit =objectWithNameSpace.split(':')
object=objectSplit[1]
trace('object: '+object)
TBH that’s what I use, if you want to make it more elegant you could make a “removeNamespace” method that splits it off for you. OR just write it in one line
obj_no_namespace = cmds.ls(sl=True)[0].split(':')[1]
dgovil
June 7, 2012, 12:20pm
3
object = cmds.ls(sl=1)[0].split(':')[1]
print('object: '+object)
edit: Matt beat me to it
If there is a chance of nested namespaces, rpartition is better
object = cmds.ls(sl=1)[0].rpartition(':')[2]
2 Likes
sjt
June 7, 2012, 2:07pm
5
[QUOTE=mje11even;16509]If there is a chance of nested namespaces, rpartition is better
object = cmds.ls(sl=1)[0].rpartition(':')[2]
[/QUOTE]
Really? I have just been doing the same thing with negative slicing indices. Hadn’t heard about rpartition, nice!
object = cmds.ls(sl=1)[0].split(':')[-1]
Here’s another method using PyMEL.
import pymel.core as pm
# create a new namespace and set it to the current namespace
myNS = pm.Namespace(':').create('MyNS')
myNS.setCurrent()
# create a transform in that namespace
myObj = pm.nt.Transform(name = 'MyObj')
pm.Namespace(':').setCurrent()
print myObj
MyNS:MyObj
myObjWithoutNS = myObj.stripNamespace()
print myObjWithoutNS
>> MyObj
(code not tested…written from dome…shame on me.) Hope it helps.
Keep in mind that stripNamespace() does not remove the namespace, it merely returns the object name without it.
Review the Namespace() class object for more details regarding namespace manipulation. http://goo.gl/xLBjb
And also the PyNode() class object for the stripNamespace() method. http://goo.gl/vLdrQ
Split from the right to left, and stop after first colon.
‘test:object:name’.rsplit(’:’, 1)[-1]
While we’re at it, split by find from right and add 1:
obj = cmds.ls(sl=True)[0]
obj_no_namespace = obj[obj.rfind(’:’)+1:]
Why not one more? I usually use replace(). This works with nested namespaces as well.
import pymel.core as pm
myObj = pm.selected()[0]
print myObj.name().replace(myObj.namespace(),'')