PDA

View Full Version : MEL : Displacement from meshes


deadalvs
01-31-2011, 07:56 PM
I have created a MEL script which helps to render perfect displacement maps from one or multiple meshes, using the Maya Software renderer.

The idea is that the setup is automated and the user does not have to setup everything manually.

Please use this script in a separate scene since it may mess up your render settings to fit the geometries to render.

[
It may happen that the render "focus" is not perfectly fitting the bounding box of the selection, in those cases Maya's "exactWorldBoundingBox" command fails since some nodes may have still some transformations applied. There's an extra button to clean those.
]

Let me know if anyone has an issue or needs additional functionality.

[ Code: see next post .. ]

Cheers !


http://www.jumpgates.com/matthias/MEL/MELDisplacement.jpg

deadalvs
01-31-2011, 07:58 PM
/***************************************************************
*** GUI
***************************************************************/

if (`window -exists "renderDisplacementMapWindow"` == 1){deleteUI "renderDisplacementMapWindow";}
window -t "Render Displacement From Objects" renderDisplacementMapWindow;
columnLayout -adjustableColumn 1;

columnLayout;
text -l "";
text -l "";
text -l "Render Resolution (longer side)";
intField -w 300 -v 1024 renderRes_intField;
text -l "";
button -label "Do start setup (camCreation, locatorCreation, shader)" -w 300 -h 20 -command "doStartSetup";
button -label "[Wrong focus: clean trafos of selection]" -w 300 -h 20 -command "cleanTraFo";
text -l "";
button -label "Align height locators to selection" -w 300 -h 20 -command "alignHeightLocators";
button -label "Assign Shader to selection" -w 300 -h 20 -command "assignShaderToSelection";
text -l "";
button -label "Do cam setup for selection" -w 300 -h 45 -command "doCamSetup";
text -l "";
button -h 20 -w 300-l "Look thru renderCam" -command ("lookThruThisCam(\"renderCam\")");
button -h 20 -w 300-l "Look thru persp" -command ("lookThruThisCam(\"persp\")");
text -l "";

setParent ..;
setParent ..;

showWindow renderDisplacementMapWindow;
doStartSetup;
/***************************************************************
*** doStartSetup
***************************************************************/

global proc doStartSetup(){
existenceRenderCam;
existenceMeasureLocators;
doShaderSetup;

}

/***************************************************************
*** existenceRenderCam
***************************************************************/

global proc existenceRenderCam(){

if (`objExists renderCam`)
{// do nothing}

}
else
{
camera -orthographic 1;
move -r 0 10 0;
rotate -r -os -90 0 0 ;
rename "renderCam";
setAttr "renderCamShape.renderable" 1;
setAttr "renderCamShape.displayFilmGate" 0;
setAttr "renderCamShape.displayResolution" 1;
setAttr "renderCamShape.overscan" 3;
setAttr renderCamShape.filmFit 3;
}
}

/***************************************************************
*** existenceMeasureLocators
***************************************************************/

global proc existenceMeasureLocators(){

if (`objExists minDistanceLocator`)
{// do nothing}

}
else
{
spaceLocator -p 0 0 0;
scale -r 0.1 0.1 0.1 ;
rename "minDistanceLocator";
}
if (`objExists maxDistanceLocator`)
{// do nothing}

}
else
{
spaceLocator -p 0 0 0;
move -a 0 1 0;
scale -r 0.1 0.1 0.1 ;
rename "maxDistanceLocator";
}
}


/***************************************************************
*** calcTotalBBox of selected objects
***************************************************************/

global proc float[] calcTotalBoundingBox(){

string $selArray[] = `ls -sl -fl`;
// xmin, ymin, zmin, xmax, ymax, zmax.

float $xMin = 1000000000;
float $yMin = 1000000000;
float $zMin = 1000000000;
float $xMax = -1000000000;
float $yMax = -1000000000;
float $zMax = -1000000000;

for ($eachObj in $selArray)
{

float $coordArray[] = `xform -q -bb $eachObj`;
if ($coordArray[0] < $xMin)
{
$xMin = $coordArray[0];
}
if ($coordArray[1] < $yMin)
{
$yMin = $coordArray[1];
}
if ($coordArray[2] < $zMin)
{
$zMin = $coordArray[2];
}
if ($coordArray[3] > $xMax)
{
$xMax = $coordArray[3];
}
if ($coordArray[4] > $yMax)
{
$yMax = $coordArray[4];
}
if ($coordArray[5] > $zMax)
{
$zMax = $coordArray[5];
}
}

float $bBoxArray[] = {$xMin,$yMin,$zMin,$xMax,$yMax,$zMax};

return $bBoxArray;
}

/***************************************************************
*** alignHeightLocators
***************************************************************/

global proc alignHeightLocators(){

string $selArray[] = `ls -sl -fl`;
float $bBoxValues[] = `calcTotalBoundingBox`;

setAttr "minDistanceLocator.translateY" ($bBoxValues[1]);
setAttr "maxDistanceLocator.translateY" ($bBoxValues[4]);

};

/***************************************************************
*** doCamSetup
***************************************************************/

