# Merging models with keeping labels

**URL:** <https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479>\
**Category:** Support\
**Created:** [February 29, 2020, 4:18pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479 "2020-02-29T16:18:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aep93](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aep93/32/5990_2.png) [@Aep93](https://discourse.slicer.org/u/Aep93)\
**Post date:** [February 29, 2020, 4:18pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/1 "2020-02-29T16:18:41Z")

</div>

Hello all,  
How should I merge two models so that each of them has a specific label in the merged model?  
I tried merge models module but it does not work and I think it is because one of my models is volumetric.

---

<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:** [February 29, 2020, 8:34pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/2 "2020-02-29T20:34:05Z")

</div>

Array values that can be found in all input meshes will be available in the output mesh, too. To merge volumetric meshes, you need to use [vtkAppendFilter](https://vtk.org/doc/nightly/html/classvtkAppendFilter.html) instead of vtkAppendPolyData.

It would be nice if you could update [MergeModels](https://github.com/Slicer/Slicer/tree/master/Modules/CLI/MergeModels) module to be able to load and merge volumetric meshes (unstructure grids) and not just surface meshes (polydata). But if you are not familiar with C++ then you can implement appending in your own script/module in a few lines of Python code.

---

<div class="post-metadata">

**Author:** ![Aep93](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aep93/32/5990_2.png) [@Aep93](https://discourse.slicer.org/u/Aep93)\
**Post date:** [February 29, 2020, 8:41pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/3 "2020-02-29T20:41:13Z")

</div>

Thank you very much for your answer. I am not very good with c++ but if I want to use the python codes, what resources can I use to find how to code such a command?  
The other question is whether this way assigns a specific label to each model after they merged. Because I want a single merged model in which different parts have different labels.

---

<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:** [February 29, 2020, 8:59pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/4 "2020-02-29T20:59:08Z")

</div>

Append operation preserves scalar values. You can assign constant scalar values before append.

---

<div class="post-metadata">

**Author:** ![Aep93](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aep93/32/5990_2.png) [@Aep93](https://discourse.slicer.org/u/Aep93)\
**Post date:** [February 29, 2020, 9:52pm UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/5 "2020-02-29T21:52:16Z")

</div>

Thank you very much. I think there is no module for assigning label to a pre-existing model and I should use the following command.  
modelNode.GetDisplayNode().SetActiveScalar(“selection”, vtk.vtkAssignAttribute.CELL\_DATA)  
Am I right?

---

<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:** [March 1, 2020, 1:25am UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/6 "2020-03-01T01:25:12Z")

</div>

To assign a point scalar array with a constant value for all the points, you can either manipulate the VTK data object (add an array, set the size to the number of points/cells, and fill with a constant) or use VTK filters (such as [vtkArrayCalculator](https://vtk.org/doc/nightly/html/classvtkArrayCalculator.html))

You can learn VTK from the VTK [textbook](https://lorensen.github.io/VTKExamples/site/VTKBook/00Preface/), the huge collection of tests and [examples](https://lorensen.github.io/VTKExamples/site/), and [API documentation](https://vtk.org/doc/nightly/html/).

You can find lots of examples of how to use VTK for processing MRML nodes in Slicer in the Slicer script repository. For example, adding a cell array with a constant value is shown [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Select_cells_of_a_model_using_markups_fiducial_points).

---

<div class="post-metadata">

**Author:** ![Aep93](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aep93/32/5990_2.png) [@Aep93](https://discourse.slicer.org/u/Aep93)\
**Post date:** [March 1, 2020, 1:54am UTC](https://discourse.slicer.org/t/merging-models-with-keeping-labels/10479/7 "2020-03-01T01:54:26Z")

</div>

Thank you very much for your help.
