I have a script which generates a curve path and sets an object to follow the path, I can see the object moving correctly in the animation move, and it also moves to the correct location (at least visually in the view port, the coordinates when you press n do not change) when I use bpy.context.scene.frame_set(desirved_frame)
. However, when I try to get the location and rotation of that object at the frame that I set, it just returns the original location of the object at frame 0. Below is the code I have used to generate the animation data and what I'm using to try and get the location.
curve = bpy.data.objects["curve"]
follower = bpy.data.objects["Follower"]
con = follower.constraints[0]
con.target = curve
con.use_curve_follow = True
bpy.ops.object.select_all(action='DESELECT')
curve.data.use_path = True
path_length_m = curve.data.splines[0].calc_length()
time = path_length_m / car_v_mps
target_fps = 30
length = int(target_fps * time)
print("Frames is: ", length)
anim = curve.data.animation_data_create()
anim.action = bpy.data.actions.new("%s-Action" % curve.data.name)
fcu = anim.action.fcurves.new("eval_time")
mod = fcu.modifiers.new('GENERATOR')
mod.coefficients = (-frame_start / length * 100, frame_start / length / frame_start * 100)
def get_ext(scene, depsgraph):
obj = scene.objects['Follower']
obj = obj.evaluated_get(depsgraph)
print("Frame: ", scene.frame_current)
print("Location: ", obj.location)
scene = bpy.data.scenes[0] #This is the only scene, == bpy.context.scene
for frame in range(scene.frame_start, scene.frame_end):
bpy.app.handlers.frame_change_post.append(get_ext)
scene.frame_set(frame)
bpy.app.handlers.frame_change_post.clear()
I have also tried replacing the final loop with:
for frame in range(scene.frame_start, scene.frame_end):
scene.frame_set(frame)
depsgraph = bpy.context.evaluated_depsgraph_get()
ob = bpy.data.objects["Follower"]
ob = ob.evaluated_get(depsgraph)
print("Location: ", ob.location)
Both methods just print out the original location of the object.
Reading this documentation I'm not sure how I would use the examples they show to extract the evaluated location and rotation from the class they register, or how to even call the execute method.
Below are some solutions and answers I have read and attempted, with no success:
How do I get a mesh data-block with modifiers and shape keys applied in Blender 2.8?
Updating a 2.7 script that uses frame_current to work with 2.8+
Number of vertices and edges report no changes after adding boolean intersect modifier
Location of an object at a specific frame
Handler frame_change_pre doesn't work in render
Sun object not moving when Rendering Animation
I'm really not sure what I'm doing wrong as some of the questions I've looked at are quite similar to my issue.
Thank you for any help you can provide.
print(f"frame: {frame} \tLocation {ob.matrix_world @ ob.location}")
$\endgroup$scene.frame_set(frame)
. However, looking on other posts, it seems this may be a common issue, with one possible solution being to add time.sleep(3) (import time) after callingbpy.ops.render
, but this did not work in my case. $\endgroup$