0
$\begingroup$

Using Blender with cell fracture I have created a code that should select the objects created by cell fracture and give them the collision attribute and it does but only one out of a 1000. So it doesn't really work but then I am a newbie to Blender and Python so what I want it to do is select one object give it collision and move on to the next object created by cell fracture and give it collision and do this until it is done.

By the way I am using Blender version 2.69

This is the code that I have so far written and I do not know why it is does not increment to the next part.

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

migrated from stackoverflow.com Mar 24 '14 at 20:00

This question came from our site for professional and enthusiast programmers.

3
$\begingroup$

First thing - you loop through all objects and then run bpy.ops.object.select_pattern() which will change your selection. It would be better to run a loop as for obj in bpy.context.selected_objects: Which will let you select the objects you want the script to alter.

The operator bpy.ops.rigidbody.object_settings_copy() will copy the rigid body settings from the active object to the rest of the selected objects. You don't need to write a script to do this as it is already available. Press T to show the toolbar and under Rigid body tools you will find a button to Copy from Active.

enter image description here

There is an addon called Copy Attributes Menu that you can enable and it will Allow you to copy modifiers from the active object to selected. Unfortunately it doesn't copy the collision settings. You can copy the collision settings individually by right clicking on a value and selecting Copy To Selected. This can be done with many properties.

enter image description here

If you still want to write a script you can start with something like -

import bpy

for obj in bpy.context.selected_objects:
    obj.modifiers.new(type='COLLISION', name='collision')
    obj.collision.permeability = 0.3
    obj.collision.stickiness = 0.25

For a collision modifier, you can only have one so always using modifier.new() is ok, no matter how many times you run the script. With other modifiers you may want to test whether one already exists before creating a new one.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy