Page MenuHome

Particles python access is broken in 2.8
Closed, ResolvedPublic

Description

Win10
Gtx 580

Blender 2.8 Beta and latest daily build

Accessing the particle_systems.particles property is broken in Blender 2.8

-Open the attached file

-It contains a cube with one particle system
-Press Run Script to print the numbers of particles of this system via python, in the console
-It returns 0 particles

Details

Type
Bug

Event Timeline

Jacques Lucke (JacquesLucke) triaged this task as Confirmed, Medium priority.Dec 5 2018, 2:41 PM
lucas veber (lucky3) added a comment.EditedDec 7 2018, 1:17 PM

Evaluating the depsgraph first seems to fix it:

ob = bpy.context.active_object
eval_ob = bpy.context.depsgraph.objects.get(ob.name, None)
ps = eval_ob.particle_systems[0]

However not sure it's the correct approach, maybe it should be made obvious for users?

Thanks Lucas for the code! It helps to at least have that.

Philipp Oeser (lichtwerk) closed this task as Resolved.
Philipp Oeser (lichtwerk) claimed this task.

There is a similar case in T60083.

Quoting from there:

From predictability point of view using an explicit evaluation object from a script will be preferable solution: this eliminates possibility of system silently switching to an object instance which you don't intent to. Points are:

  • Access to any ID field should be following one rule (as in, it shouldn't be reading some fields from given ID and other fields from an evaluated ID).
  • Scripts need to be able to read write fields which are considered inputs for evaluation.

So from this point of view i am in favor of leaving it up to script to decide what ID to access.

Yes, totally agree with @Sergey Sharybin (sergey) here. We do have a few helpers (like for raycast) where is sort of make sense to implicitly get the evaluated data from context's depsgraph, but think this should remain an exception (and a documented one).

So the solution @lucas veber (lucky3) posted is the preferred and correct one.
Guess this can be closed then...

Does this still apply? I changed the code in the attached blend to

import bpy

ob = bpy.context.active_object
eval_ob = bpy.context.depsgraph.objects.get(ob.name, None)
ps = eval_ob.particle_systems[0]

print(len(ps.particles))

But I get "AttributeError: 'Context' object has no attribute 'depsgraph'" - am I doing something wrong?

@Georg K (georg), the API has been changed in rBe693918d407: to access dependency graph use context.evaluated_depsgraph_get().
The API docs will be updated soon.