# Hom: Hou.matrix4() -> Matrix3

## Recommended Posts

Hi there,

Does anyone know the quickest way to convert a hou.Matrix4 to a hou.Matrix3?

Basically I have a 4x4 camera matrix from a HOM/SOHO snippet:

camMatrix = hou.Matrix4(cam.getDefaultedFloat('space:world', now, _ident))

# pull out euler position
pos = camMatrix.extractTranslates()

# need to get this into a Matrix 3 for rotation matrix for this renderer.  be nice to just...
# camRotMatrix = camMatrix.extractRotatesAsMatrix3()	-or-
# camRotMatrix = hou.Matrix3( camMatrix )
# -but i can't. :-/

Or is there a maketransform() equiv for hou.Matrix4s if I am forced to go via eulers?

##### Share on other sites

> Does anyone know the quickest way to convert a hou.Matrix4 to a hou.Matrix3?

Hi Jason,

I don't know if this is possible on HOM, but if you're willing to trust/integrate a 3rd party library in the process, CgKit (which is a free Python toolkit to handle 3d related tasks) has both classes and I think enough mapping between the two to solve this fairly efficiently. I hope it both covers your needs and is accessible from an integration and licensing point of view.

Here are the relevant types

Let us know how it goes, any kind of HOM examples are sought after in this world with a small amount of docs

cheers,

Andras

Edited by kodiak
##### Share on other sites

Hi there,

Does anyone know the quickest way to convert a hou.Matrix4 to a hou.Matrix3?

Hi Jason,

I'm likely wrong about all of this because I have yet to use HOM/SOHO, or even learn Python , but I just took a quick look at the hou. docs, and it would seem that you could do something like:

# pull out euler position
pos = camMatrix.extractTranslates()

# pull out rotation vector -&gt; vector3(rx,ry,rz)
torder = 'srt'
rorder = 'xyz'
Vrot = pos.extractRotates(torder,rorder)

# create rotation matrix (4x4)
Mrot = hou.hmath.buildRotate(Vrot,rorder)

The syntax is probably wrong, but you see what I mean. It would still be a Matrix4, but a rotation matrix nonetheless. I suppose you could then construct a Matrix3 by feeding it the upper-left submatrix of Mrot?

...just guessing though... and I'm surprised there's no implicit conversion from Matrix4 to Matrix3 (via truncation)...

HTH.

##### Share on other sites

The syntax is probably wrong, but you see what I mean. It would still be a Matrix4, but a rotation matrix nonetheless. I suppose you could then construct a Matrix3 by feeding it the upper-left submatrix of Mrot?

...just guessing though... and I'm surprised there's no implicit conversion from Matrix4 to Matrix3 (via truncation)...

HTH.

Damn, I forgot to look in the hou.math module for something like buildRotate(). I was just looking in hou.Matrix3.

If nothing else comes to mind it'd be good to RFE a conversion between these Matrix3&4 types, and a Matrix3 equivalent of hou.math.buildRotate(). I'll try using the top left corner as well.

I'm finding that the more often I write SOHO output to a renderer, they more often I find I need a funky conversion to some other representation. Indigo wants Euler coords for position and a 3x3 Matrix for rotations. Why can't they all [at least optionally] just use Matrix4s?

##### Share on other sites

Thanks Mario, I did what you suggested and it works - rotates + scales end up in the 3x3 and I'm good to go.

	objMatrix = hou.Matrix4(obj.getDefaultedFloat('space:world', now, _ident))
pos = objMatrix.extractTranslates()
rotMatrix = hou.Matrix3()
o = objMatrix.asTupleOfTuples()
rotMatrix.setTo( (o[0][0], o[1][0], o[2][0],  o[0][1], o[1][1], o[2][1],  o[0][2], o[1][2], o[2][2]) )

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×