# Select cells of a 3D model

**URL:** https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753
**Category:** Development
**Created:** [July 25, 2019, 8:50am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753 "2019-07-25T08:50:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 25, 2019, 8:50am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/1 "2019-07-25T08:50:29Z")

</div>

how to create a mouse event inside the 3d slicer view. Need to pick cells from a mesh object from within the 3d slice view of the slicer.

Need help

---

<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 25, 2019, 12:47pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/2 "2019-07-25T12:47:41Z")

</div>

You can get 3D position from views as shown in these examples:

- Slice views: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_current\_mouse\_coordinates\_in\_a\_slice\_view](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_current_mouse_coordinates_in_a_slice_view)
- 3D views: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_scalar\_values\_at\_surface\_of\_a\_model](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_scalar_values_at_surface_of_a_model)

If you want to select several cells, you can use markups fiducials : drop a fiducials on the cells you want to select and then use [FindCell](https://vtk.org/doc/nightly/html/classvtkCellLocator.html) to get the cell IDs. If you don’t want the fiducials to stay there then you can remove them immediately after you retrieved their position. In recent Slicer Preview builds, markups points slide on visible surfaces, so you can make position adjustments quite easily.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 26, 2019, 9:05am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/3 "2019-07-26T09:05:06Z")

</div>

Hi andras,  
I wrote the following to get the cell using fiducial

def onMouseMoved(self, observer,eventid):  
ras=[0,0,0]  
crosshairNode.GetCursorPositionRAS(ras)  
if markupsNode.GetNumberOfFiducials() == 0:  
markupsNode.AddFiducial(\*ras)  
else:  
markupsNode.SetNthFiducialPosition(0,\*ras)  
closestCell = cell.FindCell(ras)  
closestPointValue = modelPointValues.GetTuple(closestCell)  
print("RAS = " + repr(ras) + " value = " + repr(closestPointValue))

calling this within a scripted module in the run function as  
global cell, modelPointValues, crosshairNode, markupsNode  
modelNode = slicer.util.getNode(‘Model\_1’)  
#modelPointValues = modelNode.GetPolyData().GetPointData().GetArray(“Normals”)  
modelPointValues = modelNode.GetMesh().GetCells()  
markupsNode = slicer.util.getNode(‘F’)

```
if not markupsNode:
 markupsNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode","F")

#pointsLocator = vtk.vtkPointLocator() # could try using vtk.vtkStaticPointLocator() if need to optimize
#pointsLocator.SetDataSet(modelNode.GetPolyData())
#pointsLocator.BuildLocator()

cell = vtk.vtkCellLocator()
cell.SetDataSet(modelNode.GetMesh())
cell.BuildLocator

crosshairNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLCrosshairNode")
crosshairNodeObserverTag = crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)

```

The application stoped working. although there was no error. the mouse cursor was paused. Could you tell me whats wrong with the code.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 28, 2019, 11:12am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/4 "2019-07-28T11:12:44Z")

</div>

I replaced the crosshair node part with

crosshairNode=slicer.util.getNode(‘Crosshair’)  
crosshairNodeObserverTag = crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)

but still the program doessnt work. What is wrong in the code. could you please tell me. because the program haults and then the slicer application closes.

---

<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 29, 2019, 4:01am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/5 "2019-07-29T04:01:31Z")

</div>

Modify crosshair examples of the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository) step by step. If you encounter any problems then post the script, describe what you do, what you expect to happen, and what happens instead.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [July 29, 2019, 8:12am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/6 "2019-07-29T08:12:38Z")

</div>

I need cells to be selected with mouse of the mesh in attached pic. The script gives me the position of mousecursor. I defined the celllocator and using the findcell to locate the cell. Could you please guide in this respect. What I need to look at.

I need to get the pcoords of tetra cell rather then the mousecursorpoint. what should I do.

