Help with this script

So I’ve been trying to apply all hook modifiers to active object, but only from the hook-empties that are selected, so I do this by iterating through hook modifiers and checking if the names from selected objects match any hook objects, and if they do, apply that modifier as shapekey, but something is not right… need some help.

Here is the code:

ob = bpy.context.active_object

    


for mod in ob.modifiers:
    
    for i in range(0<10):# or len(ob.modifiers)
        if ob.modifiers[i].name.startswith("Hook"):
            selected = bpy.context.selected_objects
            
            
            if ob.modifiers[i].object == bpy.data.objects[selected[i].name]:

                modifier= ob.modifiers[i].name
                bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier=modifier)


Is this with the assumption that all the modifiers will be hook modifiers?

I actually have a displace modifier as well, so even if I put “if mod.type== ‘HOOK’” it somehow still counts the displace modifier and returns as an error saying the attribute “object” doesn’t exist. Any way around this? Big thanks for your response.

Trying to apply them by index number will give you problems anyway, since once you apply one (unless its last) it will change all the indexes.
First, lets get the hook modifiers:


#returns list of hook modifier names
hook_modifiers = [modifier.name for modifier in ob.modifiers if modifier.type == 'HOOK'] 

Then let’s get the corresponding selected objects:


#returns a list of selected objects' names if they appear in hook_modifier list
selected_hooks = [hook.name for hook in bpy.context.selected_objects if hook.name in hook_modifiers]

Then apply:


#apply modifiers from selected_hooks list
for mod in selected_hooks:
    bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier=mod)

Hmm, doesnt seem to work.


I’m trying to check if Object(“Springs”) in hook modifiers are in selected, & if they are, apply them as shapekey.

I have a sphere with hooks for each vertex. The sphere has a lot of hook modifiers, each of those hook modifiers has its own object value, which are the empties assigned to each modifier. When selecting the certain hooks(empties), I’d like to apply only the hook-modifiers corresponding the selected empties.

Here is another script that sort of works, but continue to get an error.

import bpy

ob = bpy.context.active_object

    


for mod in ob.modifiers:
    #mods = [if modifier.type == 'HOOK']
    for i in range(0 < len(ob.modifiers)) :
        if ob.modifiers[i].type == 'HOOK':


            selected = bpy.context.selected_objects[i]
            springs = selected.name
            #selected_springs= springs
            
            if bpy.data.objects[springs] == mod.object:
                
                modifier= mod.name
                bpy.ops.object.modifier_apply(apply_as='SHAPE', modifier=modifier)
        else:
            pass  

I figured it out, looks like you can use ‘mod’ instead of iterating through all modifiers.
Big thanks for the help btw, it definitely lead to a solution.