2
$\begingroup$

I wrote this little script to toggle the visibility of the shadows of all objects.

It works quite well, but at the end of the script, I'd like to update the viewport. I tried the "hack" with the redraw_timer, but actually I don't want to use it, because it's not recommended and mainly because it doesn't work with the blender version 2.78c. ;-)

import bpy

for ob in bpy.data.objects:
    try:
        ob.cycles_visibility.shadow ^= 1
    except:
        print ("No shadows: ", ob.name)

# Did not work:        
# bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

bpy.context.window.screen = bpy.data.screens["Scripting"]
bpy.context.window.screen = bpy.data.screens["Default"]

So I just switched to another screen layout and back to default.

That works. However, I guess there are more elegant ways to do this.

Thanks brockmann, thanks @all Here's the updated and tested version:

import bpy

for ob in bpy.data.objects:
    try:
        ob.cycles_visibility.shadow ^= 1
    except:
        print ("No shadows: ", ob.name)

bpy.context.scene.update_tag()
$\endgroup$
7
  • 2
    $\begingroup$ I think bpy.context.scene.update_tag() is the right way to do it. $\endgroup$
    – brockmann
    Jun 19, 2017 at 17:42
  • $\begingroup$ ^= 1 actually toggles? That's something new I've just learned for Python. $\endgroup$
    – TeaCrab
    Jun 19, 2017 at 18:24
  • 1
    $\begingroup$ @TeaCrab - Technically it is a bitwise exclusive or. Python uses ^ as a shortcut to it's xor() function. $\endgroup$
    – sambler
    Jun 20, 2017 at 4:26
  • $\begingroup$ As @brockmann suggests you should be able to use ob.update_tag() in the loop and finish with scene.update() but I don't see a refresh. I have used scene.frame_set(scene.frame_current) to force a refresh. $\endgroup$
    – sambler
    Jun 20, 2017 at 4:36
  • $\begingroup$ @sambler Thanks for the explanation. :D $\endgroup$
    – TeaCrab
    Jun 20, 2017 at 6:03

0

You must log in to answer this question.