Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

April 25, 2012

combined edge lengths

Also available as an addon below, but please read through the post too.
small example of how to obtain combined edge lengths, to get the right answers you must:
- select the edges in edit mode
- switch to object mode, then run the script
(confused?)
import bpy

def get_combined_length(object_data, edge_list):
    edge_length = 0
    for edge in edge_list:
        vert1 = edge.vertices[0]
        vert2 = edge.vertices[1]
        co1 = object_data.vertices[vert1].co  
        co2 = object_data.vertices[vert2].co  
        edge_length += (co1-co2).length
         
    return edge_length
    
object_data = bpy.context.active_object.data
selected_edges = [edge for edge in object_data.edges if edge.select]

summed_length = get_combined_length(object_data, selected_edges)
num_edges = len(selected_edges)

print("number of edges: ", num_edges)
print("combined length: ", summed_length)
Maybe there is a prettier solution/rewrite. Although I wouldn't defend the following rewrite as pretty, I do like it because it involves less duplicate code.
import bpy

def get_combined_length(data_verts, edge_list):
    edge_length = 0
    for edge in edge_list:
        co1, co2 = [data_verts[vert].co for vert in edge.vertices]
        edge_length += (co1-co2).length
         
    return edge_length
    
object_data = bpy.context.active_object.data
selected_edges = [edge for edge in object_data.edges if edge.select]

summed_length = get_combined_length(object_data.vertices, selected_edges)
num_edges = len(selected_edges)

print("number of edges: ", num_edges)
print("combined length: ", summed_length)
It's up to you to investigate which method is faster

I've seen a question on the forums at BA about this post, so i'm going to offer a bmesh solution below. This version uses bmesh.from_edit_mesh(mesh). Looks good, and clean.
as addon, stick it in addons_contrib, its a 3dview type addon, it places itself inside the mesh display panel when an object is selected. Instead of the function running continuously you can press 'sum' to get the total length. Get the addon here

9 comments:

  1. Hi there, I had been looking for a tool that measures selected edge lengths and I was pleased to find it here. I am not a coder so I used the addon and it worked fine. Now a few days later and on a different machine I can't get it to appear on my addon list once installed. Is it because I am using 2.64? I tried on 2.63 and still no luck.

    As I am not seeing it on my Addon list I can't enable it but maybe I am missing something.

    Can you help get me on track as this is directly related to a job I am working on.
    regards,
    Chris Lee

    ReplyDelete
  2. After some playing around I figured that I must have corrupted the python addon file in some way, as I grabbed the file from my laptop that worked and it installed fine.
    Thanks anyway. Great work and good luck on your future endeavours.
    Regards,
    Chris Lee

    ReplyDelete
  3. Hey! Great script! Is there any way to make it show the sum as imperial units instead of blender units? Currently I have to calculate the scale multiplier and then multiply that by the blender units to get the imperial unit equivalent...

    EX: if one edge says 5" on the screen and your program says 1 blender unit then

    1 * a = 5
    a = 5/1
    a = 5

    So from then on I multiply the sum of the edges by the multiplier (5) and that makes it inches.

    Zach

    ReplyDelete
    Replies
    1. Imperial units are alien to me, you should edit line 35 with your maths.

      Delete
  4. Sorry... Its called imperial in Blender. Its just inches, feet, yards, etc...

    ReplyDelete
    Replies
    1. No you seem to misunderstand me, I know what imperial units are I just don't use them. i'm looking around for a convenient way to convert them.. check back soon.

      Delete
    2. I've updated that script now, download it again!

      Delete
  5. Wow! i found this blog because i spend many hours to find some important infos about blender.
    I'm starting using blender (actually i'm using Cinema4D at home and 3D Max in company), and i'm trying to move my work workflow from previous softwares to Blender. Blender is great, but has some important missing features. I guess why no one has tried to prepare script to make "fillet" corner for curves...it's so important! I know that is script to make fillet in mesh. Have you Sir, any knowledge why this feature is not includec or written in script for blender? All 3D softwares has fillet for 2D curves or 3D Chamfering for meshes...but in blender it doesn't exist. Actually i'm usign Sketchup too and there these options (2D Fillet and 3D Chamfer) are done in ruby...i can send You these free scripts if you want. Can you say if it's possible for some programers to translate these scripts from ruby to python?

    Thanks in advance for you time and great work for computer graphic community
    Krzysztof CzerwiƄski

    ReplyDelete
    Replies
    1. Thank you for your comment Krzysztof,

      Blender 2.6+ does have fillet/chamfer but it's called 'bevel', and is accessible from the W menu. It's a relatively mature feature now, (maybe a year old!) . I suggest you ask the question you posed me again but at: Blender.StackExchange.com (Developers and Scripters hang out there and you will get decent response). Several other scripters have written additional fillets with more possibilities than the experiments on this Blog. I only concentrated on fillets on edges, but Blender does now support "fillet" on 3d surfaces.

      Unfortunately, perhaps, I do not intend to code any more for Blender in the near future.

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.