Search code examples
pythonmayamel

Writing a MEL/Python script for changing scale values


Guys in Motion Graphics are having difficulty writing a MEL script (being that none of them are programmers). So I had a go at it but I'm having great difficulty because I don't know what a lot of the variables are and syntax etc.

Basically want to select keyframes of dagObjects with scale values at 0.001 or less, then changing the scale value to 0.

I tried assigning all dagObjects into an array, then trying to iterate through the array, but after that got lost.

Also, if this can be done in Python instead then that's fine.


Solution

  • Hi it would be nice to know what form you dagObjects are in. Here's a simple python script.

    from maya import cmds
    import math
    
    dags = ['obj']
    attrs = ['sx', 'sy', 'sz']
    tol = 0.001
    
    for dag in dags:
        frames = [frame for frame in cmds.keyframe(dag, q=1)]
        for frame in frames:
            attrib_vals = [cmds.getAttr(dag+'.'+attr, time=frame) for attr in attrs]
            length = math.sqrt(sum(attr_val * attr_val for attr_val in attrib_vals))
            if length < tol:
                cmds.setKeyframe(dag, at=attrs, t=[frame], v=0)