ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

attaching an object to an updatable string of text

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ted Gore
attaching an object to an updatable string of text
on Feb 18, 2010 at 8:58:04 pm

Hello

I'm trying to attach an object to the left side of a string of right justified text. The goal is for the text to be editable, while having the object continually update it's position as the string grows or shrinks.

I have no idea where to start because I can't seem to find any data on that end of the text string.

Any ideas?

Thanks!


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Feb 18, 2010 at 11:15:12 pm

Here's a hack that should keep your layer lined up with the left edge of the text layer:

L = thisComp.layer("Your Text Layer");
for (i = 0; i <= thisComp.width; i++){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Dan



Return to posts index

Ted Gore
Re: attaching an object to an updatable string of text
on Feb 18, 2010 at 11:23:11 pm

thanks Dan,

My expression knowledge is limited. Would this go into the position expression field for my object?


Return to posts index


Ted Gore
Re: attaching an object to an updatable string of text
on Feb 18, 2010 at 11:35:29 pm

thanks dan!

Sorry, my expression knowledge is limited, but am I putting this into the position expression field for the object?

thanks


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Feb 19, 2010 at 2:12:18 am

Yes. The object will keep its own y position but the x should track the left edge of the text.

Dan



Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on May 17, 2010 at 2:52:16 am

Hi Dan-
I love this expression... it's almost exactly what I need.
Please excuse my ignorance; how could I tweak this to attach a layer to the RIGHT side of a LEFT justified text string? I've tried changing some the code elements but I can't get the result I'm looking for.
Thanks!
-Andre


Return to posts index


Dan Ebberts
Re: attaching an object to an updatable string of text
on May 17, 2010 at 5:16:24 am

I think this is the change you need to make:

L = thisComp.layer("Your Text Layer");
for (i = thisComp.width; i >= 0; i--){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Dan



Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on May 17, 2010 at 8:00:31 pm

Perfect. Thanks so much Dan!


Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on Oct 21, 2010 at 4:42:38 am

Another question about this script: I've noticed that the position of an object (the one linked to the end of a text-string) changes in sync with the position of said text-string. Is there a way to link this object to a specific position (or anchor) keyframe, so that it retains its position before/after the text-string's position value changes?

Thanks!

left side
L = thisComp.layer("");
for (i = thisComp.width; i >= 0; i--){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index


Dan Ebberts
Re: attaching an object to an updatable string of text
on Oct 21, 2010 at 4:40:04 pm

One way is to just change the last parameter for sampleImage() from "time" to the specific time you want to do the sampling (0, for example).

Dan



Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on Oct 21, 2010 at 8:30:17 pm

That's great! So simple... Thank you so much!


Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on Nov 2, 2010 at 7:27:40 am

Hello again-
Something I've noticed is that the expression seems to stop working once the object (the text that it is linked to) goes off the edge of the screen. When this happens the subject (text with expression applied) reverts to its original position value. Aside from a workaround (ie: expand the comp pixel size to accommodate object position and then render with cropped output settings) is there some way to avoid this problem?

Thanks!

L = thisComp.layer("");
for (i = thisComp.width; i >= 0; i--){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index


Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 2, 2010 at 3:31:11 pm

You can expand the bounds of the search. For example, to add 50 pixels to each edge of the comp, you could make this change:

for (i = thisComp.width+50; i >= -50; i--){


Dan



Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on Nov 9, 2010 at 12:49:42 pm

Thanks again, Dan-- You've been an immense help.

One more question: when I enter nothing or "space-button" into a targeted text-layer, the other texts whose positions are linked to it jump to the edge of the frame. Is there any way to make them hew to the position value of a text-layer with nothing or next-to-nothing in it?

right side
L = thisComp.layer("");
for (i = 0; i &lt;= thisComp.width; i++){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 9, 2010 at 5:17:46 pm

I'm not sure what you mean exactly, but maybe like this:


L = thisComp.layer("parent");
if (L.text.sourceText != ""){
for (i = 0; i <= thisComp.width; i++){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]
}else{
[L.position[0],value[1]]
}


Dan



Return to posts index


Andre Isaac
Re: attaching an object to an updatable string of text
on Nov 9, 2010 at 8:49:45 pm

Unfortunately this seems to get the same result as before. I'm trying to build a template that can work with multiple layouts within a banner, each text-layer in AE corresponding to a cel in a database. The idea is for the same template to work with different combinations of text-layers (used or unused depending on the information). There are workarounds but I'm trying to automate this as much as possible. Thanks again!


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 9, 2010 at 9:23:03 pm

Sorry, I missed the part about the space character. Try changing the second line to this:

if (L.text.sourceText != "" && L.text.sourceText != " " ){

If your text can multiple space characters or other non-printing characters, it's not going to work. You'd need to have the expression examine the text for one or more characters that print. Not hard to do, just different.


Dan



Return to posts index

Andre Isaac
Re: attaching an object to an updatable string of text
on Nov 10, 2010 at 2:36:56 am

I think that did it... THANK YOU!


Return to posts index


Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 20, 2010 at 9:11:56 pm

Hi Dan,

Currently trying to figure out how to do this on the y axis instead of X.

Currently I have textfield(A) with a variable length in the y axis. I need to somehow make sure that the BOTTOM of textfield(a) stays fixed to textfield(b).


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 20, 2010 at 10:40:19 pm

This should find the bottom of a text layer:


L = thisComp.layer("Your Text Layer");
for (i = thisComp.height; i >= 0; i--){
temp = L.sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
[value[0],i]



Dan



Return to posts index

Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 20, 2010 at 11:33:13 pm

Sorry I am not a native speaker, Ive probably been vague.

What I try is exactly the opposite. I want the bottom of the text that is being moved to attach itself to the top of the non-moving text.

So I want the bottom of L to position itself on the top of I

L = thisComp.layer(index+1);
for (i = thisComp.height; i >= 0; i--){
temp = L.sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
[value[0],i]


Return to posts index

Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 20, 2010 at 11:48:17 pm

Hey Dan,

I am not sure if I am reposting my former message, but here goes.
Pardon me if I didn't explain it enough in my former message. English is my second language... and code is probably my 1000th language. If not something almost as alien.

Well yeah anyway your code did exactly the opposite of what I tried to convey.
What happens now asfar as I understand is:

"I" places itself at the bottom of "L"

I wanted the bottom of "I" to place itself at the top of L


(I am trying to make a text stay 200px away from the bottom edge of my comp. No matter long the text)

L = thisComp.layer("Your Text Layer");
for (i = thisComp.height; i >= 0; i--){
temp = L.sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
[value[0],i]


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 21, 2010 at 12:13:14 am

I think that's the only permutation left. :-)


L = thisComp.layer("Your Text Layer");
for (i = 0; i <= thisComp.height; i++){
temp = L.sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
[value[0],i]



Dan



Return to posts index

Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 22, 2010 at 11:37:20 am

Hi again Dan,

Let me try and explain in pictures:
This is what happens now:
http://dl.dropbox.com/u/11798518/LI.jpg

Right now the top of
iiiii places itself at the top of L
I want this to happen:

http://dl.dropbox.com/u/11798518/IL.jpg

The bottom i to place itself at the top of L
I just can't get this to work, Ive been messing around/trying to combine and change the script for hours now.I just can get it to register the bottom of I.


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 22, 2010 at 4:25:28 pm

I can see three possible options.

1. Move the anchor point of the "iiii" layer to the bottom of the layer.

2. Add an offset to the last line of the expression like this:

[value[0],i - 85]

3. A more complicated solution where the expression has to find the lower extend of its own layer in addition to the upper extent of the other layer.


Dan



Return to posts index

Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 22, 2010 at 9:48:36 pm

Heh!

Well shit then I am officially out of luck.

1. Moved the anchorpoint to the bottom of the text layer, but since the content of this varies it doesn't place itself correct in relation to L.

2. Tried this aswell, but ran into the same problem as above. It defeats the purpose, since the content of the iii layer differs and the hardcoded value doesn't accommodate this.

Hell... allright I guess I will just have to deal with this manually. the lower extend of the layer should always be the same of amount of space from the comp edge... But I guess thats not the obstacle, meh.

Thanks for trying Dan, I appreciate it.

:D


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Nov 22, 2010 at 11:31:14 pm

It sounds like option 3) then. This one first finds the bottom of the text in its own layer, then finds the top of the target layer text, then moves the layer enough to put it on top of the target layer. I think it's what you're after.


for (i = thisComp.height; i >= 0; i--){
temp = sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
y1 = i;

L = thisComp.layer("Your Text Layer");
for (i = 0; i <= thisComp.height; i++){
temp = L.sampleImage([thisComp.width/2,i],[thisComp.width/2,0.5],true,time);
if (temp[3] > 0) break;
}
y2 = i;

value + [0, y2-y1]



Dan



Return to posts index

Kim Nielsen
Re: attaching an object to an updatable string of text
on Nov 23, 2010 at 8:13:51 am

At some point.
Someone gotta collect all the nuggets of gold you create here on this board.
This is amazing, and I can't imagine that it wouldnt be very useful to everybody who works with templates and large quantities of dynamic text.

Thank you, thank you, thank you.


Return to posts index

Mike Headley
Re: attaching an object to an updatable string of text
on Feb 23, 2012 at 5:30:57 pm

Hey all,

Long time reader, first time poster, here. I'm currently trying to apply the first expression listed (for right justified text) to the position of a solid layer. However, when I click away from the expression field after entering the script I get this error message:

----------------------------------
After Effects warning: Expected: ;
Expression disabled

Error occurred at line 3.
Comp: 'Comp1'
Layer: 1 ('White Solid 1')
Property: 'Position'

----------------------------------

I'm currently using a test comp while I try to figure this out. That's why the above comp and layer names are generic. I received the same message within my actual project as well. I'm pretty inexperienced with expressions beyond the basics, so please forgive me if this is something really simple. Any support is greatly appreciated.

Thanks in advance,
-Mike H.

L = thisComp.layer(TEST);
for (i = 0; i
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Feb 23, 2012 at 5:41:29 pm

Your layer name needs to be in quotes:

L = thisComp.layer("TEST");


Dan



Return to posts index

Mike Headley
Re: attaching an object to an updatable string of text
on Feb 23, 2012 at 11:47:45 pm

Hey Dan,

Thanks for the quick reply. I surrounded the layer name with quotes, but I'm still getting the same error message.

-Mike H.

L = thisComp.layer("TEST");
for (i = 0; i
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Feb 23, 2012 at 11:56:39 pm

Your code got mangled somewhere along the line. Try this:


L = thisComp.layer("TEST");
for (i = 0; i <= L.width; i++){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}

[i,value[1]]



Dan



Return to posts index

Mike Headley
Re: attaching an object to an updatable string of text
on Feb 24, 2012 at 12:22:02 am

Dan,

That did the trick! Thank you so much for the support. I do have a followup question, though. While the expression seems to be working, the solid layer is now overlapping the text layer even as I add or remove text. It actually seems that the left edge of the solid is aligned to the left edge of the textbox. I also can't move the solid with the selection tool once the expression is activated. Is there a way for me to manually adjust the position of the solid within the expression? I was able to find a workaround by applying the expression to a null object, then parenting my solid to the null.

Thanks again,
-Mike H.


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Feb 24, 2012 at 12:35:51 am

I would just move the anchor point to adjust the x, adjust the y by dragging the layer.

Dan



Return to posts index

Mike Headley
Re: attaching an object to an updatable string of text
on Feb 24, 2012 at 12:45:45 am

Eureka, that works! Thanks again for the help. This expression is really going to save me a lot of time.

Cheers,
-Mike H.


Return to posts index

Vojtěch Palme
Re: attaching an object to an updatable string of text
on Dec 14, 2012 at 5:50:53 pm

Dan. You've been tremendous help. I used the script for left adjusted text

L = thisComp.layer("TITLE");
for (i = thisComp.width; i >= 0; i--){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


and aplyed it to scale of layer on background of that text. It works. But when I load the "TITLE" content from external .txt

$.evalFile("C:/.../TITLE.txt");
try {
eval(title1)[0]
} catch (err) {
"not found"
}


and change the text within, I get this error

After Effects warning: Timeout while waiting for the engine
Expression disabled.
Error occurred at line 4.
Comp: ‘TABULKA1’
Layer: 16 (‘Title bg’)
Property: ‘Scale’


is there some way to maybe let the scale expression wait until the text is loaded? Seems to me that is looping it self or something..

And one more question, Is there a way to get number of lines of text? Or at least heigh of the text field? So when I paste text into some layer, the animator would readjust itself acordingly to display one row per second.
Thank you


Return to posts index

Cameron Pinegar
Re: attaching an object to an updatable string of text
on Aug 9, 2017 at 8:22:47 pm

Hi Dan,

I'm amazed by this script, I've been looking for something like this for a long time. I'm using the code to create dynamic underlines that change automatically with the width of the text. I'm parenting one null object to the position of the left aligned text and using your code to parent another null object to the ragged right side of the text. I'm then using the connect layers plugin to create a stroke that connects the two null objects (thus creating the underline). It seems to be the perfect way to solve my need for dynamic underlines, but the one issue I'm having is once I've pasted your script it becomes very taxing on my CPU and doesn't end up helping me save the time I was hoping to save. My laptop is not very beefy, but I'm baffled because the script seems pretty simple and I feel like I've used much more complicated scripts without this kind of lag. Any suggestions?

MacBook Pro (Retina, 15-inch, Late 2013)
2.3 GHz Intel Core i7
16 GB 1600 MHz DDR3
NVIDIA GeForce GT 750M 2048 MB
Intel Iris Pro 1536 MB

L = thisComp.layer("My name is Jonas.");
for (i = thisComp.width; i >= 0; i--){
temp = L.sampleImage([i,thisComp.height/2],[0.5,thisComp.height/2],true,time);
if (temp[3] > 0) break;
}
[i,value[1]]


Return to posts index

Dan Ebberts
Re: attaching an object to an updatable string of text
on Aug 9, 2017 at 9:31:10 pm

Anything that has sampleImage() in a loop is going to be slow. If you have a recent version of AE, you should switch to a solution that uses sourceRectAtTime() to find the edges of the text.

Dan



Return to posts index

Cameron Pinegar
Re: attaching an object to an updatable string of text
on Aug 9, 2017 at 10:05:51 pm

Thanks, Dan. That pointed me in the right direction!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]