ECToo / pymel

Automatically exported from code.google.com/p/pymel
0 stars 0 forks source link

How does Maya know that PyNodes are not iterables? #224

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Describe the problem.
We are currently porting our code from PyMEL 0.7.8 to 1.0.2.
Using python within MEL scripts I now run into this problem:
mel> python("import pymel.core as pm");
The following line doesn't return anything anymore though I'd expect it 
returning "persp".
mel> python("pm.PyNode('persp')");
A succeeding import statement causes maya to echo the according error message.
mel> python("import os");
# TypeError: object of type 'Transform' has no len() # 

The reason for this problem lies in the "python" MEL command.
It has to decide wether to return it's result as int, float, string or as an 
array of these.
If the result is a 1.0.2 PyNode it's not a string anymore like it was in 0.7.8.
But it's considered an iterable because it has a __getitem__ method.
So the "python" MEL command tries to return the PyNode as an array.
Unfortunately it also tries to access the __len__ method then.
This doesn't exist so an error occurs.
If the "python" MEL command was written properly it won't access __len__.
It then would return:
mel> python("pm.PyNode('persp')");
"p", "e", "r", "s", "p"
It should return "persp" though.

I can think of several solutions.
1. Alias can change the "python" MEL command's behaviour and use PyMEL's 
isIterable definition.
I did this on my own with a small scripted plug-in and it works fine.
Unfortunately you cannot simply override built-in MEL commands.
To do so I manipulated commandList and CommandEngine.dll in the bin directory.
But I can hardly recommend this.

2. In MEL whenever I use the python command I can think if the result is 
supposed to be a PyNode.
If this is the case I can wrap the result with the str method.
mel> string $s = python("str(pm.PyNode('persp'))");

3. __getitem__ can be removed from ProxyUnicode.
This is what I finally did.
If I really want to access a node name's letters individually I better use 
node.name().

What Version of Maya are you using?
2011

On what operating system? (be as specific as possible and include service
packs, 64bit vs 32bit, etc)
Windows XP SP 3 32bit, Windows XP SP 2 64bit

What Version of PyMEL are you using?
1.0.2

What is the expected output? What do you see instead?

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.

Does maya.cmds exhibit the same problem?

Please provide any additional information below.

Original issue reported on code.google.com by cle...@gmx.de on 3 Jan 2011 at 11:59

GoogleCodeExporter commented 9 years ago
We meant to remove __getitem__ when we removed most of the string methods in 
1.0.0, but apparently we missed __getitem__.

We still want to get rid of it (it doesn't make sense to have __getitem__ and 
no __len__), but since it would be a backward incompatible change, we're just 
marked it as deprecated for now, and we'll remove it at some later date.

Original comment by elron...@gmail.com on 18 Jan 2011 at 9:34

GoogleCodeExporter commented 9 years ago

Original comment by elron...@gmail.com on 18 Jan 2011 at 9:36