0
$\begingroup$

I have an object that is a line that fallows the animation path of a bone. Is there a way to display the length of the arm in text (that can be rendered) that changes the numbers as the arm stretches?

edit.

I tired this:

import bpy

def distance(p1, p2):
    return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2+ (p1[2]-p2[2])**2)

def update_size(scene):
    point1 = scene.objects['Vert.000'].location
    point2 = scene.objects['Vert.001'].location
    point3 = scene.objects['Vert.002'].location
    len1 = distance(point1, point3)
    len2 = distance(point2, point3)
    scene.objects['Length1'].data.body = '{:.3f}'.format(len1)
    scene.objects['Length2'].data.body = '{:.3f}'.format(len2)
    scene.objects['LengthTotal'].data.body = '{:.3f}'.format(len1 + len2)

bpy.app.handlers.frame_change_pre.append(update_size)

But it does not seem to work when run from the text editor, and when copy pasted and run in console, only updates the first frame.

$\endgroup$

1 Answer 1

1
$\begingroup$

We can use a frame change handler to alter the text objects content before each frame during playback.

import bpy

def distance(p1, p2):
    return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2+ (p1[2]-p2[2])**2)

def update_size(scene):
    point1 = scene.objects['Empty.001'].location
    point2 = scene.objects['Empty.002'].location
    scene.objects['Text'].data.body = '{:.3f}'.format(distance(point1, point2))

bpy.app.handlers.frame_change_pre.append(update_size)

# to disable
# bpy.app.handlers.frame_change_pre.remove(update_size)

Instead of using distance between two points you may prefer to add the length of the pose bones between shoulder and hand.

format(scene.objects['rig'].pose.bones['upper_arm.L'].length +
        scene.objects['rig'].pose.bones['forearm.L'].length)
$\endgroup$
3
  • $\begingroup$ First time scripting with blender, I understand python, not an issue there. When I try and run basic test [>>import bpy >>scene.objects["Length1"].data.body = "Test"] on a text object named "Length1" I get the error: name 'scene' is not defined $\endgroup$
    – Drew
    Mar 18, 2016 at 0:11
  • $\begingroup$ Figured out what was wrong, its not scene.objects[], its bpy.data.objects[] $\endgroup$
    – Drew
    Mar 18, 2016 at 0:18
  • $\begingroup$ I used scene.objects within the callback function, this works within the function as the current scene is passed in when the function is run. Outside if the function you would need to use bpy.data.objects. $\endgroup$
    – sambler
    Mar 18, 2016 at 5:42

You must log in to answer this question.

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