Re-usable attribute-driven noise expressions in MEL

Hi - I’m trying to build an expression to drive semi-configurable noise on a large number of groups.  Each group that will be driven by the expression will have a parent controller object which will have the attributes for things like the noise amount and noise frequency that are used int the noise calculation.  In addition, I want to have a global noise value/frequency on the root node so that noise-driven nodes can provide an override value or, if a “useGlobalNoise” boolean is true, look at the attributes on the root node.

My question is how to reference parent nodes and the root node of the expression-driven group’s heirarchy.

I see there’s a listRelatives command that I would think should be able to let me use this expression on multiple things, each of which would look at their corresponding parent node for attributes as well as the root node.

However, I keep getting syntax errors as I’m new to MEL.

Can anyone help unravel this code to help me understand what I’m doing wrong?

My goal is to define this expression once so I can re-use it throughout my various assets that have animation rig.  I’m used to JS/Actionscript syntax so I may be doing stupid Noob stuff with variable declarations or referencing.

string $myParent = ‘listRelatives -p’;

string $roots[] = ls -assemblies;

string $myRoot = $roots[0];

float $noiseValue;

if ($myParent.useGlobalNoise){

$noiseValue = $myRoot.noiseAmount*($myRoot.noiseFrequency*time);

}else if($myParent.noiseAmount >0){

$noiseValue = $myParent.noiseAmount*($myParent.noiseFrequency*time);

}else {

$noiseValue = 0;

}

rotateZ = $noiseValue;

ended up just writing a script to auto-rig everything and dynamically generate the references based on a naming structure.  If anyone knows of ways to reference3 the parent dynamically in a code, I’d still appreciate it.

Here’s the code I came up with to generate the attributes and rigging (assuming things are of naming structure blahAnim with child blahNoise)

string $sel[] = `ls -sl -l`;
for ($obj in $sel)
{
    print("adding attributes for "+$obj+"\n");
    addAttr -ln "noiseValue" -at "double" -minValue  0 -maxValue 180 -dv 0 ($obj);
    setAttr -e -keyable true ("|" + $obj + ".noiseValue");
    addAttr -ln "noiseFrequency" -at "double" -minValue  1 -maxValue 10 -dv 1 ($obj);
    setAttr -e -keyable true ("|" + $obj + ".noiseFrequency");
 
    string $objSplit[] = stringToStringArray($obj,"|");
    string $lastObj = $objSplit[(size($objSplit)-1)];
    print ("last is "+$lastObj+"\n");
    string $lastObjNoise = $obj+"|"+substring($lastObj,1,(size($lastObj)-4))+"Noise";
    print ("lastObjNoise = "+$lastObjNoise+"\n");
    expression -s ($lastObjNoise+".rotateZ = "+$lastObj+".noiseValue*(noise("+$lastObj+".noiseFrequency*time));")  -o $lastObjNoise -ae 1 -uc all ;
};