15
$\begingroup$

It seems the following is no longer available in blender 2.8 api

bpy.context.scene.update()

How do we update objects in the scene that have been changed by the script now?

$\endgroup$

4 Answers 4

17
$\begingroup$

Many instructions point to:

bpy.context.view_layer.update()

That's also what's mentioned in API documentation

$\endgroup$
2
7
$\begingroup$

Dependency graph API changes

Main goal here is to make it obvious and predictable about what is going on.

Summary of changes.

  • Access to dependency graph is now only possible to a fully evaluated graph. This is now done via context.evaluated_depsgraph_get().

    The call will ensure both relations and datablocks are updated.

    This way we don't allow access to some known bad state of the graph, and also making explicit that getting update dependency graph is not
    cheap.

  • Access to evaluated ID is now possible via id.evaluated_get().

    It was already possible to get evaluated ID via dependency graph,
    but that was a bit confusing why access to original is done via ID
    and to evaluated via depsgraph.

    If datablock is not covered by dependency graph it will be returned as-is.

  • Similarly, request for original from an ID which is not evaluated will return ID as-is.

  • Removed scene.update().

    This is very expensive to update all the view layers.

  • Added depsgraph.update().

    Now when temporary changes to objects are to be done, this is to
    happen on original object and then dependency graph is to be
    updated.

Some fiddling in python console.

>>> dg = C.evaluated_depsgraph_get(
evaluated_depsgraph_get()
Context.evaluated_depsgraph_get()
Get the dependency graph for the current scene and view layer,
 to access to data-blocks with animation and modifiers applied. 
 If any data-blocks have been edited, the dependency graph will be updated. 
 This invalidates all references to evaluated data-blocks from the dependency graph.
>>> dg = C.evaluated_depsgraph_get()
>>> ob = C.object
>>> ob.evaluated_get(
evaluated_get()
Object.evaluated_get(depsgraph)
Get corresponding evaluated ID from the given dependency graph
>>> ob.evaluated_get(dg)
bpy.data.objects['Cube']

>>> dg.update(
update()
Depsgraph.update()
Re-evaluate any modified data-blocks, for example for animation or modifiers. 
 This invalidates all references to evaluated data-blocks from this dependency graph.
>>> dg.update()

This is all pretty new and TBH haven't tested it against how the old scene update was used. Speculating, it appears can evaluate changes to a single object based on the depsgraph, without the need to expensively update scene and all objects in it.

$\endgroup$
3
  • 2
    $\begingroup$ Replaced bpy.context.scene.update() with dg = bpy.context.evaluated_depsgraph_get() dg.update() works for me, incidently bpy.context.view_layer.update() also gets the job done but I will accept this as the correct answer. $\endgroup$
    – Waves
    Commented May 17, 2019 at 17:35
  • $\begingroup$ I can't make much sense out of the python console part in grey. What is the difference between the >>> segments versus those that are not indented? $\endgroup$
    – Ben
    Commented Aug 16, 2019 at 16:09
  • 1
    $\begingroup$ >>> is the console prompt, anything after I've typed in. If ends with say ( I've hit autocomplete and pasted that. Go to the console and have a fiddle. $\endgroup$
    – batFINGER
    Commented Aug 17, 2019 at 9:51
3
$\begingroup$
bpy.context.view_layer.update() 

works but as mentioned here : corner cases in blender

This method can also help in removing these corner cases but use every time you toggle the property.

I experimented with it, it remove the bug of rendering the object while I set the obj.hide_render = True.

$\endgroup$
3
$\begingroup$

wrestled with this till I was ready to tear out my hair. here's a hacky catchall to force an update when all the other solutions fail.

for obj in bpy.context.scene.objects:
    obj.hide_render = obj.hide_render
$\endgroup$
2
  • $\begingroup$ That was useful, thank you (I used something similar to update some changes in grease pencil layers) $\endgroup$
    – Ommadawn
    Commented Jun 13, 2023 at 9:22
  • $\begingroup$ Finally a method that works! Thanks a lot! $\endgroup$
    – Alio
    Commented Aug 25 at 17:15

You must log in to answer this question.

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