# Set Color and Opacity to all Visible Models

**URL:** https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699
**Category:** Development
**Created:** [July 22, 2020, 11:07pm UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699 "2020-07-22T23:07:54Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 22, 2020, 11:07pm UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/1 "2020-07-22T23:07:54Z")

</div>

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](https://github.com/rgmorrison/Color_All_Visible_Models), 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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/5/05ab3bff3e73dc55e660a764fdb5ee15f6993c74.jpeg)

---

<div class="post-metadata">

### Author: ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)
#### Post date: [July 22, 2020, 11:28pm UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/2 "2020-07-22T23:28:47Z")

</div>

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

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

```

---

<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: [July 23, 2020, 1:19am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/3 "2020-07-23T01:19:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 23, 2020, 2:21am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/4 "2020-07-23T02:21:51Z")

</div>

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

---

<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: [July 23, 2020, 2:25am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/5 "2020-07-23T02:25:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 23, 2020, 2:35am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/6 "2020-07-23T02:35:17Z")

</div>

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.

---

<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: [July 23, 2020, 2:37am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/7 "2020-07-23T02:37:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 23, 2020, 3:18am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/8 "2020-07-23T03:18:41Z")

</div>

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.

---

<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: [July 23, 2020, 3:42am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/9 "2020-07-23T03:42:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 23, 2020, 3:52am UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/10 "2020-07-23T03:52:27Z")

</div>

[Here is the video](https://youtu.be/4Qrf7xjS5Cg). Even if I only view the red slice only and not the 3D view, the issue is still there.

---

<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: [July 23, 2020, 2:00pm UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/11 "2020-07-23T14:00:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ryan\_Morrison](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ryan_morrison/32/7508_2.png) [@Ryan\_Morrison](https://discourse.slicer.org/u/Ryan_Morrison)
#### Post date: [July 23, 2020, 6:11pm UTC](https://discourse.slicer.org/t/set-color-and-opacity-to-all-visible-models/12699/12 "2020-07-23T18:11:26Z")

</div>

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.
