Is it possible to get the location (world space) of every mesh particle of a particle system and following that the vertex locations of every mesh particle using python?
Asked
Active
Viewed 2,249 times
2 Answers
3
Use the depsgraph
Using the code suggested here https://developer.blender.org/T58792
Adds an empty, displayed as a cube, at the particle location. Make the object with the particle system active, move to desired frame and run script.
import bpy
from mathutils import Matrix
context = bpy.context
# beware removes all empties from scene (for testing)
for mt in (o for o in context.scene.objects if o.data is None):
bpy.data.objects.remove(mt)
# get the depsgraph and the evaluated object
dg = context.evaluated_depsgraph_get()
ob = context.object.evaluated_get(dg)
# assume context object has a ps, use active
ps = ob.particle_systems.active
# this is the instance object
po = ps.settings.instance_object
for p in ps.particles:
# make a matrix for the particles
M = p.rotation.to_matrix().to_4x4()
M.translation = p.location
# put an empty at particle
bpy.ops.object.empty_add()
mt = context.object
mt.matrix_world = M
# the scale
mt.scale = (p.size,) * 3
mt.empty_display_type = 'CUBE'
The global location of each vertex of the instance object of the particle system will be
mt.matrix_world @ vert.co
batFINGER
- 84,216
- 10
- 108
- 233
-
Thank you, that worked. Just one follow up question. When I run the script the empties are created with an offset on the Z axis. Why's that? – xrogueleaderx Sep 12 '19 at 11:00
0
Getting the location of particles of a particle system isn't particularly difficult. You can simply iterate through the particles:
particle_system = bpy.context.object.particle_systems[0]
for particle in particle_system.particles:
print(particle.location)
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
kareemalgalaly
- 61
- 1
- 3