I'm trying (desperately) to get the beginning position in space of each line of a text object, but just couldn't... :( Any help would be wildly appreciated. The positions I'm trying to get to are marked with red crosses in the attachment picture.

enter image description here


The bounding box

enter image description here

Many of the kerning options for the text object are available. IIRC there is another answer (unfindable so far) that uses these to calculate the positions.

Instead I'm going to simply use the bounding box, single first line, 2 lines, 3 lines etc.

The bounding box is the local axis aligned extent of an object, in local coordinates. The box has 8 coordinates, the zeroth coordinate is the front bottom left hand corner. eg (-1, -1, -1) for default cube Multiplying by objects matrix world gives the global location.

Test script adds an empty at beginning of each line of the text object that has context.

Answer for version 2.8. For 2.7x would use a scene.update() to rejig for changes to text body.

import bpy
from mathutils import Vector

context = bpy.context
dg = context.evaluated_depsgraph_get()
ob = context.object
mw = ob.matrix_world
text = ob.data.body

lines = text.split("\n")
for i, l in enumerate(lines):
    ob.data.body = "\n".join(lines[:i + 1])
    bbox = [Vector(b) for b in ob.bound_box]
            location=mw @ bbox[0]

Related Counting amount of lines in a text object

  • $\begingroup$ batFINGER, thank you so much for taking the time to help out. I have so much here in your code that I can learn from. $\endgroup$ – jacobo Jul 23 '20 at 12:08
  • $\begingroup$ Cheers, and oops, looks like I missed last line when pasting. edited. $\endgroup$ – batFINGER Jul 23 '20 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.