How to match tilt between two bezier curves?

I have the following code which creates two Bezier curves which share a control point:

import bpy

def CreateBezier(cp1, cp2):

bpy.ops.curve.primitive_bezier_curve_add(enter_editmode=True, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.data.resolution_u = 10
bpy.context.object.data.bevel_resolution = 0
bpy.context.object.data.bevel_depth = 0.06
points = bpy.context.active_object.data.splines[0].bezier_points
SetBezierSplinePoint(points[0], cp1)
SetBezierSplinePoint(points[1], cp2)
bpy.ops.object.editmode_toggle()

def SetBezierSplinePoint(point, cp):
point.co = cp["co"]
point.handle_left = cp["handle_left"]
point.handle_right = cp["handle_right"]
point.tilt = cp["tilt"]

cp1 = {"co" : (0,0,0), "handle_left" : (-.5,0,0), "handle_right" :  (.5,0,0),  "tilt" : 0}
cp2 = {"co" : (1,1,.5),"handle_left" : (1,.5,0),   "handle_right" :  (1,1.5,0),"tilt" : 0}
cp3 = {"co" : (2,.5,0.5),"handle_left" : (2,2.1,.4),"handle_right" :  (2,1.9,.6),"tilt" : 0}

CreateBezier(cp1, cp2)
CreateBezier(cp2, cp3)


The problem is where both bezier curves meet - the "tilt" is different between them:

Is it possible to programmatically match the tilt of the first curve when creating the second?

• Not exactly sure why or how to calculate it, but setting the second control point's turn=-25.6° on the second curve fixes it: CreateBezier(cp1, cp2) cp2['tilt']=-0.446879 CreateBezier(cp2, cp3) Oct 7, 2021 at 14:09
• It may or may not help you in your use-case, but setting: bpy.context.object.data.twist_mode = 'Z_UP' will fix (or mask) the problem. Oct 7, 2021 at 14:30
• @RonJensen - many thanks, that solution is good enough for me. Would you like to answer it officially so I can mark it as an answer? Oct 7, 2021 at 14:39
• I'm going to do some more research today to see if I can come up with a better/more complete answer. I'll create an answer later (unless @batFinger beats me to it!) Oct 7, 2021 at 14:41
• @MartyFouts I'm using multiple separate curves rather than a single curve with multiple control points as I'm creating a tree generator, which would have multiple branches merging into other branches. As far as I'm aware this can't be achieved with single bezier curves (I could be wrong!) Oct 8, 2021 at 12:27

bpy.context.object.data.twist_mode = 'Z_UP'