Hi,
I was looking for the attributeExists and discovered it is only available in MEL. How could I call it via Phyton?
10x
Hi,
I was looking for the attributeExists and discovered it is only available in MEL. How could I call it via Phyton?
10x
When you look in reference you should take a note whats a script and whats not (in the upper corner of your reference entry), in this case attribute attributeExists is a script which means theres really no such commend its just a convenient piece of mel.
Now you can jsut call it directly in mel via
CODE
import maya.mel as mel
test=mel.eval('attributeExists "ty" "nurbsSphere1"')
however due to the fact taht its a melscript you can just port it to python entirely as the code is on your disk (use mel whatIs to find it) and looks like this:
CODE
global proc int attributeExists(string $attr, string $node)
{
if ("" == $attr || "" == $node) return 0;
// check zero, see if the node exists!
//
if( !objExists $node ) return 0;
// First check to see if the attribute matches the short names
//
string $attrList[] = listAttr -shortNames $node;
int $max = size($attrList);
int $i;
for( $i = 0; $i<$max; $i++ )
{
if( $attr == $attrList[$i] )
{
return 1;
}
}
// Now check against the long names
//
$attrList = listAttr $node;
$max = size($attrList);
for( $i = 0; $i<$max; $i++ )
{
if( $attr == $attrList[$i] )
{
return 1;
}
}
return 0;
}
is converted to (i economized a bit i should chk that they are string types too)
CODE
from maya.cmds import *
def attributeExists(attr,node):
if (attr or node):
if (objExists(attr)): return 0
attrList = listAttr(node,shortNames=True)
for i in attrList:
if( attr == i ):
return 1
attrList = listAttr(node)
for i in attrList:
if( attr == i ):
return 1
return 0
Thank you very much, that's closure for me.
the above python has a few mistakes illustrated below.
[codebox]
createNode( 'transform', n='visibility')
attributeExists( 'visibility', 'persp') # incorrectly returns False because it checks if the attr exists instead of the node
attributeExists( 'tx', '') # fails
attributeExists( '', '') # returns None
[/codebox]
it should look more like this:
[codebox]
from maya.cmds import *
def attributeExists(attr,node):
if (attr and node):
if not objExists(node): return 0
if attr in listAttr(node,shortNames=True): return 1
if attr in listAttr(node): return 1
return 0
[/codebox]
also, it should be noted that it's bad practice to import maya.cmds into the main namespace because it will override many important commands, including file, eval, and help. in this case perhaps the function was intended to be in a file by itself, so it would be ok.
compare that code to the mel code and you'll see why i do what i can to avoid writing mel.
Oh yes your right theres a typo node not attr... my bad tahts what you get when you devote 2 minutes per post.
And its just shorthand dint want to delve too deep into it just convert ist as close as possible to original code to demontsrate the point. The typo should be visible fi you go trough it. But tanks people would have missed it
however:
attributeExists( '', '') # returns None
is perfectly valid it python thinks none is same as 0 for compassions sake. Now since a failure is valid difference form 0 it also enables you to catch a bad input. But yes you have some point. Maybe it should throw a exception if the type is not string if it were to be truly pythonic?
CODE
def attributeExists(attr,node):
if type(node)!=type("") or type(attr)!=type(""): raise "not a valid name"
if (attr and node):
if not objExists(node): return 0
if attr in listAttr(node,shortNames=True): return 1
if attr in listAttr(node): return 1
return 0
but this is i admit being totally anal.
besides your coding style wihile okay an recommendable has a hole:
QUOTE
override many important commands, including file, eval, and help.
you can always call them trough super so its true:
help(help)
would use the cmds version of this but:
super.help(help)
would use pythons help so in way all i did was shorten the use of my code at the expense of internal coding prefix. So im saving out on code i use most at the expense of something i MIGHT decide to use.
however your right it does make the code hard to read, but thats a inherited problem of python itself. Bust yes still bad style admitted. Not that eval and help have much of use in a piece of good code. But file actually does.
Also note
QUOTE
compare that code to the mel code and you'll see why i do what i can to avoid writing mel.
yes but thats a bad comparasion since even the mel code could be written MUCH shorter. Ist jut thet maya coders are c++ coders so they tend to write code like that (and ist a big plus they can do it without overhead atleats you claim being python is big plus so its par here!)
now compare the following mel:
CODE
global proc int attributeExists(string $attr, string $node)
{
if ("" == $attr || "" == $node) return 0;
if( !objExists $node ) return 0;
for( $i in listAttr("-shortNames",$node)) if ($i==$attr) return 1;
for( $i in listAttr($node)) if ($i==$attr) return 1;
return 0;
}
which is the exact same thing but does have error raising is directly comparable to the python code in length and simplicity. And i made it more python looking.
Ergo almost no gain (well overall i do rate python more elegant). You just dont know mel half as good as you know python.
On the otherhand you need to manage your types in python.
PS. the board has a annoying habit of deleting spaces of first indent sometimes. Will mark as bug.
PPS. Again no claim that python is worse than mel but ist sure is not MUCH better either here. Because a python user needed to make this proc mel user didn't.