python 此脚本旨在返回选择范围内的所有附近顶点,而您只查找整个对象。所以你有合作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 此脚本旨在返回选择范围内的所有附近顶点,而您只查找整个对象。所以你有合作相关的知识,希望对你有一定的参考价值。

# Origin http://forums.cgsociety.org/archive/index.php?t-904223.html

import maya.OpenMaya as api
import maya.cmds as cmds

def findNearby(point, tolerance):
	'''findNearby
		point - Either an MPoint, a tuple (x, y, z), or an object name (Uses that objects pivot)
		tolerance - The search radius for finding nearby selection
	'''
	mode = 'vertex'
	
	#convert an object to an API point (Get it's worldspace position)
	if isinstance(point, basestring):
		point = pointFromObject(point)
	point = Point(point)
	
	meshList = buildMeshList()
	selection  = api.MSelectionList()
	
	for mesh in meshList:
		matrix = api.MMatrix(mesh.inclusiveMatrix())
		
		meshObj = api.MFnMesh(mesh)
		meshBB = meshObj.boundingBox()
		meshBB.transformUsing(matrix)
		
		pointBB = api.MBoundingBox(
								Point(point.x-tolerance, point.y-tolerance, point.z-tolerance),
								Point(point.x+tolerance, point.y+tolerance, point.z+tolerance))
		
		#if the mesh is inside the bounding box for the point+tolerance
		if meshBB.contains(point) or meshBB.intersects(pointBB):
			if mode in ['vertex', 'cv', 'point']:
				iter = api.MItGeometry(mesh)
				while not iter.isDone():
					vert = iter.position(api.MSpace.kWorld)
					if pointBB.contains(vert):
						if (point-vert).length()<tolerance:
							selection.add(mesh, iter.currentItem())
					iter.next()
			else:
				pass
	matching = []
	selection.getSelectionStrings(matching)
	return matching
	
def buildMeshList():
	meshList = []
	
	iter = api.MItDag(api.MItDag.kDepthFirst, api.MFn.kMesh)
	while not iter.isDone():
		dagPath = api.MDagPath()
		iter.getPath(dagPath)
		dagPath.extendToShape()
		if not dagPath.fullPathName().endswith('Orig'):
			meshList.append(dagPath)
		iter.next()
	return meshList

#DEPS#
class Point(api.MPoint, object):
	'''Subclass of MPoint, use a nicer str() and repr()'''
	def __init__(self, x=0, y=0, z=0):
		#allow point to take a single tuple, or 3 floats, or wrap an existing MVector
		if isinstance(x, api.MPoint) or isinstance(x, api.MVector):
			super(Point, self).__init__(x)
			return
		
		if isinstance(x, tuple) or isinstance(x, list):
			super(Point, self).__init__(x[0], x[1], x[2])
		else:
			api.MPoint.__init__(self, x, y, z)
		
	def __str__(self):
		return '(%g, %g, %g)'%(self.x, self.y, self.z)
	def __repr__(self):
		return '<<MPoint (%g, %g, %g)>>'%(self.x, self.y, self.z)
	
	def asTuple(self):
		return (self.x, self.y, self.z)

def pointFromObject(object):
	'''Get an objects xform, return Point object'''
	pos = cmds.xform(object, q=True, ws=True, t=True)
	return Point(pos[0], pos[1], pos[2])


findNearby('pSphere1', 1)

以上是关于python 此脚本旨在返回选择范围内的所有附近顶点,而您只查找整个对象。所以你有合作的主要内容,如果未能解决你的问题,请参考以下文章

Python实现返回指定范围内的所有素数

获取半径内的附近街道地址

JavaRedis通过中心经纬度与半径获取范围内的结果集(类似附近的人)

MySQL:选择两个日期范围内的所有数据

使用经度和纬度查找给定距离内的所有附近客户

范围附近的导轨地理编码器