Randomize Offset


#1

Hi,

I am very new to scripting, so I was hoping to get some feedback on a simple MEL script I wrote.
The script lets the user randomize transformation, rotation and scale for each in a selection of objects. It is meant to give a more natural look to objects like bricks, shingles or anything that has the typical look of duplicated meshes.
You can find the script here.
I am very interested in ways to optimize the script.





These are some examples of what the script can do.
My next step will be to write a Python version of the script but I am even more of a beginner with Python. I would really love to get some feedback.

Thank you.


#2

Do the python version, you will not regret it :slight_smile:

Even if not really wrong, the whole if/else conditions are quite ugly and sometimes useless.
e.g. you have do the scaling like this:

if($rSX == 1 && $rSY == 1 && $rSZ == 1)
{
	scale -r 1 1 1 $eachObj;
}else if($rSX == 1 && $rSY == 1)
{
	scale -r 1 1 $randS[2] $eachObj;
}...

Well, you have a $rSX, $rSY, $rSZ related to $randS[0 - 3]. I’d try to simplify the whole process by finding an expression which sets the scaling. You want to use 1 if the $rSX is 1 and the random value if $rSX is zero:

float $scalex = (1.0 - $rSX) * $randS[0] + $rSX;
float $scaley = (1.0 - $rSY) * $randS[1] + $rSY;
 float $scalez = (1.0 - $rSZ) * $randS[2] + $rSZ;
 scale -r $scalex $scaley $scalez;


#3

The other if/else block appears twice:

if ($incR[1]<=0)
 {
 	$incR[1]=0;
 }...

Is this by intention?
Here you limit the random values to the predefined multiples of the $iRDeg. Well, here you only want the multiples, not the random values, so why not use them directly?

 float $rdeg[] = {$iRDeg, $iRDeg*2, $iRDeg*3, $iRDeg*4};
 float $incR = $rdeg[rand(3.99)];

#4

Hey haggi,

thank you very much for your help. Great tips!