# How to run physics simulation frame-by-frame using Blender's Python API?

In my scene, I drag some of the objects and move them around while the physics is running (Alt+A). This means that the physical interactions could be quite different every time I do start the physics (Alt+A). Note that I am doing all of this when using Blender with GUI.

Instead of using the cursor, I want to feed new coordinates for the locations of the target objects that I want to move. I also want to do all of this when running Blender in the background and use the Python API. So I wonder, is there a way to avoid baking the simulation and do it frame-by-frame using the Python API?

I was initially thinking to run bpy.ops.screen.animation_play() but this command does not work when running Blender in the background. Even if there is a way to get it to work I don't know how how to feed in new location coordinates for the next frames. Also, I wouldn't know the frame rate and this might mess things up even further.

I also cannot bake the simulation due to the dynamic nature of my work: new location coordinates might be different on every run. Therefore, I think the only way to do what I want would be to do the simulation step-by-step. I would appreciate if someone can help me with this

P.S. I know I shouldn't change the position of the objects in the scene when the physics is working rather than applying force to them. I will figure this out later but any tip in the comments for resolving this would also be appreciated.

If I understand your question correctly then the way that I do this is using bpy.context.scene.frame_set(). I set it to 1 at the beginning of the simulation and then inside a loop to step through the frames I use bpy.context.scene.frame_set(bpy.context.scene.frame_current + 1).