ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

reading text from a file

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Vasyl Iskovych
reading text from a file
on Jul 9, 2020 at 9:30:37 am

Hello.
I am trying to create a project using reading text from a file.
But I cannot make it so that I can use the characters " and '.
I have attached a screenshot to show this.
Please tell me if this is possible.
Thank.
https://images.creativecow.net/332560/0_.png


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 10:07:12 am

Yes, the way you have this set up, the external file needs to be valid Javascript.
in Javascript you need to escape quotes to use within a quoted string:


myvalue= "myvalue is using \"these kinds of quotes\" even though it's delimited by double quotes";


Furthermore, I thought $.evalFile() was officially not supported, but seems to work here.



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 11:15:03 am

[Filip Vandueren] "using \"these kinds of quotes\""
Thank you very much.
You helped me a lot.
[Filip Vandueren] "$.evalFile() was officially not supported"
May I ask why this is not suitable?
Can you advise another solution?
I am not very experienced in this.
I am trying to make a template for VideoHive.
If this does not work or there are errors, then they will not accept it from me.

I would like to ask one more question.
Can I find out the number of lines in the text?
I want to make the text align vertically if a new line is added to it.
If I write the expression text.sourceText.length, then I get the number of characters.
I would like to know the number of lines.
Thank.


Return to posts index


Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 11:16:14 am

[Filip Vandueren] "using \"these kinds of quotes\""
Thank you very much.
You helped me a lot.
[Filip Vandueren] "$.evalFile() was officially not supported"
May I ask why this is not suitable?
Can you advise another solution?
I am not very experienced in this.
I am trying to make a template.
If this does not work or there are errors, then they will not accept it from me.

I would like to ask one more question.
Can I find out the number of lines in the text?
I want to make the text align vertically if a new line is added to it.
If I write the expression text.sourceText.length, then I get the number of characters.
I would like to know the number of lines.
Thank.


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 12:17:22 pm

on this page:
https://helpx.adobe.com/be_nl/after-effects/using/legacy-and-extend-script-...

it says it's not supported, but the page may be out of Date,
or it may be unsupported but work anyway ☺
I can't get it to work, but I am on Mac and maybe a different version of After Effects than you are.

Alternatively:
You can add .json or .csv files as footage inside of a project and get their sourcetext, sourceData, or JSON.parse() them.

a txt.json file could look like this:

{
"text_1": "My first text \r has multiple \n lines",
"text_2": "My second text \"using quotes\" "
}


If you make valid JSON-files, then there are methods to know how many members there are in the JSON object, like this:


Object.keys(footage("txt.json").sourceData).length;


if you want to know how many lines are in a string, you should count the number of \n or \r characters in it. My preferred method is like this:

myString = =footage("txt.json").sourceData.text_1;
lines = myString.split(/\r|\n/).length;




Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 12:29:57 pm

To better address your last question, you were asking about text that is already in sourceText:

lines = text.sourceText.split(/\r|\n|\u0003/).length;

notice I also check for Unicode character 0003, which is "end of text", and is what After Effects uses for soft line breaks (shift-enter) that start a new line, but not a new Paragraph.



Return to posts index


Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 12:57:13 pm

Thanks so much for your detailed answers.
Please tell me why the \n character is used?
I did not measure the difference if using \n or not using it.
Unfortunately, I did not find information on the Internet.
If possible, please tell me where to read about these values.
Thank.


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 3:13:24 pm

Just an old habit,

line feed \n
carriage return \r

https://en.wikipedia.org/wiki/Newline#Issues_with_different_newline_formats



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 3:27:07 pm

Thank you very much.
You helped me a lot.
But I want to ask more.
Hope this will be the last question.
I am trying to work with a .json file.
But when, after I accidentally corrupt the file, I cannot resume its work, even after I fix the file.
I tried to clear the memory, replace the footage, and even deleted and imported the file again. but he refuses to work.
Please watch the video to better understand what I mean.
And this does not always happen, but only sometimes.
I recorded a video several times to catch this error.






Return to posts index


Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 3:35:05 pm

Hmmm…
I see, I can replicate the same problem.