```python
global cell, modelPointValues, crosshairNode, markupsNode
modelNode = slicer.util.getNode('Model_1')
modelPointValues = modelNode.GetMesh().GetCells().GetData()

cell = vtk.vtkCellLocator()
cell.SetDataSet(modelNode.GetMesh())
cell.BuildLocator

crosshairNode=slicer.util.getNode('Crosshair')
crosshairNodeObserverTag = crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)

def onMouseMoved(self, observer, eventid):  
        ras=[0,0,0]
        crosshairNode.GetCursorPositionRAS(ras)
        if markupsNode.GetNumberOfFiducials() == 0:
         markupsNode.AddFiducial(*ras)
        else:
         markupsNode.SetNthFiducialPosition(0,*ras)
        closestCell = cell.FindCell(ras) /**//Problem starts here
        print("inside mouse event")
        #closestPointValue = modelPointValues.GetTuple(closestCell)
        print("RAS = " + repr(ras) )#+ " value = " + repr(closestPointValue))

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/5/35bd9b8106e6aae846036e6e2d919969df9a5d5f.jpeg)

---

<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 29, 2019, 1:52pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/7 "2019-07-29T13:52:27Z")

</div>

The code crashed because you did not build the locator. `cell.BuildLocator` just prints the address of the `BuildLocator` method. You need to add `()` to call the method: `cell.BuildLocator()`.

Another issue is that you mix two approaches: getting point coordinates from markups and crosshair. If you use markups then you don’t need to use crosshair, as you can get 3D positions from the markup points.

See a complete working example here (requires recent Preview Release of Slicer): [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Select\_cells\_of\_a\_using\_markups\_fiducial\_points](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Select_cells_of_a_using_markups_fiducial_points)

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [August 22, 2019, 5:50am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/8 "2019-08-22T05:50:22Z")

</div>

Hi Andras,  
Is there a way to select cells on the surface of mesh using onMouseClicked event.

Please suggest.

Regards,  
Saima Safdar

---

<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: [August 22, 2019, 4:05pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/9 "2019-08-22T16:05:09Z")

</div>

> [@Saima](#):
>
> Is there a way to select cells on the surface of mesh using onMouseClicked event.

It should be possible, but the example script that I linked above can already add markups and thus select cells on mouse click.

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [August 23, 2019, 2:51am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/10 "2019-08-23T02:51:20Z")

</div>

Andras I need to use only the mouse clicks to select the cells of a mesh without using the fiducials.  
I understand adding three fiducial points to locate the cell will be not feasible.

I would want to select may cells from the surface of the brain where I can apply force. the craniotomy induced part I need to select and it will be different for each MRI. So I need to select the area by mouse. Any suggestions how can I do it?

Thank you so much for replies.

Looking forward to your answer.

Regards,  
Saima Safdar

---

<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: [August 23, 2019, 3:34am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/11 "2019-08-23T03:34:49Z")

</div>

Can you write a bit more about what exactly you would like to achieve?

What is your input data? 3D volumes? Segmentations? Surface scans?

What would you like to do with the data? What would you like to get as end results?

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [August 23, 2019, 9:11am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/12 "2019-08-23T09:11:54Z")

</div>

Data = 3D volumetric brain Mesh  
What would you like to do with the data? Need to select the surface cells from the brain portion where craniotomy was induced. I will be using the preoperative mri images.  
What would you like to get as end results? after selecting cells I will save those cells in a file and will pass those cells along with the complete brain mesh to external Meshless algorithm to compute the craniotomy induced brain shift.

---

<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: [August 23, 2019, 4:28pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/13 "2019-08-23T16:28:44Z")

</div>

> [@Saima](#):
>
> Need to select the surface cells from the brain portion where craniotomy was induced. I will be using the preoperative mri images.

We have very good tool to mark regions in volumetric images: Segment Editor module. You can get cell IDs that overlap the craniotomy region by following these steps:

- segment the craniotomy region on the image using Segment Editor
- export segmentation to a labelmap volume
- use “Probe volume with model” to label all mesh points with 1/0 (corresponding to overlap / does not overlap with the segmented region)
- find cell IDs corresponding to points that are labelled with “1” - you can use VTK filters (similarly to [this](https://www.paraview.org/Wiki/VTK/Examples/Cxx/PolyData/ExtractCellsUsingPoints)) or just by iterating through all the cells and see if any of its points are labelled with “1”

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 2, 2019, 8:36am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/14 "2019-09-02T08:36:06Z")

</div>

Hi Andras,  
Can above be done through scripting. Please help

thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 2, 2019, 2:06pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/15 "2019-09-02T14:06:57Z")

</div>

Hi ANdras, I used the steps you told me in 3d slicer its producing the following results instead of only highlighting the segmented volume portion.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/4/54798555ed7e95d52e6ae2e63d934361f88f1bcb.png)

What am I doing wrong.

I loaded the skull stripped volume.  
created a segmentation using scissors and mask volume effect.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/3/c3ca8c44288bbdbf4cd114ec3d2571006b0baf48.png)  
new volume generated  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/e/be7a01eddd9064a3fc67bf3b5ec4da248b5e9592.png)  
exported segmentation as labelmap volume.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/0/f0a78839b60fa5b137a23f5fd59a20108b0eacad.png)

used the new volume for input in probe volume model.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/c/5cb74bf40a45d5776b5e8c5b9c5d389a99af8f99.png)  
one time I click on the model it looks like below:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/2/22fa5709f339ee4495f0d50cd214b4e3d5743d8c.png)  
other time I click on the same model eye open eye close it looks like below:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/c/5cb66c5e977b4d27ef13c8431eb3765d2429fdce.png)

Could you please guide me what am I doing wrong I need only the segmented volume region to be selected on the model.

Please help.

Also I need to do all this through scripting.

Regards,  
Saima Safdar

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 4, 2019, 7:58am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/16 "2019-09-04T07:58:04Z")

</div>

Hi andras,  
I applied the probe volume with model filter. Got the cell ids using the script below:

```python
for cellIndex in range(ncells):
        cellpoints = mesh.GetCellPoints(cellIndex,idList)
        scalarArray = mesh.GetPointData().GetArray(0)
        #print(scalarArray)
        numPoints = idList.GetNumberOfIds()
        #print(numPoints)
        for pointIndex in range(numPoints):
            value = scalarArray.GetValue(idList.GetId(pointIndex))
            if(value == 1):
               print(cellIndex)

