Quick overview: I'm working on some computer vision work where I need lots of simulated images where the rocks have been labeled. Using blender I can easily produce the images I need, and I'm now working to automate getting their image space bounding boxes. I created these rocks by defining the particle system on (in this example) "my_object" using the GUI, and I'm using "render as collection", and pointing it a collection I made of various rocks.

I have (I think) most of the code for this worked out. I can get the 2D bounding box of a mesh object thanks to this post, and I can get all of the individual particles as well, but I can't seem to figure out how to get the corresponding mesh of an individual particle, which presumably is all I'm missing now. But here is the relevant portion of my script (I've got a lot going on, so I've tried to cut it down to just the part that I'm struggling with):

my_particle_system = my_object.particle_systems[0]
for particle in my_particle_system.particles:
    particle_mesh = particle.???? # Or something else?  How to get this information?
    bounding_box = camera_view_bounds_2d(bpy.context.scene, camera, particle_mesh)

NOTE: camera_view_bounds_2d() comes directly from the post I linked to, and accepts in the scene, camera object, and the mesh object in question.

Any guidance would be really appreciated! I'm going to continue searching docs (as I have been for the past few hours), but i figured I'd ask here if anyone could point me in the right direction.

EDIT: So from this post, it seems that:

"Note: this doesn't seem to work in the latest 2.8 builds, where it appears that both particles and child_particles are empty collections. I'm guessing this is a bug, as it works just fine in 2.79".

This explains some of the weirndess I've seen and the troulbes I've been having if its true (if its not, then I'm clearly a bit more lost than I should be!). I'd prefer not to downgrade my blender version though, so I'm going to continue working on this, and if I find a solution I will post it here!

Edit 2: So I've figured out how to actually gain access to the particles. This new code will do the trick:

depsgraph = bpy.context.evaluated_depsgraph_get()
my_object_eval = my_object.evaluated_get(depsgraph)
ps = my_object_eval.particle_systems[0]
for p in ps.particles:
    p.size #or whatever you need from them

Still struggling to relate this to their specific geometry so I can generate the bounding boxes... but again, I'll update here if I figure it out as it could be useful for other people.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.