Solution: Create your grapes model, make it consist of about 50 elongated spheres, with slightly different scaling for each. Rename it so it look like grape1, grape2, ...., grape50 or so. You can use renaming function in maya or do a simple MEL script for it. Select all the spheres, and open script editor, in your script editor type in this small code and execute it. string $selections[] = `ls -sl`;
for ($selection in $selections)
{
rename $selection "grape#";
}The first line of this script is just list all your selected object, and put the list into a string array variable called $selections. The second line told maya to do following command for each member of the string array and we do not need to declare $selection as string since maya know already that members of a string array should be a string. The command that applied to each member of the string array was enclosed in curly bracket, which rename the selection into grape1, grape2, .... etc.. Do not forget to type semicolon for terminating a command line. Arrange it so it looks like a this image. 
- Open hypershade and create:
- One blinn material, rename it to grapesM
- One V-Ramp texture, rename it to grapes_ramp.
- One single shading switch, rename it to grapes_switch.
Connect grapes_ramp to color channel of grapesM. Connect output of grapes_switch to V Coord of grapes_ramp.
Select all grapes object and assign grapesM to it. Modify grapes_ramp so it look like this image. 
All basic connection is set. Now its time for the tedious part.
Select grapes_switch, open the attribute editor and as usual click Add Surfaces. You will see that the left pane of switch attributes will be populated by the grape models. 
But we need to tell maya what value to be used on the right pane, which is the output of grape_switch. For each grape object, we will add a dynamic attribute, let's name it vcoord and use this value for the right pane of grape_switch node. But since doing it manually one by one is not fun, let's try to write a simple mel script to do it. Open your script editor, and type in the following code into it, but don't execute it yet. string $selections[] = `ls -sl`;
for ($selection in $selections)
{
addAttr -ln vcoord -at double -min 0 -max 1 $selection;
setAttr -e -keyable true ($selection + ".vcoord");
}The first line of this script is just list all your selected object, and put the list into a string array variable called $selections. The second line told maya to do following command for each member of the string array. The command that applied to each member of the string array was enclosed in curly bracket, which add an attribute (addAttr) with longname (-ln) vcoord and type (-at) a float with minimum value (-min) 0 and maximum value (-max) 1. After maya add this attribute then she will show it in channel box by setting the attribute (setAttr) as keyable (-keyable true). Select all your grapes object and execute the script. It won't take long to add attribute to 50 models or so. Now you can check on your channel box and for each grape model you will see the new attribute will appear at the very last line of your channels list. 
Now either you can type in the inSingle of grape_switch the exact name of this attribute (for example : grape1.vcoord), or again using a mel script to do the task. It's up to you but I prefer to do it in Mel script. Select the switch node from hypershade and type this code in your script editor and execute it. string $selections[] = `ls -sl`;
string $switchNode = $selections[0];
string $connections[] = `listConnections -source true -destination false`;
for ($i = 0; $i < size($connections); $i++)
{
connectAttr -f ($connections[$i] + ".vcoord") ($switchNode + ".input[" + $i + "].inSingle");
}The first two line is used to get the name of your switch node. The third line will list all incoming connection (-source true) but not outgoing connection (-destination false), and store it into a string array $connections. But in this case I'm using index instead using "for in loop". The index will start from 0 (first member of the string array) to the last member of the array which is calculated using "size" function, and increment the index by 1 for each loop. We need this index so we can connect attribute to the right surface. The loop command (which is in curly bracket) will connect ".vcoord" attribute to the ".inSingle" attribute of grape_switch. For 50 or so connection it should take less than a minute to execute the script, and it will type in all your attribute which would take several minutes to do it manually. Now if you look at the attribute editor of the switch node, you will see all connection was setup already. 
Render it, you will be disappointed because the result show all grapes will have same color. So what the heck is this, after working with that silly code still doesn't give the result what we want?. One thing that you have to know, even though we have connected everything but if you look at the value of vcoord attributes of each grape it is all the same 0 value. To have variation in color we need to put in random value between 0 to 1 in this attribute. Again MEL will help you to speed up things. string $selections[] = `ls -tr "grape*"`;
for ($selection in $selections)
{
setAttr ($selection + ".vcoord") (rand(0,1));
}On the first line, there is a new flag that is used, instead of listing selected objects, this command will list all transform (-tr) with a wildcard name "grape*", which will return grape1, grape2 .... grape50 or so. On the loop command, this script will assign a random value between 0 and 1 to .vcoord attribute. This random value then will be used as a v-coord of your grape_ramp. Render it again then you will have variance of grape color depends on your ramp texture. You still be able to adjust the ramp texture to your liking as long as the connection is there all changes will be reflected in the render. 
scene file is here or in zipped file (shadingSwitch.zip)
|