Expressions/Scripts/Presets

Moderator: Paul Tuersley

 
jordanwade33
Topic Author
Posts: 11
Joined: Mon Dec 08, 2014 11:11 pm

Copy Effect with Parameters

Fri Nov 06, 2015 1:31 pm

I'm struggling to come up with an easy solution to my problem, but I think there should be one out there. What I'm looking to do is to use a script to copy an effect from one layer to another along with it's parameters. The trick is that every effect has a different amount of properties and can have sub-properties at different property depths. I can easily find the effect's matchname and add it to layer with .Effects.addProperty(matchname) but that doesn't carry it's parameters.

Is the only way to accomplish this by cycling the script through all the possible parameters of an effect, storing the values in an array, and then running through the new effect on a different layer and retrieve the value through the array? Does anyone have a better idea? I'm still new to scripting so it's a battle between efficiency and how I think things should work. Thanks!
 
beginUndoGroup
Posts: 77
Joined: Tue Nov 27, 2012 6:41 am

Re: Copy Effect with Parameters

Sat Nov 07, 2015 1:56 am

It's not easy, and even if you write a decent function that does it, it will frequently generate errors due to properties for which script cannot read/set values, like curves, histogram, etc: all properties that have propertyValueType: CUSTOM_VALUE or NO_VALUE.

A workaround it to use the menu commands "Copy" and "Paste".
In CC, there are also alternatives: "Copy with Property Links" and "Copy with Relative Property Links" that can be useful too.
I've just tried this function, it seems to work:

function copyPropertyBase(source, target){
   
   if (source.propertyDepth<1) {alert("Cannot copy a layer into another!"); return;};
   
   var layer1 = source.propertyGroup(source.propertyDepth);
   var comp1 = layer1.containingComp;
   var time1 = comp1.time;
   
   var layer2 = target.propertyDepth === 0 ? target : target.propertyGroup(target.propertyDepth);
   var comp2 = layer2.containingComp;
   var time2 = comp2.time;
   
   comp1.time=comp2.time=0;
   
   comp1.openInViewer();
   app.executeCommand(app.findMenuCommandId("Deselect All"));
   try{source.selected=true}catch(e){alert(source.name + ": cannot copy this property:\r\r" + e.message); comp1.time=time1; comp2.time=time2; return;};
   app.executeCommand(app.findMenuCommandId("Copy"));
   layer1.selected = false;
   
   if (comp2!=comp1){
      comp2.openInViewer();
      app.executeCommand(app.findMenuCommandId("Deselect All"));
      };
   try{target.selected=true}catch(e){alert(target.name + ": sorry, cannot copy into this property:\r\r" + e.message); comp1.time=time1; comp2.time=time2; return;};
   app.executeCommand(app.findMenuCommandId("Paste"));
   
   comp1.time=time1;
   comp2.time=time2;
   
   return;
   };




A few tests:

var comp = app.project.activeItem;
var layer1 = comp.layer("ORIGINAL");
var layer2 = comp.layer("COPY");

// copy effect(3):
while(layer1.effect.numProperties<3) layer1.effect.addProperty("ADBE Slider Control");
copyPropertyBase(layer1.effect(3), layer2);                                                 // this adds a new effect
copyPropertyBase(layer1.effect(3), layer2.effect(layer2.effect.numProperties)); // this does not add an effect, but copies the content of the source effect into the target effect

// copy anchorPoint and position:
copyPropertyBase(layer1.transform.position, layer2);
copyPropertyBase(layer1.transform.anchorPoint, layer2);

// copy one (locked) mask
if (layer1.mask.numProperties===0) layer1.mask.addProperty("ADBE Mask Atom");
layer1.mask(1).locked = true;
copyPropertyBase(layer1.mask(1), layer2);

// copy all layers styles
copyPropertyBase(layer1.layerStyle, layer2);

// etc
 
jordanwade33
Topic Author
Posts: 11
Joined: Mon Dec 08, 2014 11:11 pm

Re: Copy Effect with Parameters

Mon Nov 09, 2015 2:20 am

I just tried the function that you came up with and it works great for what I need it to. It's sad to admit, but it never occurred to me to just call the menu items to copy and paste. Thanks so much for the help!

Who is online

Users browsing this forum: No registered users and 7 guests