Resetting a transform in an existing node

Hi,

In my extension, in some context, I need to reset a transform to Identity, without changing its node otherwise (that is, I need to keep its state wrt volumes, display-nodes etc). When I first wrote this, a few years ago, I found an effective way to do this was by calling

transformNode.GetTransformToParent().Identity()

But recently, I found that (at least as of Slicer 5.8.1, maybe even before that) there’s a problem with this: afterslicer.mrmlScene.AddNewNodeByClass("vtkMRMLTransformNode"), the transform to parent is not initialized, and transformNode.GetTransformToParent() returns None.

An obvious workaround is:

if to_parent := transformNode.GetTransformToParent():
    to_parent.Identity()

But it feels dirty. Is there a cleaner way?

You can set the transform matrix to identity with:

transformNode.SetMatrixTransformToParent(vtk.vtkMatrix4x4())

This is the way that it’s done from the transform module widget: Slicer/Modules/Loadable/Transforms/qSlicerTransformsModuleWidget.cxx at feeb45346728c3a37115783aa40e03da3bfb2bf2 · Slicer/Slicer · GitHub