Cube to bone tail, Blender console and external script yielding different results

In an external script I would like to rotate a bone (pose mode) and then move a cube to the tail of that bone’s new position. I have attached the .blend file for this. This is my situation:

The initial state:


(The resting location of the bone’s tail is at: <0.0, 0.0, 1.0>)

If I run the following code in Blender’s console it works fine. The cube moves to the new location of the bone’s tail.
Code:cube = bpy.data.objects[“Cube”]
armature = bpy.data.objects[“Armature”]
pose_bone = armature.pose.bones[“Bone”]
pose_bone.rotation_euler[0] = 0.568977
cube.location = pose_bone.tail
Moved location:


If the exact same code is run in the text editor or in an external script the result is different. The bone still rotates to the new position as shown in the above picture, but pose_bone.tail doesn’t get updated and thus the cube goes to the bone’s tail position before rotation (<0.0, 0.0, 1.0>).

I noticed that if I rotate the bone via a script and then save the .blend file, the bone’s tail position updates. So I fixed the problem by doing this:cube = bpy.data.objects[“Cube”]
armature = bpy.data.objects[“Armature”]
pose_bone = armature.pose.bones[“Bone”]
pose_bone.rotation_euler[0] = 0.568977
bpy.ops.wm.save_mainfile()
cube.location = pose_bone.tail

But this isn’t a good solution. I’ll be changing bone tails thousands of times during my script execution, saving an entire .blend file for each move will slow my program down too much. Please let me know if anyone has a better solution (better performance).

Attachments

tail_position.blend (464 KB)