number of vertices


I’m trying to get my self going with python but i stumbled on this probably stupid question:

when i loaded an existing meshobject( with faces and vertices) is there a way to find the number of vertices in that mesh? or the number of faces?

for iteratif perposes


me = obj.getData() # or whatever... just get an nmesh object

print "%d faces"%len(me.faces)
print "%d verts"%len(me.verts)

thx :slight_smile:

If you only need to know the length of the verts list to iterate through them you can just do sometthing like this:

for vertex in me.verts:

which I believe is equal to:

for i in range(len(me.verts)):
    vertex = me.verts[i]

though I am not 100% sure that iterations like in the first example follow the same order as in the second. AFAIC Python is particularly oriented to the use of lists, and both iterations should match, but if you are not particularly concerned about the order with wich you are scanning the vertices the first one will do fine and save you a line of code

yes it’s the same.

The fist code should AFAIK be much faster than the second. Because I think the verts are stored in a linked-list or tree or something like that. While the first code goes through all vertices, the second have to search the vertics on position i every loop-iteration! -> in the case of a linked-list the performance of the second code would be O(n^2), while the first code’s performance is only O(n).

But if in some case you can’t use such a for loob, e.g. because you have to iterate two lists of equal length at once, you cold use following code:

lst1 = [ ... ]
lst2 = [ ... ]

iter2 = iter( lst2 )

for val1 in lst1:
   val2 =
   do_something( val1, val2 )

About this iteration throught multible lists I had an idea:

class multiiter:
	def __init__(self,mlist):
		self.mlist = mlist
		self.iters = []
		for lst in self.mlist.lists:
			self.iters.append( iter(lst) )

	def __iter__(self):
		return self

	def next(self):
		return tuple([ for it in self.iters])

class multilist:
	def __init__(self,lists):
		self.lists = lists

	def __iter__(self):
		return multiiter(self)

# example usage:


for v1, v2 in multilist([lst1,lst2]):
	print v1, v2

1 5
2 6
3 7
4 8

Only in Python… you got to love this language!