Reply To: Quick tip. Pin modifiers

Forum Archive Quick tip. Pin modifiers Reply To: Quick tip. Pin modifiers

#99361
Rikard Jespersen
Participant

    It can be done with a custom script at least. But I have no idea how to make it so that it works for “ctrl+1/2/3”. In my example below, it will register as a separate script with a shortcut. I set it to default to “ctrl+6 (which is unused I think). This way at least, it is just a shortcut to make sure it is put last, no need to check the modifier list.

    As I’m not a coder I have no idea how to actually make this script part of my installation … but there are script coding courses here on cgcookie, so perhaps I need to take a sidestep and check those, before continuing with my rigging journey :D. So much to learn, everything so exciting!

    (Edit: I found out I just had to save the script as a .py file and then install it as if it were a real addon. It’s actually quite handy to have :D)

    I have tested this on my machine, nothing blew up so it should be safe but I can’t make ANY guarantees what so ever. My advise, read through the code before you run it 🙂

    The script is intended to do the following.

    1. Check if one or more valid objects are selected
    2. Checks if there is a subsurf modifier present. (If there isn’t, one is created for each valid object)
    3. The subsurf modifier is moved to the bottom of each stack (for each selected object)
    4. The modifier is pinned to last in each stack

    —–

    bl_info = {
        "name": "Pin Subdivision Modifier to Last",
        "blender": (5, 0, 1),
        "category": "Object",
        "version": (0, 1),
        "author": "Carramone",
        "description": "Pins the subdivision modifier to the last position in the modifier stack for selected mesh objects.",
    }
    
    import bpy
    
    
    def pin_subdivision_modifier_to_last(obj):
        subdivision_modifier = None
        for mod in obj.modifiers:
            if mod.type == 'SUBSURF':
                subdivision_modifier = mod
                break
        if not subdivision_modifier:
            subdivision_modifier = obj.modifiers.new(name="Subdivision", type='SUBSURF')
        while obj.modifiers[-1] != subdivision_modifier:
            bpy.ops.object.modifier_move_down(modifier=subdivision_modifier.name)
        subdivision_modifier.use_pin_to_last = True
    
    
    class OBJECT_OT_pin_subdivision(bpy.types.Operator):
        bl_idname = "object.pin_subdivision_modifier"
        bl_label = "Pin Subdivision Modifier to Last"
        bl_options = {'REGISTER', 'UNDO'}
    
    
        def execute(self, context):
            selected_objects = context.selected_objects
            valid_objects = [obj for obj in selected_objects if obj.type == 'MESH']
            if not valid_objects:
                self.report({'WARNING'}, "No valid mesh objects selected.")
                return {'CANCELLED'}
            for obj in valid_objects:
                pin_subdivision_modifier_to_last(obj)
            self.report({'INFO'}, "Subdivision modifier pinned to last for selected objects.")
            return {'FINISHED'}
    
    addon_keymaps = []
    
    
    def register():
        bpy.utils.register_class(OBJECT_OT_pin_subdivision)
        wm = bpy.context.window_manager
        km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
        kmi = km.keymap_items.new(OBJECT_OT_pin_subdivision.bl_idname, type='SIX', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))
    
    
    def unregister():
        bpy.utils.unregister_class(OBJECT_OT_pin_subdivision)
        for km, kmi in addon_keymaps:
            km.keymap_items.remove(kmi)
        addon_keymaps.clear()
    
    if __name__ == "__main__":
        register()