0
$\begingroup$

This is what i want to do (out rolled example):

#mod_idx = '' or '.001' or '.002' or '.003'

mod_id    = global_prefs.mod_string                                         
mod_id_01 = global_prefs.mod_string + '.001'                                        
mod_id_02 = global_prefs.mod_string + '.002'                                        
mod_id_03 = global_prefs.mod_string + '.003'                                        
mod_id_04 = global_prefs.mod_string + '.004'                                        
mod_id_05 = global_prefs.mod_string + '.005'                                        
mod_id_06 = global_prefs.mod_string + '.006'                                        
mod_id_07 = global_prefs.mod_string + '.007'                                                                            

custom_id    = bpy.context.object.modifiers.get(mod_id)                                    
custom_id_02 = bpy.context.object.modifiers.get(mod_id_02)                                    
custom_id_03 = bpy.context.object.modifiers.get(mod_id_03)                                    
custom_id_04 = bpy.context.object.modifiers.get(mod_id_04)                                    
custom_id_05 = bpy.context.object.modifiers.get(mod_id_05)                                    
custom_id_06 = bpy.context.object.modifiers.get(mod_id_06)                                    
custom_id_07 = bpy.context.object.modifiers.get(mod_id_07)                                    

if custom_id or custom_id_01 or custom_id_02 or custom_id_03 or custom_id_04 or custom_id_05 or custom_id_06 or custom_id_07 :                                             
                        name = mod_id or mod_id_01 or mod_id_02 or mod_id_03 or mod_id_04 or mod_id_05 or mod_id_06 or mod_id_07  

My goal is to adjust all existing modifier with the same prefix.

To adjust them like a group and ignore all other existing ones!

Any hints how to do that in a meaningful short way?

        selected = bpy.context.selected_objects 
        for obj in selected:
            if global_prefs.mod_string != '':

                mod_id = global_prefs.mod_string                                                            
                custom_id = bpy.context.object.modifiers.get(mod_id)                                                                                
                if custom_id:                                             
                    name = mod_id

                    context = bpy.context.copy()
                    context['object'] = obj    

                    for mod in obj.modifiers: 
                        context['modifier'] = mod                                                  

                    for obj in selected: 
                        view_layer.objects.active = obj 

                        if self.mod_processing == "RENDER" or global_prefs.mod_processing == "RENDER":                                                        
                            if mod.show_render == True:                         
                                bpy.context.object.modifiers[name].show_render = False
                            else:
                                bpy.context.object.modifiers[name].show_render = True   

This works so far for a single modifier! Link to the complete blender addon: modifier by type

Thaughts:

                i = 0
                pref = global_prefs.mod_string
                while obj.modifiers.get(pref):
                    name_id = pref + '.' + '001'    
                    i += 1      

This is not working! Blender hangs up!

Next:

                i = 0
                idx = '.00'                                        
                pref = global_prefs.mod_string
                name = "%s%s%d" % (pref, idx, i)
                while obj.modifiers.get(pref):  
                    i += 1                  
                    name = "%s%s%d" % (pref, idx, i)

This does not stop at the last number....

$\endgroup$
8
  • 1
    $\begingroup$ Unclear what you're asking based on your pseudo-code. Just renaming all the modifiers? $\endgroup$
    – brockmann
    Jan 26, 2020 at 21:04
  • $\begingroup$ Related blender.stackexchange.com/a/125098/15543 $\endgroup$
    – batFINGER
    Jan 27, 2020 at 7:54
  • $\begingroup$ @batFINGER > thank you for the link! The examples shows how to create new modifier with custom id suffix: like 1,2,3 But i don´t want to create new modifier! Just finding the prefix and ignore the numberDigits *.001.. to adjsut the existing ones! $\endgroup$
    – MKBreuer
    Jan 27, 2020 at 18:13
  • $\begingroup$ What exactly do you want to adjust? Properties of each modifier based on the type or based on which other criteria? @MKBreuer $\endgroup$
    – brockmann
    Jan 27, 2020 at 18:20
  • $\begingroup$ @MKBreuer Your example cannot work since the condition of the while loop doesn't change and always remains true, thereby creating an infinite loop that freezes Blender. I've added an example to my answer that shows how to iterate through the modifiers. If you want to access them by name then you should create a loop around the string creation and use a single .get() within that loop. $\endgroup$
    – Robert Gützkow
    Jan 27, 2020 at 19:27

1 Answer 1

3
$\begingroup$

There are (at least) two ways to approach this, either you can differentiate the modifiers through their name or their type.

Filter by Prefix

If you want to filter them by a prefix you could use the following approach to retrieve references to the modifiers.

First, get the modifier collection of the object. All (key, value) pairs can be accessed through the .items() function. The keys are the names of the modifiers, therefore these have to be checked for a prefix match. Python has the .startswith() function to check if a string starts with a given prefix. If a match is found for a key, the corresponding value is one of the modifiers you're searching for. All of this can be combined to a list comprehension as shown below.

import bpy


prefix = "Subdiv"
modifier_collection = bpy.context.object.modifiers 
modifier_matches = [modifier for key, modifier in modifier_collection.items() if key.startswith(prefix)]

print(modifier_matches)

Filter by Type

Sometimes you may not know the name of the modifiers, since these are user editable, but you may know their type. Searching for modifiers by their type can be accomplished by getting all modifiers of the object through .values() and then checking if they are of a certain type, e.g. SUBSURF in the example below.

import bpy


modifiers = bpy.context.object.modifiers.values()
modifier_matches = [modifier for modifier in modifiers if modifier.type == "SUBSURF"]

print(modifier_matches)

The example below shows how to iterate through all modifiers of an object, check if its name starts with a given prefix and if it does, disable it for rendering by setting show_render to False.

import bpy


for key, modifier in bpy.context.object.modifiers.items():
    if key.startswith("Subdiv"):
        modifier.show_render = False
$\endgroup$
5
  • $\begingroup$ Thank you for the help. I tried to use it, but it is not possible to adjust several modifier by list! $\endgroup$
    – MKBreuer
    Jan 27, 2020 at 18:22
  • 1
    $\begingroup$ @MKBreuer You can simply iterate over the list and adjust the parameters of each modifier. If you have a specific example of what you're trying to adjust I can add it to the answer as well. $\endgroup$
    – Robert Gützkow
    Jan 27, 2020 at 18:26
  • 1
    $\begingroup$ In case you're only adjusting the modifiers once and then don't need a reference to them anymore, you could remove the list comprehension and simply iterate over the modifiers and perform your adjustments. $\endgroup$
    – Robert Gützkow
    Jan 27, 2020 at 18:32
  • $\begingroup$ I added more example to the first post! $\endgroup$
    – MKBreuer
    Jan 27, 2020 at 18:43
  • $\begingroup$ Your example works for fantastic! Thanks a lot! It founds all custom prefix and adjust them! $\endgroup$
    – MKBreuer
    Jan 27, 2020 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .