• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to Prevent Unintended Modifications to Variables in After Effects Scripting

Contributor ,
Jul 05, 2024 Jul 05, 2024

Copy link to clipboard

Copied

 

I have a variable defined as aaa= layer.property("Source Text").value;.

To avoid modifying aaaa during subsequent processing, I created another variable called bbb:

var bbb = aaa;

However, when I modify bbb as follows:

bbb.text = ccc.text;

I noticed that aaa.text gets modified as well.

How can I resolve this issue?

TOPICS
Scripting

Views

181

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 06, 2024 Jul 06, 2024

Copy link to clipboard

Copied

Without the full code nobody can understand your problem. That said, if that is all you have of course the variables will all hold the same values and/ or you are mistaking the output as the variables' values. Really impossible to say without knowing what you are actually trying to do there and how you debug the whole thing.

 

Mylenium 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 06, 2024 Jul 06, 2024

Copy link to clipboard

Copied

Let's put it this way, I want bbb to be a backup of aaa. I want to modify the contents of bbb without affecting aaa. I have tried other methods, such as using JSON for deep copying, but I always got errors like:

  • "text document not of Box document type"

Could you please help me with this issue?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 06, 2024 Jul 06, 2024

Copy link to clipboard

Copied

I think you misunderstand how this works. You cannot arbitrarily store variables persistently in expressions or the scripting engine. You can only force it to "compare" stuff on a continual basis by running things in a loop or such. So the simplest solution would be to have an if()else{} test in your code to determine the contents of a variable based on whatever criteria and then the source value remains available whenever the criteria aren't met.

 

Mylenium

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 06, 2024 Jul 06, 2024

Copy link to clipboard

Copied

Got it, thank you for the explanation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 06, 2024 Jul 06, 2024

Copy link to clipboard

Copied

In JavaScript, when you set a variable equal to an object (or an array) the variable contains only a reference to the object, not a copy of the object itself. So in your example, you end up with two references to the same object. Change the object via either reference, and the object will change for both references.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 07, 2024 Jul 07, 2024

Copy link to clipboard

Copied

thank you for the explanation,
is there any way to make a copy of an object when handling it, or can it only be a reference?
and thank you for your contribution to the animation expression. 😊
I have noticed that many animation scriptwriters have referenced your expressions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 07, 2024 Jul 07, 2024

Copy link to clipboard

Copied

It's not simple, but you could do something like this that should capture the attributes that aren't read-only:

var layer = app.project.activeItem.layer(1);
var aaa = layer.property("Source Text").value;
var bbb = new TextDocument("");
for (var i in aaa){
	try{
		bbb[i] = aaa[i];
	}catch(err){
	}
}
aaa.text = "new text";
alert (bbb.text);

What is it that you're trying to do exactly? Maybe there's another way...

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 08, 2024 Jul 08, 2024

Copy link to clipboard

Copied

LATEST

thank you.
it will throw an error of "text document not of Box document type"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines