1
$\begingroup$

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 stngs.fluid not None, but 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?

Edit: Solved

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 'FLUID' or '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 .fluid and .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 .fluid or .boid without a scene update. If anyone has insights, I'm curious!

$\endgroup$
1
  • $\begingroup$ Not clear to me why adding bpy.context.scene.update() did not work for you. Perhaps you can post the code that did not work for you and that might help others $\endgroup$
    – J. Bakker
    Commented Mar 7, 2018 at 7:19

1 Answer 1

1
$\begingroup$

Thanks for the clear question. I just added the bpy.types.Scene.update call and in Blender 2.79a it works.

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'
bpy.context.scene.update()
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

Sleep

sleep will never work in Blender as blender will halt mostlyduring the running of a script. Blender will just wait until the script has finished completely and then run the internal variant of bpy.types.Scene.update.

$\endgroup$
1
  • $\begingroup$ Thanks. With your nudge, I figured it out and edited the question to explain what my issue was. Apologies for apparently not actually trying the scene update in my test script. Also, thanks for the tip on sleep. $\endgroup$ Commented Mar 7, 2018 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .