3
$\begingroup$

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?

$\endgroup$
3
  • 1
    $\begingroup$ Suggest using 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$
    – batFINGER
    Commented Dec 12, 2017 at 14:55
  • $\begingroup$ I think I know what's happening. In the long string there are indeed 6 groups of 5 Is, so my assumption was that the length of the long string should be 6 times the length of the short one. But the long string also contains 5 extra inter-character spaces. I think that if I set the inter-character spacing to zero, the length of the long string will become exactly 6 times the length of the short one, which would be re-assuring, because it would prove that there is no discrepancy (just false expectations on my side). I'll try to do this tomorrow and I'll post the code changes here if it works. $\endgroup$
    – Dan
    Commented Dec 12, 2017 at 23:10
  • $\begingroup$ Open up the console first, that is where your text editor output goes. $\endgroup$
    – Leander
    Commented May 28, 2018 at 6:38

1 Answer 1

1
$\begingroup$

The reason for the discrepancy is the inter-character spacing (obj.data.space_character, where obj is the long text object). There are 5 additional inter-character spaces in the long text object, and if I add them to the short string length x 6 then the lengths almost match. There is still a small discrepancy (of 0.06 mm/batch of 5 Is) which I can't explain, but it's so small that it can be safely ignored for the purpose of this exercise.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .