I'm getting an error when I try to edit particle fluid physics through python. Using Blender 2.79.
Here's some test code that fails when run from the Blender text editor (or from the script runner addon).
import bpy bpy.ops.mesh.primitive_plane_add() emitter = bpy.context.object mod = emitter.modifiers.new("ParticleSystem", type = 'PARTICLE_SYSTEM') stngs = mod.particle_system.settings stngs.physics_type = 'FLUID' stngs.fluid.solver = 'CLASSICAL' #This line results in the error stngs.fluid.fluid_radius = 0.05 #So does this one if I skip the one above
The error is:
AttributeError: 'NoneType' object has no attribute 'solver', and if I print
stngs.fluid before the offending line, it indeed prints
None. The same thing happens for boid physics.
Strangely, if I type the lines one at a time into the Blender python console,
stngs.fluid is not
None, and everything works as desired.
I thought there might be a race condition with some process that makes
time.sleep(10) before the offending line didn't help.
I've also tried updating the scene with bpy.context.scene.update(), which also didn't work. Is there something analogous to the scene that I could update?
Updating the scene actually does make the test script work. Thanks to J. Bakker for pointing this out. In the main script I'm working on, the emitter object wasn't linked to the scene at the time of the scene update, so it didn't work.
So, to edit fluid- or boid-specific settings for particle system physics via python, you need to make sure the emitter is linked and then update the scene after switching the physics type to
'BOID'. Interestingly, settings for other physics types can be edited before the emitter object is linked to a scene, which I suppose has something to do with the
.boid properties being type None by default, and therefore not having the other properties at all. I'm not sure why setting the physics type doesn't change
.boid without a scene update. If anyone has insights, I'm curious!