Get modifier parameter value input by the user

Hi there. I’m trying to get the value that the user inputs for a modifier parameter. Is there a way to do this? I’ve looked it up on Google, searched the API, read a few other posts and even asked ChatGPT without success. Thanks!

There’s a few ways, the way to do it though depends on when you need to get that information.

Is this something that needs to constantly check for any change to that parameter and get the value when any change happens, or something that just needs to check the value when some other script logic is run?

It only needs to be retrieved when running the script.

Do you have an example of what parameter you need?

You can hover over any parameter, if you have Python tooltips enabled, and see how to access it from API.
image

Ahh right on! That actually makes it easier.

References to modifiers are stored on the object.
As an example, this script will print out the number of viewport subdivisions the active object has on it if it has a subdivision surface modifier:

import bpy
obj = bpy.context.active_object
obj_modifiers = obj.modifiers
for modifier in obj_modifiers:
    if modifier.type == 'SUBSURF':
        value = modifier.levels
        print(value)

Here’s a breakdown of how it works.
First off, get the active object and its modifiers:

import bpy
obj= bpy.context.active_object
obj_modifiers = obj.modifiers

Now loop through all the modifiers to find the type of modifier we’re looking for. This example looks for a sub surface (‘SUBSURF’) modifier:

for modifier in obj_modifiers:
    if modifier.type == 'SUBSURF':

(If you’re looking for a different modifier type that isn’t ‘SUBSURF’, you can use this page to figure out what type to look for: https://docs.blender.org/api/current/bpy_types_enum_items/object_modifier_type_items.html#rna-enum-object-modifier-type-items )

Back to the script, once the script finds a matching modifier type we can query the modifier for the attribute we’re looking for:

        value = modifier.levels
        print(value)

Here ^ I’m using ‘.levels’ because that is what the levels viewport attribute is called for a subdivision surface modifier.

If you’re not sure what the attribute you’re looking for is called, you can turn on python tooltips (edit>preferences>interface> Python Tooltips)
image

when you do that, you can hover the mouse over an attribute and the tooltip will now also provide the python attribute name. That is how I knew to use ‘.levels’ for the script:
image


Hopefully this makes some kind of sense? it’s a lot to unpack for sure, let me know if you need more clarification on any of that