Face midpoints

I’m new to python and I’m learning it by way of writing plugins for Blender. So please excuse if this is a n00bish question.

I want to iterate through the faces of the selected mesh object and display the midpoints of each faces’ vertices on stdout. But I’m not getting what I would expect. Here is my code. I took out the division operation for the sake of troubleshooting.


import Blender

objects = Blender.Object.GetSelected()

for obj in objects:
	objdata = obj.getData()
	for f in objdata.faces:
		print "face"
		x = 0
		y = 0
		z = 0
		numverts = 0
		for v in f.v:
			numverts += 1
			print "	",v.co
			x += v.co[0]
			y += v.co[1]
			z += v.co[2]
		#divide all terms by numverts when I get this working
		print "	sums: ",[x,y,z]

I run this with a simple cube selected.
My output from this is:


face
        [1.0000, 1.0000, -1.0000]
        [1.0000, -1.0000, -1.0000]
        [-1.0000, -1.0000, -1.0000]
        [-1.0000, 1.0000, -1.0000]
        sums:  [2.384185791015625e-07, 4.76837158203125e-07, -4.0]
face
        [1.0000, 1.0000, 1.0000]
        [-1.0000, 1.0000, 1.0000]
        [-1.0000, -1.0000, 1.0000]
        [1.0000, -1.0000, 1.0000]
        sums:  [-4.76837158203125e-07, -7.7486038208007812e-07, 4.0]
face
        [1.0000, 1.0000, -1.0000]
        [1.0000, 1.0000, 1.0000]
        [1.0000, -1.0000, 1.0000]
        [1.0000, -1.0000, -1.0000]
        sums:  [3.9999998211860657, -1.1920928955078125e-06, 0.0]
face
        [1.0000, -1.0000, -1.0000]
        [1.0000, -1.0000, 1.0000]
        [-1.0000, -1.0000, 1.0000]
        [-1.0000, -1.0000, -1.0000]
        sums:  [-1.1324882507324219e-06, -4.0000000596046448, 0.0]
face
        [-1.0000, -1.0000, -1.0000]
        [-1.0000, -1.0000, 1.0000]
        [-1.0000, 1.0000, 1.0000]
        [-1.0000, 1.0000, -1.0000]
        sums:  [-4.0000000596046448, 8.9406967163085938e-07, 0.0]
face
        [1.0000, 1.0000, 1.0000]
        [1.0000, 1.0000, -1.0000]
        [-1.0000, 1.0000, -1.0000]
        [-1.0000, 1.0000, 1.0000]
        sums:  [8.9406967163085938e-07, 3.9999997615814209, 0.0]

Where are these strange floating point numbers coming from?

your mesh isnt’ grid alligned, thats also why your gettign 1.000 instead of 1.0

go into your cube mesha nd select all the verticies and go Shift+s, align selected to grid.

its a problem that happens every once and a while cause blender isn’t perfect with everything.

hope your learning goes well

MacBlender

Thank you.

You may want to consider using a formatted print statement for printing your numbers. That way you will not be confused by things like 8.9406967163085938e-07 which is pretty damn close to zero (0.000000894).

It has nothing to do with “blender isn’t perfect with everything” but rather is an issue with floating point numbers and their representation.

stiv, the main problem he was talking about was that the verticies were just slightly out of sync with the grid thats a blender problem. even if you format the float it’ll just show up at 8.4E-7 not 0.

MacBlender

No, this is incorrect. for example:


x = 8.97123456/10000000.0
print x
print  "%7.3f" % x

And as a minor nitpick, 1.000 is simply a more exact version of 1.0

how come you don’t use

x=round(my_uber_longdong_misbehaving_badbad_float, how_short_I_want_it_to_stick_neatly)

It all depends on what you want to do. Using round() creates a new number, different than the old. Using a formatted print simply prints the existing one using a certain precision with control over things like leading zeros, blank padding, field width and signs.