After avoiding MEL scripting for far too long, I started working through "Introduction to MEL" by Digital Tutors, in the hopes that I could finally make sense of the more complicated material presented in In Silico.
Full information on the lessons can be found here, as they covered a wide variety of topics. This included a look at MEL syntax, creating primitive objects, editing attributes, using WHILE/FOR/IF commands and then combining all of the taught content into a single project, which resulted in the ability to create a textured flower with randomised attributes, simply by clicking a single button. The image below shows some of these random flowers;
After completing these lessons, I feel much more confident in using simple MEL expressions, and I intend on continuing this development, which will hopefully allow me to create a more complicated script which will import large amounts of numerical data and automate the creation of objects and animation.
For those who may be interested (and I realise it may not be many!), here is the complete script used to create the flowers...
global proc flowerMaker () {
int $numPetals = `rand 10 20`;
int $createShader = 1;
// Check for shader.
string $shads[] = `ls -mat`;
for ($myNode in $shads) {
if ($myNode == "petalColor") {
$createShader = 0;
}
}
switch ($createShader) {
case 0:
print "The petal shader already exists. /n";
break;
case 1:
// Build the petal shader.
shadingNode -asShader lambert -n "petalColor";
shadingNode -asTexture ramp -n "petalRamp";
sets -renderable true -noSurfaceShader true -empty -n petalColorSG;
connectAttr -f petalColor.outColor petalColorSG.surfaceShader;
connectAttr petalRamp.outColor petalColor.color;
setAttr "petalRamp.colorEntryList[3].color" -type double3 1 0 0;
setAttr "petalRamp.colorEntryList[3].position" 1;
setAttr "petalRamp.colorEntryList[2].color" -type double3 1 1 0;
setAttr "petalRamp.colorEntryList[2].position" 0.5;
setAttr "petalRamp.colorEntryList[1].color" -type double3 1 0 0;
setAttr "petalRamp.colorEntryList[1].position" 0;
setAttr petalRamp.type 8;
// Build the core shader.
shadingNode -asShader lambert -n "petalCore";
setAttr "petalCore.color" -type double3 0.4 0.2 0.14;
sets -renderable true -noSurfaceShader true -empty -n petalCoreSG;
connectAttr -f petalCore.outColor petalCoreSG.surfaceShader;
break;
}
// Create the flower core.
sphere -ax 0 1 0 -n "core";
string $myCore[] = `ls -sl`;
move 0 0.2 0;
scale 1 0.5 1;
// Connect core to shader.
pickWalk -d down;
string $myCoreShape[] = `ls -sl`;
sets -edit -forceElement petalCoreSG $myCoreShape[0];
// Build the petal.
sphere -ax 0 1 0;
move 0 0 -1.6;
scale 0.7 0.3 1.7;
FreezeTransformations;
ResetTransformations;
string $myPetal[] = `ls -sl`;
parent $myPetal $myCore;
// Connect petal to shader.
select $myPetal;
pickWalk -d down;
string $myPetalShape[] = `ls -sl`;
sets -edit -forceElement petalColorSG $myPetalShape[0];
// Move the tip of the petal up.
select ($myPetal[0] + ".cv[3][7]");
move -r 0 1.5 0;
// Select the inner part of the petal and move it down.
for ($uCV = 5; $uCV <= 6; $uCV++) {
for ($vCV = 0; $vCV <= 7; $vCV++) {
select ($myPetal[0] + ".cv[" +$uCV+ "][" +$vCV+ "]");
move -r 0 -0.3 0;
}
}
// Duplicate petals.
select $myPetal[0];
float $degreeApart =(360 / $numPetals);
for ($i = 2; $i <= $numPetals; $i++) {
duplicate;
rotate -r 0 $degreeApart 0;
// Randomly rotate the petals.
float $petalRX = `rand -10 10`;
float $petalRY = `rand -10 10`;
float $petalRZ = `rand -10 10`;
FreezeTransformations;
rotate $petalRX $petalRY $petalRZ;
}
select $myCore;
rename "flower0";
}
global proc flowerMaker () {
int $numPetals = `rand 10 20`;
int $createShader = 1;
// Check for shader.
string $shads[] = `ls -mat`;
for ($myNode in $shads) {
if ($myNode == "petalColor") {
$createShader = 0;
}
}
switch ($createShader) {
case 0:
print "The petal shader already exists. /n";
break;
case 1:
// Build the petal shader.
shadingNode -asShader lambert -n "petalColor";
shadingNode -asTexture ramp -n "petalRamp";
sets -renderable true -noSurfaceShader true -empty -n petalColorSG;
connectAttr -f petalColor.outColor petalColorSG.surfaceShader;
connectAttr petalRamp.outColor petalColor.color;
setAttr "petalRamp.colorEntryList[3].color" -type double3 1 0 0;
setAttr "petalRamp.colorEntryList[3].position" 1;
setAttr "petalRamp.colorEntryList[2].color" -type double3 1 1 0;
setAttr "petalRamp.colorEntryList[2].position" 0.5;
setAttr "petalRamp.colorEntryList[1].color" -type double3 1 0 0;
setAttr "petalRamp.colorEntryList[1].position" 0;
setAttr petalRamp.type 8;
// Build the core shader.
shadingNode -asShader lambert -n "petalCore";
setAttr "petalCore.color" -type double3 0.4 0.2 0.14;
sets -renderable true -noSurfaceShader true -empty -n petalCoreSG;
connectAttr -f petalCore.outColor petalCoreSG.surfaceShader;
break;
}
// Create the flower core.
sphere -ax 0 1 0 -n "core";
string $myCore[] = `ls -sl`;
move 0 0.2 0;
scale 1 0.5 1;
// Connect core to shader.
pickWalk -d down;
string $myCoreShape[] = `ls -sl`;
sets -edit -forceElement petalCoreSG $myCoreShape[0];
// Build the petal.
sphere -ax 0 1 0;
move 0 0 -1.6;
scale 0.7 0.3 1.7;
FreezeTransformations;
ResetTransformations;
string $myPetal[] = `ls -sl`;
parent $myPetal $myCore;
// Connect petal to shader.
select $myPetal;
pickWalk -d down;
string $myPetalShape[] = `ls -sl`;
sets -edit -forceElement petalColorSG $myPetalShape[0];
// Move the tip of the petal up.
select ($myPetal[0] + ".cv[3][7]");
move -r 0 1.5 0;
// Select the inner part of the petal and move it down.
for ($uCV = 5; $uCV <= 6; $uCV++) {
for ($vCV = 0; $vCV <= 7; $vCV++) {
select ($myPetal[0] + ".cv[" +$uCV+ "][" +$vCV+ "]");
move -r 0 -0.3 0;
}
}
// Duplicate petals.
select $myPetal[0];
float $degreeApart =(360 / $numPetals);
for ($i = 2; $i <= $numPetals; $i++) {
duplicate;
rotate -r 0 $degreeApart 0;
// Randomly rotate the petals.
float $petalRX = `rand -10 10`;
float $petalRY = `rand -10 10`;
float $petalRZ = `rand -10 10`;
FreezeTransformations;
rotate $petalRX $petalRY $petalRZ;
}
select $myCore;
rename "flower0";
}
No comments:
Post a Comment