# I try to replace \`markups\` with \`model\` in \`MarkupsToModel\` , but

**URL:** <https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018>\
**Category:** Development\
**Tags:** python\
**Created:** [August 31, 2022, 2:55am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018 "2022-08-31T02:55:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 31, 2022, 2:55am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/1 "2022-08-31T02:55:47Z")

</div>

I try to replace `markups` with `model` in `MarkupsToModel` , but…👇, what’s wrong?

```python
def fids2Mod(fids, curve=True):
# if isinstance(fids,str):
    fids = slicer.util.getNode(fids)

    outputModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLModelNode())
    outputModel.CreateDefaultDisplayNodes()
    outputModel.GetDisplayNode().SetSliceIntersectionVisibility(True)
    outputModel.GetDisplayNode().SetColor(1,0,0)

    markupsToModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLMarkupsToModelNode())

    markupsToModel.SetAutoUpdateOutput(True)
    markupsToModel.SetAndObserveModelNodeID(outputModel.GetID())
    markupsToModel.SetAndObserveMarkupsNodeID(fids.GetID())
    if curve:
        markupsToModel.SetModelType(0)
        markupsToModel.SetTubeRadius(0.05)
        markupsToModel.SetTubeLoop(1)
        markupsToModel.SetCurveType(0)

```

```python
# while mn is model
fids2Mod(mn,curve=False)

```

👇? got None???

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/2/02e8a2e216f7f9e21ce9082d6ff6d5414b772095.png)

```auto
# mn is model
fids2Mod(mn,curve=True)

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/6/c654b7dbb39ce01e76b1b54dce4259702e4f302b.png)

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 31, 2022, 3:03am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/2 "2022-08-31T03:03:17Z")

</div>

```python
# fids is markups
fids2Mod(f"{mn}s",curve=False)

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/2/32b6edd49fa678e7edc947c8ed4a5936a6fd1b7f.png)

---

<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 31, 2022, 3:34am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/3 "2022-08-31T03:34:57Z")

</div>

Regardless of input node type (markups fiducial or model node), you’ll get the same output model for the same input points. If you get empty results for model node input then most likely the input model node points are not set correctly. If you provide example data (saved as a .mrb file, uploaded to dropbox/onedrive) and the code snippet that you expect to work after loading that scene, then I can have a closer look.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [August 31, 2022, 4:51pm UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/4 "2022-08-31T16:51:22Z")

</div>

@lassoan [theFile](https://drive.google.com/file/d/1GbFIoHrlZdyE1hQu2vRvfJqFtaB2j-Cm/view?usp=sharing)

---

<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 3, 2022, 3:17am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/5 "2022-09-03T03:17:28Z")

</div>

Everything works well for me, I just had to delete the extra points (for example, in `3_xPMods` keep only 31 to 61). If you are reconstructing a tool trajectory path then I would recommend to only start recording when you start pulling back the tool.

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

If I copy the point coordinates into a model then “Markups to model” module creates the exact same tube from that.

```python
markupsPoints = getNode('3_xPMods')

# Get point coordinates as numpy array
coords = arrayFromMarkupsControlPoints(markupsPoints)

# Create new model node and allocate points
modelNode=slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
pts = vtk.vtkPoints()
pts.SetNumberOfPoints(len(coords))
pd = vtk.vtkPolyData()
pd.SetPoints(pts)
modelNode.SetAndObservePolyData(pd)

# Copy point coordinates from markup to model
outCoords = arrayFromModelPoints(modelNode)
outCoords[:] = coords

```

Closed surface reconstruction worked well, too:

```python
outputModel = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
outputModel.CreateDefaultDisplayNodes()
outputModel.GetDisplayNode().SetSliceIntersectionVisibility(True)
outputModel.GetDisplayNode().SetColor(1,0,0)

markupsToModel = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsToModelNode")
markupsToModel.SetAndObserveInputNodeID(fids.GetID())
markupsToModel.SetAndObserveOutputModelNodeID(outputModel.GetID())
slicer.modules.markupstomodel.logic().UpdateOutputModel(markupsToModel)

```

If you want to reconstruct an open surface then the simplest is to use a closed curve:

```python
# Convert any markup node type into a closed curve node
outputCurve = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsClosedCurveNode")
outputCurve.SetCurveTypeToLinear()
updateMarkupsControlPointsFromArray(outputCurve, arrayFromMarkupsControlPoints(fids))

# Get surface area
areaMeasurement = outputCurve.GetMeasurement('area')
areaMeasurement.SetEnabled(True)
slicer.modules.models.logic().AddModel(areaMeasurement.GetMeshValue())

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/0/50f8fe6dce8ceb3a1f2512477ba80a6e16270547.png)

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 3, 2022, 4:50am UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/6 "2022-09-03T04:50:17Z")

</div>

@lassoan Is there a way to sort the index of the points (np.array) according to the clockwise or counterclockwise and adjacent nearest points?

---

<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 3, 2022, 12:23pm UTC](https://discourse.slicer.org/t/i-try-to-replace-markups-with-model-in-markupstomodel-but/25018/7 "2022-09-03T12:23:48Z")

</div>

Sorting is only available for polynomial fitting, along an approximately linear trajectory. You would need to implement sorting for a circular trajectory yourself.

What is your overall goal?