global proc doCamSetup(){
string $selArray[] = `ls -sl -fl`;
float $bBoxValues[] = `calcTotalBoundingBox`;
float $xDimension;
$xDimension = abs ($bBoxValues[0] - $bBoxValues[3]);
float $zDimension;
$zDimension = abs ($bBoxValues[2] - $bBoxValues[5]);

// orthowidth
float $orthoWidthValue = 1;
if ($zDimension >= $xDimension){
$orthoWidthValue = $xDimension;
}
else{
$orthoWidthValue = $zDimension;
}
// ortho width
setAttr "renderCam.orthographicWidth" $orthoWidthValue ;
float $sizeRatio = $zDimension / $xDimension; // z/x
int $renderRez = `intField -q -v renderRes_intField`;

// defaultResolution.width / .height
if ($sizeRatio <= 1){
// landscape
setAttr "defaultResolution.width" ($renderRez);
setAttr "defaultResolution.height" ($renderRez * $sizeRatio);
}
else
{
// portrait
setAttr "defaultResolution.width" ($renderRez / $sizeRatio);
setAttr "defaultResolution.height" ($renderRez);
}
// device aspect ratio
setAttr "defaultResolution.deviceAspectRatio" (1 / $sizeRatio) ;
// hide render globals

// pixel aspect ratio

select -r renderCam;
move -a (($bBoxValues[0] + $bBoxValues[3] ) / 2) ($bBoxValues[4] + 5) (($bBoxValues[2] + $bBoxValues[5] ) / 2);
select - r $selArray;
}


/***************************************************************
*** cleanTraFo
***************************************************************/
global proc cleanTraFo(){
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0;
ResetTransformations;
CenterPivot;
}

/***************************************************************
*** shaderSetup
***************************************************************/

global proc doShaderSetup(){

if (`objExists output_surfaceShader`)
{
// do nothing
}
else
{
// min max y values of the two locators, put them in math nodes with an expression
shadingNode -asUtility multiplyDivide;
rename "minDistanceLocatorValue";
expression -s "minDistanceLocatorValue.input1Y = minDistanceLocator.translateY;" -o minDistanceLocatorValue -ae 1 -uc all ;
shadingNode -asUtility multiplyDivide;
rename "maxDistanceLocatorValue";
expression -s "maxDistanceLocatorValue.input1Y = maxDistanceLocator.translateY;" -o maxDistanceLocatorValue -ae 1 -uc all ;

// sampler info node to get the sampled point in world coordinates
shadingNode -asUtility samplerInfo;
rename "worldLocation";

// division top value
shadingNode -asUtility plusMinusAverage;
rename "zaehler";
setAttr zaehler.operation 2; // subtract

//connectAttr -f worldLocation.pointWorld zaehler.input3D[0];
//connectAttr -f worldLocation.pointWorldY zaehler.input3D[1].input3Dy;
connectAttr -f worldLocation.pointWorldY zaehler.input3D[0].input3Dy;
connectAttr -f minDistanceLocatorValue.input1Y zaehler.input3D[1].input3Dy;

// // division bottom value
shadingNode -asUtility plusMinusAverage;
rename "nenner";
setAttr nenner.operation 2;

connectAttr -f maxDistanceLocatorValue.input1Y nenner.input3D[0].input3Dy;
connectAttr -f minDistanceLocatorValue.input1Y nenner.input3D[1].input3Dy;

// division node
shadingNode -asUtility multiplyDivide;
rename "divNode";
setAttr divNode.operation 2;

connectAttr -f zaehler.output3Dy divNode.input1Y;
connectAttr -f nenner.output3Dy divNode.input2Y;

// connect to color values
shadingNode -asShader surfaceShader;
rename "output_surfaceShader";

connectAttr -f divNode.outputY output_surfaceShader.outColorR;
connectAttr -f divNode.outputY output_surfaceShader.outColorG;
connectAttr -f divNode.outputY output_surfaceShader.outColorB;

}
}

/***************************************************************
*** shaderSetup
***************************************************************/

global proc assignShaderToSelection(){

hyperShade -assign output_surfaceShader;

}

/***************************************************************
*** findActiveCameraPanel
***************************************************************/

global proc string findActiveCameraPanel(){
string $allPanels[] = `getPanel -vis`;
string $camPanel = "";
for($i=0;$i<size($allPanels);$i++)
{
if(`match "^modelPanel" $allPanels[$i]` == "modelPanel")
{
global string $activeCamPanel;
$camPanel = $allPanels[$i];
$activeCamPanel = $camPanel;
return $activeCamPanel;
//break;
}
}
//end findActiveCameraPanel
}

/***************************************************************
*** lookThruThisCam
***************************************************************/

global proc lookThruThisCam(string $desiredView){
string $activeCamPanel = `findActiveCameraPanel`;
lookThroughModelPanel ($desiredView) ($activeCamPanel);
//end lookThruThisCam
}

/***************************************************************
*** end
***************************************************************

eof
02-19-2011, 08:17 AM
Thank you! Very useful.

deadalvs
02-19-2011, 11:21 AM
hi !

good to hear .. i did some a small fix in the code.

just copy paste the new code.

CGTalk Moderation
02-19-2011, 11:21 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.