I'm writing a Python script that is pretty sensitive to the x-size ("length") of text objects created with the function text_add(). (I'm also new to Blender and still very confused about where the standard output from the Text Editor goes; some people mention it should go to "the console", but it definitely doesn't go to my Python Console inside Blender - and I'm using version 2.79 - so the following code sample reflects this confusion).
Problem: a text object displaying 30 Is (capital I) is not six times longer than a similar text object displaying 5 Is - it's much longer. I'm running the script from the Text Editor, but it shows the same results from the Python Console. Since I don't know how to show these values in some Blender window (see above) I had to rig the example so it shows them in other text objects. The two text objects (the "short" and the "long" one) are renamed to T1/T2 after creation. Two "control" text objects (T1_Ctl/T2_Ctl) display the x-size ("length") of T1/T2. The following code could be run after loading the default blend file, deleting the cube, and opening a Text Editor window.
import bpy
# Set the scene units to millimeters and set the grid to a reasonable size
bpy.context.scene.unit_settings.system = 'METRIC'
bpy.context.scene.unit_settings.scale_length = 0.001
for area in bpy.context.screen.areas:
if (area.type == 'VIEW_3D'):
area.spaces.active.grid_scale = 0.01
# Add a "short" text object
bpy.ops.object.text_add(radius=7, location=(-60, 14, 0), rotation=(0, 0, 0))
obj = bpy.data.objects['Text']
obj.name = 'T1'
obj.data.body = 'IIIII' # 5 Is
# Add a "long" text object (presumably 6 times longer)
bpy.ops.object.text_add(radius=7, location=(-60, 0, 0), rotation=(0, 0, 0))
obj = bpy.data.objects['Text']
obj.name = 'T2'
obj.data.body = 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIII' # 30 Is
# Control text object - shows the length of the "short" text object
bpy.ops.object.text_add(radius=7, location=(0, 14, 0), rotation=(0, 0, 0))
obj = bpy.data.objects['Text']
obj.name = 'T1_Ctl'
obj.data.body = str(bpy.data.objects['T1'].dimensions.x)
# Control text object - shows the length of the "long" text object
bpy.ops.object.text_add(radius=7, location=(0, 0, 0), rotation=(0, 0, 0))
obj = bpy.data.objects['Text']
obj.name = 'T2_Ctl'
obj.data.body = str(bpy.data.objects['T2'].dimensions.x)
After running the script, the length of the short text object is reported as 7.686, while the length of the long text object shows as 51.436 (6.7 times longer), which means the longer text is almost 7 times, not 6 times longer.
Does anybody know what's happening here? Where is this extra "padding" coming from? How can I get an accurate length of a text object?
obj = context.object
to reference an object after an operator that creates a new object and sets it to context. Do you get same result when font fixed width and proportional? $\endgroup$