Skip to main content
Teguh82
Inspiring
May 25, 2014
Answered

I need help with my first AE CS5 Script

  • May 25, 2014
  • 3 replies
  • 699 views

Hi designers fellow out there,

I newbie on AE Scripting here. I run my CS 5 on Win 7. I try to make GUI to create a simple solid and control it opacity through slider. I fail and don't now how to make it works. Here is the code I just wrote as referenced to many source and tutorials:

myComp = app.project.item(1); //akses comp 1

mySolid = myComp.layers.addSolid([1.0,1.0,0], "my square", 50, 50, 1);//solid

var myWin = new Window("palette", "Transform", undefined); //create pallete

    myWin.orientation = "row";

var myOpacity = myWin.add("panel", undefined, "Solid Opacity");

    myOpacity.orientation = "column";

   

    myOpacity.add("statictext", undefined, "Kontrol Opacity");

   

    var opacitysliderCtrl = myOpacity.add("slider", undefined, 50, 0, 100);//make slider

   

    var layerOpacity = mySolid.property("opacity");

  

    opacitysliderCtrl.onChanging = function()

  {

         var val = Math.round(opacitysliderCtrl.value);

         layerOpacity.value = val;

  };

myWin.center();

myWin.show();

Please help me to see the problem here. Thanks

This topic has been closed for replies.
Correct answer UQg

There is more wrong than right in your piece of work

As it is now you script attaches a slider to a single layer (mySolid, the one you add at the beginning).

That's not very useful, you can't use that slider for other layers.

You should also keep in mind that that solid can be deleted, hence the mySolid object can become invalid (refering to an solid that doesnt exist anymore).

I don't know what exactly is your plan, but usually a script does not define any AE-related object in its many body (upon launching).

Only when the user clicks/changes a widget the script tries to retrieve a comp, layer, property or whatever that the user has specified through "some kind of secret code" (most of the time: the comp, layer, property, etc, is selected).

If you wanted the slider to control the opacity value of all selected layers in the active comp, it would be something like this:

var myWin = new Window("palette", "Transform", undefined); //create pallete

myWin.orientation = "row";

var myOpacity = myWin.add("panel", undefined, "Solid Opacity");

myOpacity.orientation = "column";

myOpacity.add("statictext", undefined, "Kontrol Opacity");

var opacitysliderCtrl = myOpacity.add("slider", undefined, 50, 0, 100);//make slider

opacitysliderCtrl.onChanging = function onOpacitySliderChanging(){

 

        // retrieve the AE things that have to be handled: in this case, all selected layers in the active comp

        var myComp, myLayer, n;

        var layerOpacity, val;

     

        myComp = app.project.activeItem;

        if (myComp.numLayers>0){

            for (n=0; n<myComp.selectedLayers.length; n++){

                myLayer = myComp.selectedLayers[0];

                if (myLayer.hasVideo){

                    // you forgot the .transform there:

                    var layerOpacity = myLayer.transform.opacity;

                    // var val = Math.round(opacitysliderCtrl.value) can work but the following is better

                    var val = Math.round(this.value);

                    // you can't set a property's value by just writing property.value=val

                    // you must use setValue:

                    // layerOpacity.value = val;

                    layerOpacity.numKeys ? layerOpacity.setValueAtTime(myComp.time, val): layerOpacity.setValue(val);

                    };

                };

            };

        return;

        };

myWin.center();

myWin.show();

Xavier

3 replies

Teguh82
Teguh82Author
Inspiring
May 26, 2014

Hi, UQj, can you help me some more with few questions? I add some code on your code above to call the value of the onChanging opacity and succeed (thanks to your explanation so I mention how to call it). My problem begin when I wanna add more panel for scale. First, I fail to get access to "scale" property with same code then I guess it's need different code, am I right? Is it possible to give multiple order (for opacity, scale, and rotation) at single "function"? And how? Thanks for helping. Here is my last code:

var myWin = new Window("palette", "Transform", undefined); //create pallete

myWin.orientation = "row";

var myOpacity = myWin.add("panel", undefined, "Solid Opacity");

myOpacity.orientation = "column";

myOpacity.add("statictext", undefined, "Kontrol Opacity");

opacitydisplayTextLbl = myOpacity.add("statictext", undefined, "Current value:");

opacitydisplayLbl = myOpacity.add("statictext", undefined, "100");

var opacitysliderCtrl = myOpacity.add("slider", undefined, 50, 0, 100);//make slider

opacitysliderCtrl.onChanging = function onOpacitySliderChanging(){

        // retrieve the AE things that have to be handled: in this case, all selected layers in the active comp

        var myComp, myLayer, n;

        var layerOpacity, val;

      

        myComp = app.project.activeItem;

        if (myComp.numLayers>0){

            for (n=0; n<myComp.selectedLayers.length; n++){

                myLayer = myComp.selectedLayers[0];

                if (myLayer.hasVideo){

                    // you forgot the .transform there:

                    var layerOpacity = myLayer.transform.property("opacity");

                    // var val = Math.round(opacitysliderCtrl.value) can work but the following is better

                    var val = Math.round(this.value);

                    // you can't set a property's value by just writing property.value=val

                    // you must use setValue:

                    // layerOpacity.value = val;

                    opacitydisplayLbl.text = val;//set current value

                    layerOpacity.numKeys ? layerOpacity.setValueAtTime(myComp.time, val): layerOpacity.setValue(val);

                    };

                };

            };

        return;

        };

myWin.center();

myWin.show();

Sorry that I learn it really slow as a beginer

UQg
Legend
May 26, 2014

I just read your other post on about the same topic: it is true that writing a script to change a layer's opacity isn't that useful since the built-in way is already very efficient.

The built-in way works like this: Select the layers you want to target, hit "T" on the keyboard: it reveal the Transform>Opacity property of all selected layers, and hides anything else.

Then you just need to scrub the opacity text of one of the layers to offset the opacity of all layers by some amount, or you can set a new value by entering a new value in the edit text, etc.

Way simpler.

It is the same principle for Scale (hit "S"), for Position (hit "P"), and for Anchor Point (hit "A"). Please confirm that you definitely want to do that by script.

Xavier.

Teguh82
Teguh82Author
Inspiring
May 26, 2014

Yes, I want to do that by script. It's not only because I need it but also because I wanna dig deeper on it and get more understanding about what the extend possibility to do with script. My office just move (3 weeks ago) from flash to AE and up till now we do animation in AE manually (I mean never use script or expression). It's terrible and make us move really slow. The reason is that no body have ability in English except me (as almost the tutorial and designer communities are in English)  We are small business still grow company who dont have enough to hire more people to do it either. That's why I do some experiment here to learn more. Honestly I'm not really sure that I start with the right thing here. Thanks for your help and I think will still need your guidance more in the future

Teguh82
Teguh82Author
Inspiring
May 26, 2014

Hi UQj, Thanks for help. I think you grasp my point somehow. I new here and still trying to figure out how to think like programmer. You help me a lot with you code and explanations. Thanks again

UQg
UQgCorrect answer
Legend
May 25, 2014

There is more wrong than right in your piece of work

As it is now you script attaches a slider to a single layer (mySolid, the one you add at the beginning).

That's not very useful, you can't use that slider for other layers.

You should also keep in mind that that solid can be deleted, hence the mySolid object can become invalid (refering to an solid that doesnt exist anymore).

I don't know what exactly is your plan, but usually a script does not define any AE-related object in its many body (upon launching).

Only when the user clicks/changes a widget the script tries to retrieve a comp, layer, property or whatever that the user has specified through "some kind of secret code" (most of the time: the comp, layer, property, etc, is selected).

If you wanted the slider to control the opacity value of all selected layers in the active comp, it would be something like this:

var myWin = new Window("palette", "Transform", undefined); //create pallete

myWin.orientation = "row";

var myOpacity = myWin.add("panel", undefined, "Solid Opacity");

myOpacity.orientation = "column";

myOpacity.add("statictext", undefined, "Kontrol Opacity");

var opacitysliderCtrl = myOpacity.add("slider", undefined, 50, 0, 100);//make slider

opacitysliderCtrl.onChanging = function onOpacitySliderChanging(){

 

        // retrieve the AE things that have to be handled: in this case, all selected layers in the active comp

        var myComp, myLayer, n;

        var layerOpacity, val;

     

        myComp = app.project.activeItem;

        if (myComp.numLayers>0){

            for (n=0; n<myComp.selectedLayers.length; n++){

                myLayer = myComp.selectedLayers[0];

                if (myLayer.hasVideo){

                    // you forgot the .transform there:

                    var layerOpacity = myLayer.transform.opacity;

                    // var val = Math.round(opacitysliderCtrl.value) can work but the following is better

                    var val = Math.round(this.value);

                    // you can't set a property's value by just writing property.value=val

                    // you must use setValue:

                    // layerOpacity.value = val;

                    layerOpacity.numKeys ? layerOpacity.setValueAtTime(myComp.time, val): layerOpacity.setValue(val);

                    };

                };

            };

        return;

        };

myWin.center();

myWin.show();

Xavier