TransWikia.com

How to get the beginning position of each line of a text object (Pyhton)

Blender Asked by jacobo on December 7, 2021

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

One Answer

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])
    dg.update()
    bbox = [Vector(b) for b in ob.bound_box]
    bpy.ops.object.empty_add(
            location=mw @ bbox[0]
            )

Related Counting amount of lines in a text object

Answered by batFINGER on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP