PDA

View Full Version : Expression affect render performance?


braam
01-29-2003, 09:38 AM
I wrote two versions of expression which would have the same results when rendering.

> 3 locators positioned at different Y-distance from plane

> Plane's incandesent is mapped to ramp.

> Ramp's U Coordinate attribute is connected by the output of setDistanceRange (Set Range node)

> I use expression to determine the distance between the point (Sampler info) and the position of every locator. Determined values are stored into the array which then sorted out to get the minimum value at $array[0]. The minimum value then connect to setDistanceRange.valueX.

si = sampler info node
getDistanceRange = set range node

Version 1: 3 mins to render
float $array[];
int $i;

for ($i=1;$i<4;$i++)
{

float $a = eval("getAttr locator"+$i+".translateX");
float $b = eval("getAttr locator"+$i+".translateY");
float $c = eval("getAttr locator"+$i+".translateZ");

$array[$i-1] = mag(<<$a-si.pointWorldX,$b-si.pointWorldY,
$c-si.pointWorldZ>>);
}

$array = sort($array);
setDistanceRange.valueX = $array[0];


Version 2: 4 seconds to render
float $array[];

$array[0] = mag(<<locator1.translateX-si.pointWorldX, locator1.translateY-si.pointWorldY, locator1.translateZ-si.pointWorldZ>>);

$array[1] = mag(<<locator2.translateX-si.pointWorldX, locator2.translateY-si.pointWorldY, locator2.translateZ-si.pointWorldZ>>);

$array[2] = mag(<<locator3.translateX-si.pointWorldX, locator3.translateY-si.pointWorldY, locator3.translateZ-si.pointWorldZ>>);

$array = sort($array);
setDistanceRange.valueX = $array[0];

What make the rendering slow?

Judge Drury
01-29-2003, 10:14 AM
From the Maya 4.0 docs...

Avoid using the getAttr and setAttr commands in expressions. Using these commands can slow down the performance of your expressions, and they do not create dependency graph node connections.

:bounce:

stunndman
01-29-2003, 10:16 AM
from a programmer's view and at a quick glance

your first code fragment's loop redeclares three variables three times - so it's nine times per execution

because you declare the variables within the loop they are destroyed and recreated everytime the loop starts over (and this can be expensive with an interpreter language like MEL)

try declaring your variables outside of the loop and just assign them within the loop

and post the new result

still the second version is more compact and definitely preferable

stunndman
01-29-2003, 10:48 AM
Avoid using the getAttr and setAttr commands in expressions. Using these commands can slow down the performance of your expressions, and they do not create dependency graph node connections.


that's definitely an additional factor in your comparison

maya's expressions obviously have some kind of special optimization layer that is not available in normal MEL scripts

only in expressions you can access attributes of nodes directly by using the dot notation (e.g. locator2.translateY) - using the getAttr does circumvent this optimization and definitely adds to the high discrepancy

very well pointed out by Judge Drury

alexx
01-29-2003, 11:03 AM
nothing to add really concerning the solution, since i guess it is the getAttr..

and remember: since you are using a sampler info, your expression gets evaluated a hell lot (for each and every sampled pixel)

if you want to check the speed and evaluation time of your code to see what is the better solution you can use the MEL command:
timerX

cheers

alexx

braam
01-29-2003, 11:47 AM
Stunndman and Judge Drury, thanx for the replies.

I used 3 locators and the script will be like below...

float $array[];

$array[0] = mag(<<locator1.translateX-si.pointWorldX, locator1.translateY-si.pointWorldY, locator1.translateZ-si.pointWorldZ>> );

$array[1] = mag(<<locator2.translateX-si.pointWorldX, locator2.translateY-si.pointWorldY, locator2.translateZ-si.pointWorldZ>> );

$array[2] = mag(<<locator3.translateX-si.pointWorldX, locator3.translateY-si.pointWorldY, locator3.translateZ-si.pointWorldZ>> );

$array = sort($array);
setDistanceRange.valueX = $array[0];

...but is it possible to count the locators in the worldspace and determine distances? Like if you have 100+ locators in the space and I don't want to type long expression.

Any idea how I will get short expression?

braam
01-29-2003, 11:58 AM
thanks alexx

I will check TimerX to improve my knowledge in MEL. Thanks for planting a seed in my brain!

:thumbsup:

cheers
braam

CGTalk Moderation
01-14-2006, 07:00 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.