I collect and order some basic PyMel procedures, like get or assign material, setup Arnold render settings, export-import alembic etc.
Possible, someone who just start to dive into python will find this notes helpful.
I collect and order some basic PyMel procedures, like get or assign material, setup Arnold render settings, export-import alembic etc.
Possible, someone who just start to dive into python will find this notes helpful.
Nice thanks.
For the object name from attribute I usually use the node() method:
attributeObject = pm.ls("sdfsadf.attibute")[0]
object = attributeObject.node()
The surface shader of a geometry can be found a bit more python like with:
o=pm.PyNode(objectname)
o.outputs(type="shadingEngine")[0].surfaceShader.input()
To add a color attribute, you can just say: pm.addAttr(n, ln=‘attr’, type=‘float3’, usedAsColor=True)
That works for non-colors too and will give you X,Y,Z. Unfortunately, you seem to still have to do it all manually if you want to add a rotation attribute (angle).
Thread should be renamed to PyMEL tips and tricks.
Connecting attributes (aka secret operators)
import pymel.core as pm
cube = pm.polyCube()[0]
ball = pm.polySphere()[0]
pm.connectAttr(cube.tx, ball.ty)
# is equivilant to:
cube.tx >> ball.ty
pm.disconnectAttr(cube.tx, ball.ty)
# is equivilant to:
cube.tx // ball.ty
I recommend avoiding that “magic”. It’s a bad API. >> and // should never have side-effects in a Python API, and it just obfuscates code, making it hard to read. It’s an API written by somebody who just learned what operator overloading is, but who hasn’t yet learned how it should be used.
o.outputs(type=“shadingEngine”)[0].surfaceShader.input()
Cant get it working.
def getShader():
sel = pm.ls(sl = 1 )
for i in sel:
object = pm.PyNode(i)
print object.outputs(type="shadingEngine")[0].surfaceShader.input()
getShader()
pm.addAttr( ‘objectShape’, ln = ‘attr’, type=‘float3’, usedAsColor=True)
Also no luck to run. # Invalid flag ‘type’
What do you mean by nad and side-effects? I have never even heard about those operators causing any problems.
The problem isn’t that it won’t work, it’s that it’s unconventional in Python (and most languages), and gives strange, hard to read code. It’s very weird to have a line of code in Python that looks like “x // y”. It looks like it doesn’t do anything. Writing good Python code isn’t about having two-letter shortcuts for things, it’s about clarity and consistency.
(You can do what you like, of course, I’m just cautioning others reading the thread.)