-
Query pixel bounds of text layer
{ // Round down to the nearest integer value function round_down(n) { var floor = Math.floor(n); if (n == floor) { return n; // already rounded down } var window = 0.01; if (Math.abs(n - floor) < window) { return floor; // very close to floor } var ceil = Math.ceil(n); if (Math.abs(ceil - n) < window) { return ceil; // very close to ceil } return floor; } // Round up to the nearest integer value function round_up(n) { var ceil = Math.ceil(n); if (n == ceil) { return n; // already rounded up } var window = 0.01; if (Math.abs(ceil - n) < window) { return ceil; // very close to ceil } var floor = Math.floor(n); if (Math.abs(n - floor) < window) { return floor; // very close to floor } return ceil; } // Given a layer, return an array contiaing the [x, y, width, height] // coordinates of the bounding box for the layer. The coord values // are rounded to whole pixels and are in terms of the comp coordinates. // For a text layer, return the actual coordinates of the rendered text. function layer_bbox(thisLayer, time) { var rect = thisLayer.sourceRectAtTime(time, true); // The left value indicates an offset from the layer position // to the left most position where the layer is rendered. // The width value indicates an offset from the left position // to the right most rendered position. var left = round_down(rect.left); var right = round_up(rect.left + rect.width); var width = right - left; // The top value is the pixel height above the layer position, // rounded down to the nearest pixel. var top = round_down(rect.top); var bottom = round_up(rect.top + rect.height); var height = bottom - top; // Convert offsets from layer origin to offsets in terms // of comp coordinates. var pos = chordLayer.transform.position.value; var anchor = chordLayer.transform.anchorPoint.value; var origin_x = Math.round(pos[0] - anchor[0]) + left; var origin_y = Math.round(pos[1] - anchor[1]) + top; return [origin_x, origin_y, width, height]; } thisComp = app.project.activeItem; if (thisComp == null) { alertStr = "No active comp found"; alert(alertStr); } else { // alertStr = "thisComp.name is \\"" + thisComp.name + "\\""; // alert(alertStr); chordLayer = thisComp.layer("Chord"); var bbox = layer_bbox(chordLayer, 0.0) ; var origin_x = bbox[0]; var origin_y = bbox[1]; var width = bbox[2]; var height = bbox[3]; alertStr = "bbox x,y = " + origin_x + "," + origin_y + " WxH = " + width + "x" + height; alert(alertStr); } } // end of file scopeHello all
I recently tried to figure out how to get the bounding box of a text layer, that is the
X,Y and WIDTH and HEIGHT, so that I could put a box around the text. I found the
Layer.sourceRectAtTime() API, but it took a little code to turn its results into something
I could use to create a box. The trick was that I wanted X,Y values in terms of pixels in
the comp, so a little rounding of the coord values was required. I am posting this script
in case it is helpful to others.cheers
Mo DeJong