Back On topic:
I am trying to write a script that writes values and properties from an effects template file to a certain effect.
In order to write the values, I need to find a uniform way to address the values from a certain effect-property. Depending on the type of the value, it can be a single value or values in an array. (e.g. values for coordinates have 2 elements, RGB-color has 3 or 4 values)
I wrote a short example code, which adds the Remove Grain effect to a selected layer, and sets the color of the Preview box.
Code: Select all
//********************************************************
// writing effect values at a certain property from an effect
var the_layer = app.project.activeItem.selectedLayers[0];
var ename = "Remove Grain";
var effectsGroup = the_layer("Effects");
var data=new Array();
if (effectsGroup != null ) // Always best to check if it's safe before adding:
{
if (effectsGroup.canAddProperty(ename))
{
var effectBase = effectsGroup.addProperty(ename);
if (effectBase != null) // Effect was applied, now set parameter values
{
var prpty = 7; // Remove Grain, Box Color
var numElmnts = 3; // array has 3 elements
data[1] = .4; // R
data[2] = .5; // G
data[3] = .6; // B
switch (numElmnts)
{
case 1: // noArray
{
the_layer("Effects")(ename).property(prpty).setValue( data[1] );
break;
}
case 2: // array with two elements
{
the_layer("Effects")(ename).property(prpty).setValue([ data[1],data[2] ]);
break;
}
case 3: // array with three elements
{
the_layer("Effects")(ename).property(prpty).setValue([ data[1],data[2],data[3] ]);
break;
}
case 4: // array with four elements
{
the_layer("Effects")(ename).property(prpty).setValue([ data[1],data[2],data[3],data[4] ]);
break;
}
default:;
}
}
}
}
//********************************************************
As one may see, I used a switch-case statement to differentiate between single elements, and arrays with 2 till 4 elements.
Instead of using a case statement, I prefer using a method, in which I may directly address the n-th element of a certain effects property from an array. To find this out, I tried the opposite: reading the values from an effect. In this way I also have to differentiate between non-array and array values. But at least for array values, it's size doesn't matter. This uniform way looks like:
For the first effect (1) on the selected layer, in
case of an array, if the effect is e.g. "Remove Grain";
then prpty is the property type.
- 1:Viewing Mode
2:Preview Region
3:Center
etc.
If property prpty is not an array, we retrieve the value data of the first effect on the first layer with:
Code: Select all
data=app.project.activeItem.selectedLayers[0]("Effects")(1)(prpty).value;
If property prpty is an array, we retrieve the number of elements numElmnts of the array with:
Code: Select all
numElmnts=app.project.activeItem.selectedLayers[0]("Effects")(1)(prpty).value.length;
Each single element can be easily read out. Asume property prpty is an effects property which holds an array. Since we know the number of elements of this array, we get the value by iterating with j from 1 to numElmnts with:
Code: Select all
data=app.project.activeItem.selectedLayers[0]("Effects")(1)(prpty).value[j];
In contrast to reading a value, I would like to have for writing a value something like:
Code: Select all
app.project.activeItem.selectedLayers[0]("Effects")(1)(prpty).value[j]=data;
This method doesn't work, but everyone could understand that I want to write the value of the j-th element. The script guide suggests to use the method
.setValue for this.
While experimenting, I tried all folowing methods:
Code: Select all
the_layer("Effects")(ename)(prpty)(j).setValue([data])
the_layer("Effects")(ename)(prpty).property(j).setValue([data]);
the_layer("Effects")(ename)(prpty).setValue(j,[data]);
Al these methods failed. The only way the official script guide provides is:
Code: Select all
// two dimensional array
the_layer("Effects")(ename)(prpty).setValue([ data[1],data[2] ]);
// three dimensional array
the_layer("Effects")(ename)(prpty).setValue([ data[1],data[2],data[3] ]);
// four dimensional array
the_layer("Effects")(ename)(prpty).setValue([ data[1],data[2],data[3],data[4] ]);
Making an extra case statement necessarry, which diferentiates between the number of elements in the array, which I do not find very elegant.
So does someone know a way with methods to address the j-th element of an array...