Skip to content

Instantly share code, notes, and snippets.

@Genora51
Last active July 9, 2019 12:00
Show Gist options
  • Save Genora51/f62ea852abe06b7e4689e535d1e40526 to your computer and use it in GitHub Desktop.
Save Genora51/f62ea852abe06b7e4689e535d1e40526 to your computer and use it in GitHub Desktop.
proc float randAbout(float $range, float $centre) {
// Generate random numbers within a range
return rand(-$range/2, $range/2) + $centre;
}
proc string pad4(int $i) {
// Pad an int with zeros up to length 4
return `python ("'%04d' % "+string($i))`;
}
proc string padf(float $f) {
// Pad an float with zeros
return `python ("'%06.2f' % "+string($f))`;
}
proc float cubeVolume(string $name) {
float $height = `polyCube -q -h $name`;
float $width = `polyCube -q -w $name`;
float $depth = `polyCube -q -d $name`;
return $height * $width * $depth;
}
proc string[] sort_sizes(string $objects[]) {
string $sized[];
for ($object in $objects) {
$sized[size($sized)] = padf(`cubeVolume $object`) + ":" + $object;
}
$sized = `sort $sized`;
return $sized;
}
global proc assortment(int $num, float $smax, float $height, float $depth, float $width, string $prefix) {
// Randomly distribute cubes
for ($i=0; $i < $num; $i++) {
// Generate random attributes
$x = randAbout($width,0);
$y = randAbout($height,0);
$z = randAbout($depth,0);
$size = rand($smax);
// Create cube and name it
polyCube -d $size -h $size -w $size;
$nameo = $prefix + pad4($i + 1) + "_GEP";
rename $nameo;
// Rotate and move randomly
$rx = rand(360);
$ry = rand(360);
$rz = rand(360);
rotate $rx $ry $rz;
move $x $y $z;
}
}
global proc tex_assort(int $num, float $smax, float $height, float $depth, float $width, string $prefix, string $texture) {
for ($i=0; $i < $num; $i++) {
$y = randAbout($height, 0);
do {
$x = randAbout($width,0);
$z = randAbout($depth,0);
$u = ($x + ($width/2)) / $width;
$v = ($z + ($depth/2)) / $depth;
float $tex[] = `colorAtPoint -o RGB -u $u -v $v $texture`;
}
while(rand(0, 1) <= $tex[0])
}
}
global proc sort_assort(string $prefix, float $spacing, float $xstart) {
// Arrange the assorted cubes in a line
// Filter by prefix and suffix
string $objects[] = `ls ($prefix+"*_GEP")`;
// Sort by size
string $sized[] = `sort_sizes $objects`;
$numobj = size($sized);
$xpos = $xstart;
// Place in a row by size order
for ($idx = 0; $idx < $numobj; $idx++) {
string $toks[];
$toknum = `tokenize $sized[$idx] ":" $toks`;
$oname = $toks[1];
if ($idx != 0) {
$xpos += `pow (float($toks[0])) (1.0/3.0)` * $spacing;
}
move $xpos 0 0 $oname;
}
}
global proc shade_assort(string $prefix, float $startc[], float $endc[]) {
// Shade by volume
// Filter by prefix and suffix
string $objects[] = `ls ($prefix+"*_GEP")`;
// Sort by size
string $sized[] = `sort_sizes $objects`;
$numobj = size($sized);
// Shade by size
for ($idx = 0; $idx < $numobj; $idx++) {
string $toks[];
$toknum = `tokenize $sized[$idx] ":" $toks`;
$oname = $toks[1];
$shader = `shadingNode -asShader lambert`;
$frac = float($idx) / $numobj;
$r = ($endc[0] * $frac) + ($startc[0] * (1 - $frac));
$g = ($endc[1] * $frac) + ($startc[1] * (1 - $frac));
$b = ($endc[2] * $frac) + ($startc[2] * (1 - $frac));
setAttr ($shader + ".color") $r $g $b;
select $oname;
hyperShade -assign $shader;
}
}
global proc assort() {
// Window for assorting cubes
$w = `window -visible true -title "Assort Cubes" AssortWindow1`;
columnLayout ColumnLayout;
frameLayout -labelVisible false -marginWidth 5 -marginHeight 5;
columnLayout;
text -label "Number of Cubes";
string $ncubes = `intField -minValue 0 -value 10`;
text -label "Max scale";
string $maxscale = `floatSliderGrp -field true
-minValue 0.0 -maxValue 10.0 -value 1
-fieldMinValue 0.0 -fieldMaxValue 100.0`;
text -label "Bounds Height";
string $bh = `floatSliderGrp -field true
-minValue 0.0 -maxValue 10.0 -value 5
-fieldMinValue 0.0 -fieldMaxValue 100.0`;
text -label "Bounds Depth";
string $bd = `floatSliderGrp -field true
-minValue 0.0 -maxValue 10.0 -value 5
-fieldMinValue 0.0 -fieldMaxValue 100.0`;
text -label "Bounds Width";
string $bw = `floatSliderGrp -field true
-minValue 0.0 -maxValue 10.0 -value 5
-fieldMinValue 0.0 -fieldMaxValue 100.0`;
text -label "Object Prefix";
string $pref = `textField -text "obj_"`;
$cmd = "applyAssort "
+ $ncubes + " "
+ $maxscale + " "
+ $bh + " "
+ $bd + " "
+ $bw + " " + $pref;
button -label "Apply" -command $cmd;
showWindow $w;
window -e -wh 300 250 $w;
}
global proc applyAssort(string $ncubes, string $maxscale, string $bh, string $bd, string $bw, string $pref) {
// Get fields from window and assort
int $nc = `intField -q -value $ncubes`;
float $ms = `floatSliderGrp -q -v $maxscale`;
float $h = `floatSliderGrp -q -v $bh`;
float $d = `floatSliderGrp -q -v $bd`;
float $wi = `floatSliderGrp -q -v $bw`;
string $pf = `textField -q -tx $pref`;
assortment $nc $ms $h $d $wi $pf;
}
global proc arrange() {
// Window for arranging cubes
$w = `window -visible true -title "Arrange Assorted Cubes" ArrangeWindow1`;
columnLayout ColumnLayout;
frameLayout -labelVisible false -marginWidth 5 -marginHeight 5;
columnLayout;
text -label "Start x-value";
string $sx = `floatSliderGrp -field true
-minValue -10.0 -maxValue 10.0
-fieldMinValue -100.0 -fieldMaxValue 100.0`;
text -label "Spacing";
string $sp = `floatSliderGrp -field true
-minValue 0.0 -maxValue 10.0 -value 2.0`;
text -label "Object Prefix";
string $pref = `textField -text "obj_"`;
$cmd = "applyArrange "
+ $sx + " "
+ $sp + " " + $pref;
button -label "Apply" -command $cmd;
showWindow $w;
window -e -wh 300 150 $w;
}
global proc applyArrange(string $sx, string $sp, string $pref) {
// Get fields from window and arrange
float $start = `floatSliderGrp -q -v $sx`;
float $space = `floatSliderGrp -q -v $sp`;
string $pf = `textField -q -tx $pref`;
sort_assort $pf $space $start;
}
global proc arrange_shade() {
// Window for arranging cubes
$w = `window -visible true -title "Shade Assorted Cubes" ShadeWindow1`;
columnLayout ColumnLayout;
frameLayout -labelVisible false -marginWidth 5 -marginHeight 5;
columnLayout;
text -label "Minimum colour";
string $sc = `colorSliderGrp -rgb 0 0 0`;
text -label "Maximum colour";
string $ec = `colorSliderGrp -rgb 1 1 1`;
text -label "Object Prefix";
string $pref = `textField -text "obj_"`;
$cmd = "applyShade "
+ $sc + " "
+ $ec + " " + $pref;
button -label "Apply" -command $cmd;
showWindow $w;
window -e -wh 300 150 $w;
}
global proc applyShade(string $sc, string $ec, string $pref) {
float $startc[] = `colorSliderGrp -q -rgb $sc`;
float $endc[] = `colorSliderGrp -q -rgb $ec`;
string $pf = `textField -q -tx $pref`;
shade_assort $pf $startc $endc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment