Set Spline Tangents with Python Generator


My goal is to use a Python generator to create a spline with Bezier interpolation where each point has tangents lying in the same plane as the plane spanned by the point itself and its two adjacent points and is also parallel to the line going through the two adjacent points. C4D does this natively when you double click on a point. See the following video example:

The video does exactly what I want, but now I need to figure out the mathematics of this in the Python generator. This is the code that I have so far, but I don’t understand how to calculate the tangents to get exactly what I want:

def main():
    verts = [c4d.Vector(50*ii, 50*(-1)**ii, 50*(-1)**int(ii/2)) for ii in range(10)] # just a sample
    total_points = len(verts)
    spline = c4d.SplineObject(total_points, c4d.SPLINETYPE_BEZIER)
    spline.ResizeObject(total_points, 1)
    spline.SetSegment(0, total_points, False)

    mag = op[c4d.ID_USERDATA,1]
    for pntnum, vert in enumerate(verts):
        spline.SetPoint(pntnum, vert)
        # i need to set the tangent here somehow!

    return spline

The magnitude of the tangents would be a scalar value that I would want to control separately. If you could help me figure out how to calculate the tangents that are identical to what C4D created automatically in that video (besides magnitude), that would really help me out. Does anybody know the mathematics to calculate these tangents?


Sorry, no answer to your actual question.
I just wanted to add a word of warning: The Python Generator can not generate splines. It can only generate objects. So, your generator returns a object, which happens to be a spline, but not the spline itself. The difference may seem subtle, but has some consequences, as the result will not behave correctly as a spline in combination with other generators (e.g. effectors or modifiers). Some may wok others won’t.

Don’t get me wrong, there’s nothing fatally with your approach, just the result may not meet you expectations.

If you want or need a real spline, your only option is to create a ObjectData plugin with OBJECT_ISSPLINE flag set.
The Python Generator has never been designed for this and thus does not have this flag set. Maxon (or a plugin developer) would need to provide us with a Python Spline Generator with this flag set. It’s also not possible as an option on the Python Generator, as it’s a decision taken during plugin registration.
But you shouldn’t shy away from the task. There’s not that much more to it, than you currently do with you generator. Simply use an SDK example (I think, thee’s one already creating a spline) and dump your code in there.



For my purposes, it is OK that it’s an object. I can set its tangents in the Python generator with this line of code:

spline.SetTangent(pntnum, left_tangent, right_tangent)

So the only thing holding me back from achieving what I want is that I don’t know how to calculate the left and right tangents for a given point subject to the constraints I mentioned in my original post. That’s all I need help with.