Force material update OpenGL render active viewport


(Sammey) #1

Sometimes doing OpenGL render active viewport is a very fast way of rendering an animation, especially 2D animations, when no lighting or fancy shading is needed.

There is a problem where materials don’t update in the viewport, like switching textures or animating UV’s.

This little script helped me to fix that problem for both scrubbing through the timeline, and rendering/playblasting the viewport:

Tested in Blender v2.77. Just run it once after opening the .blend you are working on.


import bpy


mats = bpy.data.materials


def my_handler(scene):
	for m in mats:
		m.invert_z = False
		
bpy.app.handlers.frame_change_pre.append(my_handler)

It adds a custom handler that loops through all the materials and sets a material property so Blender thinks it needs an update. I just picked invert_z because it has that value by default, and it doesnt ruin anything else.

I hope this helps some other people out!

Edit: Thanks for the code wrapping tip MCHammond and proxe!


(bkjernisted) #2

Please remove cursor indicator after you paste the code in the thread below. The code in this thread is incorrect. See below.

Attachments



(bkjernisted) #3

/uploads/default/original/4X/4/d/8/4d856073f21ec08434ef54a9a71bbfd93320ec0a.PNGstc=1

Attachments



(bkjernisted) #4

When I tried to enable the script, I could not get it to load. Any suggestions? Sammey, sorry about the mistakes in my posts. I was not sure what you meant by tab and was looking for some clarification. Is the quotation mark supposed to be in the script? I am not a code guy. Once the script is enabled what is the procedure with the addon? Thanks


(matali) #5

Thank you for your tip :slight_smile:


(MCHammond) #6
import bpy

mats = bpy.data.materials

def my_handler(scene):
    for m in mats:
        m.invert_z = False

bpy.app.handlers.frame_change_pre.append(my_handler)

(proxe) #7

Indeed, Sammey you need to encase the properly formatted text in CODE brackets or edit your post and click Go Advanced properly format the code, select it and click the # in the toolbar region of the editor.

Thanks for the share :slight_smile:


(Looch) #8

so if I want to do this for 1 geometry?.. how would it be when there is a linked character?


(CurtYoung) #9

Exactly what I was looking for! :wink:
Really helpfull
This script should be directly included!

Thanks! It’s the first time I copy/paste a script code inside Blender :smiley:

If someone could do a .py file of it, it will be cool. I tried and failed (lol)

I want a .py script file cause I could add it to my user preference. (Actually we have to copy paste the script each time we launch blender…)


(tonton) #10

Hi thanks a lot for the little script ! i didn’t have a chance to test it yet, but this is super useful !
Does it work with animated texture ? this has always been a problem in the viewport in blender for me…
Anyway i put together a little installable script (like any other addon) which put a texture icon in the down header of the 3d view. It’s an on off toggle for the forcing of materials update, i thought it could be great as an addon but you don’t need it all the time, and the handler can make performance decrease in playback… Feel free to modify it in any way !
and thanks again !

edit : i confirm, it works perfectly with video texture, i tested the script in 2.78 and 2.79 on mac so far, when playing (not going frame to frame with right or left arrow, when playback with shift A), it allows to update the video texture :smiley:
glad to find this script after 1 year of existence !


(CurtYoung) #11

Great!
Thanks for the addon, I still use 2.77 and it works fine :yes:

It’s really helpful :wink:


(morritzio) #12

Thank you! I’ve been looking for a solution to this problem FOREVER. It’s such a pain in the ass as an animator having to export OpenGL renders to After Effects and animate the transparency of layers just to get an idea of how your timings are going to work. Even more so when you have multiple object and layers and you have to work out how to stack you layers in 2d to get the effect you need. I seriously hope this gets addressed in 2.8 is a default feature of Eevee. Anyway this little script should save me HOURS of work on every project. So again thanks so much for sharing your knowledge in this thread :smile:


(Sammey) #13

Nice to read morritzio! :slight_smile: And I trust this will not be a problem anymore with Eevee. Good luck with your projects!


(CurtYoung) #14

Can somebody reupload the .py ?

It will be cool to also add all the BL info otherwise the .py doesn’t show up…

Thanks!


(tonton) #15


here s the addon file ! what do you mean about bl info ? it doesnt work anymore ?