Hi there,
You could do that using just expression links and some trig:
set cut_paste_input [stack 0]
version 6.1 v5
push $cut_paste_input
Cube {
translate {-3 2 0}
rotate {{"degrees(atan2(Cube3.translate.y - this.translate.y,
sqrt((Cube3.translate.x - this.translate.x)**2 + (Cube3.translate.z -
this.translate.z)**2)))"} {"-degrees(atan2(Cube3.translate.x -
this.translate.x, - (Cube3.translate.z - this.translate.z)))"} {curve}}
name Cube4
selected true
xpos 189
ypos -145
}
Cube {
inputs 0
translate {3 0 2.58999991}
rotate {{"degrees(atan2(Cube4.translate.y - this.translate.y,
sqrt((Cube4.translate.x - this.translate.x)**2 + (Cube4.translate.z -
this.translate.z)**2)))"} {"-degrees(atan2(Cube4.translate.x -
this.translate.x, - (Cube4.translate.z - this.translate.z)))"} {curve}}
name Cube3
selected true
xpos 13
ypos -145
}
However, if the point is to learn how to do it in Python using Vector and
Matrix objects, I would do something like this:
#########################
cube1=nuke.toNode('Cube1')
cube2=nuke.toNode('Cube2')
# I would recommend doing this to get each cube's position, since
translate.x, translate.y and translate.z may not be the same as the object's
final position
cube1Mat=cube1.knob('transform').value() # Get the cube's transformation
matrix
cube1Pos = cube1Mat.transform(nuke.math.Vector3(0,0,0)) # Get the cube's
position
cube2Mat=cube2.knob('transform').value()
cube2Pos = cube2Mat.transform(nuke.math.Vector3(0,0,0))
# Now build a rotation matrix that would rotate cube1 to look at cube2
cube1RotMatrix = nuke.math.Matrix4()
cube1RotMatrix.makeIdentity()
V = cube2Pos - cube1Pos # Vector from cube1 to cube2
V.normalize()
zAxis = cube1Mat.zAxis() # Initial "look at" vector of your cube
zAxis.normalize()
# Get the axis around which we'll rotate the matrix, and the angle (in
radians) of rotation
# I looked this one up online, so feel free to use any method you want to
build your rotation matrix
rot_angle = math.acos( V.dot(zAxis) )
rot_axis = V.cross(zAxis)
rot_axis.normalize()
cube1RotMatrix.rotation(rot_angle, rot_axis)
# This bit is just to convert the matrix to a right-handed coordinate
system, since the method above builds it for a left-handed
cube1RotMatrix.transpose()
cube1RotMatrix.scale(1,1,-1)
cube1RotMatrix.transpose()
# At this point you already have a "lookAt" rotation matrix
# If you want to get the Euler components to use them in a "rotate knob",
you can do:
EulerRotations = cube1RotMatrix.rotationsZXY()
rotX, rotY = math.degrees(EulerRotations[0]),
math.degrees(EulerRotations[1])
###########################
Hope that helps.
Cheers,
Ivan
On Tue, Jul 5, 2011 at 9:06 AM, j00ey <[email protected]>wrote:
> **
> Hi all
>
> I'm new to python and am trying to learn some by writing a script that will
> make one object look at another and I'm stuck. I have the positions of each
> object and the vector between them but I can't figure out where to go next.
> This is what I have so far...
>
> ################
>
> cube1=nuke.toNode('Cube1')
> cube2=nuke.toNode('Cube2')
>
> cube1Trans=cube1.knob('translate').getValue()
> cube2Trans=cube2.knob('translate').getValue()
>
> cube1TransVec=nuke.math.Vector3()
> for i in range(len(cube1Trans)):
> cube1TransVec[i] = cube1Trans[i]
>
> cube2TransVec=nuke.math.Vector3()
> for i in range(len(cube2Trans)):
> cube2TransVec[i] = cube2Trans[i]
>
> vec=nuke.math.Vector3()
> vec=cube1TransVec-cube2TransVec
>
> ###############
>
>
> Can anyone help me out? Many thanks in advance
>
> _______________________________________________
> Nuke-python mailing list
> [email protected], http://forums.thefoundry.co.uk/
> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>
>
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python