Getting the active property value in Blender using context

To get the current active object in a Blender scene, we usually use the context and its context.object (or context.active_object) property. Similarly, when working with the interface, we can access the context to get the currently active property – the value of the field in the Blender UI over which we hover the cursor or on which we clicked with the mouse. The context.property value is used for this.

Let’s look at an example of how to get the value of the currently active property from the N-panel of the Blender interface.

First, define the simplest class for printing the context.property value to the system console.

In the “execute” function, we write the output to the console of the context.property value.

Don’t forget to register it in the Blender Python API.

When executing the operator, the current context will be used – the context of the UI element on which the user performs the current action. Let’s say, for example, this is a call to the context menu – which opens when the user right-clicks.

Add a new item to the context menu, clicking on which will call our operator. To do this, define the “extend_menu” function.

And add its call to the UI_MT_button_context_menu handler:

Now, when we right-click on any property field in the N-panel, a new item “Context Property” will be displayed in the menu.

When we select this menu item, our operator will be executed.

Right-click on the Location – X property and select our new item “Context Property” from the menu that opens. The following tuple will be displayed in the system console:

The first element of the tuple will always be a reference to the data-block to which this property belongs.

On the second place will be the name of this property.

And on the third – the ordinal index of the element, if the property is of array type.

Expand the data output of our operator a little by adding a few more lines to the “execute” function:

Here we additionally receive the actual data of the active property and, if it is of array type, specifically the value of the current element.

Our console output now looks like this:

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments