I vaguely remember submitting this as a bug and getting a 'Yes, we know' 
from Autodesk.
Anyway, giving the name right away only messes with the spaceLocator 
command.
I remember spotting it in one other command, but I can't remember which one 
it was.

Since Maya selects the newly created item you can actually query the 
selection as a workaround! :)

import maya.cmds

def safeSpaceLocator(*args, **kwargs):
    """ Wrapper around spaceLocator command since it doesn't return unique 
names correctly if name argument is provided """
    maya.cmds.spaceLocator(*args, **kwargs)
    return maya.cmds.ls(sl=1)

The same fix works for PyMel of course.
If you'd rather stay away from querying selections because you might feel 
it's the root of all evil.
Then here's another workaround, create the node with the createNode command.
print maya.cmds.createNode('locator', name='test')

If you want to have a fixed command without querying selection that still 
allows you to manipulate it with argument like the default spaceLocator 
command, then here's a draft:
import maya.cmds

def safeSpaceLocator(*args, **kwargs):
    
    createKwargs = {}
    name = kwargs.pop('name', kwargs.pop('n', None))
    if name is not None:
        createKwargs['name'] = name
        
    locator = maya.cmds.createNode('locator', **createKwargs)
    
    if args or kwargs:
        maya.cmds.spaceLocator(locator, e=1, *args, **kwargs)
    
    return [locator]

Note that it doesn't allow to use the last safeSpaceLocator() command in 
edit mode. 
It's trivial to implement if someone wants to monkey patch Maya's command 
plus has the urge to work around querying the selection.

/geek out
-Roy


On Thursday, March 5, 2015 at 9:00:23 PM UTC+1, Eric Thivierge wrote:
>
>  One thing to maybe mention Chris, is that while list comprehensions take 
> up less lines, the quick readability may diminish. If you have lots of them 
> lined up next to each other or used consecutively it can be very hard to 
> read the code. Especially if it isn't your own.
>
> It's a balance of use and readability.
>
> Eric T.
>
> On 3/5/2015 2:55 PM, Chris Lesage wrote:
>  
> "One of the reasons that I like to use PyMel is that when you create an 
> object you get a reference to that object and you are not dependent of the 
> name when you want to find the object." 
>
>  Yes, I agree. Well, in this case, you are dependent on the name. Or you 
> cause a bug. :)
>
>  Also I couldn't resist writing this as list comprehensions. Doing it 
> this way stores your locators as a list of objects that you can reference 
> even after your range(10) is complete.
>
>   locators = [pm.spaceLocator(n='test#') for x in range(10)]
> groups = [pm.group(loc, n='group#') for loc in locators]
>
>  By the way, zfill() is a nice way to give number padding when making a 
> numbered naming convention. This would make test_000, test_001, test_002, 
> etc. ...But this will still cause a nodeError if you don't have unique 
> names:
>
>   locators = [pm.spaceLocator(n='test_{0}'.format(str(x).zfill(3))) for x 
> in range(10)]
> groups = [pm.group(loc, n='group#') for loc in locators]
>  
>  
>   
>  

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/45ba8c35-0b82-48d7-9328-b6876e0e5f56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to