Monday, December 3, 2012

Flower Petals with Mel Script

I was attempting to use mel script to create an aesthetically pleasing piece.

The background is a matte painting, but the flower petals are all created dynamically through mel script.

To create the petals through mel script, I use a For Loop to create each petal.
1. Create plane.
2. Set starting position and rotation randomly.
3. Turn plane into NCloth.
4. Attach plane to fields.

I was trying to integrate NCloth, which Im pretty new at into melscript, and I think it turned out pretty well. One thing I would like to adjust in the future is the lighting on the petals. The color isn't quite right when its in the shadows. The movement is pretty close to what I was looking for though.

Here is the mel script and a playblast. Enjoy!




//Clean Up
SelectAll;
doDelete;


// User Definitions
string $currentSelection[];
string $myTurbulence[];
string $myAir[];

//Set Up Background
polyPlane -w 1 -h 1 -sx 1 -sy 1 -ax 0 1 0 -cuv 2 -ch 1;
move -2.076 -4.896 7.866;
rotate 0 0 90;
scale 9 1 16;
sets -e -forceElement surfaceShader1SG;
$currentSelection = `ls -selection`;
select -r (($mySelection[0]) + ".map[0:3]");
polyEditUV -pu 0.5 -pv 0.5 -a 90 ;
polyEditUV -pu 0.5 -pv 0.5 -su -1 -sv 1 ;

//Create Fields
turbulence -pos 0 0 0 -m 6 -att 0 -f 1 -phaseX 0 -phaseY 0 -phaseZ 0 -noiseLevel 0 -noiseRatio 0.707  -mxd -1  -vsh none -vex 0 -vof 0 0 0 -vsw 360 -tsr 0.5 ;
$myTurbulence = `ls -selection`;
air -pos 0 0 0 -m 15 -att 0 -dx 0 -dy -.75 -dz 1 -s 5 -iv 1 -iro 1 -vco 0 -es 0  -mxd 20  -vsh none -vex 0 -vof 0 0 0 -vsw 360 -tsr 0.5 ;
$myAir = `ls -selection`;

//Create petals For Loop
for( $i=0; $i<10; $i++)
{
    //create plane
    polyPlane -w .25 -h .5 -sx 10 -sy 10 -ax 0 1 0 -cuv 2 -ch 1;
    move -r (rand(-1,1)) (rand(-1,1)) (rand(-1,1));
    rotate (rand(0,360)) (rand(0,360)) (rand(0,360));
    sets -e -forceElement flowerPetalTexture2SG;
   
    //Adjust NCloth settings
    createNCloth 0;
    $currentSelection = `ls -selection`;
    setAttr (($currentSelection[0]) + ".drag") (rand(.05, 5));
    setAttr (($currentSelection[0]) + ".pointMass") (rand(.1, 2));
    setAttr (($currentSelection[0]) + ".bendResistance") (rand(.1, 2));
    setAttr (($currentSelection[0]) + ".stretchResistance") (rand(1, 200));
   
    //Attach Fields
    connectDynamic -f $myAir $currentSelection;
    connectDynamic -f $myTurbulence $currentSelection;
   
    select -r $currentSelection nucleus1 ;
    setAttr "nucleus1.gravity" 0;
}




No comments:

Post a Comment