2
$\begingroup$

I want to subdivide the curve between two control points of a bezier curve using a python script. It's very easy if I do i manually. Just enter the edit mode and select two point, then click the right mouse ,choose subdivide. Show in Image 1,2,3. Image 1

Image 2

Image 3

However, this seems so hard by using a python script. I use the code below, but it doesn't work.

    bezier_curve =  bpy.context.active_object
    bpy.ops.object.mode_set(mode='EDIT', toggle=False) 
    points = bezier_curve.data.splines[0].bezier_points
    points[1].select_control_point = True
    points[2].select_control_point = True
    bpy.ops.curve.subdivide()

The code above just subdivide the whole curve like Image 4 show.Anyone know how to get the effect I expected? Thank you. Image 4

$\endgroup$

1 Answer 1

2
$\begingroup$

Your code works fine as long as not all points are selected before execution.

To avoid this add bpy.ops.curve.select_all(action='DESELECT') before selecting your active controlpoints.

bezier_curve =  bpy.context.active_object
bpy.ops.object.mode_set(mode='EDIT', toggle=False) 
points = bezier_curve.data.splines[0].bezier_points

bpy.ops.curve.select_all(action='DESELECT') # new

points[1].select_control_point = True
points[2].select_control_point = True
bpy.ops.curve.subdivide()
$\endgroup$
2
  • $\begingroup$ Thank you! It works very well😄 $\endgroup$
    – 陈墨白
    Commented Dec 3, 2021 at 0:50
  • $\begingroup$ array points start with index 0, so points[0]..= True and pointſs[1]..=True works better in a general case. $\endgroup$
    – Mike75
    Commented Feb 5, 2022 at 16:13

You must log in to answer this question.

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