AE selected items trouble

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
SFR75
Posts: 17
Joined: April 3rd, 2007, 10:49 am

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.
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

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.
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: CA
Contact:

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
Darkmoon_UK
Posts: 62
Joined: September 5th, 2006, 3:45 am
Location: Chiswick, London, UK
Contact:

Yes, and what jeff said :wink:
SFR75
Posts: 17
Joined: April 3rd, 2007, 10:49 am

Darkmoon_UK, redefinery!
Thanks a lot, guys. This is excellent!!!
Post Reply