Jump to content

Python beginner - placing a bone between two locators


gweiss

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')
bl.set(b1)

Thanks
gweiss
 

 

angle.png

Link to comment
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")
cam1.setWorldTransform(cam1.buildLookatRotation(lookat_obj))

See also hou.ObjNode.setWorldTransform().

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...