This, of course, does not work but the intention is intact.
How would I convert this two line pseudo code into working Blender 2.7 code that converts the string to variables and makes a matrix that when applied to a camera object makes the camera point towards the world origin?
If I type in the same exact numbers from Houdini camera into the Blender Camera and print the blender camera matrix the numbers are quite different. Also the Blender camera does not point at the world origin like the Houdini camera does. So some kind of internal translation must be going on.
hmm, it is too late in the night for me to look at any math but i think the blender and houdini matrices you mentioned in post #3 are just transposed. So basically
mat_bln = mat_hou.transposed()
where both of them are Matrix objects ofc
A 4x4 matrix encodes rotation, translation, and scaling data. If you want to convert a 4x4 from a left-handed system to a right-handed system then you might have to decompose the 4x4 into a 3x3 rotation and a 1x4 translation vector.
Converting the translation is pretty simple. You your translation vector should be (x, y, z, 1) you just invert the z value. i.e.:
vec.z *= -1.0
Converting the rotation seem to be more complicated and depends partly on how Houdini put the rotation together and which axis you want to invert. This seems to have some formulas to follow:
Thanks for the replies. That PDF is where I got the image from.
I just don’t understand the work flow or how to read colledge math symbols.
This matrix math is my kryptonite. I don’t know it. But I am good at logic and could use a black-box function that encompasses the process. That is why I am looking for working code, not just suggestions on how to proceed.
The simple flip of axis does not work, that was the first thing I tried. It does get you a narrow solution as long as the camera stays in one quadrant (like x positive and y negative) But as soon as you animate the camera across any world origin axis the rules for the math conversion change. That is why I fear it must be some kind of matrix math solution.
import bpy
from mathutils import Vector,Matrix
def applyTransform(sMatrix, passedOb):
if passedOb != None:
s = sMatrix
tmp = eval(s)
mtx = Matrix(tmp)
passedOb.matrix_world = mtx
passedOb.location = [mtx[3][0],-mtx[3][1],mtx[3][2]] #Flip Z and Y axis.
# String from Houdini matrix.
s = "[[0.900496, 3.27826e-007, -0.434865, 0], [-0.117881, 0.962558, -0.244102, 0], [0.418583, 0.271075, 0.866779, 0], [5.32776, 2.57303, 7.33473, 1]]"
ob = bpy.data.objects.get("Camera")
applyTransform(s,ob)
This gets me close. When I look through the camera I see basically the scene I have in Houdini except the entire scene in Blender needs rotated 90 degrees minus along the X-axis. But if I just rotate all the objects by 90 degrees that does not work. I guess I need to rotate the entire scene around the world origin?
After searching this forum for rotate around, I found Kastoria’s post on the subject. His code mixed with my initial black box template, above, produced the solution I was looking for.
Thanks everyone for the tips and links.
import bpy, math
from mathutils import Vector,Matrix
def applyTransform(sMatrix, passedOb):
if passedOb != None:
tmp = eval(sMatrix)
mtx = Matrix(tmp)
passedOb.location = [mtx[3][0],-mtx[3][1],mtx[3][2]] # Flip Z and Y axis.
mtx.transpose() # From BL Stack user.
rot_x_neg90 = Matrix.Rotation(math.pi/2.0, 4, 'X') # From Kastoria.
passedOb.matrix_world = rot_x_neg90 * mtx # From Kastoria.
# Matrix string from Houdini object.
s = "[[0.900496, 3.27826e-007, -0.434865, 0], [-0.117881, 0.962558, -0.244102, 0], [0.418583, 0.271075, 0.866779, 0], [5.32776, 2.57303, 7.33473, 1]]"
ob = bpy.data.objects.get("Camera")
applyTransform(s,ob)