Advertisement
KeithFromCanada

Blender Path File Write

Sep 25th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # -----------------------------------------
  2. #  Here is a simple Python script for Blender (in Edit Mode) to write the
  3. #  verts on the shortest path between two (pre-selected) points to a file.  
  4. #  (Change 'filename' and 'measurement' before running.  The script
  5. #  appends new lines onto the file each time.)
  6. # -----------------------------------------
  7.  
  8. import os, bpy, bmesh
  9.  
  10. # Ensure all folders of the path exist
  11. path = "P:/"
  12. os.makedirs(path, exist_ok=True)
  13. filename = "measurement.txt"
  14.  
  15. measurement = "Label for measurement path"
  16.  
  17. oa = bpy.context.active_object
  18. obj = bpy.context.object
  19.  
  20. bpy.ops.mesh.shortest_path_select() #add for request
  21.  
  22. me = obj.data
  23. bm = bmesh.from_edit_mesh(me)
  24.  
  25. v1,v2 = [elem for elem in bm.select_history if isinstance(elem, bmesh.types.BMVert)]
  26.  
  27. list = [v1]
  28. verts = len([v.index for v in bm.verts if v.select])
  29.  
  30. cont=0
  31.  
  32. while cont< verts:
  33.     v=list[cont]
  34.     edges = v.link_edges
  35.  
  36.     for e in edges:
  37.         if e.select:
  38.             vn = e.other_vert(v)
  39.             if vn not in list:
  40.                 list.append(vn)
  41.  
  42.     cont+=1
  43.  
  44. ii = 0
  45. outpath = ''
  46. for v in list:
  47.     if ii > 0:
  48.         outpath = outpath + ','
  49.     outpath = outpath + str(v.index)
  50.     ii = ii + 1
  51.    
  52. with open(path + filename, "a+") as vertfile:
  53.     vertfile.write(measurement+' ['+outpath+']\n')
  54. bmesh.update_edit_mesh(me, True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement