// ========================================================================== // http://www.tjgalda.com // // Copyright (C) 2007 TJ Galda. All rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files contain unpublished // information proprietary to TJ Galda which is protected by U.S. and // Canadian federal copyright law and by international treaties. // // The Data is provided for use exclusively by You. You have the right // to use, modify, and incorporate this Data into other products for // purposes authorized by TJ Galda, without fee. // // The copyright notices in the Software and this entire statement, // including the above license grant, this restriction and the // following disclaimer, must be included in all copies of the // Software, in whole or in part, and all derivative works of // the Software, unless such copies or derivative works are solely // in the form of machine-executable object code generated by a // source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. // TJ GALDA DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED // WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF // NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR // PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR // TRADE PRACTICE. IN NO EVENT WILL TJ GALDA AND/OR ITS LICENSORS // BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, // DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF TJ GALDA // AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY // OR PROBABILITY OF SUCH DAMAGES. // // http://www.tjgalda.com // ========================================================================== // // TJ Galda Script File // MODIFY THIS AT YOUR OWN RISK // // Creation Date: tj, may 31, 2007 // // Procedure Name: // None (follow along mel for tutorial: how do you sample a colour in a ramp? // // Description: // follow along mel for the tutorial // // // Input Arguments: // None // // Return Value: // None. // // http://www.tjgalda.com // ========================================================================== //THIS IS THE ACCOMPANYING MEL FOR THE TUTORIAL, PLEASE VISIT OUR WEBSITE. //Create the gradient using a ramp: string $tempName = `shadingNode -asShader ramp`; string $tj_rampNode = `rename $tempName "tj_gradientColour"`; string $tj_rampWin = `window -t "tj Ramp Viewer" tj_rampWin`; columnLayout; rampColorPort -node $tj_rampNode; showWindow $tj_rampWin; setAttr ($tj_rampNode + ".colorEntryList[0].color") -type double3 0.988 1 0.8 ; setAttr ($tj_rampNode + ".colorEntryList[1].color") -type double3 0 0 0.235 ; setAttr ($tj_rampNode + ".colorEntryList[1].position") 1.0; removeMultiInstance -break true ($tj_rampNode + ".colorEntryList[2]"); showWindow $tj_rampWin; //Now that we have the ramp, we can gather information about the ramp. //We need to determine where are they and what colour, create an array to catch //each position and match the entry in the list of the ramp. Finally we will create //an array to catch colour, use a vector array to maintain match in the list of the ramp. //Where are they and what colour? Create an array to catch each position and match entry in list. int $entries = `getAttr -size ($tj_rampNode+".colorEntryList")`; vector $entry_colour[]; for ($i = 0; $i < $entries; $i ++) { $entry_pos[$i] = `getAttr ($tj_rampNode+".colorEntryList["+$i+"].position")`; float $entryList_colours[] = `getAttr ($tj_rampNode+".colorEntryList["+$i+"].color")`; $entry_colour[$i] = << $entryList_colours[0],$entryList_colours[1], $entryList_colours[2]>>; } //and print back the results print "\n$entry_pos[] is \n==================\n"; print $entry_pos; print "\n\n==================\n$entry_colour[] is \n==================\n"; print $entry_colour; //In our example now, we can see that we have the two colour entries at 0 and 1, //with the associated colour mix. Now we can work with some basic calculations //to deduce what the colours are between. After we know the colour information, //we can then begin to deduce what the ramp is like. The first step is to find the //difference or delta between the two colours on each channel. From there, //we can divide up the colour on regular intervals and determine what the exact //colour is at any specific point on the map. //This is a sampling step. How many intervals should we break the ramp into? int $step = 20; //Find the delta/difference for each channel. Use temp vectors to get data out //of the array easily. For our example to keep it simple, we can make some assumptions //about where each entry is and what colours they are. We will assume only two //colours, and that the first colour is brighter. Of course, one should build in robustness //to remove these assumptions with true data. vector $tempTopV = $entry_colour[0]; vector $tempBotV = $entry_colour[1]; //Use absolute to ensure it's not a negative value float $differenceR = `abs($tempTopV.x - $tempBotV.x)`; float $differenceG = `abs($tempTopV.y - $tempBotV.y)`; float $differenceB = `abs($tempTopV.z - $tempBotV.z)`; //Divide up the delta per step. This is the amount of change per step that each colour travels. float $deltaStepR = $differenceR / $step; float $deltaStepG = $differenceG / $step; float $deltaStepB = $differenceB / $step; //Now, start number + (delta per step * the number of steps) = end number, in other //words, we have broken down what the difference can be at each interval. //Finalize by building the arrays. float $colourStepsR[], $colourStepsG[], $colourStepsB[]; $colourStepsR[0] = $tempBotV.x; $colourStepsG[0] = $tempBotV.y; $colourStepsB[0] = $tempBotV.z; //Each new step is the old step + the delta for that colour. for ($i = 1; $i <= $step; $i++) { $colourStepsR[$i] = $colourStepsR[$i - 1] + $deltaStepR; $colourStepsG[$i] = $colourStepsG[$i - 1] + $deltaStepG; $colourStepsB[$i] = $colourStepsB[$i - 1] + $deltaStepB; } print ("\n===================\n Colour intervals for R(with a step of "+$deltaStepR+") is:\n"); print $colourStepsR; print ("\n===================\n Colour intervals for G(with a step of "+$deltaStepG+") is:\n"); print $colourStepsG; print ("\n===================\n Colour intervals for B(with a step of "+$deltaStepB+") is:\n"); print $colourStepsB;