Should be possible to add an option to save models with its transform hardened?

Hi everyone,

Many times I have scene completed with many models properly located with trasnforms and I should like to save them as STL to manage outside Slicer. I shouldn´t like to harden them because sometimes a little position fix is needed later.

It should be very usefull to be able to save models with/without transform in Save Panel as:

image

Thanks on advance!

OS: Win10

Scene save is primarily for data saving. There are various modules that are better suited for data export.

For example, you can export models from the Data module’s subject hierarchy tree using right-click menu if you install Slicer Morph extension. If needed, the behavior can be tuned (e.g., make sure transform is applied, allow export an entire folder, etc).

@muratmaga

As @lassoan eluded, if you install SlicerMorph extension, there will be an ExportAs plugin added to the data module.

If you do want to retain the transform and the model, you clone both, harden the cloned pair and use the ExportAs.

1 Like

Clone models and harden transforms is the way I´m working nowadays using Scene Save. The trouble is when many models have to be experted from the scene.
I´ve install Slicer Morph extension and export models directly in Data Module is a really usefull way to save data as STL.
If the plugin should add the option I´ve proposed, it should be even better solution for this task.

Thanks both @lassoan and @muratmaga.

Note that you can customize entire Slicer’s behavior to make you highly productive using short Python code snippets. For example, the entire “Export as” feature is essentially 30 lines of Python code. You can start from that and modify it to fit your needs (e.g., clone the node and harden the transform). If it works well for you then you can contribute it back to SlicerMorph (and in some form the export feature will be integrated into the Slicer core).

Ok. Working on it. Give a while… I´ll post it when it works…

image

Thank you!

Good night! I´m a bit in trouble with this task.

Code added to extension:

shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
itemIDToClone = shNode.GetItemByDataNode(node)
clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemIDToClone)
clonedNode = shNode.GetItemDataNode(clonedItemID)
transformLogic = slicer.vtkSlicerTransformLogic()
transformLogic.hardenTransform(clonedNode)
fileName = qt.QFileDialog.getSaveFileName(slicer.util.mainWindow(),"Export As...", clonedNode.GetName(), writerFormat)
extension = slicer.vtkDataFileFormatHelper.GetFileExtensionFromFormatString(writerFormat)
if not fileName.endswith(extension):
   fileName = fileName + extension
 if fileName == "":
   return
writerType = slicer.app.coreIOManager().fileWriterFileType(clonedNode)
success = slicer.app.coreIOManager().saveNodes(writerType, {"nodeID": clonedNode.GetID(), "fileName": fileName})
if success:
  logging.info(f"Exported {clonedNode.GetName()} to {fileName}")
else:
  slicer.util.errorDisplay(f"Could not save {clonedNode.GetName()} to {fileName}")

Original Scene:

Add Trasnform to OriginalModel_Yellow:

Second

Right click menu to save hardened model:
Fourth

But result (new cloned Mode in Red)…:

Original yellow model come back to original position but is still trasnformed??? Cloned model is OK. If I import saved model, it is in hardened position as expected too.

What am I doing worng with OriginalModel_Yellow? I think I´m just cloning it but don´t looks like.

OS: Win10
Slicer ver: 4.11.20200930 r29402 / 002be18 (stable)

Thanks!

Ok. I answare myself.
I think is something related with Slicer version. In new 4.13 when you clone a node, new cloned node is not related to original node transform.

I´ve add 2 lines to code to apply transform to cloned node and when I harden it there is no change in the original node.

it works for me!
@lassoan, who can I share modded file ExportAs.py?

Great, congratulations!

The proper way of proposing changes to source code is via github (fork the repository into your github account, make the changes there, and then create a pull request). Since changes affect a single file, you can do all these steps with a few clicks on the github website: click the edit button here, make the changes in the code, and click “Propose changes” button at the bottom.

2 Likes

Congrats. You can fork the SlicerMorph repo (GitHub - SlicerMorph/SlicerMorph: Extension to import microCT data and conduct 3D morphometrics in Slicer) make changes and submit a PR. We will review and incorporate the changes (as is or revised) @smrolfe @pieper

1 Like

Done!
Thanks to all.