I'm having an application where I want to remove and add points to a curve dynamically from within a frame-change handler. Before you think that this is not good practice and I should better use the start- and end-mapping feature of Blender: The actual application behind that is described here: Troubles with creating a track that is evolving frame by frame This is also including a description why start- and end-mapping is apparently not suitable for that application (as of the state of my understanding as of this writing...).
At first I wanted to care about the removal of a number of points at the end of the curve. I did run into an issue I was not able to solve yet. Here is my according handler that is being called with every frame-change:
def on_frame_changed2(Scene):
current_frame = bpy.context.scene.frame_current
# Force the EDIT-mode
bpy.ops.object.mode_set(mode = 'EDIT')
# Get a reference for the object that is containing the curve.
trackpath_object = bpy.context.scene.objects.get('TrackPath2')
# Determine the number of points we have in the curve
current_point_count = trackpath_object.data.splines.active.point_count_u
# If there are more points in the curve than the current frame number, delete
# all points past the current frame number.
if current_point_count-1 > current_frame:
# Deselect everything so that we delete only those points we select later.
bpy.ops.curve.select_all(action='DESELECT')
# Select all points to be deleted.
for i in range (current_frame+1, current_point_count):
point = trackpath_object.data.splines.active.points[i]
point.select = True
# Delete all selected points
bpy.ops.curve.delete(type='VERT')
The object "TrackPath2" is a path with poly-lines that has been created before.
Now the problem is that this is working fine as far as I did "some sort
of selecting" the path manually beforehand. I write "some sort of
selecting" because apparently this has nothing in common with the
selection within the Blender GUI. When I'm in EDIT mode and choose
"Select->All" for instance, there are selected all points of the path
but not the path as such. Instead, I need to click on either a point of
the path or on a link between two points. Then the path is "some sort of
selected", I can change the frame, and the frame change handler deletes
all desired points. As a result of the
bpy.ops.curve.delete(type='VERT')
operation the path becomes "some sort of unselected" automatically and I need to click on it again to make the script working again.
When the path is not selected in the way it appears to be needed, the script is already failing at the evaluation of trackpath_object.data.splines.active.point_count_u Apparently, trackpath_object.data.splines.active is empty then resp. returns "None". I can check this also in the Python console. As soon as I click on the path it is not empty any more.
So the question is: How do I mimick this "click on the path" in the script so that it will be working under all circumstances?