Mesh Toolkit Python ByVerticesAndIndices

#1

Hello,
How do you call Mesh Toolkit from python to create a mesh ByVerticesAndIndices? I want to accomplish something like:

m = MeshToolkit.Mesh.ByVerticesIndices(vList, iList)

Do I need to addReference and import etc? I can’t find an example that works, but it seems if you already have a mesh, you can say something like aMesh.BooleanUnion(anotherMesh)… but how do you create meshes from scratch in python without visual programming?

Thank you

#2

You’ll first need to import the MeshToolkit namespace. I like to assign it to an alias like mtk. After that calling the method is relatively straight-forward:

import clr

clr.AddReference('MeshToolkit')
import Autodesk.Dynamo.MeshToolkit as mtk

verts, indices = IN
OUT = mtk.Mesh.ByVerticesAndIndices(verts, indices)

The only tricky part is that your list of indices has to be of type List< int >.

#3

Thank you. You predicted the tricky part well. So I have a function in the same python script that is returning a list of Points and a list of ints (indices) into the Points list.

fn fixMesh(aMesh):
      . . .
      # vertices is a list of Dynamo Points
      # indices is a list of 3 ints each row pointing into the list of vertices
      return [vertices, indices]

then:

returnValue = fixMesh(aMesh)
mtk.Mesh.ByVerticesAndIndices(returnValue[0], returnValue[1])

Very simple I thought, but mtk.Mesh.ByVerticesAndIndices is choking on it:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 100, in <module>
TypeError: Error in IEnumeratorOfTWrapper.Current. Could not cast: System.Int32 in IronPython.Runtime.List

What am I doing wrong or not doing? Thanks again.

#4

If you’re using a Dynamo list of ints directly, the code would just work:

But if you’re generating the list in python, then it will most likely fail if you don’t cast it to the expected c# list type:

import clr

clr.AddReference('MeshToolkit')
import Autodesk.Dynamo.MeshToolkit as mtk

import System
from System.Collections.Generic import List

verts = IN[0]
indices = (i for i in IN[1]) #generator as an example

meshIndices = List[int](indices)
# would fail when using 'indices' directly 
OUT = mtk.Mesh.ByVerticesAndIndices(verts, meshIndices)
2 Likes
#5

Hi again. I followed your example closely and I am now getting the following error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 18, in <module>
TypeError: Error in IEnumeratorOfTWrapper.Current. Could not cast: System.Int32 in System.Int64

Here is the python code:

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference('MeshToolkit');
import Autodesk.Dynamo.MeshToolkit as mtk
import System
from System.Collections.Generic import List
verts = IN[0]
indices = (i for i in IN[1])
meshIndices = List[int](indices)
OUT = mtk.Mesh.ByPointsFaceIndices(verts, meshIndices)

And here is the screenshot of the definition:

Thanks for all your help.