Use sourceText as a variable?

Community Beginner ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

Hello! Interested in the question is it possible to use sourceText as a variable? For example, in my project I have the same expressions that are repeated on different layers, and when something needs to be changed, it becomes a problem every time.


Say, for example, if you put "thisComp.layer("Null")" in sourceText and specify the path to sourceText in the expression fragment, can you make it work? As I understand it, the string data type is used here and it needs to be converted somehow.

So far, I've been able to use sourceText as a numeric-only variable using parseInt().

The main question is whether it is possible to make sourceText work with a line of code? 🙂 Thanks.

TOPICS
Expressions

Views

90

Likes

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

correct answers 1 Correct answer

Adobe Community Professional , Jul 07, 2022 Jul 07, 2022

You could have a text layer (named "expression" in this example) with this text:

a = 50;
b = 50;
a + b

and then have an expression use that code like this:

txt = thisComp.layer("expression").text.sourceText;
eval(txt)

I think that's what you meant.

Likes

Translate

Translate
Adobe Community Professional ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

You could have a text layer (named "expression" in this example) with this text:

a = 50;
b = 50;
a + b

and then have an expression use that code like this:

txt = thisComp.layer("expression").text.sourceText;
eval(txt)

I think that's what you meant.

Likes

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 Beginner ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

Dan Ebberts. Yes, this is what you need!) Thank you! But for some reason it only works inside one composition (( I created a text layer and placed a block of code on it. txt = thisComp.layer("expression").text.sourceText.value eval(txt); - inside the composition everything works! I try to specify let from another composition - txt = comp("Comp 1").layer("expression").text.sourceText.value eval(txt); (error: layer named "Comp 1" is missing or doesnot exist ) although the path is absolutely correct, I can't figure it out. Inside the composition I try the absolute path comp("Comp 1")... the same error. Inside another composition I try index instead of the composition name txt = comp("Scene 08").layer(index =1).text.sourceText.value eval(txt); (error - TypeError: Cannot read property 'sourceText' of undefined. ) I can’t understand what could be the reason why it works only within one composition and does not find the path from another composition? Thank you!

Likes

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
Adobe Community Professional ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

So the idea is that you have a text layer with an expression as the text, and then on some other property of some other layer (probably not a text layer) you have an expression that does an eval() of the text on the text layer. From your description, it doesn't sound like that's how you set it up. Maybe a screenshot would help?

Likes

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 Beginner ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

I mean that it only works with thisComp - calling comp("name") doesn't work. Because of what I cannot refer to the same "sourceText" from another composition.

Likes

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
Adobe Community Professional ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

So in Comp 1, I have a text layer named "expression" that has "100" as its text. In Comp 2 I have a solid with this expression for rotation:

txt = comp("Comp 1").layer("expression").text.sourceText;
eval(txt)

and it works fine.

Likes

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 Beginner ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

LATEST

I'm sorry, I found the problem. The fact is that in my project structure there were two compositions with the same names)) that's why AE gave an error, not understanding which of the files to use.
I already thought that I have schizophrenia!), Comp1 and Comp2 how it works for you, but there is no working composition.
Didn't think to check right away. It seems overtired)) Thank you very much!!! You helped me a lot, I was tormented by this idea for a long time in order to optimize my project. Very cool! Thank you again!

Likes

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
Adobe Community Professional ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

I usually use the layer index instead of the layer name. For example, thisComp.layer(index - 1).position will return the layer's position above the layer with the expression. 

 

If you want to have the ability to change the layer, an expression is pointed to by typing the layer name in a text layer, name your text layer ref, and try this expression:

ref = thisComp.layer("Layer Controller").text.sourceText.value;
thisComp.layer(ref).position

 If you change the text in the layer named Layer Controller to say Background, the layer with the expression will move to the same position as the Background layer. If you edit the text in the Layer controller to Logo and have a layer named Logo in the composition, the layer with the expression will move to the Logo layer's position. You will get an error message if the text does not precisely match a layer name in the timeline. 

 

I do have a few Animation Presets that I have created to link arms, hands, and legs together that use layer names instead of index. Still, most of my automation uses index + or index - to retrieve information from other layers. The only time I can think of that I wrote an expression that used a text layer to choose the layer was when I was working on a template for an automated historical timeline animation. I used the technique to position the text layer over the appropriate graphic. If I had a graphic named Industrial Revolution, the text layer that read Industrial Revolution would automatically position itself right over the graphic.

 

Does that help? 

Likes

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 Beginner ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

Рик Джерард Thank you! For me it is very useful!

Likes

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