Page 1 of 1

AE selected items trouble

Posted: November 7th, 2007, 8:32 am
by SFR75
Hello,

Second time I run into the same problem when creating a script for After Effects. There is no way to tell which layer is currently selected by the user in the timeline window.

The thing is that I can select one composition in project panel (or nothing at all), yet select a layer in already open tab for any other composition. It looks like AE Object model doesn't reflect this at all.

So basically, if I understood it correctly, there is NO way of telling which layer user have selected in his currently open tab. Is that correct ?

Thanks, M.

Posted: November 7th, 2007, 9:01 am
by Darkmoon_UK
Good news, this is directly referable from the API, though I think its worth building into a function just to perform a bit of checking on the active item. Like so:

Code: Select all

function getSelectedLayers()
{
   var currentComp = app.project.activeItem;
   if(currentComp instanceof CompItem) return(currentComp.selectedLayers); //Return the current selected layers
   return false; //User didn't have a Comp open
}
It returns an array of currently selected layer objects.

Re: AE selected items trouble

Posted: November 7th, 2007, 9:07 am
by redefinery
SFR75 wrote:So basically, if I understood it correctly, there is NO way of telling which layer user have selected in his currently open tab. Is that correct ?
hi SFR75,

if the Project panel is not currently active, the app.project.activeItem attribute should give you the currently (or last) active composition, regardless of the selection in the Project panel. If the Project panel has focus, .activeItem is based on what's selected in that panel, regardless of what's open/active in any Composition viewer.

once you have an activeItem that's a comp, you can get the selected layers by accessing .selectedLayers, or .selectedLayers[0] for the first selected layer in that comp. be sure to check that .activeItem isn't null (for no selection or multiple selection) and is a CompItem, as in something like this:

Code: Select all

var activeComp = app.project.activeItem;
var firstSelectedLayer = null;
if ((activeComp != null) && (activeComp instanceof CompItem))
{
	if (activeComp.selectedLayers.length > 0)
		firstSelectedLayer = activeComp.selectedLayers[0];
}
if you need to ensure that only one layer is selected in the active comp, change the if statement to:

Code: Select all

	if (activeComp.selectedLayers.length == 1)
hope this helps. (excuse any errors in the above code ...i haven't had my morning coffee yet :-) )

:jeff

Posted: November 7th, 2007, 9:23 am
by Darkmoon_UK
Yes, and what jeff said :wink:

Re: AE selected items trouble

Posted: November 7th, 2007, 9:49 am
by SFR75
Darkmoon_UK, redefinery!
Thanks a lot, guys. This is excellent!!!