How to import slicer transfoms (.h5 files) into Blender?

Hi everyone!

I want to extract rotation and position data from an Slicer transform (TR.h5) into Blender.

For now, I did:

1.- Install h5py library in Blender with add-on blender_pip: Python module manager from GitHub - amb/blender_pip: A Blender addon for managing Python modules inside Blender with PIP

2.- Extract matrix values:

with h5py.File(path, “r”) as f:
g = f[group]
params = np.array(g[‘TransformParameters’][:], dtype=float)
fixed = np.array(g[‘TransformFixedParameters’][:], dtype=float) if ‘TransformFixedParameters’ in g else np.zeros(3)

I don´t know how to build a valid matrix to make a Blender transform from these data.

Thanks in advance for any help!

You don’t have to install h5py (HDF5 can be a problematic library due to ABI incompatibilities). It is easier to save the transform as a text file (.tfm format) and then you can use this code snippet in the script repository to get the transformation matrix.

However, I would recommend not to waste time with saving and loading files. It is easier to start an OpenIGTLink server in Slicer (using OpenIGTLink extension) to expose any number of transforms, then in Blender use pyigtl to get transforms from Slicer in real-time (or send transforms to Slicer). You can also send/receive meshes, images, etc.