4
$\begingroup$

I want to create a face, using given tuples, representing the xyz-coordinates of vertices. For that I need to arrange the vertices in a list. They need to be structured "counter-clockwise" if that makes sense.

p1 = [1,1.5]
p2 = [0,-2]
p3 = [-1,-2]
p4 = [-1,1.5]
p5 = [1, 0]

points = [p1,p2,p3,p4,p5]

These are the unsorted points. The desired outcome is:

points = [p3,p2,p5,p1,p4]

Thanks in advance :D

$\endgroup$
4
  • $\begingroup$ Counter-clokwise? Like ascending vs descending? And the order you provided doesn't make sense to me, p4 in particular. $\endgroup$ Commented May 10, 2022 at 9:26
  • $\begingroup$ They should be the vertices of a new face, I have a method, which uses the points list to create a face between them. If you draw the coordinates on a paper, p4 would be in the "Top Left" Corner. P3 would be Bottom left and so on. Hope this makes sense, idk @Lukasz-40sth $\endgroup$
    – quest-12
    Commented May 10, 2022 at 10:39
  • $\begingroup$ I think this solution will probably get you what you need. Numpy comes packaged with blender so it should transfer 1:1 $\endgroup$
    – Jakemoyo
    Commented May 10, 2022 at 11:31
  • $\begingroup$ Here's how Blender creates a face from a vertex cloud $\endgroup$ Commented May 10, 2022 at 13:27

1 Answer 1

4
$\begingroup$
>>> import bmesh
>>> bm = bmesh.from_edit_mesh(C.object.data)
>>> bm.verts.ensure_lookup_table()
>>> verts = [v for v in bm.verts]
>>> print([v.index for v in verts])
[0, 1, 2, 3, 4]

>>> print([v.co.xy.to_tuple() for v in verts])
[(1.0, 1.5), (0.0, -2.0), (-1.0, -2.0), (-1.0, 1.5), (1.0, 0.0)]

>>> angles = [atan2(*v.co.yx) for v in verts]
>>> angles
[0.982793723247329, -1.5707963267948966, -2.0344439357957027, 2.158798930342464, 0.0]

>>> combined = zip(angles, verts)
>>> sorted_by_angle = list(sorted(combined))
>>> print([t[1].index + 1 for t in sorted_by_angle])
[3, 2, 5, 1, 4]

In 3D space you probably want to estimate the normal of the face (if all vertices lay on the same plane, that's just cross product of 2 vectors between vertices, otherwise perhaps you want to average the plane first, then calculate normal) and rotate the coordinate system so the normal points up...

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .