how to make value persistent

Moderators: Disciple, zlovatt

Post Reply
pirelouit
Posts: 18
Joined: January 16th, 2006, 12:08 pm

Hi! I made an expression (applied on timeRemap of a sequence) that displays a specific frame of a sequence based on the position of a guide layer. If the guide layer (called "temoin" in the following code) is over a certain reference layer (in "targetComp" in the code), it then displays a specific frame based on the reference layer index. The problem is that by default it always resets to 1. I would like it to keep the last valid value. Is there a a way to achieve this?

Thanks,
Louis

Code: Select all

targetComp = comp("as_claydog");
temoin = thisComp.layer("temoin");
curClosestIndex = null;
previousFrame = timeRemap;

for (var i = 1; i <= targetComp.numLayers; i++)
{
	layerPos	= targetComp.layer(i).position;
	layerWidth	= targetComp.layer(i).width/2*(targetComp.layer(i).scale[0]/100);
	layerHeight= targetComp.layer(i).height/2*(targetComp.layer(i).scale[1]/100);
	minX		= layerPos[0] - (layerWidth);
	maxX		= layerPos[0] + (layerWidth);
	minY		= layerPos[1] - (layerHeight);
	maxY		= layerPos[1] + (layerHeight);
	curPos	= temoin.position;
	if ( curPos[0] > minX && curPos[0] < maxX)
	{
		if ( curPos[1] > minY && curPos[1] < maxY)
		{
			curClosestIndex = i;
		}
	}
}

if (curClosestIndex != null)
{
	frames = curClosestIndex -1;
	fps = 1.0 / thisComp.frameDuration;
	timeRemap = framesToTime(frames, fps );
} else {
	timeRemap = previousFrame;
}
Mylenium
Posts: 139
Joined: July 20th, 2005, 12:07 am

You need to create another loop that forces the expression to evaluate for every frame while(x < thisComp.duration){}.

Mylenium
[Pour Mylène, ange sur terre]
pirelouit
Posts: 18
Joined: January 16th, 2006, 12:08 pm

Thank you, it worked perfectly. Here is the final script.

Code: Select all

targetComp		= comp("as_claydog");
temoin				= thisComp.layer("temoin");
curClosestIndex	= null;
timeIndex			= time;
fps					= 1.0 / thisComp.frameDuration;

while (curClosestIndex == null && timeIndex > temoin.startTime)
{
	curClosestIndex = evalPos(targetComp, temoin, timeIndex);
	timeIndex = timeIndex - framesToTime(1, fps );
}

frames		= curClosestIndex -1;
timeRemap	= framesToTime(frames, fps );


function evalPos(targetComp, temoin, timeIndex)
{
	for (var i = 1; i <= targetComp.numLayers; i++)
	{
		layerPos		= targetComp.layer(i).position.valueAtTime(timeIndex);
		layerWidth	= targetComp.layer(i).width/2*(targetComp.layer(i).scale.valueAtTime(timeIndex)[0]/100);
		layerHeight	= targetComp.layer(i).height/2*(targetComp.layer(i).scale.valueAtTime(timeIndex)[1]/100);
		minX			= layerPos[0] - (layerWidth);
		maxX			= layerPos[0] + (layerWidth);
		minY			= layerPos[1] - (layerHeight);
		maxY			= layerPos[1] + (layerHeight);
		curPos			= temoin.position.valueAtTime(timeIndex);
		if ( curPos[0] > minX && curPos[0] < maxX)
		{
			if ( curPos[1] > minY && curPos[1] < maxY)
			{
				return(i);
			}
		}
	}
	return(null);
}
Post Reply