LumaPictures / pymel

Python in Maya Done Right
Other
490 stars 131 forks source link

Creating object with name clash to another object not in top level throws MayaNodeError #320

Closed pmolodo closed 8 years ago

pmolodo commented 9 years ago

From anthony....@greenworm.net on October 06, 2012 04:53:23

Describe the problem. -- If you attempt to create a new object (e.g. a locator) with an explicit name set and another object with the same name exists outside of the top level (e.g. in a group) the node requested will be created and the name the name will be set to the same as the existing node prepended with a |, but you'll also have a MayaNodeError thrown What Version of Maya are you using? -- Maya 2012

On what operating system? (be as specific as possible and include service packs, 64bit vs 32bit, etc) -- Win 7 SP1 x64 What Version of PyMEL are you using? -- 1.0.3 What is the expected output? What do you see instead? -- Ideally, I'd like to have the exception not thrown, and the name selection to match what I'd get if i used the rename function (i.e. if there's a clash, it'll return a name suffixed with a number that's one higher than any other object in the scene with the same base number)

If possible, provide a few lines of code to reproduce the problem. It helps us if your code will reproduce the problem from a new scene.

setup and cleanup

import pymel.core as pm import traceback pm.newFile(force=True) pm.flushUndo()

begin reproduction:

locatorList = list()

create ten locators called 'theLocator[1:10]'

for x in range(10): locatorList.append(pm.spaceLocator(name="theLocator1"))

group objects to move them into a non-root level

pm.group(locatorList)

try create a new locator, "theLocator5"

throws a MayaNodeError, but creates the node anyway.

run this block again, and you'll find a locator created with a name of theLocator11

try:

just to prove it's not specific to the 'first' number

theLocator = pm.spaceLocator(name="theLocator5") 

except: traceback.print_exc()

Traceback (most recent call last):

File "", line 21, in

File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\factories.py", line 883, in newFuncWithReturnFunc

res = returnFunc(res[0])

File "C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\core\general.py", line 1679, in new

raise _objectError( name )

MayaNodeError: Maya Node does not exist: u'theLocator5' Does maya.cmds exhibit the same problem? -- No. Maya.cmds will not throw an exception but the node created will still be named with the duplicate name, and subsequent calls will also increment from the top. Please provide any additional information below. --No idea if it helps, but just poking around a little further, if I catch the exception and check what theLocator is, it's surprisngly (to me) theLocator11.

--Again, no idea, but is this related to issues 225 and 221?

Original issue: http://code.google.com/p/pymel/issues/detail?id=286

mjmvisser commented 8 years ago

I just ran into the same problem, here's a more concise repro kit:

pm.newFile(force=True)
loc = pm.spaceLocator(name='p01')
pm.group(loc)
pm.spaceLocator(name='p01')

# Error: Maya Node does not exist (or is not unique):: u'p01'
# Traceback (most recent call last):
#   File "<maya console>", line 4, in <module>
#   File "/prod/tools/maya/python/pymel/internal/factories.py", line 986, in newFuncWithUnpack
#     res = beforeUnpackFunc(*args, **kwargs)
#   File "/prod/tools/maya/python/pymel/internal/factories.py", line 960, in newFuncWithReturnFunc
#     res = returnFunc(res[0])
#   File "/prod/tools/maya/python/pymel/core/general.py", line 2097, in __new__
#     raise _objectError(name)
# MayaNodeError: Maya Node does not exist (or is not unique):: u'p01' # 

The problem is that cmds.spaceLocator can create duplicate objects, as long as the long name is different. It doesn't return the long name, though, so this line in general.py (line 2026) returns None:

res = _api.toApiObject(name, dagPlugs=True)

Since the PyNode new can't find the object, it throws an exception.

I'm not sure how to fix it. The workaround is to avoid creating duplicate objects with pm.spaceLocator.

pmolodo commented 8 years ago

There's a handful of maya commands which return non-unique names, which is a royal pain because it makes the return results worthless. (The worst offender? cmds.duplicate - the one command GUARANTEED to have return results with non-unique names... returns non-unique names.)

In this case though - the fact that spaceLocator doesn't take a 'parent' option might mean we can just always prefix the return result with a '|' to indicate it's parented under the world. Unless someone knows of a way that you can change the 'context' in some way to make it so that nodes by default are parented somewhere else?

pmolodo commented 8 years ago

should be fixed by:

https://github.com/LumaPictures/pymel/commit/8bdcc62399f60de4b1186ce7b517d22eeb71a0f2

salvatimarc commented 6 years ago

sorry i duplicated the issue https://github.com/LumaPictures/pymel/issues/410 i think this is the same issue i still have in maya 2016,2017 i.e pymel 1.0.8 and pymel 1.0.9 with ikHandle, spaceLocator.. 8bdcc62 does not seem to have fixed it, or it has been re-introduced by other changes

pmolodo commented 6 years ago

I just tested this in 2017, and it seems to work - no error, and it returns a pynode. I've confirmed your issue in #410 , though, so I'll respond there.

salvatimarc commented 6 years ago

It s right. The problem was existing for space locator in maya 2016, but is corrected in maya 2017. although the problem with ikHandle still existed. Thank you for the fast feedback.