Set property using path from `path_from_id` / prop `path_resolve`

path_from_id is very convenient as it allows getting relative prop path from it’s ID data-block, even if property is nested.

modifier.path_from_id("count")
# 'modifiers["Array"].count'

Is there some general method in Blender for setting value for prop using such path?
E.g. something like

prop_path = modifier.path_from_id("count")
obj.set_from_path(prop_path, 20)

Or it can be only worked out by parsing the path and chaining couple setattr calls?

Also noticed that there is a way to get prop object without evaluating it’s value. Perhaps, it can be used to somehow set that prop after?

prop = C.object.path_resolve(prop_path, False)
print(prop) # <class 'bpy_prop'>

If you have the name of the property (i.e ‘count’), then setattr is probably the best solution.

If you have the path (‘modifiers[“Array”].count’), then setattr won’t be able to find the correct property, and you need to split the path first…

def path_resolve(obj, path):
    if "." in path:
        extrapath, path= path.rsplit(".", 1)
        obj = obj.path_resolve(extrapath)
    return obj, path

obj = bpy.data.objects['X']
# fobj, prop = path_resolve(obj, "modifiers['Array'].count") # this won't work
fobj, prop = path_resolve(obj, 'modifiers["Array"].count') #works
setattr(fobj, prop, 10)  # set the array count to 10

path corrected, as explained by Andrej in the following post

Thank you, looks simple enough - path_resolve will take care of the parsing indices/key names.

Though note if someone is going to use it too - "modifiers['Array'].count" won’t work due to https://projects.blender.org/blender/blender/issues/120140 and need to make sure to keep double quotes in the actual case.