I need a script that adds a new modifier in-between two other modifiers by name.
import bpy
bpy.context.scene.objects.active = bpy.data.objects[‘Suzanne’]
bpy.ops.object.modifier_add(type=‘BOOLEAN’)
bpy.context.object.modifiers[“Boolean”].object = bpy.data.objects[‘Sphere’]
bpy.context.object.modifiers[“Boolean”].operation = ‘INTERSECT’
bpy.ops.object.modifier_apply(apply_as=‘DATA’, modifier=‘Boolean’)
import bpy
bpy.context.scene.objects.active = bpy.data.objects[‘Suzanne’]
bpy.ops.object.modifier_add(type=‘BOOLEAN’)
bpy.context.object.modifiers[“Boolean”].object = bpy.data.objects[‘Sphere’]
bpy.context.object.modifiers[“Boolean”].operation = ‘INTERSECT’
bpy.ops.object.modifier_apply(apply_as=‘DATA’, modifier=‘Boolean’)
How is this script supposed to help me achieve my goal?
Apologies Im on small device and can’t type a full script right now. I usually count the number of places I need to move and use a for loop to move it up. The only problem is that this uses an operator instead of manipulating blender data directly so if the object is complex and the modifiers are computationally expensive then it can be quite slow because I think it recalculates the whole mesh and updates the scene each time. I haven’t tried turning all modifiers off, moving them around, then turning them back on.
-Best
Patrick
Try this!
def insert_new_mod_below(obj, mod_name, new_mod_type = 'SMOOTH'):
'''
obj - blender object
mod_name - existing modifier to add new modifier below
new_mod_type - type of modifier to add
'''
if mod_name not in obj.modifiers:
print('error, this modifier is not in this object mod stack')
return None
count = 0
for i, mod in enumerate(obj.modifiers):
if mod.name == mod_name:
count = i
break
N = len(obj.modifiers)
move_up = N - count - 1 #number of slots to move the modifier up
bpy.ops.object.select_all(action = 'DESELECT')
bpy.context.scene.objects.active = obj
obj.select = True
obj.hide = False
mod = obj.modifiers.new('New Mod', type = new_mod_type)
for n in range(move_up):
bpy.ops.object.modifier_move_up(modifier = mod.name)
return mod