3
$\begingroup$

I'm troubleshooting a large particle system and for that I'd like to get the total number of particles. Is there a Python command that I can use to retrieve it?

I expect this number to be the same as the number of vertices used to generate the particle system (and to be no larger than the number of particles specified in the Particles Properties tab), but I need to confirm to rule out one possible source of error in the model.

Many thanks in advance if you know the answer and care to share it!

$\endgroup$

1 Answer 1

1
$\begingroup$

In case you mean the number of live particles, rather than the total number of particles, you can get it thus:

o  = bpy.context.object        # Active object
ps = o.particle_systems.active # Active particle system
nLiveParticles = len([ 
    p for p in ps.particles if p.alive_state == 'ALIVE' 
])

For the general number of particles, you can simply print:

print( len( ps.particles ) )

This will likely be identical to the number you've specified in your particle system settings, which you can also access here:

print( ps.settings.count )

So if your object is a mesh, and you want to test whether the total particle count or live particle count equals the number of vertices, you can just assert these conditions, and get an assertion error if any of them is false:

>>> assert len( ps.particles ) == ps.settings.count, "Total number of particles equals PS settings"

>>> assert len([ p for p in ps.particles if p.alive_state == 'ALIVE' ]) == len( o.data.vertices ), "Number of live particles equals number of vertices"
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AssertionError: Number of live particles equals number of vertices
$\endgroup$
0

You must log in to answer this question.