Mel scripting, need help. Trying to find the corner vertices of an object.


#1

I am trying to write a script that finds the bottom corner vertices of an object and places locators there. Any help would be greatly appreciated. Here is an image to illustrate what I mean.


#2

You could read the bounding box dimensions and search the nearest vertices to the according bbox edges.


#3

corner verts have 3 incoming edge connections all others have 4.
So you could iterate through all verts, get the number of edges they have incoming, and filter those out with 3.
Then you can sort the results by their y value and output the 4 lowest (only works if mesh is more or less flat on xz plane).
Then locators.


#4

Hi Hunterhawk29,
I tried to make this tool using python.
Please could you tell me what kinds of situation is this tool useful in?

import pymel.core as pm
import math

def CreateLocatorBottomCornerVerts():
	
	def getBottomCorner(list):
		val = 0
		if len(list)>1:
			for i in range(0,len(list)):
				pm.select(list[i],r=True)
				pos = pm.xform(q=True,a=True,ws = True, t = True)
				if i == 0:
					minY = pos[1]
				else:
					if minY>pos[1]:
						minY = pos[1]
						val = i
		return list[val]
	
	tmp = pm.ls (sl = 1)
	verts = pm.polyListComponentConversion (tmp, tv=True)
	pm.select(verts,r=True)
	verts = pm.ls(sl=True,fl=True)
	cnrVerts = []
	for i in range(0,len(verts)):
		pm.select(verts[i],r=True)
		pm.runtime.ConvertSelectionToEdges()
		edges = pm.ls(sl=True,fl=True)
		if len(edges) == 3:
			cnrVerts.append(verts[i])
	pm.select(cnrVerts,r=True)

	upperLeft = []
	lowerLeft = []
	upperRight = []
	lowerRight = []

	pm.select(tmp,r=True)	
	boundingBox = pm.polyEvaluate(boundingBox=True)				
	xMaxBb = boundingBox[0][1]
	xMinBb = boundingBox[0][0]
	zMaxBb = boundingBox[2][1]
	zMinBb = boundingBox[2][0]
	
	for i in range(0,len(cnrVerts)):
		pm.select(cnrVerts[i],r=True)
		pos = pm.xform(q=True,a=True,ws = True, t = True)
		pos[1] = 0
	
		cnr = []
		cnr.append([xMinBb - pos[0],zMaxBb - pos[2]])#lowerLeft
		cnr.append([xMinBb - pos[0],zMinBb - pos[2]])#upperLeft
		cnr.append([xMaxBb - pos[0],zMaxBb - pos[2]])#lowerRight
		cnr.append([xMaxBb - pos[0],zMinBb - pos[2]])#upperRight
	
		val = 0
		for j in range(0,4):
			tmpMag = math.sqrt(cnr[j][0]*cnr[j][0]+cnr[j][1]*cnr[j][1])
		
			if j == 0:
				mag = tmpMag	
			else:
				if mag > tmpMag:
					mag = tmpMag
					val = j
																									
		if val == 0:
			lowerLeft.append(cnrVerts[i])
		elif val == 1:
			upperLeft.append(cnrVerts[i])
		elif val == 2:
			lowerRight.append(cnrVerts[i])
		elif val == 3:
			upperRight.append(cnrVerts[i])
	
	final=[]
	final.append(getBottomCorner(lowerLeft))
	final.append(getBottomCorner(upperLeft))
	final.append(getBottomCorner(lowerRight))
	final.append(getBottomCorner(upperRight))
		
	for i in range(0,len(final)):
		pm.select(final[i],r=True)
		pos = pm.xform(q=True,a=True,ws = True, t = True)
		pm.general.spaceLocator(a=True,position = pos)
	
	pm.select(tmp,r=True)
		
CreateLocatorBottomCornerVerts()