# Adding VTK method?

**URL:** https://discourse.slicer.org/t/adding-vtk-method/832
**Category:** Development
**Created:** [August 5, 2017, 7:28pm UTC](https://discourse.slicer.org/t/adding-vtk-method/832 "2017-08-05T19:28:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)
#### Post date: [August 5, 2017, 7:28pm UTC](https://discourse.slicer.org/t/adding-vtk-method/832/1 "2017-08-05T19:28:27Z")

</div>

VTK provides a method for determining the center of mass of a dataset. See:

[https://itk.org/Wiki/VTK/Examples/Cxx/PolyData/CenterOfMass](https://itk.org/Wiki/VTK/Examples/Cxx/PolyData/CenterOfMass)

I’m interested in adding this capability to an extension in Slicer, the goal being to determine the center of mass of a given segment.

I’ve done some reading of the source (I know C++ and a little python) but I am very much a newbie on the architecture of Slicer. I’m wondering if someone has done something similar (adding VTK method and accessing it through python for an Extension GUI) that I can borrow/steal.

Thanks in advance,

-Hollister

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [August 5, 2017, 8:08pm UTC](https://discourse.slicer.org/t/adding-vtk-method/832/2 "2017-08-05T20:08:35Z")

</div>

Hi Hollister -

Yes, pretty much all vtk functionality is accessible through python and easy to use in a python scripted extension.

These two programming tutorials are a good start:

[https://www.slicer.org/wiki/Documentation/Nightly/Training#Tutorials\_for\_software\_developers](https://www.slicer.org/wiki/Documentation/Nightly/Training#Tutorials_for_software_developers)

Also you can find examples the script repository here:

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository)

For example, if you have created a default segmentation with a surface mode, you can do this:

```auto
>>> n = getNode('Segmentation')
>>> s = n.GetSegmentation()
>>> ss = s.GetSegment('Segment_1')
>>> pd = ss.GetRepresentation('Closed surface')
>>> com = vtk.vtkCenterOfMass()
>>> com.SetInputData(pd)
>>> com.Update()
>>> com.GetCenter()
(33.686966862924784, 10.410245678682282, 5.923210996529306)

```

---

<div class="post-metadata">

### Author: ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)
#### Post date: [August 5, 2017, 11:05pm UTC](https://discourse.slicer.org/t/adding-vtk-method/832/3 "2017-08-05T23:05:01Z")

</div>

Hi Steve,

That’s _exactly_ what I was looking for!

Thank you very much!

-Hollister
