Custom Color Map .txt file Implementation via code

Hello,

I have a custom color map .txt file that I’d like to apply to a displayNode.
I have the resourcePath to my .txt file as follows:

customColorMapPath = moduleDir +‘/Resources/CustomColorMaps/custom.txt’

It would be perfect if I could find a way to fit this into the code below to replace the ColdToHotRainbow.txt.

node.GetDisplayNode().SetAndObserveColorNodeID(‘vtkMRMLColorTableNodeFileColdToHotRainbow.txt’)

What would be the best way to do this?
I’ve tried looking up the documentation however could not find anything related to a custom .txt file - Install Slicer — 3D Slicer documentation

Please note that I’m looking to get this done all programmatically (no editing via the user interface or dragging the file in). Ideally the .txt file is imported by code on the backend.

I’m fairly new to 3DSlicer, so a simple, clear answer would be appreciated!

If using python, there are a lot of common functions available in slicer.util. Take a look at the loadColorTable method below which will allow you to programmatically load the custom color table file. This method returns the node from which you can GetID() for use in the SetAndObserveColorNodeID method that you mentioned.

Or if you want to create the color table programmatically rather than from file:

https://slicer.readthedocs.io/en/latest/developer_guide/script_repository/gui.html#create-custom-color-table

Color Table file format:
https://slicer.readthedocs.io/en/latest/developer_guide/modules/colors.html

1 Like

Thanks so much, this worked great.