Jump to content

Python beginner - placing a bone between two locators

Recommended Posts

learning python I'm trying to write a script that will place a bone between two locators.
I managed to read the locators position and place the bone in the first locator's position
I got the distance between the two locators using hou.distanceTO() and passed it to the bone's length channel.
the only thing I'm missing and can't find the way to do it is find the angles for the rotation of the bone to aim at the second locator. how can I do that?

this is my code:

import objecttoolutils

#store selected objects
sl = hou.selectedNodes()
sla = sl[0]
slb = sl[1]

#evaluate position of selected objects
n1 = sla.worldTransform().extractTranslates()
n2 = slb.worldTransform().extractTranslates()

#create bone and position to first selected object
my_bone = hou.node("/obj").createNode('bone')
px = my_bone.parm("tx").set(n1[0])
py = my_bone.parm("ty").set(n1[1])
pz = my_bone.parm("tz").set(n1[2])

pt1 = hou.Vector3(n1)
pt2 = hou.Vector3(n2)

b1 = pt1.distanceTo(pt2)

bl = my_bone.parm('length')




Share this post

Link to post
Share on other sites

There's a lookat matrix that can help you out there.

buildLookatRotation(to_node, up_vector=None)  hou.Matrix4

Return a matrix that will rotate this object to look at the specified object.

The returned hou.Matrix4 object transforms this object from its current position in world space so that its negative z axis points at the origin of the to_node object.

up_vector can either be a hou.Vector3 object or None. If it is None, this method uses an up vector of hou.Vector3(0, 1, 0).

You can extract the rotation values from the return value with hou.Matrix4.extractRotates(). You can set an object’s transform with hou.ObjNode.setWorldTransform().

# Set the cam1 object's transform so it points at geo1.
cam1 = hou.node("/obj/cam1")
lookat_obj = hou.node("/obj/geo1")

See also hou.ObjNode.setWorldTransform().

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now