3
$\begingroup$

In Blender, in the Material tab, there are different properties such as Base Color (1), Metallic (2), etc.

Material Tab Properties


I need to find a way to determine which property the mouse pointer is currently hovering over.


Note:

The problem described below is specific to pre 4.4 versions of Blender.

*which I wasn't aware of at the time when posted this question.


My first thought was to use bpy.context.property:

prop_owner,prop_name,path_id = bpy.context.property

However, this doesn't help here, since prop_owner and prop_name are identical for both of the mentioned properties. For example, if I print them:

print(prop_owner,prop_name)

I get the following identical results:

(1) `<bpy_struct, ShaderNodeTree("Shader Nodetree") at 0x000001B837146720> default_value`
(2) `<bpy_struct, ShaderNodeTree("Shader Nodetree") at 0x000001B837146720> default_value`

So how can I determine whether the mouse is hovering over the Base Color value, the Metallic value, or some other property?

$\endgroup$

1 Answer 1

4
$\begingroup$

I've written a sample Operator for this. Simply hover over a property, press F3 and run Test Hover Property. It will print the property's name in the System Console.

import bpy

class TEST_OT_hover_prop(bpy.types.Operator):
    """Check current property under mouse (if possible)"""
    bl_idname = "test.hover_prop"
    bl_label = "Test Hover Property"

    def execute(self, context):
        print("Test property name hovered over:")
        prop = context.property
        if not prop:
            self.report({'INFO'}, "No property detected")
            return {'FINISHED'}
        
        owner, path, _ = prop
        self.report({'INFO'}, f"RNA Path: {path}")
        try:
            node_name = path.split('["')[1].split('"]')[0]  # e.g. nodes["Principled BSDF"].inputs[0].default_value
            input_index = int(path.split('inputs[')[1].split(']')[0])
            node = owner.nodes[node_name]
            socket_name = node.inputs[input_index].name
            self.report({'INFO'}, f"Property name: {socket_name}")
        except Exception as e:
            self.report({'INFO'}, f"Failed to get name: {e}")

        return {'FINISHED'}

def register():
    bpy.utils.register_class(TEST_OT_hover_prop)

def unregister():
    bpy.utils.unregister_class(TEST_OT_hover_prop)

if __name__ == "__main__":
    register()
$\endgroup$
1
  • 1
    $\begingroup$ Oh, I see why I struggled with this. I was developing my addon quite a long time, hence started (and continued) to test it on 4.3 version of Blender (while planning to add compatibility with newer versions of Blender right after addon is finished). The problem I've described in my initial question is related to 4.3 version, since before 4.4 the path from context.property wasn't actually a path, but a property name only. Ok, notes taken. Migrating to newer version of Blender. And cutting-off some parts of backward compatibility with older Blenders. $\endgroup$ Commented Nov 9 at 10:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.