Python script error on sorting points in a clockwise order

Hi all,

I would like to use Python to re-organise in a clock wise order all the nested lists of points within one list.

I have several nested lists of points sorted by level and I was trying to use python to sort points on each level in a clock wise order.

import clr
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

import math

pt = IN[0]
newTupla = []

for tupla in range (len(pt)):
	newList = []
	sortedList = []

x = 0
y = 0

# calcs the medium point
for i in pt:
	x += i[0]
	y += i[1]
x = x / len(pt)
y = y / len(pt)
med = [x,y]

#calcs the arc tangent for each point

for j in pt[tupla]:
	rads = math.atan2(-1*(j[1]-med[1]),j[0]-med[0])
	rads %= 2*math.pi
	degs = math.degrees(rads)
	newPoint = (j[0],j[1],j[2],degs)

#sorts the list using the angle
newList = sorted(newList, key = lambda x : x[3])

#removes the angle from the list
for j in newList:
	newPoint = (j[0],j[1],j[2])

OUT = sortedList   

Does someone have a solution for that?
Thank you for your time !

Hi @Jcp ,
could you provide more infos about what you’re trying to achieve? Clockwise arount what axis? and what is the goal?

Hi @Mostafa_El_Ayoubi,

I am trying to go through a parametric design exercise to generate building envelope options by translating design constrains on a set of rules in dynamo.

The screenshot bellow shows the envelope of the building represented by their vertex and every nested list of points is the intersection of this lines with the levels defined in Revit.
As next steep I would like to create a Polycurve.ByPoints per level and is here where I need to sort points on each level in a clock wise order.
My goal is to place adaptive components between levels based on 4 reference points

Back to the issue of sorting this set of points with python as you can see in the python code I am trying to calculate the medium point then angle of each point to the medium point and sort the list using the angle.

The Ampersand package has a Point.SortAzimutally node, maybe that will work for you.

Hi @jostein_olsen,

Thanks for the suggestion but for some reason Point.SortAzimutally node is not reorganising my list of points.

maybe something like this?

I first get the average point, then create a “reference vector” that allows to get angles to sort the points.

That’s what it looks like before sorting :


I can make it work in python in 1 list of points with the same principle you are using in dynamo. My issue is how to do it with multiple nested lists within a list.

Is it possible to make it work with Math.average?

here’s an example with a list structure similar to yours :

just make sure you set the lacings to “longest” as shown in the screenshot above


I cannot flatten the list, each of my nested lists are sort by level and I need to keep this structure for the next steps.

Yeah I got that . When the lacing of flatten node is set to longest , you keep your first level of lists. If you take a look at my previous screenshot, or try it yourself, you’all see that .

1 Like

It works perfect!!

Thank you @Mostafa_El_Ayoubi