1
$\begingroup$

I would need to perform a function on the render engine change, for example:

def function_on_engine_change(self,context):

   if context.engine =='CYCLES':
       print('Cycles')
   else:
       print('Is not Cycles')

The call of this function should obviously take place automatically if the engine is changed

I was looking on the blender handles but found nothing, I wonder if there is any trick that does this

$\endgroup$
2

1 Answer 1

2
$\begingroup$

Use the msgbus

There is a relatively new and mostly undocumented feature of blender that fires a callback when a property is changed. Check if your version has this feature in python console.

>>> bpy.msgbus
<module 'msgbus'>

Related

https://developer.blender.org/P563

Origins to the down of the object by default

How to get an event when an object is selected?

For this case (IMO) can substitute context.scene.render.engine for context.engine

>>> C.engine
'BLENDER_EEVEE'

>>> C.scene.render.engine
'BLENDER_EEVEE'

>>> C.scene.render
bpy.data.scenes['Scene'].render

>>> type(C.scene.render)
<class 'bpy.types.RenderSettings'>

enter image description here

Use msgbus to invoke a callback whenever the "engine" property of bpy.types.RenderSettings is altered.

import bpy


handle = object()

subscribe_to = bpy.types.RenderSettings, "engine" 


def notify_test(context):
    print("Notify changed!", context.scene.render.engine)

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=handle,
    args=(bpy.context,),
    notify=notify_test,
)

bpy.msgbus.publish_rna(key=subscribe_to)

Note using bpy.types.Context, "engine" does not work.

For versions without msgbus

Can use a depsgraph_update_post (or prior scene_update_post) handler.

import bpy

engine = bpy.context.scene.render.engine

def engine_change(scene):
    global engine
    if scene.render.engine != engine:
        engine = scene.render.engine
        print("changed", engine)


if hasattr(bpy.app.handlers, "scene_update_post"):
    handler = bpy.app.handlers.scene_update_post
else:
    handler = bpy.app.handlers.depsgraph_update_post

handler.append(engine_change)
$\endgroup$
3
  • $\begingroup$ It sounds great, I was also thinking of a function called by bpy.app.handlers.depsgraph_update_post, here it would check a variable if the engine really changed, what do you think of this? Too much cpu work? blender 2.79 not support bpy.msgbus $\endgroup$
    – Noob Cat
    Feb 6, 2020 at 15:46
  • 1
    $\begingroup$ Yes that would work too. I would use msgbus $\endgroup$
    – batFINGER
    Feb 6, 2020 at 15:51
  • 1
    $\begingroup$ Edited answer to include. Getting hard to keep track of changes. Do you have depsgraph_update_post in 2.79, if not use scene_update_post (it fires at a huge rate) $\endgroup$
    – batFINGER
    Feb 6, 2020 at 16:10

You must log in to answer this question.

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