Set Color and Opacity to all Visible Models

I’d like to apply a R,G,B value and opacity to hundreds of models all at once. I would like to apply the color and opacity only to visible models in the 3D view. I’ve created a python extension, but the code is not quite there yet. Would someone be able to take a look and guide me as to how to fix the code?

color

You can use something like this to iterate over all of the model nodes in the scene, and change their color.

for modelNode in slicer.util.getNodesByClass("vtkMRMLModelNode"):
  if modelNode.GetHideFromEditors():
    continue # Skip hidden nodes, such as Red/Green/Yellow slice models
  modelDisplayNode = modelNode.GetDisplayNode()
  modelDisplayNode.SetColor(red,green,blue)
  modelDisplayNode.SetOpacity(opacity)
1 Like

You may also consider creating a hierarchy in Data module, as you can set color of all the nodes in a folder by right-clicking on the eye icon of the folder and choosing “Apply color to all children”.

Thanks Andras, I was not aware of the “Apply color to all children” option. This is a separate issue but do you know why 3DSlicer takes such a long time to turn and off visibility or apply a color to a large amount of models in a hierarchy (100+ models)?

Update of hundreds of nodes is probably much faster if you start batch processing before the mass update and end batch processing when you finished. Also make sure you use latest Slicer Preview Release, as it contains lots of improvements and fixes.

I am using the most recent 3DSlicer. Toggling visibility in the data module is what is taking a long time when there are lot of models in one hierarchy.

How long does it take? Can you share the scene with us?

Showing/hiding all the nodes in the entire NAC brain atlas (containing 300 models) takes less than a a second (by toggling visibility of a branch in data module). Can you check the performance of this on your computer? (you can get NAC brain atlas from DataStore module, Atlas collection category).

I did as you asked with the NAC brain atlas. The issue seems to be my computer as toggling visibility causes 3D slicer to hang for about a 45 seconds. Strange because my computer is a decently powerful computer since I built it myself (AMD 8320 cpu, GTX660ti gpu, 8gb 2600Mhz ram). None of the cpu, gpu, or ram percent usage are that high either in Task Manager during toggling of the nodes.

Can you record a screen capture video or take a screenshot and mark where you click? Is it slow if you don’t add any observers to the scene?

Here is the video. Even if I only view the red slice only and not the 3D view, the issue is still there.

You still use the Slicer Stable Release. Please use latest Slicer Preview Release, as it contains lots of performance improvements, including show/hide of branches in hierarchies.

Wow, what an incredible performance difference! Upgrading to 4.11.0 fixed the issue. I really cannot thank you enough Andras and Kyle! Your fast response times and insightful tips are what enabled me to complete my project with 3DSlicer.

3 Likes