I am running a simulation in Blender and I need to change the value and direction of the gravity acceleration at each frame. Is this possible?
I try doing the (very basic) scene.gravity = [value of gravity], but it doesn’t seem to change the value of the acceleration, even if I do ‘scene.frame_set(nFrame)’ and ‘bpy.context.view_layer.update()’.
You could do the calculation mathematically, then translate accordingly, that way your animation is not dependent on the scene gravity at all. This is just a suggestion, since I see no one has responded on this issue.
First of all, thanks for the answer. Up to now, I was deleting and baking all frames after the acceleration value was changed at each frame, which takes super long to run whenever the number of frames goes a bit high (nFrames need to be baked nFrames times, so it scales with nFrames**2).
I have been able to reproduce your code and seems to work fine. In fact, if I visually check the acceleration value, I see how it changes. However, when I try to implement that in my code, it does not seem to work the same. I think I am having some trouble understanding the use of handlers because the acceleration value I see in my simulation remains constant from the beginning.
So you can understand the situation better, my code would look something like this:
script.py → script that is called from command line together with the blender.exe so it runs it in background.
GenerateScenario → Function that is called within script.py that sets up the environment
This solution favors real-time simulation better. Though it won’t be ideal caching since for example after the simulation is cached, the handler is not needed anymore. But once you try to change some setting and then the simulation is cached again you will end up with something totally different than the previous and it might ruin your previous desired result.
I think there can be some good idea here you can try. I saw in the Doom2 source code that for performance at the time they did not use the “random” function in C but they had an array with pregenerated random values. You can try to cache all random values in a binary (frame-value) and load this list at startup.
So, what I have found works best is to add a keyframe with the gravity value at each frame.
Problem is that I (at least I have not found another way to do it) need to re-bake all the dynamics at each step, which, for nFrames larger than 100 (at least in my computer), becomes too slow to be operative in any way.
Would there be a way to do this using handlers instead? or any way to bake just specific frames instead of all of them? Or any other solution that still works but at a much lower computational effort?