# Export DTI as Ply rotational error

**URL:** <https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478>\
**Category:** Support\
**Tags:** diffusion\
**Created:** [November 28, 2022, 1:14pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478 "2022-11-28T13:14:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuanci](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yuanci/32/17491_2.png) [@yuanci](https://discourse.slicer.org/u/yuanci)\
**Post date:** [November 28, 2022, 1:14pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/1 "2022-11-28T13:14:01Z")

</div>

Hi,

I’m still trying to figure out the full capabilities of Slicer, and just recently started using the DMRI extension. I tried to export a tract as PLY using the “Export tractography to PLY (mesh)” tool but every time I export it I notice it has been rotated 180 degrees about the Z axis when I open it in another 3D program such as Blender. Is there a step I’m missing or something I’m not taking to account?

Thanks in advance,  
Leanne

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 28, 2022, 3:21pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/2 "2022-11-28T15:21:40Z")

</div>

That code snippet exports the tracts as they are in the scene, in RAS coordinate system. If you need the mesh in LPS coordinate system then you can invert the sign of the first two coordinate components. I’ve updated the example [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#export-a-tract-fiberbundle-to-blender-including-color).

---

<div class="post-metadata">

**Author:** ![yuanci](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yuanci/32/17491_2.png) [@yuanci](https://discourse.slicer.org/u/yuanci)\
**Post date:** [December 10, 2022, 6:50pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/3 "2022-12-10T18:50:42Z")

</div>

Thanks for the reply, but I don’t know how to follow the instructions on that link. I found the “TractographyExportPLY.py” is this something I need to modify on that file?

---

<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:** [December 10, 2022, 7:00pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/4 "2022-12-10T19:00:18Z")

</div>

@yuanci -the feature in SlicerDMRI was implemented based on the script example that @lassoan pointed to. He updated the example, but to update the module the same change needs to be made here [in the SlicerDMRI source code](https://github.com/SlicerDMRI/SlicerDMRI/blob/366d232d8406479fe2ec9fbd391d55978c2dfc11/Modules/Scripted/TractographyExportPLY/TractographyExportPLY.py#L164-L224). Is there any chance you could make the change, test it, and submit a pull request to SlicerDMRI? If that’s not something you are familiar with probably someone else can easily do it.

Note the LPS/RAS distinction is [described here](https://www.slicer.org/wiki/Coordinate_systems).

---

<div class="post-metadata">

**Author:** ![yuanci](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yuanci/32/17491_2.png) [@yuanci](https://discourse.slicer.org/u/yuanci)\
**Post date:** [December 10, 2022, 8:01pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/5 "2022-12-10T20:01:27Z")

</div>

Figured it out! Thank you so much for the input

I defined the global variable after the imports

> outputCoordinateSystem = “LPS”

Then I replaced line 206 of the “TractographyExportPLY.py”  
From

> plyWriter.SetInputData(triangles.GetOutput())

To

> if outputCoordinateSystem == “RAS”:  
> plyWriter.SetInputData(triangles.GetOutput())  
> elif outputCoordinateSystem == “LPS”:  
> transformRasToLps = vtk.vtkTransformPolyDataFilter()  
> rasToLps = vtk.vtkTransform()  
> rasToLps.Scale(-1, -1, 1)  
> transformRasToLps.SetTransform(rasToLps)  
> transformRasToLps.SetInputData(triangles.GetOutput())  
> plyWriter.SetInputConnection(transformRasToLps.GetOutputPort())  
> else:  
> raise RuntimeError(“Invalid output coordinate system”)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 10, 2022, 9:14pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/6 "2022-12-10T21:14:58Z")

</div>

Could you please add a `outputCoordinateSystem == "RAS"` argument to `exportFiberBundleToPLYPath`, test if it all works well, and then send a pull request with the change? You can create a pull request very easily by editing the .py file directly in github and answering yes to when you are asked if you want to fork the repository, create a branch, and submit a pull request.

---

<div class="post-metadata">

**Author:** ![yuanci](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yuanci/32/17491_2.png) [@yuanci](https://discourse.slicer.org/u/yuanci)\
**Post date:** [December 10, 2022, 10:34pm UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/7 "2022-12-10T22:34:27Z")

</div>

It does work! It doesn’t allow me to edit the .py file on github, but I have the .py file here: [Dropbox - TractographyExportPLY.py - Simplify your life](https://www.dropbox.com/s/y8dvbibgrbc2s1v/TractographyExportPLY.py?dl=0)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 11, 2022, 4:38am UTC](https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/8 "2022-12-11T04:38:16Z")

</div>

Thank you. I’ve polished it a bit (use None as default to prevent overwriting default; write coordinate system information to image header) and created a pull request:

> <https://github.com/SlicerDMRI/SlicerDMRI/pull/162>
>
> Images are saved in LPS coordinate system to be consistent with image coordinate… system.
> 
> See discussion here: https://discourse.slicer.org/t/export-dti-as-ply-rotational-error/26478/5
