Skip to content

Commit

Permalink
improve edge lines vector dump
Browse files Browse the repository at this point in the history
  • Loading branch information
Pullusb committed Feb 12, 2025
1 parent 9225e97 commit ffc1841
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions snippets/dev/dump_edges_to_lines_coords.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Dump mesh edges to lines coordinate
## Dump mesh edges to lines coordinate list of vector pairs, as string to clipboard

import bpy
from mathutils import Vector
Expand All @@ -9,28 +9,64 @@

## Pixel multiplier
## At 10, one blender unit is 10px long
mult = 20
mult = 1


def dump_edges(ob, decimal=7, evaluated=True, as_vector2=False, multiplier=1) -> str:
"""Mesh edges as a flat list of vector pair coordinates
Args:
ob: object to get edge from (has to be of mesh type)
decimal: how many decimals to keep (ex: 2 -> 1.23)
evaluated: wether to evaluate mesh after enabled modifiers are applied
as_vector2: keep only xy coordinate (e.g: usefull to trace icons)
multiplier: multiplier for the coordinates
useful in 2D mode(as_vector2). At 10, one blender unit is 10px long
Example:
for an 2d output like an icon, set as_vector2 to True, a multipllier of 20, with decimal to 0
Return:
String representation of the list with name of object as assigned variable
"""
if evaluated:
## Work on evaluated object (after modifiers are applied)
dg = bpy.context.evaluated_depsgraph_get()
mesh_eval = ob.evaluated_get(dg)
me = mesh_eval.to_mesh()
else:
## work on basse object
me = ob.data

def dump_edges(ob):
me = ob.data
lines = []
## list edge pair vertices.coord
for edge in me.edges:
for idx in edge.vertices:
lines.append(
# Vector(me.vertices[idx].co[:2]) # add as Vector2
[me.vertices[idx].co[0] * mult, me.vertices[idx].co[1] * mult] # add as sublist
)
if as_vector2:
lines.append(
[me.vertices[idx].co[0] * multiplier, me.vertices[idx].co[1] * multiplier] # add as sublist
)
else:
lines.append(
[me.vertices[idx].co[0] * multiplier, me.vertices[idx].co[1] * multiplier, me.vertices[idx].co[2] * multiplier] # add as sublist
)

text = []

## list on by one
#for l in lines:
# text.append( f'Vector(({l[0]:.0f}, {l[1]:.0f})),' )

## list two by two
for i in range(0, len(lines)-1, 2):
text.append( f'Vector(({lines[i][0]:.0f}, {lines[i][1]:.0f})), Vector(({lines[i+1][0]:.0f}, {lines[i+1][1]:.0f})),' )
if as_vector2:
## list on by one
#for l in lines:
# text.append(f'Vector(({l[0]:.{decimal}f}, {l[1]:.{decimal}f})),' )
## list two by two
for i in range(0, len(lines)-1, 2):
text.append(f'Vector(({lines[i][0]:.{decimal}f}, {lines[i][1]:.{decimal}f})), Vector(({lines[i+1][0]:.{decimal}f}, {lines[i+1][1]:.{decimal}f})),' )
else:
## list on by one
#for l in lines:
# text.append(f'Vector(({l[0]:.{decimal}f}, {l[1]:.{decimal}f}, {l[2]:.{decimal}f})),' )
## list two by two
for i in range(0, len(lines)-1, 2):
text.append(f'Vector(({lines[i][0]:.{decimal}f}, {lines[i][1]:.{decimal}f}, {lines[i][2]:.{decimal}f})), Vector(({lines[i+1][0]:.{decimal}f}, {lines[i+1][1]:.{decimal}f}, {lines[i+1][2]:.{decimal}f})),')

## join text with indent
ltext = '\n '.join(text)
Expand All @@ -46,6 +82,6 @@ def dump_edges(ob):
for o in C.selected_objects:
if o.type != 'MESH':
continue
full.append( dump_edges(o) )
full.append( dump_edges(o, decimal=4, evaluated=True, as_vector2=False, multiplier=mult) )

C.window_manager.clipboard = '\n\n'.join(full)

0 comments on commit ffc1841

Please sign in to comment.