Hey everyone,
I've got a Maya scene, where a few hundred objects simply need to translate slowly a bit in the x and y direction to get some life in the scene. I found a nice expression for that at cgsociety, and deleted the y-translation out of it and reduced the amount of the noise.
proc applyExpressionToSelection() { string $sel[] = `ls -sl`; int $id = 0; for($object in $sel) { string $cmd = "vector $offset = dnoise(<<0.1,0,0.1>> * time*3 + " + $id + ");\n"; $cmd += ($object + ".translateX = $offset.x;\n"); $cmd += ($object + ".translateZ = $offset.z;\n"); $id += 1; expression -s $cmd -o $object -ae 1 -uc all; } } applyExpressionToSelection();
It already looks really like what I wanted, but when I apply the expression on my objects, they all make a huge jump to a seemingly random place. This sadly destroys the order I had intended completely. I know some python but only a small bit of Mel, as I'm just getting into it right now. I can't see what would cause it so I'm a bit at a loss.
So know my question, would anyone be so kind as to explain to me why my objects jump around and how to get rid of this? I would be most thankful 🙂
Hi Juli
The problem is that you are calculating the new position by using a noise function which will give you a relatively random value, and then are applying it directly to the position of your objects. However this takes no account of the object's original position and so that data is essentially lost once you have applied the expression.
What you need to do is to apply the data relative to the position of the objects at the point you created the expression like this:
proc applyExpressionToSelection() { string $sel[] = `ls -sl`; int $id = 0; for($object in $sel) { float $initPos[] = `xform -q -ws -t $object`; string $cmd = "vector $offset = dnoise(<<0.1,0,0.1>> * time*3 + " + $id + ");\n"; $cmd += ("$offset += <<" + $initPos[0] + ", " + $initPos[1] + ", " + $initPos[2] + ">>;\n"); $cmd += ($object + ".translateX = $offset.x;\n"); $cmd += ($object + ".translateZ = $offset.z;\n"); $id += 1; expression -s $cmd -o $object -ae 1 -uc all; } } applyExpressionToSelection();
All of the noise will now be added to te original location of the objects rather than overriding it.
Hope it helps
Mike