mesh.VertexColors.SetColors not working in Python script

The code below worked for jordy1989 to color a mesh:

 Dim colors As System.Drawing.Color() = New System.Drawing.Color(Mesh.Vertices.Count - 1) {}
            For i As Integer = 0 To Mesh.Vertices.Count - 1
                Dim z As Double = Mesh.Vertices(i).Z

                If z < 10 Then
                    colors(i) = Drawing.Color.Red
                Else
                    colors(i) = Drawing.Color.Green
                End If
            Next
            Mesh.VertexColors.SetColors(colors)

How do I write this in a Python script using Rhino common calls? I cannot figure how to replace the line:

Dim colors As System.Drawing.Color() = New System.Drawing.Color(Mesh.Vertices.Count - 1) {}

I tried:

	vcount = mesh.Vertices.Count
	colors = System.Drawing.Color[vcount]

but Python complains: Message: expected Array[Type], got int

I cannot use a list instead as the call to mesh.VertexColors.SetColors(colors) requires an array and not a simple Python list.

As a work around I am using:

for c in colors: mesh.VertexColors.Add(c)

but this is slow for my 20M face meshes. Please help!

Regards,
Terry.

Hi Terry,

I’m on my phone so this is without testing. But since you just posted it might help you enough to keep you moving.

Does this help:
http://www.ironpython.info/index.php?title=Typed_Arrays_in_IronPython

-Willem

1 Like

Willem,

Perfect help. From the documentation you pointed to I used:

from System import Array
colors = Array.CreateInstance(Color, num_colors)

Now the colors load 6X faster for a mesh with 2.2M vertices.

I am so happy!

Regards,
Terry.

2 Likes

Here’s an alternative way, to me it’s more comprehensible

from System.Drawing import Color
from System import Array

colors = Array[Color](colors_list)