Forum: Poser Python Scripting


Subject: Setting an object's face to 'point towards' another object.

timarender opened this issue on Jul 06, 2013 · 7 posts


timarender posted Sat, 06 July 2013 at 3:47 AM

Consider an object (e.g. a box) where one side faces a camera. I want one side of the box to continue to face the camera when the camera moves.

Posers 'Point At' would probably be sufficient; except that I cannot control which Face points towards the camera.

I now have a mass of code (which does not work!) where I attempted (unsuccessfully) to link up sins/cos/tan asin/acos/atan values with the X Y and Z Rotations of the Object.

Can anyone point me towards an example of suitable code?

In effect, if the Object's coords are (x1,y1,z1); and the Camera's coords are (x2,y2,z2); then what rotX, rotY and rotZ should I give the Object so it points towads the camera?
 


EnglishBob posted Sat, 06 July 2013 at 5:24 PM

You just need to set the object's origin and endpoint correctly for 'Point At' to work properly. This can be done with the joint editor.

That said, 'Point At' isn't always very successful when the object is pointing at a camera - a figure's eyes are a particular case in point. You may be able to get "good enough" results for something less anthropomorphic though.  


markschum posted Sat, 06 July 2013 at 10:36 PM

Darn, beaten by EnglishBob  :cursing:

Point at does not work well for billboards since they will tip if the camera moves up or down.

It can be done by script but its probably easier to adjust manually for a still image.for angle a

Plane geometry will give you the calculation for say (1,1) and camera at (9,7)  x and Z values.

 xstep is 8, zstep is 6 

so for angle a  x and z are opposite over adjacent = 6/8 = tan(a)

so atn(a) = 0.75 = 0. 643 radians = 0.643 * 180/pi = 36.8 degrees

 so rotate in Y to 36.8 to face the target.


timarender posted Sun, 07 July 2013 at 6:46 AM

You guys are amazing! Sadly, I can't get it to work. markschum's suggestion seems to fit generally with what I was doing. I am wondering if someone might examine my code and see what I am doing wrong.

My aim is to have an animation which includes a number of these objects; which is why I would prefer to script the Rotation calculations.

I realise that checking other people's code is not much fun, so I am grateful for any assistance:

ball = scene.Actor("BallCone_1")
cam = scene.Actor("box_1")
ballx = (ball.ParameterByCode(poser.kParmCodeXTRAN).Value())
bally = (ball.ParameterByCode(poser.kParmCodeYTRAN).Value())
ballz = (ball.ParameterByCode(poser.kParmCodeZTRAN).Value())

for v in range (0,scene.NumFrames()):
    scene.SetFrame(v)
    camx = (cam.ParameterByCode(poser.kParmCodeXTRAN).Value())
    camy = (cam.ParameterByCode(poser.kParmCodeYTRAN).Value())
    camz = (cam.ParameterByCode(poser.kParmCodeZTRAN).Value())
    
    xstep=camx-ballx    
    ystep=camy-bally    
    zstep=camz-ballz    

    if xstep<>0:
        tany=(zstep+0.0)/(xstep+0.0)
        roty= math.degrees(math.atan(tany))

    if ystep<>0:
        tanx=(zstep+0.0)/(ystep+0.0)
        rotx= math.degrees(math.atan(tanx))

    if ystep<>0:
        tanz=(xstep+0.0)/(ystep+0.0)
        rotz= math.degrees(math.atan(tanz))

    ball.ParameterByCode(poser.kParmCodeXROT).SetValue(rotx)
    ball.ParameterByCode(poser.kParmCodeYROT).SetValue(roty)
    ball.ParameterByCode(poser.kParmCodeZROT).SetValue(rotz)

scene.SetFrame(0)


markschum posted Mon, 08 July 2013 at 12:00 AM

have a look at the attached code using atan2

This just uses the rotate in y for the current frame.


timarender posted Mon, 08 July 2013 at 3:58 PM

Many thanks. That was extremely helpful. I have also had a chance to learn something (ATAN2) about maths that I was was not taught 50 years ago. Sadly, whilst attempting to solve it myself, I ran into "quaternion" maths; of which I understand 0% and don't have a clue to use; but I presume may be even more satisfactory to use.

However, markschum's solution is good enough for my purposes. I am also grateful to Englishbob; who reminded me that I really should learn a bit about from the manual about joint editing.

Thanks again.

 


markschum posted Mon, 08 July 2013 at 9:28 PM

quaternions melted my brain years ago.

They are required to get the world transforms for body parts as I understand it.

One of the python gurus has some code to do the calculations. Try posting a specific request to this forum.  I only did maths 40 years ago and I dont remember much about array calculations. ;(

ATAN2 seems to aavoid the need for a buch of IF statements to find what quadrant you are in to add the correction to the atan derived angle.

 

My motto for code is "whatever works ! "