Hi,
I’m totally new to Python and general coding.
I wanted to take some measurement on more then 1 edges, I used the Measure Panel add-on.
But this would not do my job.
I created this simple script by looking at other scripts and snippets examples and the Blender py gui files.
It takes the selected edges measures them and outputs there sum. I also put in a nice little tool panel with 1 button and the output.
Maybe someone else could use this. Or has some comment, adjustments, better ways to do this please post,.I want to use this tool to measure scanned body’s.
here is the code:
# select edges in edit mode
# outputs: sum of selected edge lenght
import bpy
from mathutils import Vector
#toggle edit/object mode for (selection refresh?)
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
#creat needed list
List = []
#find selected edges
selected_edges = [v for v in bpy.context.active_object.data.edges if v.select == 1]
print("selected",selected_edges)
#append vertices from selected edges to our lists
for i in selected_edges:
List.append([i.vertices[0], i.vertices[1]])
#find the distance between 2 vertices in an edge
List2 = []
meshdata = bpy.context.active_object.data.vertices
for pair in List:
vec1 = meshdata[pair[0]].co
vec2 = meshdata[pair[1]].co
dist = (vec1-vec2).length
List2.append(dist)
print("length",List2)
print("debugger: 1 = good:",List2.count(dist))
#sum of all lenghts
S = sum(List2)
print("total",S)
#************GUI***********
def getSingleObject(context):
if len(context.selected_objects) == 1:
return context.selected_objects[0]
return None
PRECISION = 4
dist = S
context = bpy.context
if hasattr(bpy.context.scene, "totaal_length"):
context.scene.totaal_length = dist
context.area.tag_redraw()
texts = [("Dist:")
]
class ToolsPanel(bpy.types.Panel):
bl_label = "Circumference Tool"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
layout = self.layout
scene = context.scene
rd = scene.render
col = layout.column(align=True)
col.label(text="Selected edges length:")
col.prop(context.scene, "totaal_length")
col.operator("hello.hello")
class OBJECT_OT_HelloButton(bpy.types.Operator):
bl_idname = "hello.hello"
bl_label = "Measure"
def execute(self, context):
bpy.ops.script.python_file_run(filepath="your/this script.py")
return{'FINISHED'}
class OBJECT_OT_HelloButton2(bpy.types.Operator):
bl_idname = "hello2.hello2"
bl_label = "Total Length"
def execute(self, context):
bpy.ops.script.python_file_run(filepath="your/this script.py")
return{'FINISHED'}
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.totaal_length = bpy.props.FloatProperty(
name="Total",
precision=PRECISION,
unit="LENGTH")
pass
def unregister():
bpy.utils.unregister_module(__name__)
pass
if __name__ == "__main__":
register()