```

could you please tell me how could I get those cells coloured differently on the model.

Looking forward to your reply.

Thank you

Regards,  
Saima Safdar

P.S. Also please tell me how can i use the probe volume with model using script. thankyou

---

<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: [September 4, 2019, 2:42pm UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/17 "2019-09-04T14:42:57Z")

</div>

While the VTK code above that gives you point IDs that have a certain scalar value is correct, you may find it easier to get the point data as numpy array and use numpy for processing:

```python
import numpy as np
voxelValues = slicer.util.arrayFromModelPointData(getNode('Output Model'), 'NRRDImage')
pointIDs = np.where(voxelValues == 1)

```

> [@Saima](#):
>
> could you please tell me how could I get those cells coloured differently on the model

You can use Models module / Display / Scalars section to color a model by a selected scalar.

> [@Saima](#):
>
> please tell me how can i use the probe volume with model using script

“Probe volume with model” module is a CLI module, so you can run it from Python as described [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Running_a_CLI_from_Python).

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 5, 2019, 3:38am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/18 "2019-09-05T03:38:10Z")

</div>

> [@lassoan](#):
>
> voxelValues = slicer.util.arrayFromModelPointData(getNode(‘Output Model’), ‘NRRDImage’)

n = getNode(“Output Model\_1”)

> > > n  
> > > (MRMLCorePython.vtkMRMLModelNode)0000023BCBDE8E28  
> > > voxelValues = slicer.util.arrayFromModelPointData(n, ‘NRRDImage’)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > File “C:\Users\22374464\AppData\Local\NA-MIC\Slicer 4.11.0-2019-07-06\bin\Python\slicer\util.py”, line 1009, in arrayFromModelPointData  
> > > arrayVtk = modelNode.GetPolyData().GetPointData().GetArray(arrayName)  
> > > AttributeError: ‘NoneType’ object has no attribute ‘GetPointData’

getting the above error. Why dont understand?

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 5, 2019, 4:59am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/19 "2019-09-05T04:59:40Z")

</div>

> [@lassoan](#):
>
> You can use Models module / Display / Scalars section to color a model by a selected scalar.
> 
> ![](https://avatars.discourse.org/v4/letter/s/9d8465/40.png) Saima:

I need to color the selected cells. For example I want to color the cell in the model. I am getting the location of that cell then using that location to set the activescalar. is it right. I dont know how to color. Any help?

nn = slicer.util.getNode(outputVolume.GetID())  
d = nn.GetModelDisplayNode()  
l = nn.GetMesh().GetCellLocationsArray()  
v = l.GetValue(4740)  
d.SetActiveScalar(‘R’,v)  
d.SetAndObserveColorNodeID(‘FullRainbow’)  
d.ScalarVisibilityOn()  
d.AutoScalarRangeOn()

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [September 12, 2019, 9:20am UTC](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753/20 "2019-09-12T09:20:26Z")

</div>

Hi Andras,  
Is there a way to change the color for different cells in a 3d volumetric mesh by scripting. i know cell ids I need to change the color for those cells. How can I do it. please guide

Thank you

Regards,  
Saima Safdar

[Next page](https://discourse.slicer.org/t/select-cells-of-a-3d-model/7753.md?page=2)
