[python]Convert Houdini Matrix To Blender Matrix?

   5556   4   0
User Avatar
Member
2529 posts
Joined: June 2008
Offline
Hi All,

I am trying to convert a Houdini matrix to Blender matrix so that when the matrix is applied in Blender the camera faces the same direction and is in the same place as it was in Houdini.

Does anyone have any python code to achieve such a task?

I am fetching the Houdini matrix like so…

hou.node(“/obj/cam1”).parmTransform()

This produces a string like this.

[,
,
,
]


I can blindly create a matrix out if it in Blender but when I assign that matrix to the Blender camera it just sits at world origin although it does have some rotation applied.

s = "[, , , ]"
ms = eval(s)
mtx = Matrix(ms)
ob_camera.matrix_world = mtx


If I extend that code by manually extracting the location from the Houdini matrix the Blender camera will move to a new location but it does not match the way the Houdini camera looks.

ob_camera.location.x = 5.32776
ob_camera.location.y = 2.57303
ob_camera.location.z = 7.33473



I am looking for some kind of “black-box” python function that will accept a Houdini matrix and return a valid Blender matrix. Like shown in this math image.

Attachments:
formula.jpg (18.6 KB)

Using Houdini Indie 20.0
Ubuntu 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
387 posts
Joined: Nov. 2008
Offline
Blender uses column-major storage for matrix but Houdini uses row-major.
So for getting right position you need transpose that matrix.

Something like:
import mathutils
mtx.transpose()
ob_camera.matrix_world = mtx

Also don't forget that in Houdini is Y-axis up but Blender is Z-up, so you need to do conversion somewhere.
User Avatar
Member
2529 posts
Joined: June 2008
Offline
Thanks for that line of code. It does seem to help but the x axis is still wrong.

Do you know how to rotate a matrix along the x-axis using the world origin as the pivot point?

Here is the code I currently have working.

s = "[, , , ]"
tmp = eval(s)
mtx = Matrix(tmp)
ob_camera.location = [mtx,-mtx,mtx] #Flip Z and Y axis.
mtx.transpose()
ob_camera.matrix_world = mtx

It seems I have to transpose the matrix after I assign the location.
Using Houdini Indie 20.0
Ubuntu 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
387 posts
Joined: Nov. 2008
Offline
You cannot just switch axis in translation part of transform matrix, you need do transformation on whole matrix.

You need inverted task of this:
http://www.sidefx.com/index.php?option=com_forum&Itemid=172&page=viewtopic&t=22279&view=next&sid=65fb6e0ccd2b297e3082811ce081a01b [sidefx.com]

And some info about that:
http://blog.duber.cz/3ds-max/the-transformation-matrix-is-useful-when-understood [blog.duber.cz]
User Avatar
Member
2529 posts
Joined: June 2008
Offline
Thanks, I got it to work, but I am still not sure I fully understand the Matrix manipulations.


import bpy, math
from mathutils import Vector,Matrix

def applyTransform(sMatrix, passedOb):
if passedOb != None:
tmp = eval(sMatrix)
mtx = Matrix(tmp)
passedOb.location = [mtx,-mtx,mtx] # 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 = "[, , , ]“
ob = bpy.data.objects.get(”Camera")
applyTransform(s,ob)
Using Houdini Indie 20.0
Ubuntu 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
  • Quick Links