Python - Change local rotation 1 axis at a time

Python - Change local rotation 1 axis at a time

jmalaska
Advocate Advocate
3,081 Views
8 Replies
Message 1 of 9

Python - Change local rotation 1 axis at a time

jmalaska
Advocate
Advocate

I'm a Mobu newbie with Python. 

 

I'm trying to find a way to just change 1 local rotation axis value. I'm familiar with FBVector3d, but that changes all 3 at the same time. Is there some command that'll do it on a local rotation axis, rather than global?

0 Likes
3,082 Views
8 Replies
Replies (8)
Message 2 of 9

pixerati
Enthusiast
Enthusiast

I think there are two different questions there.

 

1. To only rotate around one axis, use GetVector to get the current values, store them in a variable, then out them straight back in the SetVector, changing only the axis you want to change.

2. To rotate around the local axis, just set the global flag in SetVector to false (check out the docs). Make sure though that you also set the global flag to false in the GetVector or you'll be copying global values back as locals when you use SetVector and that would just be a bit silly.

0 Likes
Message 3 of 9

petecmartin
Advocate
Advocate

How do you change only the one axis?

For instance if you wanted to rotate the current y axis by 90 degrees how would you go about doing this?

I've tried this:

lVector = FBVector3d()

mRotationOffset = FBVector3d(0,90,0)

FBSystem().Scene.Evaluate()

#obj.GetVector(lVector,local_translation,False)

obj.GetVector(lVector,local_rotation,False)

Offset = lVector - mRotationOffset


#obj.SetVector(Offset,local_translation, False)

obj.SetVector(Offset,local_rotation, False)



This however, doesn't do that same as selecting the object in local and rotating in the Y axis.  It's all over the place.

Cheers.

0 Likes
Message 4 of 9

Mocappy
Advocate
Advocate

Think this does what you're looking for...

from pyfbsdk import *

# Create Cube
myCube = FBModelCube("RotateMe")
myCube.Show = True
# Set random local rotation Vector
myCube.SetVector(FBVector3d(0,14,0), FBModelTransformationType.kModelRotation, False)
# Store current local roation values
lVector = FBVector3d()
myCube.GetVector(lVector, FBModelTransformationType.kModelRotation, False)
# Create new vector
newVector3d = FBVector3d(lVector[0]+0, lVector[1]+90, lVector[2]+0)
# Set new vector
myCube.SetVector(newVector3d, FBModelTransformationType.kModelRotation, False)

 

Message 5 of 9

petecmartin
Advocate
Advocate

Thank you mocappy!  I'll give it a whirl & see if it works.  Cheers.

0 Likes
Message 6 of 9

petecmartin
Advocate
Advocate

Thank you for the feedback Mocappy.

I've tried your method and, sadly, I get the same results as before.

I manage to get around this, however, by: creating two nulls at 0,0,0, then rotating one of them by 90 degrees ; then parent/child constraining the rotated one to the none-rotated one and then zero parent/child constraining the un-rotated null to the object I want.  Voila!

It's mad that the local rotation method doesn't  want to work how it says on the tin :(.

0 Likes
Message 7 of 9

vdebaie
Advocate
Advocate

This method works for me, I just ran Mocappy's script - a few times - and I get the intended result of only the the local y axis being affected.

0 Likes
Message 8 of 9

petecmartin
Advocate
Advocate

Hi, 

 

It looks like it's a gimble thing.  My null was aligned to a camera.  I then run Mocappy script to just affect the y-axis and it rotates the y but also the x and z?  Is it something i'm doing wrong?

0 Likes
Message 9 of 9

Mocappy
Advocate
Advocate

Hi,

 

You might need to use FBMatrix() for something like this - sorry, didn't realise you have a parent child constraint going on.

 

I created the same setup as you based on your description: created a camera, a null and a parent/child constraint. Set camera as Source(Parent), and null as Constrained object(Child) - should also work if Null is child of camera in hierarchy. 

 

Run the following script and the null rotates -90 in Y:

from pyfbsdk import *

# Set Offset Rotation Vector x, y, z
offsetVector3d = FBVector3d(0,-90,0)
# Find 'Null'
null = FBFindModelByLabelName("Null")
# Get Null RotationOrder
nullRotationOrder =  "FBRotationOrder."+str(null.RotationOrder).replace("Euler","")
# Create Empty Matrix for Null
nullMatrix = FBMatrix()
# Get Null Matrix
null.GetMatrix(nullMatrix)
# Create Empty Matrix for Offset
offSetMatrix = FBMatrix()
# Convert 3dVector offset to rotation Matrix
FBRotationToMatrix(offSetMatrix, offsetVector3d, eval(nullRotationOrder))
# Multiply Matrices
nullMatrix *= offSetMatrix
# Set Null Matrix
null.SetMatrix(nullMatrix)

 

Hope this is  more like what you are after,