Page MenuHome

bpy.msgbus.subscribe_rna
Open, Waiting for Developer to ReproducePublic

Description

System Information
Operating system: Windows-10-10.0.17134 64 Bits
Graphics card: GeForce GTX 970/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 431.60

Blender Version
Broken: version: 2.80 (sub 75), branch: master, commit date: 2019-07-29 14:47, hash: rBf6cb5f54494e
Worked: (optional)

Short description of error
Callback of bpy.msgbus.subscribe_rna is never called when moving the object in the scene. It only works when changing the property in the Transform panel.

Exact steps for others to reproduce the error

  1. In the scripting view, copy/past the code from here: https://blender.stackexchange.com/questions/150809/how-to-get-an-event-when-an-object-is-selected
  2. Run the script
  3. Toggle system console to see outputs
  4. In the 3D view, select the cube, open the transform panel and change one of the location's components -> the callback print something
  5. Now select the cube and move it directly in the scene -> no output

And an auxiliary question: how to unsubscribe?

Thanks!

Details

Type
Bug

Event Timeline

Philipp Oeser (lichtwerk) lowered the priority of this task from Needs Triage by Developer to Waiting for Developer to Reproduce.Tue, Dec 3, 12:39 PM

And an auxiliary question: how to unsubscribe?

bpy.msgbus.clear_by_owner

Regarding the missing update: can confirm the behavior, but it could very well be that this is how it is supposed to work:
I am a bit on shaky ground here, but from my understanding, only the following go through RNA to write to data (in most scenarios):

  • Python API
  • User Interface
  • Animation System

(check https://wiki.blender.org/wiki/Source/Architecture/RNA also)

The transform system doesnt really go through RNA iirc, so subscribing wont succeed here.

Definitely going to let @Campbell Barton (campbellbarton) decide on this [also on the following suggestion as a workaround]:
If msgbus does not handle all updates, do we have to rely on handlers here (less elegant, probably less performant as well...)?

import bpy
from mathutils import *

ob = bpy.context.object
ob["locstore"] = ob.location

def my_handler(scene):
    loc_old = Vector(ob["locstore"])
    if loc_old != ob.location:
        ob["locstore"] = ob.location
        print('Object "{}" changed its location to: {}: '.format(
            ob.name, ob.location)
            )

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

Maybe this also belongs more to devtalk, there is a similar thread open, see https://devtalk.blender.org/t/using-bpy-msgbus-subscribe-rna

Note that this is a general issue for operators that access struct members directly, not via RNA.

In this case we can get the transform system to send the message that the object location changed.