Assign a Houdini Matrix To A Blender Object?

Hi All,

I have reduced the problem to a two line pseudo code example.

 
mtx = "[[0.995962, 3.75651e-007, -0.0897709, 0],
 [-0.0315305, 0.936289, -0.349811, 0],
 [0.0840514, 0.351229, 0.932509, 0],
 [0.501335, 3.35349, 8.86189, 1]]"
ob.matrix_world = mtx

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?

Thanks

Ok, a little bit closer.


import bpy
from mathutils import Vector, 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]]"
cmd = "mtx = Matrix(%s)" % s
exec(cmd)
print(type(mtx))
print(mtx)

ob = bpy.data.objects.get("Camera")
ob.matrix_world = mtx

This does appear to rotate the camera object but leaves it a world origin?

The XYZ position are here [5.32776, 2.57303, 7.33473, 1]

I thought Matrix operations handled all coordinate values as one variable?

What more do I need to do to make the matrix use the translation part?

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.


mtx_hou = "[[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]]"
mtx_bln = "[[ 0.9005, -0.1179, 0.4186, 5.3278],[0.0000,  0.9626, 0.2711, 2.5730], [-0.4349, -0.2441, 0.8668, 7.3347], [0.0000,  0.0000, 0.0000, 1.0000]]"

After searching around a bit I found a graphic image that shows, in math symbols, the kind of conversion that I need to do in code.

Does anyone understand how to implement this image in python code?

Attachments


hmm, it is too late in the night for me to look at any math :stuck_out_tongue: 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

hope this helps

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.

I guess the black box building starts here.

Here is what I have.


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)

you can replace
math.pi/2.0

by a more readable

from math import radians

radians(90)