# Exporting a DTI object as a 3D File

**URL:** https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987
**Category:** Support
**Tags:** diffusion
**Created:** [September 1, 2017, 3:36am UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987 "2017-09-01T03:36:01Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![vascopontinha](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@vascopontinha](https://discourse.slicer.org/u/vascopontinha)
#### Post date: [September 1, 2017, 3:36am UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/1 "2017-09-01T03:36:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [September 1, 2017, 2:09pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/2 "2017-09-01T14:09:46Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_a_fiber_tracts_to_Blender.2C_including_color)

HTH,  
Steve

---

<div class="post-metadata">

### Author: ![vascopontinha](https://avatars.discourse-cdn.com/v4/letter/v/dfb087/32.png) [@vascopontinha](https://discourse.slicer.org/u/vascopontinha)
#### Post date: [September 15, 2017, 7:54pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/3 "2017-09-15T19:54:10Z")

</div>

Hi Steve,

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

Vasco

---

<div class="post-metadata">

### Author: ![pedro\_luis\_solarte](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@pedro\_luis\_solarte](https://discourse.slicer.org/u/pedro_luis_solarte)
#### Post date: [April 26, 2018, 1:34pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/4 "2018-04-26T13:34:43Z")

</div>

Hi, Where do I have to put that script?

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [April 26, 2018, 1:50pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/5 "2018-04-26T13:50:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pedro\_luis\_solarte](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@pedro\_luis\_solarte](https://discourse.slicer.org/u/pedro_luis_solarte)
#### Post date: [April 26, 2018, 5:01pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/6 "2018-04-26T17:01:32Z")

</div>

**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.**

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [April 26, 2018, 8:59pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/7 "2018-04-26T20:59:16Z")

</div>

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))

---

<div class="post-metadata">

### Author: ![pedro\_luis\_solarte](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@pedro\_luis\_solarte](https://discourse.slicer.org/u/pedro_luis_solarte)
#### Post date: [May 2, 2018, 9:12am UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/8 "2018-05-02T09:12:58Z")

</div>

Thank you help me Can I export it with color?

---

<div class="post-metadata">

### Author: ![pedro\_luis\_solarte](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@pedro\_luis\_solarte](https://discourse.slicer.org/u/pedro_luis_solarte)
#### Post date: [May 2, 2018, 11:22am UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/9 "2018-05-02T11:22:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [May 2, 2018, 12:45pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/10 "2018-05-02T12:45:53Z")

</div>

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:

```auto
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())

```

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)
#### Post date: [June 27, 2018, 6:28pm UTC](https://discourse.slicer.org/t/exporting-a-dti-object-as-a-3d-file/987/11 "2018-06-27T18:28:42Z")

</div>

A post was split to a new topic: [Save derived DTI heatmap](https://discourse.slicer.org/t/save-derived-dti-heatmap/3319)
