Exporting a DTI object as a 3D File

Operating system: macOS
Slicer version: 4.3 & 4.7

Hi everyone,

While I was building a tractography display, Slicer allows me to export it as a *.obj file, however, it fails to export the labels file. Thus, when I try to open that *.obj file in any 3D modeler, it does not have the colors associated with the tractography analysis.
I already read the thread Export a tractography display as a 3D file, but the code provided by James and Steve does not seem to work on Slicer 4.3 nor Slicer 4.7.

Can anyone help me out with this? Is is possible to merge the label files with the object/model file?

I used the following source-code:

Slicer 4.3
plyFilePath = “/tmp/fibers.ply”

lineDisplayNode = getNode(“LineDisplay”)

tuber = vtk.vtkTubeFilter()
tuber.SetInput(lineDisplayNode.GetOutputPolyData())

tubes = tuber.GetOutput()
tubes.Update()
scalars = tubes.GetPointData().GetArray(0)
scalars.SetName(“scalars”)

triangles = vtk.vtkTriangleFilter()
triangles.SetInput(tubes)

colorNode = lineDisplayNode.GetColorNode()
lookupTable = vtk.vtkLookupTable()
lookupTable.DeepCopy(colorNode.GetLookupTable())
lookupTable.SetTableRange(0,1)

plyWriter = vtk.vtkPLYWriter()
plyWriter.SetInput(triangles.GetOutput())
plyWriter.SetLookupTable(lookupTable)
plyWriter.SetArrayName(“scalars”)

plyWriter.SetFileName(plyFilePath)
plyWriter.Write()

Slicer 4.7 (James mentioned it was untested and it does not effectively work)
plyFilePath = “/tmp/fibers.ply”

lineDisplayNode = getNode(“LineDisplay”)

tuber = vtk.vtkTubeFilter()
tuber.SetInputData(lineDisplayNode.GetOutputPolyData())

tubes = tuber.GetOutputData()
tubes.Update()
scalars = tubes.GetPointData().GetArray(0)
scalars.SetName(“scalars”)

triangles = vtk.vtkTriangleFilter()
triangles.SetInputConnection(tubes)

colorNode = lineDisplayNode.GetColorNode()
lookupTable = vtk.vtkLookupTable()
lookupTable.DeepCopy(colorNode.GetLookupTable())
lookupTable.SetTableRange(0,1)

plyWriter = vtk.vtkPLYWriter()
plyWriter.SetInputConnection(triangles.GetOutputPort())
plyWriter.SetLookupTable(lookupTable)
plyWriter.SetArrayName(“scalars”)

plyWriter.SetFileName(plyFilePath)
plyWriter.Write()
Thanks in advance,

Vasco Pontinha

Hi Vasco -

I updated the example script here and it worked for me with Slicer 4.7-2017-08-10 with SlicerDMRI installed. There was a missing update for the triangle filter.

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_a_fiber_tracts_to_Blender.2C_including_color

HTH,
Steve

1 Like

Hi Steve,

Thank you so much for updating the code - I was successful in exporting the file!!

Vasco

Hi, Where do I have to put that script?

The script is now available as a module in SlicerDMRI in SlicerPreview (nightly) builds, called Export tractography to PLY (mesh).

1 Like

Hi, I tried to do like that but it was this error

Traceback (most recent call last):
File “C:/Users/AIDE/AppData/Roaming/NA-MIC/Extensions-27159/SlicerDMRI/lib/Slicer-4.9/qt-scripted-modules/TractographyExportPLY.py”, line 149, in onExport
number_of_sides = self.numSidesSelector.value)
File “C:/Users/AIDE/AppData/Roaming/NA-MIC/Extensions-27159/SlicerDMRI/lib/Slicer-4.9/qt-scripted-modules/TractographyExportPLY.py”, line 180, in exportFiberBundleToPLYPath
tuber.SetNumberOfSides(number_of_sides)
TypeError: SetNumberOfSides argument 1: integer argument expected, got float

I do not know what to do.

Thanks for the report. I pushed a fix that will be available in the SlicerPreview build tomorrow. For a quick local fix, you can edit the file:

C:/Users/AIDE/AppData/Roaming/NA-MIC/Extensions-27159/SlicerDMRI/lib/Slicer-4.9/qt-scripted-modules/TractographyExportPLY.py`

and change line 180 from

tuber.SetNumberOfSides(number_of_sides)

to

tuber.SetNumberOfSides(int(number_of_sides))

1 Like

Thank you help me Can I export it with color?

Hi. How do I export the tractography with the colors in windows 10?

Color export works – try setting the color mode to FractionalAnisotropy in the Tractography Display module (I checked PLY output in MeshLab). The issue is that the range is set to [0,1] for all modes, which is too wide for some of the scalar maps. In those cases most/all of the mesh vertices end up clamped to one end of the colormap range.

I will make this an option in the GUI, but here is a change you can make locally for now:

diff --git a/Modules/Scripted/TractographyExportPLY/TractographyExportPLY.py b/Modules/Scripted/TractographyExportPLY/TractographyExportPLY.py
index 7de1b31b6..ce782ddf1 100644
--- a/Modules/Scripted/TractographyExportPLY/TractographyExportPLY.py
+++ b/Modules/Scripted/TractographyExportPLY/TractographyExportPLY.py
@@ -194,7 +194,8 @@ class TractographyExportPLYLogic(ScriptedLoadableModuleLogic):
     colorNode = lineDisplayNode.GetColorNode()
     lookupTable = vtk.vtkLookupTable()
     lookupTable.DeepCopy(colorNode.GetLookupTable())
-    lookupTable.SetTableRange(0,1)
+    scalarRange = lineDisplayNode.GetScalarRange()
+    lookupTable.SetTableRange(scalarRange[0], scalarRange[1])

     plyWriter = vtk.vtkPLYWriter()
     plyWriter.SetInputData(triangles.GetOutput())

A post was split to a new topic: Save derived DTI heatmap