Thanks @papsphilip and @tamte for this solution! For an HDA with many parameters I wanted to export all parameters including color and float ramps as well as multiparms, so I extended the script a bit. Now it seems like all standard parameter types are supported. While ramps are detected automatically, for now multiparms need to be manually defined in the script. Does anyone know a way to isolate multiparms in Python?
### Global
import json, os
# Manually Define Multiparms Here
mplist = ['mp_a', 'mp_b', 'mp_c']
# Wrappers for Separating Ramp, Standard and Multiparm Data in the .json File
stdgroup = 'standard'
rampgroup = 'ramps'
mpgroup = 'multiparms'
### Export Parameters
def write_params(kwargs):
hda = kwargs['node']
params = hda.parmsInFolder(['Parameters'])
from sys import platform
dir = hda.parm('path').evalAsString() + '/cache/'
# Windows and MacOS Handle File Paths Slightly Different
if platform == 'darwin':
dir = hda.parm('path').evalAsString() + 'cache/'
filepath = dir + '_config_' + hda.parm('preset').evalAsString() + '.json'
data = {}
ramp = {}
ramps = {}
mp = {}
for parm in params:
if not parm.isHidden():
# Loop Through Ramp Parameters
if parm.isMultiParmParent():
rampname = parm.name()
ramp = parm.evalAsRamp()
interplookup = [hou.rampBasis.Constant, hou.rampBasis.Linear, hou.rampBasis.CatmullRom, hou.rampBasis.MonotoneCubic, hou.rampBasis.Bezier, hou.rampBasis.BSpline, hou.rampBasis.Hermite]
keylist = []
for i,key in enumerate(ramp.keys()):
rampdata = { 'posindex': i,
'pos' : key,
'value': ramp.values()[i],
'interp': interplookup.index(ramp.basis()[i])}
keylist.append(rampdata)
ramp = { rampname: keylist }
ramps.update(ramp)
# <continue> ends the for loop’s current iteration
continue
# Write Standard Parameters
data[parm.name()] = parm.eval()
# Loop Through Multiparms
for mpi in mplist:
mparam = hda.parm(mpi)
mp_inst = {
mpi: []
}
num = hda.parm(mpi).evalAsInt()
instances = hda.parm(mpi).multiParmInstances()
for i in range(num):
block_data = {}
for instance in instances:
if instance.name().find(str(i+1)) >=0:
name = instance.name()
value = instance.eval()
block_data[name] = value
mp_inst[mpi].append(block_data)
mp.update(mp_inst)
#print('........\n')
# Wrapper for Standard Parameters
datas = { stdgroup: data }
# Wrapper for Ramp Parameters
ramps = { rampgroup: ramps }
# Wrapper for Multiparms
mps = { mpgroup: mp }
# Append Ramp Values to the Final Dictionary
datas.update(ramps)
# Append the Mltiparm Values to the Final Dictionary
datas.update(mps)
if not os.path.exists(dir):
os.makedirs(dir)
with open(filepath, 'w') as outfile:
json.dump(datas, outfile, indent=4)
print('Parameters exported to:', filepath)
### Import Parameters
def read_params(kwargs):
hda = kwargs['node']
params = hda.parmsInFolder(['Parameters'])
from sys import platform
dir = hda.parm('path').evalAsString() + '/cache/'
if platform == 'darwin':
dir = hda.parm('path').evalAsString() + 'cache/'
filepath = dir + '_config_' + hda.parm('preset').evalAsString() + '.json'
if len(filepath) > 0 and os.path.isfile(filepath):
# Read JSON for Standard Parameters
with open(filepath, 'r') as outfile:
parameters = json.load(outfile)[stdgroup]
# Read JSON for Ramp Parameters
with open(filepath, 'r') as outfile:
rparameters = json.load(outfile)[rampgroup]
# Read JSON for Multiparms
with open(filepath, 'r') as outfile:
mparameters = json.load(outfile)[mpgroup]
# Set Standard Parameters
for parm_name, value in parameters.items():
parm = hda.parm(parm_name)
if parm is not None:
parm.set(value)
# Set Ramp Parameters
# Outer Loop Runs Over Every Ramp Parameter Name
for parm_name, value in rparameters.items():
parm = hda.parm(parm_name)
if parm is not None:
keys = []
values = []
bases = []
interplookup = [hou.rampBasis.Constant, hou.rampBasis.Linear, hou.rampBasis.CatmullRom, hou.rampBasis.MonotoneCubic, hou.rampBasis.Bezier, hou.rampBasis.BSpline, hou.rampBasis.Hermite]
# Inner Loop - We Query the Current Ramp Name With <[parm_name]>
for i in rparameters[parm_name]:
keys.append(i['pos'])
values.append(i['value'])
bases.append(interplookup[i['interp']])
# Initialize New Ramp
ramp = hou.Ramp(bases, keys, values)
# Set Ramp Parameters
parm.set(ramp)
# Set Multiparms
# Outer Loop Runs Over Every Multiparm Name
for parm_name, value in mparameters.items():
parm = hda.parm(parm_name)
parm.set(0)
if len(mparameters) > 0:
blocks = mparameters[parm_name]
for i in range(len(blocks)):
parm.insertMultiParmInstance(i)
for name, value in blocks[i].items():
hda.parm(name).set(value)
print('Parameters imported from:', filepath)
else:
hou.ui.displayMessage('Couldnt find the file!!')