8 Replies Latest reply on Sep 15, 2017 3:43 AM by UQg

    How to Get the Active Comp

    justin2taylor Level 1

      Does anyone know of a way to consistently access the active Comp? I'm currently using app.project.activeItem, however that only works if the last thing you selected was the comp or timeline. If you have a footage item selected in your project panel, for example, then I don't see a way to access the active Comp. Any thoughts are appreciated, thanks!

        • 1. Re: How to Get the Active Comp
          Christian Lett Level 3

          Hi Justin,

           

          This looks like a question for the AE Scripting forum. You'll probably have more luck finding an answer to this over there as this is for the C++ SDK.

          • 2. Re: How to Get the Active Comp
            Mike_Abbott Level 4

            justin2taylor  wrote

             

            ... If you have a footage item selected in your project panel, for example, then I don't see a way to access the active Comp....

             

            If you have a footage item selected in your project panel, then I don't think you have an active comp.

            • 3. Re: How to Get the Active Comp
              Alan_AEDScripts Level 2

              Mike is right. There are some new methods in scripting now...   .openInViewer()  and  the added viewer object, that would allow you more control. It is a scripting job.

               

              Viewer object — After Effects Scripting Guide

              • 4. Re: How to Get the Active Comp
                justin2taylor Level 1

                Came back to this same issue after a few months and made this workaround:

                 

                app.activeViewer.setActive();
                var activeComp = app.project.activeItem;
                

                 

                Basically setActive() will ensure that the project has an activeItem, and that that activeItem is not what is selected in the Project Panel, but in the Composition Panel.

                • 5. Re: How to Get the Active Comp
                  Tomas Sinkunas Adobe Community Professional

                  This isn't necessarily true, because you might have footage/layer in the viewer.

                  • 6. Re: How to Get the Active Comp
                    justin2taylor Level 1

                    Tomas, that's a good point, this method doesn't work if you have the Footage Panel or a Layer Panel in focus instead of the Composition panel. It's the best solution I can find, but if you know of a better method let me know. Thanks!

                    • 7. Re: How to Get the Active Comp
                      Tomas Sinkunas Adobe Community Professional

                      Nope, nothing better out there.

                      I had your solution in mind, but didn't want to propose it here, as it's not 100% accurate.

                      • 8. Re: How to Get the Active Comp
                        UQg Level 4

                        This is what i've been using for some time now, it works ok but is much circonvoluted.

                        It would definitely help a lot if the Viewer object had an attribute telling what that viewer is actually showing.

                         

                        In this snippet, setActive is called only if necessary (to prevent the graphic focus ring to show up every time - annoying) and only if the active viewer is of type "COMPOSITION" (other types considered ambiguous).

                        If called twice in a row, it is most likely that the second time it will return a comp, even if the first time it returned null (because of the use of setActive).

                         

                        function activateCompViewer(){
                            // setActive is supposed (guide) to return a Boolean, but in practice it returns nothing, therefore this doesnt work:
                            // return app.activeViewer && app.activeViewer.type===ViewerType.VIEWER_COMPOSITION && app.activeViewer.setActive();
                           
                            var A = (app.activeViewer && app.activeViewer.type===ViewerType.VIEWER_COMPOSITION);
                            if (A) app.activeViewer.setActive();
                            return A;
                            };
                        function getActiveComp(){
                           
                            var comp;                                // the returned quantity
                            var X = app.project.activeItem;    // the initial activeItem
                            var selComp = app.project.selection.length ===1 && app.project.selection[0].typeName === "Composition" ? app.project.selection[0] : null; // the unique selected comp, or null
                            var temp;
                           
                            if (X instanceof CompItem){
                                if (selComp === null){
                                    comp = X;
                                    }
                                else if (selComp !== X){
                                    comp = null; // ambiguity : the timeline panel is active, X is the front comp, but another comp is selected
                                    }
                                else{
                                    // X and selComp coincide
                        
                                    X.selected = false;
                                    temp = app.project.activeItem;
                                    X.selected = true;
                                   
                                    if (temp === null){
                                        // the project panel is active and the active item was initially a selected comp
                                        // if that comp is already opened in a viewer and is the front comp, return the comp, else : ambiguity
                                        comp = (activateCompViewer() && app.project.activeItem === X) ? X : null;
                                        }
                                    else{
                                        // deselecting the comp didnt change the activeItem : the timeline panel is active, and the active item was the front comp, that happened to be also selected.
                                        comp = X;
                                        };
                                    };
                                }
                            else{
                                comp = activateCompViewer() ? app.project.activeItem : null;
                                };
                            return comp;
                            };
                        var comp = getActiveComp();
                        alert(comp && comp.name);
                        

                         

                         

                        Xavier

                        2 people found this helpful