1
$\begingroup$

I'm writing a Python script to move a bone of an armature to a desired Global 3D point. When armature has only one bone my function works well, but when I add not connected bones, there seems to be an error in the retrieved local coordinates.

target_vec = Vector((0.0405, -2.0305, 3.1172))
bone.location += (armature.matrix_world * bone.matrix).inverted() * target_vec 

I want the center of the cone to touch one of the sides of the cube (see Figure).

The above function should send the bone to the fp point but it is sending it to the cp point instead.

https://drive.google.com/open?id=1FeIGb1C24Y44RJLfExFJB7EGhgSnzk1w

enter image description here

Any ideas of what am I missing?

$\endgroup$
0
$\begingroup$

Snap active pose bone to 3D cursor.

Using methods outlined in this answer, with a slight change to keep the orientation (rotation) of bone, and set the location to the 3D cursor. To test run in pose mode with an active pose bone, which is snapped to 3D cursor.

import bpy
from mathutils import Matrix

context = bpy.context
scene = context.scene
pb = context.active_pose_bone
arm = pb.id_data

locmat = Matrix.Translation(scene.cursor_location)
# create tramslation matrix in pose space
matrix = arm.matrix_world.inverted() * locmat
# set the new translation to bone.
pb.matrix.translation = matrix.translation
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.