Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active March 30, 2023 08:01
Show Gist options
  • Save BigRoy/2b220611a86ca1c5e666df4937c619f4 to your computer and use it in GitHub Desktop.
Save BigRoy/2b220611a86ca1c5e666df4937c619f4 to your computer and use it in GitHub Desktop.
Maya: Export ramp attribute samples to JSON. Sample multiple times along the ramp curve for its value.
import maya.cmds as cmds
import maya.api.OpenMaya as om
import json
def get_ramp_attribute(attr):
"""Return MRampAttribute for attribute name."""
sel = om.MSelectionList()
sel.add(parm)
plug = sel.getPlug(0)
return om.MRampAttribute(plug)
node = cmds.ls(selection=True)[0] # first selected node
attr = node + '.value'
ramp = get_ramp_attribute(attr)
samples = 100 # amount of samples
step = 1.0 / (samples - 1.0)
values = {}
for sample in range(samples):
position = sample * step
value = ramp.getValueAtPosition(position)
values[position] = value
# Write out to json
with open("S:/maya_ramp_attribute_samples.json", "w") as f:
json.dump(values, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment