Page 1 of 1

Centre text vertically depending on nr of lines used

Posted: January 8th, 2008, 3:39 am
by sundstedt
I am looking into if it there is a way to centre text on the screen (with script) depending on the number of lines used. I.e. it could be anything between for example 1 and 20 lines. The issue is that it needs to work for any font and any size, not just for a static, measured size. I am thinking about writing a script for this, but first I am trying to find out if it's possible to get the text properties for "size" and "number of lines" or at least "size of text region". If I could retrieve the text "box" size (like in Photoshop) I could centre the text area based on this information.

Any help is appreciated! Even if it's just regarding feasibility of this idea.

Posted: January 8th, 2008, 4:32 am
by Paul Tuersley
There are a couple of ways to find the size of a text layer, depending on which version of AE you're using. In CS3 you can use the new sourceRectAtTime() method. In earlier versions you can temporarily add a mask to the text layer and read the mask bounds to find the size. There's a thread about it here:
viewtopic.php?t=150&postdays=0&postorder=asc&start=45

And here's a chunk of code from my SSA Karaoke Animator script where I'm using both methods so I can estimate the position of each word/syllable:

Code: Select all

if (usePointControl == true) {
				//add a temporary text layer to test the preset
				theTextLayer = activeItem.layers.addText(finalText);
				
				if (parseFloat(app.version) < 8) {	// if app is AE7, use nab's mask technique
					app.executeCommand(2367);  //adds a mask
					var theMask = theTextLayer.Masks.property(1);
					var theVertices = theMask.maskShape.value.vertices;
					// layer bounds = top, left, width, height
					layerBounds= [theTextLayer.position.value[1] + theVertices[0][1],
					theTextLayer.position.value[0] + (theVertices[0][0] / activeItem.pixelAspect),
					(theVertices[2][0]-theVertices[0][0]) / activeItem.pixelAspect,
					(theVertices[2][1]-theVertices[0][1])];
					//alert("top = " + layerBounds[0] + " left = " + layerBounds[1] + " width = " + layerBounds[2] + " height = " + layerBounds[3]);
				} else {	// app is AE8 or greater
					//var thePosition = theTextLayer.position.value;
					var boundsObject = theTextLayer.sourceRectAtTime(roundToFrames(startTime), true);
					layerBounds = [theTextLayer.position.value[1] + boundsObject.top,
					theTextLayer.position.value[0] + (boundsObject.left / activeItem.pixelAspect),
					(boundsObject.width/ activeItem.pixelAspect),
					boundsObject.height];
					//alert("top = " + layerBounds[0] + " left = " + layerBounds[1] + " width = " + layerBounds[2] + " height = " + layerBounds[3]);
				}
				theTextLayer.remove();
			}

Posted: January 8th, 2008, 5:20 am
by sundstedt
Thanks for the tips, I am almost done writing my own script based on NAB. I will check out your script if I get stuck. Cheers!

Posted: January 8th, 2008, 8:54 am
by sundstedt
I used the tips from nabscripts and wrote my own script, thanks Paul for tips on how to find the size of a text layer! I then did some calculations so I can position my text layer(s) vertically aligned with a graphical background. I didn't test your example code, but thanks anyway.

Where can I find more info about - app.executeCommand(xxxx) ?

sorted!