I would like to move a modifier (which I have already identified) to the bottom of the list of existing modifiers. Unfortunately it does not work and I don't get why...? However, if the object is active, it moves perfectly to the bottom:

#ob = my object, not necessarily it must be selected, it is found by a function
#smooth = #my modifier (found through a function)
for m in ob.modifiers:
    bpy.ops.object.modifier_move_down(modifier = smooth.name)

This only works if the active object is ob. Do I have to override the context? There is a similar question to this but I have no idea how to apply this in my case.

It seems that I have solved my problem by overriding the context members:

for m in ob.modifiers:
    c = {}
    c["object"] = c["active_object"] = ob
    bpy.ops.object.modifier_move_down(c ,modifier = smooth.name)

Any thoughts? Is there a better way of doing that?

  • 1
    $\begingroup$ I guess you mean to the top right? See: blender.stackexchange.com/q/98891/31447 $\endgroup$
    – brockmann
    Apr 25 '20 at 8:31
  • $\begingroup$ Ty @brockmann I had already taken a look at this question, but I see that it is practically the same thing that I am doing, the problem is precisely that bpy.ops seems not to work if the object is not active and selected . Please correct me if I'm wrong $\endgroup$
    – Noob Cat
    Apr 25 '20 at 17:43

Override the context by passing the actual object {'object': obj} to bpy.ops.object.modifier_move_up() or bpy.ops.object.modifier_move_down() operator.

Example on how to move all subdivision modifiers by name (they are usually called 'Subdivision') of multiple objects to the top (position 0) in the modifier stack:

C = bpy.context
mod_name = "Subdivision"

for obj in C.selected_editable_objects:
    # Test whether there is a modifier called "Subdivision" at all 
    if any(m for m in obj.modifiers if m.name == mod_name):
        # Iterate through all modifiers in reverse
        for mod in reversed(C.object.modifiers):
            # Move the modifier as long as its position is 0
            while obj.modifiers.find(mod_name) != 0:
                bpy.ops.object.modifier_move_up({'object': obj}, modifier=mod_name)

Related questions:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.