Tracking when the user switching between object and edit mode

It is impossible to track the switching between object and edit mode by the user with common handlers in Blender, there are no handlers for such event type in the bpy.app.handlers. However, this can be tracked through the Message Bus.

Let’s define the simplest function for printing to the system console (main menu – window – toggle system console) the name of the current active object and the mode that the user switched it to.

In order for this function to be called at the moment the user switches object-edit mode by pressing the TAB key or from the area menu, we will sign it as a handler for the Message Bus.

In the first parameter – “key”, we pass a pointer to the tracked property of the current active object – “mode”. The correct path to this pointer can be obtained with the “path_resolve” function.

The second parameter, “owner”, specifies the owner for this subscription. Set the current active object itself as the owner.

Our “on_mode_change” function has no input parameters, so we set  an empty tuple for the “args” parameter. If necessary, here we can pass the necessary parameters to our function.

And finally, in the “notify” parameter, we assign a pointer to our function, which will be called when the user switch the mode of the object.

After executing the above code, the following information will be displayed in the system console every time the user switch the mode of the currently active object:

Such a subscription tracks the mode changing of  a particular object. If we want to track mode switching for other objects, we will need to call our code for each of them.

We can simplify the situation and track the mode switching for all objects in the scene at once.

To do this, instead of a specific object in the “key” parameter, specify a tuple with two elements: a pointer to the type of the tracking object and the name of the tracking property.

The owner of the subscription must also be assigned a more general, non-object specific, for example – “bpy”.

It is enough to execute this code once, and our function specified in the “notify” parameter will be called for each object in the scene when the user changes the object mode.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments