Change color of each model uploaded

Hi,

Is there a way to give each model a unique color when uploaded to 3DSlicer? They all come out the same color if I upload them all at once and changing them one by one takes a long time!

This is easy to do via scripting, and I don’t think there is a way through UI. Interestingly enough, I couldn’t find an example of changing a color of a model node in script repository. But ChatGPT has quite helpful with the question:
how can I change the color of a model node in 3D slicer with python?

It gave me this non-functional (with extraneous stuff in it) example.

import slicer
import vtk

# Get the model node by name
model_node = slicer.util.getNodeByName("YourModelNodeName")

# Create a color table node
color_table_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLColorTableNode")

# Define the color (e.g., red)
color = vtk.vtkUnsignedCharArray()
color.SetNumberOfComponents(3)
color.InsertNextTuple3(255, 0, 0)

# Set the color of the model node
model_node.GetDisplayNode().SetColor(color_table_node.GetColorIndex(color))

# Update the 3D view
slicer.util.resetFiducialsMarkersColor()

But it has enough pointers. So it was easy change it into this, in which the model node is called A.

import slicer

# Get the model node by name
model_node = slicer.util.getNode("A")

# Set the color of the model node
model_node.GetDisplayNode().SetColor(255,255,0)

you can then modify this example with your models and change their colors as you which by specifying their RGB codes.

Hi,

Thanks for this! Interesting way of using ChatGPT!