however:
I never add the JSON to my timelines. If you have huge datasets, it takes superlong and a lot of memory to create all those properties in the timeline.

I just get my data straight from the footage in the project-window wherever I need it.

footage("txt.json").sourceData.text_1;


If the source JSON breaks, the expression does give an error.
When the file is fixed and refreshed; it immediately works again.



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 3:53:23 pm

Thank you very much.
I thought that if the data is not displayed on the timeline, then they will not work in the expression.
I did not check it then.
Now I checked it and it works.
Thank you again.


Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 4:38:42 pm

Unfortunately, I can’t achieve the desired result, since the expression for the number of lines is not taken into account automatic line breaks (when the text is limited to text boxing).
1_.png
Is it possible to take into account such breaks?
Thank.


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 4:44:25 pm

You cannot know the number of forced line breaks.
But you can know the width and height in pixels of a text-layer.
Most expressions to automatically scale or reposition text are based on this:

sourceRectAtTime()

There are many examples of this on the forum



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 5:06:17 pm

I can not use sourceRectAtTime () because the text jumps up and down, depending on the characters that are entered.
And the position of the text, which should be located on one line - will be different, depending on the characters.
I wanted to make sure that the text did not go beyond certain limits horizontally.
I did this with a snap to null.
But I would like to add the ability to automatically wrap text on a new line and shift the text up after a new line appears.
I thought I would do this by restricting the text to text box, and add expressions to correct the vertical position, depending on the number of lines.






Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 5:10:58 pm

Now, the text reaches a certain width, and begins to decrease.
I wanted to make it so that the text reached the set width, and wrap on the second line.
And it began to decrease when both lines reached this width.


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 9, 2020 at 9:09:36 pm

If you use a textbox, and set a fixed number for leading (not Auto), then you can check in multiples of that leading.
This way you don't get small differences of a few pixels. (the difference between -, x, j, l; | etc.)



expression for anchorPoint:

myLeading = text.sourceText.style.leading;
h=sourceRectAtTime().height;
x=Math.floor(h/myLeading);

value + [0,myLeading/2]*x;


This can be still combined with an expression for scale that makes lines wider when there are just a few characters, and the textbox will do wrapping if the line gets longer

w=sourceRectAtTime().width;
maxUpScale = 2;
textBoxWidth=1440;

if (w>0) {
s=Math.min(maxUpScale , textBoxWidth/w);
[s,s]*100;
} else {
value
}




Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 9, 2020 at 10:34:10 pm

I am very grateful to you for your help.
Unfortunately, I still did not understand how these expressions work, but they work exactly the way I wanted.
Can I thank you somehow?
Maybe I can somehow pay you for your work?
I will not be able to send a large amount.
But I can offer $ 20- $ 30;
(Please don’t laugh if this is too little for you)


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 10, 2020 at 10:51:45 am

No need, I'm happy to help out and feel appreciated.



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 10, 2020 at 12:57:37 pm

Thank.
I really appreciate your help.
I would like to ask one more question.
Before sending the project to VH, I planned using a script to translate all the expressions into binary code.
For example:
eval ("@ JSXBIN @ ES @ 2.0 @ MyBbyBn0ABJAnAARCFdjYFd2kQBf0DzABByB")
But such expressions only work if Legacy ExtendScript is installed in the project settings.
And your Anchor Point expression only works if JavaScript is installed.
Is there any way to fix this?
And a good or bad idea, with the conversion of expressions to binary code?


Return to posts index

Filip Vandueren
Re: reading text from a file
on Jul 10, 2020 at 3:41:02 pm

I’ve never used binary code, so I don’t know about the compatibility issues.

I think it is more useful to protect your expression when the code is the actual product you’re selling (a script) than when it is just a little helper that complements the design.

The code can probably be altered a bit so it would work in legacy/eval (it probably just needs some thisLayer.... or thisComp... Prefixes), but I wouldn’t bother, personally.
We’ve shared the code here anyway...



Return to posts index

Vasyl Iskovych
Re: reading text from a file
on Jul 10, 2020 at 6:38:54 pm

OK.
I understood.
I just saw binary code in several other projects that I was looking at and wanted to do the same.
Its use is not a necessity.
Thank you again.


Return to posts index

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