Tuesday, July 9, 2013

Blender: Find the longest edge in a mesh

I had to write this simple script for my thesis on acoustic simulations on a 3D model of a human head (HRTF stuff).

So this script iterates over all the selected edges of the current object and show the longest one's length (you have to look at the console to view the message).
It also writes the maximum frequency that can be calculated using the BEM according to a paper by Brian Katz:
$$f_{max} = \frac{c}{6 \times  edge_{max}}$$


import bpy

sound_speed = 343.0

obj = bpy.context.active_object
edges = obj.data.edges
vertices = obj.data.vertices
matrix = obj.matrix_world.copy()

mm = 0.0
for ee in edges:
    vv = ee.vertices
    v1 = matrix * vertices[ee.vertices[0]].co
    v2 = matrix * vertices[ee.vertices[1]].co
    mm = max((v1-v2).length, mm)

max_freq = sound_speed / (6 * mm)

print("Longest edge's length: %f"%mm)
print("fmax: %f"%max_freq)