25 Replies Latest reply: Jan 4, 2014 3:30 PM by cty71 RSS

    image export/import script won't work, need guru help

    zipkiev Community Member

      hi guys, i try export image from AE to png file and import this file back to AE with adding to timeline via script, exporting working excellent but importing not work....

      script:

       

      function test()

      {

      var myProj = app.project;

      var myComp = myProj.activeItem;

      app.project.activeItem.saveFrameToPng(app.project.activeItem.time, new File("C:\\Users\\UserName\\Desktop\\1111\\2test.png"));

      var importOptions = new ImportOptions("C:\\Users\\UserName\\Desktop\\1111\\2test.png");

      var importImg = app.project.importFile (importOptions);

      myComp.layers.add(importImg);

      }

       

      thanks

        • 1. Re: image export/import script won't work, need guru help
          David Torno Community Member

          You are not referencing a file object for your ImportOptions argument, only a string. You may want to cleanup the function a bit too.

           

          Instead of this:

          function test(){

               var myProj = app.project;

               var myComp = myProj.activeItem;

               app.project.activeItem.saveFrameToPng(app.project.activeItem.time, new File("C:\\Users\\UserName\\Desktop\\1111\\2test.png"));

               var importOptions = new ImportOptions("C:\\Users\\UserName\\Desktop\\1111\\2test.png");

               var importImg = app.project.importFile (importOptions);

               myComp.layers.add(importImg);

          }

           

          Try this this setup. It's a little more organized and you don't need to call the activeItem everytime, just the one time and store it in a variable.

           

          function test(){

               var myProj = app.project;

               var myComp = myProj.activeItem;

               var myFile = new File("C:\\Users\\UserName\\Desktop\\1111\\2test.png");

               if(myComp instanceof CompItem){

                    myComp.saveFrameToPng(myComp.time, myFile);

                    var importOptions = new ImportOptions(myFile);

                    var importImg = myProj.importFile (importOptions);

                    myComp.layers.add(importImg);

               }

          }

          • 2. Re: image export/import script won't work, need guru help
            zipkiev Community Member

            thanks, it exactly is what i need

            • 3. Re: image export/import script won't work, need guru help
              zipkiev Community Member

              I found a bug when I use a command app.project.activeItem.saveFrameToPng in  script, all Created Pictures(png file) have a black glow around the perimeter ( they are not suitable in this condition for further work with them) can this bug be correct?

               

              Oh... then everything was just perfect with the script , (it exports and imports pictures as I wanted) to but because of this problems script useless .... there is clearly a problem in app.project.activeItem.saveFrameToPng , so bad

               

              Please someone check with app.project.activeItem.saveFrameToPng (app.project.activeItem.time, new File ("C: \ \ Users \ \ UserName \ \ Desktop \ \ 1111 \ \ test.png")); and say you have black glow on image?

               

              PS Function "Composition> Save frame As ...> file" works as it should and frames are stored, without black on image

               

               

              what are the other alternatives? , such as can be possible to use Composition> Save frame As ...> file via script to automatical save to png in one click?

               

              sorry for bad english

              • 4. Re: image export/import script won't work, need guru help
                David Torno Community Member

                Odd, I haven't come across this issue, I also haven't tested it on PC though. It works fine on Mac. I can try it on a PC machine at the office, but that won't be until Monday unfortunately.

                • 5. Re: image export/import script won't work, need guru help
                  zipkiev Community Member

                  ok,thx

                   

                  PS this image from AE shows this issue(i have issue in cs 5.5 and cs6  )

                  examle_exporttopng.jpg

                  • 6. Re: image export/import script won't work, need guru help
                    David Torno Community Member

                    Ah, that looks like an image with alpha that is not set to premultiply.

                    • 7. Re: image export/import script won't work, need guru help
                      zipkiev Community Member

                      if i use Composition> Save frame As ...> file " then set png sequence, rgb+alpha AE rendering png file with good alpha, but if i use app.project.activeItem.saveFrameToPng AE creates  png with black, i try google search for more options for saveFrameToPng but didn't find anything, haw i can fix this "premultiply" bug?

                      • 8. Re: image export/import script won't work, need guru help
                        Arie Stavchansky Community Member

                        You may need to create a specific Render Template (Edit > Templates > Render Template) for exporting a composition to PNG + alpha.  Then, instead of using app.project.activeItem.saveFrameToPng, you would simply need to write code to add the current frame of the current composition into your render queue, and then render it out to a file of your choice.

                         

                        Here is some code to help you understand what I am suggesting:

                         

                         

                        var renderComp = app.project.activeItem,
                            frame = 15; //assumes you know how to get the current position in the comp's timeline as a frame number
                        
                        
                        app.project.renderQueue.items.add(renderComp);
                        app.project.renderQueue.item(1).applyTemplate("CUSTOM PNG RENDER TEMPLATE");
                        app.project.renderQueue.item(1).outputModule(1).applyTemplate("CUSTOM PNG OUTPUT TEMPLATE")
                        app.project.renderQueue.item(1).outputModule(1).file = File("/path/to/where/you/want/to/save/export/png");
                        app.project.renderQueue.item(1).timeSpanStart = (frame/renderComp.frameRate);
                        app.project.renderQueue.item(1).timeSpanDuration = renderComp.frameDuration;
                        
                        
                        app.project.renderQueue.render();
                        
                        

                         

                        Your file should now be rendered as if it was rendered out of the render queue with your custom render template.

                         

                        Best,

                        Arie

                        • 9. Re: image export/import script won't work, need guru help
                          David Torno Community Member

                          Curious, have you tried setting the image alpha to premultiply when importing it back in, or are these png outputs being used outside of AE?

                           

                          Pg. 70 in the CS6 Scripting Guide

                           

                          FootageSource alphaMode attribute:

                          app.project.item(index).mainSource.alphaMode

                          app.project.item(index).proxySource.alphaMode

                           

                          Description:

                          The alphaMode attribute of footageSource defines how the alpha information in the footage is to be inter- preted. If hasAlpha is false, this attribute has no relevant meaning.

                           

                          Type:

                          An AlphaMode enumerated value; (read/write). One of:

                           

                          AlphaMode.IGNORE

                          AlphaMode.STRAIGHT

                          AlphaMode.PREMULTIPLIED

                          • 10. Re: image export/import script won't work, need guru help
                            David Torno Community Member

                            Another thing you could try too, is to make sure there is a solid as the bottom layer in the composition, so the exported PNG would not have an alpha issue. This is of course is assuming that you do not need the alpha on this export.

                            • 11. Re: image export/import script won't work, need guru help
                              zipkiev Community Member

                              Hi guys...

                              Arie Stavchansky

                              thx, i will try this

                               

                              David Torno

                              thx David for try helping, but i don't understend how i can use this code

                              AlphaMode.IGNORE

                              AlphaMode.STRAIGHT

                              AlphaMode.PREMULTIPLIED

                               

                              i used command  saveframetopng() where i can set alpha mode for it(or for layer what i used for export)

                              now problam in savefremetopng 100%, becouse when i save frame from AE to windows folder this frame already has black!!

                               

                              i need export good png, becouse i use this png in other place(in 2d game engine) so i need true alpha image without black

                               

                              sorry for bad english

                              • 12. Re: image export/import script won't work, need guru help
                                David Torno Community Member

                                i need export good png, becouse i use this png in other place(in 2d game engine) so i need true alpha image without black

                                Ah, ok. The alpha mode isn't gonna help you then. It's a process within AE to interperet the alpha channel of an asset for use in AE.

                                 

                                I think Arie's solution may be your best bet. It's more involved to create the PNG, but should do solidly for you.

                                • 13. Re: image export/import script won't work, need guru help
                                  zipkiev Community Member

                                  i used Arie Stavchansky method , and it works(there are the problems with adding 00000(number of frame) to file name, and fix this with renaming file name), but now i have new question, has AE script can check if template preset exist? then select it, or create new template via script?

                                  • 14. Re: image export/import script won't work, need guru help
                                    Arie Stavchansky Community Member

                                    Glad you were able to get it to work. I now recall that file naming bug.  Glad you fixed that one, too.

                                     

                                    You can retrieve an array of available templates via some method that I cannot recall at the moment.  Do a search / find in the Scripting guide for "templates" and you'll find the code.  Once you have that array, you can write code to check and see if the one you want to use is in there.  If it is, use it.

                                     

                                    When I'm back at my desktop I can provide more details, but maybe that's a good start.

                                     

                                    --Arie

                                    • 15. Re: image export/import script won't work, need guru help
                                      Arie Stavchansky Community Member

                                      Here is the documentation I was referring to in my last post.  On page 168 of the CS6 guide:

                                       

                                       

                                      RenderQueueItem templates attribute

                                      app.project.renderQueue.item(index).templates Description

                                       

                                       

                                      The names of all Render Settings templates available for the item. See also “RenderQueueItem saveAsTem- plate() method” on page 167.

                                      Type

                                      Array of strings; read-only.

                                       

                                      • 16. Re: image export/import script won't work, need guru help
                                        zipkiev Community Member

                                        also i have problem with render Queue(if RQ has not rendered/rendered items), for example i need save all renders what i do early, but if i lunch script it not work properly and do something weird(like rendering all files in render Queue, or rendering one file but not my one frame, or other stuff...) now for code work i try do loop, for cleaning render Queue and after that add my file render preset(but i want to save all previous render if its possible?)

                                        example of code:

                                         

                                         

                                        for (i = 1; i <=app.project.renderQueue.numItems; i++)

                                                            {

                                                            app.project.renderQueue.item(i).remove();

                                                            }

                                         

                                                            frame=activeframenimber;

                                                            app.project.renderQueue.items.add(renderComp);

                                                            RQindex=app.project.renderQueue.numItems;

                                                            app.project.renderQueue.item(RQindex).applyTemplate("test_template");

                                                            app.project.renderQueue.item(RQindex).outputModule(1).applyTemplate("test_template2") //can find how i can create this template via script

                                                            app.project.renderQueue.item(RQindex).outputModule(1).file = File("C:\\Users\\username\\Desktop\\1111\\"+filename);

                                                            app.project.renderQueue.item(RQindex).timeSpanStart = (frame/renderComp.frameRate);

                                                            app.project.renderQueue.item(RQindex).timeSpanDuration = renderComp.frameDuration;

                                                            app.project.renderQueue.render(RQindex);

                                                            app.project.renderQueue.item(RQindex).remove();

                                         

                                        Also i try add RQindex to set index element of renderQueue, i think AE script must work with  RQindex index, but it not work, so for now i must clean render queue every time

                                        • 17. Re: image export/import script won't work, need guru help
                                          zipkiev Community Member

                                          UPD

                                          just see:

                                          this code not work

                                          for (i = 1; i <=app.project.renderQueue.numItems; i++)

                                                              {

                                                              app.project.renderQueue.item(i).remove();

                                                              }

                                           

                                          and this also

                                          var i=0;

                                          while (i <app.project.renderQueue.numItems)

                                          {

                                          i++;

                                          app.project.renderQueue.item(i).remove();

                                          }

                                           

                                          what wrong with it? how i can clean my RQ?

                                          • 18. Re: image export/import script won't work, need guru help
                                            Dan Ebberts Community Member

                                            You should probably start at the end of the queue and move backwards:

                                             

                                            for (var i = app.project.renderQueue.numItems; i > 0; i--)

                                                                {

                                                                app.project.renderQueue.item(i).remove();

                                                                }

                                             

                                             

                                            Dan

                                            • 19. Re: image export/import script won't work, need guru help
                                              zipkiev Community Member

                                              tnx Arie, for your reply, but i don't understand from CS6 script guide how i can create template from scratch(via script), i think method   

                                              RenderQueueItem saveAsTemplate() get a already created item present in RQ and save it to templates?

                                               

                                              thx also Dan, back loop is working fine, but who know? can i use my script without cleaning RQ or its is bug of AE? and AE cant add new item without affecting on other items that is already in Queue?

                                               

                                               

                                              with 2014 Year Guys thx2all

                                              • 20. Re: image export/import script won't work, need guru help
                                                Dan Ebberts Community Member

                                                When you remove item 1, the remaining items get renumbered, so when you next remove item 2, it is actually the original item 3, and the original item 2 (now item 1) gets left behind. I guess you could continually remove item 1 until the queue is empty (but I've never tried it).

                                                 

                                                Dan

                                                • 21. Re: image export/import script won't work, need guru help
                                                  Arie Stavchansky Community Member

                                                  I'm not sure it is possible to create a render template via scripting.  I have not tried it, and I've always assumed that a user would create the template via AE's GUI.  If you find out how to do it, please let us know.

                                                  • 22. Re: image export/import script won't work, need guru help
                                                    zipkiev Community Member

                                                    thx for reply, i searched in internet and i think creating templates via script is impossible?, i want cheaty i try find my presets(test_template, test_template2) in AE files for writing my templates to this file(via script), and i find this AE preference file, but structure of this AE preference file is totally not readable ( for AE cs5.5 in windows it stores here C:\Users\username\AppData\Roaming\Adobe\After Effects\10.5\Adobe After Effects 10.5-x64 Prefs.txt) so, for now i will be ignoring, this problem.....oh so bad....

                                                     

                                                    PS also i cant find(in google,ae scripting sites) example for working with templates array, who can post little example?

                                                     

                                                    PS2 AE scripts has command something like mycomp.SetFocus()?  because when i add comp to render, focus transfer to RQ, but i need get it back to my comp.

                                                    • 23. Re: image export/import script won't work, need guru help
                                                      cty71 Community Member

                                                      Dan!  I'm trying to understand how to set alphaMode on import.  For some reason a few of my imported sequences have 'alpha ignored" no matter how I set up the import preferences.  I am able to use remeber interpretation but would like to avoid having to do this step and have the import script set the alpha mode.  I've modified the SmartImport script a bit to import and drop the files into a comp.  I'd also like to force the alphamode to be premultiplied at the same time.  Is there a simple way to do this? 

                                                       

                                                       

                                                      This is what I have:

                                                       

                                                                     var myFootageItems = [];

                                                                         var myFootage = app.project.importFile(importOptions);

                                                                         myFootageItems.push(myFootage);

                                                                         for (var i = 0; i < myFootageItems.length; i++){

                                                       

                                                              //add footage to comp

                                                       

                                                                         myComp.layers.add(myFootageItems[i],masterComp.duration);

                                                      • 24. Re: image export/import script won't work, need guru help
                                                        Dan Ebberts Community Member

                                                        I haven't tried this, but I think it would be like this:

                                                         

                                                        myFootageItems[i].mainSource.alphaMode = AlphaMode.PREMULTIPLIED;

                                                         

                                                         

                                                        Dan

                                                        • 25. Re: image export/import script won't work, need guru help
                                                          cty71 Community Member

                                                          Thanks Dan!  Now I get it!!!  ;)