Help with adding modifiers to multiple objects

Hi,

I have a scene with 700 objects, all starting with the word “Sphere”. I would like to add a collision modifier to all of the spheres using python. It would literally take me all day to apply the collision modifier to all of these objects manually.

I have tried this using python and I can’t get the script to loop through all the objects, it only works on the active selected object. I have selected multiple objects, and it still only works on the last selected object. I tried Ctrl-L too, and it doesnt copy the modifier to the other selected objects. Grrr.

How can I code python to apply the modifier, then search for the next object starting with the word “Sphere”, apply then modifier, and continue doing this until the last found object? Like a loop, or batch run.

  • find first object in scene starting with “Sphere”
    - apply collision modifier
  • find next object in scene starting with “Sphere”
    - apply collision modifier
  • continue this script until the last object is found starting with “Sphere”
  • when done, print “finally! this code saved you from clicking your mouse 1400 times!!!”

Thanks!

  • cswita

I think this will do what you want:

import bpy
for i in bpy.data.objects:
    if i.name.startswith("Sphere"):
        # Select object:
        bpy.ops.object.select_name(name=i.name)
        # Add modifier:
        bpy.ops.object.modifier_add(type='COLLISION')
print("finally! this code saved you from clicking your mouse 1400 times!!!")

Hi BrikBot,

Thank you 1400 times!!!

This is what I was doing, which DOES NOT WORK


import bpy

for obj in bpy.context.scene.objects:
    if obj.name.startswith('Sphere'):
        bpy.ops.object.modifier_add(type='COLLISION')

Thank so much!

  • cswita

No problem. Happy Blending :smiley:

Hi all,
My problem could be similar.
I have copied a Mesh Deform Modifier from one source-object to multiple target-objects (through the Copy Attributes Add-on) but the “cage” is unbound in all of them.
Has someone ever written a script to hit the “bind” button on selected objects in the mesh deform modifier?
Thanks for your help

There doesn’t seem to be an RNA method, so you rely on the meshdeform_bind operator (which also unbinds if already bound):

#ob = bpy.data.objects['your_object']
#mod = ob.modifiers['MeshDeform']

context = bpy.context.copy()
context['object'] = ob
context['modifier'] = mod
bpy.ops.object.meshdeform_bind(context, modifier=mod.name)

The above code builds the right context, there is no need to make the object active. Not entirely sure about the context membe “modifier”, it worked without for me.

Thanks CoDEmanX

Knowing nothing about scripts, I may have a silly question:
since there is no need to select the object, the script will apply to all objects in the file that have the modifier (even if I don’t want some of the object to be unbound) ?

No, it operates on the object with name ‘your_object’ only. But you could loop over an arbitrary set of objects.

By chance is there any additional ways to complete this as there have been blender updates… or still the python code. (my objects have different names…)

BrikBot’s objects have different names too, not sure what you mean?

CoDemanX - My project “organization” should have been better - But with duplication of objects, deleting, misnaming — i ened up with oligo001.002 and GoodOligo03.02 and finaloligo10.040 all sorta referencing the same objects. some with different keyframes or other ajustments - perhaps different layers also. I really know on my next project I will make a ‘object name’ list and stick to it! This project has been a longer one and I just get different names — now its sorta a mess with 1000+ objects. But a question for you, is there a way to rename all visible objects on a layer? I think if i could do that, then there would again be some direction in the name process my .blend.

Sure, there should even be a few addons to do that.

If you want a custom script, here’s a very basic starting point:

// is object currently visible?
for ob in bpy.context.visible_objects:
    // ignore objects other than mesh
    if ob.type == 'MESH':
        // give them the name "NewName" (Blender will automatically append .###)
        ob.name = "NewName"

Hey guys :slight_smile:

I tried using the first script in this Thread,

import bpy
for i in bpy.data.objects:
if i.name.startswith(“Sphere”):
# Select object:
bpy.ops.object.select_name(name=i.name)
# Add modifier:
bpy.ops.object.modifier_add(type=‘COLLISION’)
print(“finally! this code saved you from clicking your mouse 1400 times!!!”)

and it worked. BUT I want to have all the objects to have the colision modifier, not only the rigid body modifier, which somehow doesnt work with my cloth sim. I can not apply the Collision modifier to my selection of objects (which is no problem to select, since I have it on a seperate layer)
Thus, I can not just tell Python to add the modifier via script. This only adds it to one object.

Can you somehow see what I’m trying to tell you with all this babbling? :smiley:

Thanks in advance

-NEMO

I have faced same problem. Exploding mesh (200 pcs) to blow through smoke. Copying modifiers, attributes… all works… just not for Physics modifiers (collision, fluid, smoke…)??? Or how?