Skip to main content
Participating Frequently
February 13, 2015
Answered

Using Singleton Pattern to Retain Previous Variable Values?

  • February 13, 2015
  • 1 reply
  • 608 views

Is it possible using ExtendScript to retain the previous value of a property even after it has been updated by the user? I know after effects expressions update every frame, but I believe script values should be able to to persist. Is this sort of thing possible with ExtendScript?

I'm thinking something like this should be do-able using a singleton like pattern, but so far I have been unsuccessful. Heres what I've tried so far:

    var instance;

   

    //Check current instance value

    alert(getInstance().value);

   

    //Check if instance has a value, if not assign one, otherwise return current value

    function getInstance(){

        if(!instance){

            instance = createInstance();

        }

        return instance;

    }//End getInstance

   

   //Create new object with a value and return it

   function createInstance() {

        var object = new Object(mySel.property("Effects").property("Transform").property(1));

        return object;

    }//End createInstance

Or, if singletons are the wrong approach is there any type of storage mechanism that can be called in ExtendScript to write values to?

Thanks!

This topic has been closed for replies.
Correct answer UQg

I don't know one (which does not mean there is none), but maybe your problem could be simplified.

Your best luck would be that you want want to know whether the property is modified at all, in which case you can use p.isModified.

But if you want to know if it has been modified since last modification, then its another story. Properties objects can't be instantiated on their own, they are linked to actual properties in layers and can't be "frozen".

Brutal way :

If the property is suspected to have a bunch of keys, you'd be better of caching the stream of values in an array;

Else cache the keys (still lots of things to do...times, values, interpolations) and the expression, but then comparing two states can be uneasy.

.. for every p in your effect.

Hopefully you'll get a better answer

Xavier.

1 reply

UQg
Legend
February 13, 2015

You createInstance function is a bit strange:

var p = app.project.activeItem.selectedProperties[0];

var a = new Object(p);

a==p; // true ===> point to the same property : if p is modified, a will be too

// and actually:

a===p; // true ===> a and p are exactly the same object

What are you trying to store ? The whole object ([object Property]) or the keyframable value (stream of numbers, arrays, etc) ?

bpoole24Author
Participating Frequently
February 13, 2015

Shoot, thats what I was afraid of, is there no way to prevent one from changing when the other is modified? In this case I was trying to store the whole object so I could later access properties of it, but mostly I'd probably just be storing its .value property.

UQg
UQgCorrect answer
Legend
February 13, 2015

I don't know one (which does not mean there is none), but maybe your problem could be simplified.

Your best luck would be that you want want to know whether the property is modified at all, in which case you can use p.isModified.

But if you want to know if it has been modified since last modification, then its another story. Properties objects can't be instantiated on their own, they are linked to actual properties in layers and can't be "frozen".

Brutal way :

If the property is suspected to have a bunch of keys, you'd be better of caching the stream of values in an array;

Else cache the keys (still lots of things to do...times, values, interpolations) and the expression, but then comparing two states can be uneasy.

.. for every p in your effect.

Hopefully you'll get a better answer

Xavier.