ScriptHelp:Make sure a modifier is inbetween others.

Is it possible to have a script that adds three modifiers and makes sure one of them is inbetween the others by name?

Modifiers are stacked in the order they are created. I’m not sure, but I don’t think you can change the stack order without using bpy.ops or using some copy/delete trickery.

This will do the trick.


import bpy


def validateModifierPosition(object, name, index):
    return object.modifiers[index] == name
    
def getModifierPosition(object, name):
    i = 0
    for m in object.modifiers:
        if m.name == name:
            break
        i += 1
    return i


def changeModifierPosition(object, name, indexTarget):
    if validateModifierPosition(object, name, indexTarget) == True:
        return
    
    index = getModifierPosition(object, name)
    moveCount = indexTarget - index if indexTarget > index else index - indexTarget
    direction = "down"              if indexTarget > index else "up"
    for i in range(0, moveCount):
        if direction == "up":
            bpy.ops.object.modifier_move_up(modifier=name)
        else:
            bpy.ops.object.modifier_move_down(modifier=name)


def run():
    #bpy.context.object.modifiers.new("Modifier 3", "SUBSURF")
    #bpy.context.object.modifiers.new("Modifier 1", "SOLIDIFY")
    #bpy.context.object.modifiers.new("Modifier 2", "BEVEL")
    
    changeModifierPosition(bpy.context.object, "Modifier 1", 0)
    changeModifierPosition(bpy.context.object, "Modifier 2", 1)
    changeModifierPosition(bpy.context.object, "Modifier 3", 2)
    


run()