Skip to main content
videoeditorlvrgmedia
Participant
July 31, 2019
Answered

Can you help me fix my After Effects SCRIPT?

  • July 31, 2019
  • 2 replies
  • 472 views

Hello! I'm trying to change the position of the first layer of all the comps in my After Effects project. This script works with "activeItem" but doesn't work with "items." Can someone help me fix it?

Script:

------------------------------------------

var theComp = app.project.items; 

var theLayer = theComp.layer(1);

var changePos = theLayer.position;

changePos.setValue([540.0,1005.0,0.0]);

    This topic has been closed for replies.
    Correct answer OussK

    this one must work

          //loop through all items in project

          var myitems=app.project.items;

          for (var i = 1; i <= myitems.length; i++){

            var item = myitems;

          if ( item instanceof CompItem ){

            var layer = item.layer(1);

            layer.position.setValue([540.0,1005.0,0.0]);

          }

    }

    2 replies

    OussK
    Community Expert
    OussKCommunity ExpertCorrect answer
    Community Expert
    August 3, 2019

    this one must work

          //loop through all items in project

          var myitems=app.project.items;

          for (var i = 1; i <= myitems.length; i++){

            var item = myitems;

          if ( item instanceof CompItem ){

            var layer = item.layer(1);

            layer.position.setValue([540.0,1005.0,0.0]);

          }

    }

    Martin_Ritter
    Legend
    July 31, 2019

    You'll have to cycle through all items of your project, check if it's a comp, and if so, do the position change.

    That's a for-loop, a if-condition and the stuff you already have.

    Happy coding

    *Martin

    videoeditorlvrgmedia
    Participant
    July 31, 2019

    Like this:

    var theComp = app.project.items; 

    for (var i = 1; i <= theComp; i++) {

    var theLayer = theComp.layer(1);

    var changePos = theLayer.position;

    changePos.setValue([540.0,1005.0,0.0]);

    }

    Martin_Ritter
    Legend
    August 1, 2019

    One step closer... but no.

    app.project.items gives you an array with everything in your project. Well, that's what you need - fine.

    Now, you'll need to go through this array and check every item, if it's a comp or not.

    Your for-loop is wrong and show's me, that you have no glue what you are doing, yet.

    First, arrays always start at 0, therefore it's var i = 0;

    Next, you have to tell what you want to use from the array. If you just put it in like you did, it will be just a long list of objects. What you need is the length, the number of all items.

    This gives you a code like:

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

    Now the concept of a for-loop is, that you have an index variable (i in this case), which you increase with every cycle. In your case the for-loop is pointless because you did not use the index variable nowhere. The for-loop will do the same on the same item over and over again.

    You'll need theComp so make sense of all. With increasing i you are "moving" through the array theComp, point to a new object on every cycle.

    I don't like to write your script in a whole, because you won't learn nothing if I do so. Therefore, keep going. You are on the right way.

    The first thing to do in the for-loop is to check if the present item is a comp. You need a if-condition for this.

    Keep me updated.

    *Martin