Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5793

Dictionary key using Point3d fails in Python script

$
0
0

@Terry_Chappell wrote:

I tried using these three points in a mesh face:

p1 = Point3d(30.26158, 98.61151, 99.7001)
p2 = Point3d(30.06, 98.78133, 99.66506)
p3 = Point3d(30.04039, 98.67528, 99.86947)

as keys to a dictionary but p1 and p2 map to the same value creating a degenerate face. If the points are multiplied by 2 or more and then used as a key, all 3 points map to unique values. I noticed this when removing duplicate vertices in a mesh. A dictionary with the vertex as a key was used to detect duplicate vertices in the code below:

    vt = {}; i = 0; ii = -1; num = 0; xlate = {}
	for pt in vertices:
		kpt = 2*pt
		if pt in vt: vt[kpt].append(i); num += 1
		else: vt[kpt] = [i]; ii += 1; nvertices.Add(pt)
		xlate[i] = ii
		i += 1
	print 'At start, number of identical vertices = ',num

If a multiplier of 1 is used in place of 2 above, then 26,990 out of 1,364,883 vertices are reported as identical. With a multiplier of 2, no identical vertices are reported.

Another important case for me is trimming a mesh and preserving vertex colors. In this case it is very convenient to use the vertex point as a dictionary key for easy recovery of the vertex colors in the trimmed mesh.

In the starting mesh I do this:

vertices = meshGeo.Vertices
colors = meshGeo.VertexColors
vcount = vertices.Count
for i in xrange(vcount): p2c[vertices[i]] = colors[i]

Then for the trimmed mesh which has all new vertices and faces, I do this to recover the colors:

from System import Array
vertices = trimmed_meshGeo.Vertices
vcount = vertices.Count
# Create .NET type array for holding vertex colors.
colors = Array.CreateInstance(Color, vcount)
for i in xrange(vcount):
   vertex = vertices[i]
   # Try getting color from vertex in start mesh.
   if vertex in p2c: colors[i] = p2c[vertex]
meshGeo.VertexColors.Clear()
meshGeo.VertexColors.SetColors(colors)

Is there another way to do this without using vertices as keys?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 5793

Trending Articles