Assign espression to selected property after adding effect

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
axymark
Posts: 23
Joined: November 29th, 2013, 7:04 am

November 24th, 2014, 1:40 pm

Im having a problem finding a simple solution with this. I want to add an expression on selected property after i create a few sliders on the same layer. But, once you use .addProperty application seems to lose the selection (property selection, at least).

So, if this:

Code: Select all

var selectedProperty = selectedLayer.selectedProperties[0];
var addSlider = selectedLayer.Effects.addProperty("ADBE Slider Control");
selectedProperty.expression = "my expression";
actually does not work because after adding a slider, selectedProperty is not valid any more.

Any ideas how to get around this without inventing some convoluted way of traversing through parent properties to find out the full path just so i can recall it after i lose the selection?

Thanks to anyone who points out something obvious that im missing and saves me a lot of trouble...
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

November 27th, 2014, 11:34 am

Hi.

If selectedProperty is not inside "Effects" there is no reason why it should be invalidated by adding a new effect.
But if it is inside effect, it will invalidated and then you don't have much other choice that saving info to find it back.
propertyIndex is probably the best choice for saved data.
I just tried this and it works:

Code: Select all

function saveIndexPath(p){
	// returns an array of integers
	// entry 0 : layer.index
	// entry j corresponds to a property of depth j
	var path = [];
	while (p.propertyDepth>0) {path.unshift(p.propertyIndex); p=p.parentProperty;};
	path.unshift(p.index);
	return path;
	};
function loadIndexPath(indexPath, p){
	// "loads" the index path starting from p (propertyBase, ie something with a propertyDepth)
	// p should be strictly "shorter" than the path
	// returns a property or null
	var j=p.propertyDepth;
	if (indexPath.length <= j) return null;
	while (++j<indexPath.length && (p=p.property(indexPath[j])));
	return p;
	};

var selectedLayer = app.project.activeItem.selectedLayers[0];
var selectedProperty = selectedLayer.selectedProperties[selectedLayer.selectedProperties.length-1];
var indexPath = saveIndexPath(selectedProperty);
var addSlider = selectedLayer.effect.addProperty("ADBE Slider Control");
selectedProperty = loadIndexPath(indexPath, selectedLayer);
if (selectedProperty!==null && selectedProperty.canSetExpression) selectedProperty.expression = "value;";
Xavier
User avatar
axymark
Posts: 23
Joined: November 29th, 2013, 7:04 am

November 27th, 2014, 1:19 pm

Thanks Xavier. I hacked something quickly with propertyIndex as well so i can move on but ill take a closer look at your code as it seems a bit nicer than what i have.

thanks again
Post Reply