I’m quite new to python and coding in general but a recent project has made me realize that I really need to start learning. Basically, I’ve made this low-poly-styled protoplanet by making an icosphere (with subdivisions set to 6) and adding a decimate modifier to make look low-poly, then I used the Vronoi Cell Fracture tool to fracture it into 3,168 pieces. Then I used the ‘random selection’ tool and scaled the fragments several times to achieve this fragmented look. However this meant the fractures intersect with each other, and as I need to be able to convert the fragments to rigid-bodies so I can smash it I will need to correct all the intersections or else it just explodes.
So I came up with the idea of making a script that would make every selected object add a Boolean modifier (set to ‘Difference’) to every other selected object, and apply them as it goes. Then the meshes could be joined to be re-fractured with the Cell Fracture tool, or just converted straight to rigid bodies.
Obviously as there are 3,168 fragments it would need to do this 10,036,224 times (which will obviously take a while), but if the script could detect combinations that have already occurred (e.g. fragment 1 has already Boolean-ed with fragment 2, so fragment 2 doesn’t need to do it again with fragment 1) and/or detect which fragments are intersecting with which, then it could be significantly optimized (though this may over-complicate it).
I’ve written little macro scripts before, but I have no idea how to write this one. If anyone could help me write this or suggest a better method of doing it, that would be much appreciated
P.S. Here’s the link to a blender file containing just the fractured surface of the protoplanet if you want to take a look at the meshes: http://www.pasteall.org/blend/27289
Writing bigger scripts is a lot like writing smaller scripts, you just have to take each step and break it down until you have individual actions.
For example, you already know what you want the script to do:
Go though all the objects
For each object, add a boolean modifier to all the other objects
Apply the modifier
Do you know how to iterate all the objects in your file?
Do you know how to add a boolean modifier to the active object?
Do you know how to change which object is the active object?
Do you know how to apply a modifer?
That’s all you really have to know to make the script work as described. I suspect that you will have to do some optimizations to get this to run without crashing blender. Doing a few million boolean modifiers sounds…intensive. For speed purposes you might have to start limiting the problem set. For example, only do boolean modifiers against objects that intersect the bounding box or sphere for the current object.
Thanks for the reply (breaking it down like that definitely helps!), and the answers are No, yes, no, yes :P. I can add and apply Boolean modifiers to specific objects with a script, and I can make it add a Boolean modifier to every selected object and make them target a specific object, but I don’t know how to make every object add Boolean modifiers that target every other one (minus themselves), applying them as they go so Blender doesn’t have a heart attack.
However, whilst doing some research I did find this piece of code someone wrote to help another person wanting to do a similar thing: http://blender.stackexchange.com/questions/5752/simplify-mesh-by-cutting-out-inside-parts
(The line “if target.name != object.name:” looks like it could solve my issue with objects targeting themselves with the Boolean modifier)
This script looks like it does almost exactly what I am trying to do, and with a couple of minor modifications this script looks like it could work pretty well (namely changing ‘particles’ to something else, and using the ‘Difference’ mode instead of ‘Union’, and adding the line of code to apply the modifiers) but it might be a bit slow.
In theory I think the script could be more stable and more organised if instead of trying to do everything at once it should apply the modifiers one at a time. For example, it could select the first object, add a Boolean modifier to it, target the second object with the Boolean modifier and apply it, then add another Boolean modifier targeted at the third object and apply it etc. until all the objects (minus the first) have had Boolean modifiers applied to them. Then it will go back and select the second object, and repeat the process until it has finished. I think this might be possible with lists, or commands where objects can be selected by number, but I’ll need to try it and see.
Once I have access to Blender again I’ll have a go and try putting something together and getting it to work, then I’ll see if I can optimize it. Hopefully I’ll eventually be able to allow the script to detect intersecting objects too, but I should make something that works first
That script does look like a good starting place. To answer the questions that you don’t know how to do:
To iterate all the objects in your .blend
import bpy
for obj in bpy.data.objects:
# Gets called once for each object in your scene
# Each loop though 'obj' will point to a different object
# in your scene.
So the most simply code loop to do what you want looks something like this:
import bpy
for obj in bpy.data.objects:
# Called once for each object, you want to apply
# a boolean mod for all the other objects... so you need
# another loop
for target in bpy.data.objects:
# First you don't want to do a boolean against the 'obj'
if obj == target:
continue
# Add the modifier to 'obj' subtracting 'target'
# Then apply the modifer on 'obj'.
That’s about all you really have to do. I agree that you probably want to apply each modifier as you go. I doubt Blender would like have millions of boolean modifiers processing on each other…