SplineData: Sort knots by x-value

On 25/03/2016 at 12:31, xxxxxxxx wrote:

Hello everybody,

I´d like to imitate the behavior of SplineData by manually changing x-values of knots with python. Meaning, if I pick up a knot by hand and drag it over/under the x-value of another knot, the line of spline will be set well from one to the other knot. If I use python the line will be broken.

spl = op[c4d.ID_USERDATA,1]  
  spl.SetKnot(0, c4d.Vector(0.5,0,0))  
  op[c4d.ID_USERDATA,1] = spl

I already tried to sort the list of splinedata.GetKnots() by knots["vPos"].x ...

for i, knot in enumerate(sorted(op[id].GetKnots(), key=lambda k: k['vPos'].x)) :  
          spl.SetKnot(index=i,   
                      vPos=knot["vPos"],   
                      lFlagsSettings=knot["lFlagsSettings"],   
                      bSelect=knot["bSelect"],   
                      vTangentLeft=knot["vTangentLeft"],   
                      vTangentRight=knot["vTangentRight"],   
                      interpol=knot["interpol"])

... but the result is equal SplineData.SortKnots() and so not what I´m looking for.

Finally I´ve found the following note in the SDK >

Note
Also contains the point index ID. The ID can be accessed using these functions:

def SplineKnotGetID(flags) :
return ((flags >> 16) & 0x0000ffff)

def SplineKnotSetID(flags, flag_id) :
flags = (flags & 65535) | ((flag_id & 0x0000ffff) << 16)

Important
This means that the flags must only be accessed using '|=' and ' &=' (a good advice for any set of flags, for maximum forward compatibility).

... but I really don´t know how to use this function rigth.

Does anyone knows a solution?

Thx and greetings
rownn

On 28/03/2016 at 02:18, xxxxxxxx wrote:

Hi,

Have you tried to swap the active and next knots? The spline shouldn't brake.

Don't worry about the spline knot ID. This is set at the creation of a knot. To add a knot to the spline use SplineData.InsertKnot().
In your case you don't need to create a new knot, just swap 2 existing ones.