PyMel get attribute, question

Is there a way i can get the radius of a sphere, in oop way?

This is a simple example which doesn’t work for me:

#######################
import pymel.core as pm

pm.newFile(f=1);
x = pm.polySphere(n=“MyFirstSphere”,r=5);
y = x[0];

z=y.name();
print z;

r = pm.polySphere( ‘MyFirstSphere’, q=True, r=True );
print r;

a = y.radius.get()
print a;
#######################

In the above,
a)r = pm.polySphere(‘MyFirstSphere’, q=True, r = True);
works just fine.
b)“y.radius.get()” doesn’t seem to work.
And what i want, is to get the radius in an oop way…

What should i do?
What am i doing wrong?

from quickly searching in the pymel documentation, this is what i found


getRadius(**kwargs)

This flag specifies the radius of the sphere. C: Default is 0.5. Q: When queried, this flag returns a float.

Derived from mel command maya.cmds.polySphere

so perhaps just calling ‘a = y.getRadius()’ will do the trick?

EDIT: Seems like i was wrong :frowning:
EDIT2: your problem is here ‘y = x[0]’ this is the transform node, not the shape… the radius comes from the shape and not the transform node, so if you do ‘y=x[1]’: great success!

You need to check the nodeType that’s returned by pymel. As already mentioned the polySphere command gives you back a list, [nt.transform, nt.polysphere] so yes, y[1].radius.get() works as it’s operating on the nt.polyshere. Similarly if you select the node in the outliner and do

pm.selected()[0].radius.get()

that’ll return the radius as the select command in this case returns the nt.polySphere.

Thank you both for your help.

I understand now.

Thanks again!