Page 1 of 1

boolean operations within python scripts

Posted: Mon Jul 24, 2006 12:25 pm
by pouchon
Dear group,

I am rather new to the topic, so please excuse my triavial question.

I can't figure out how to perform boolean operations (union, intersection, difference) on my primitives (cubes,...) from a python script. I have seen a script project wich address these boolean operations, but as I understood, the aim was to increase performance. And I would like to use the standard blender boolean tools.

Thanks a lot
Manuel

Posted: Tue Jul 25, 2006 2:33 am
by ideasman
Have a look at the new modifiers Module in Blender 2.42

Posted: Tue Jul 25, 2006 11:49 am
by pouchon
...
ok, thanks a lot.
I think I start to understand the concept.
My code now looks like this:

--------------------------------------------------
from Blender import *

ob = Object.Get('Cube') # retrieve 1st cube
obj = Object.Get('Cube.001') # retrieve 2nd cube
mods = ob.modifiers # get the object's modifiers
mod = mods.append(Modifier.Type.BOOLEAN) # add boolean modifier
mod[Modifier.Settings.OBJECT] = obj # defines the object to operate
--------------------------------------------------

So I have initially two cubes, I add a modifyier (BOOLEAN) to the first one, and state the second one for making the boolean operation.
My only problem remaining is: how can I specify, that I want to do the "Difference" operation, and apply it?

Thank a lot for the help!
Manuel

Posted: Wed Jul 26, 2006 4:34 pm
by ideasman
Crap. I forgot to add that before release :/

Posted: Thu Jul 27, 2006 8:03 am
by pouchon
...
ok, thanks for the information. So I will look for an alternative. Will it be implemented for the next release?

Thanks a lot
Manuel

Posted: Thu May 29, 2008 2:36 pm
by jcak77
ideasman wrote:Crap. I forgot to add that before release :/
Is this ok now? How can I apply a boolean operation on two meshes in python?

Thanks!

Posted: Sat Jul 26, 2008 4:25 pm
by strsizemela
ideasman wrote:Crap. I forgot to add that before release :/
I have seen the script for the intersections and it works. Can someone tell me whether the other boolean operations (union, difference) are also working and how to code it.

Thanks a lot for your help

Boolean union operation example script -- but: colors lost!!

Posted: Thu Jan 29, 2009 1:41 am
by bam93
strsizemela wrote:Can someone tell me whether the other boolean operations (union, difference) are also working and how to code it.
Hi, below you find a script that does a union operation. This is defined via

Code: Select all

Modifier.Settings.OPERATION
I still have a problem with the script: it loses color information! Which is not the case if I use a boolean union via the GUI. Easy to test: create two IcoSpheres of different color and check for yourself. Any help greatly appreciated.

The idea of the script is to "post-process" mesh data generated by an external application as primitives (eg overlapping spheres etc.) and generate a single "BREP"-type mesh from this data. I guess some mesh-refinement/cleanup/optimisation after the union would be nice, too.. So any suggestions also welcome!

Code: Select all

import bpy
import Blender
from Blender import *
from Blender.Draw import *

def applyUnion(obj1, obj2):
	scn = Blender.Scene.GetCurrent()                            # get current scene 
	mod = obj1.modifiers.append(Blender.Modifier.Type.BOOLEAN)  # add boolean modifier 
	mod[Modifier.Settings.OBJECT] = obj2                        # defines the object to operate
	mod[Modifier.Settings.OPERATION] = 1                        # defines the operation
	obj1.makeDisplayList()                                      # Needed to apply the modifier
	Blender.Window.RedrawAll()                                  # View the change
	m = Mesh.New()
	m.getFromObject(obj1.name)
	obj1.modifiers.remove(mod)
	obj1.link(m)

flag=0
scn = bpy.data.scenes.active

for ob in bpy.data.scenes.active.objects:
	if ( ob.type == 'Mesh' ) :
		if (flag==1):
			unionmesh = ob
			print "Making union of first mesh and"
			print unionmesh.name
			applyUnion(firstmesh,unionmesh)
			scn.unlink(unionmesh)
		else:
			print "First mesh used is"
			print ob.name
			firstmesh = ob
			flag=1

Re: Boolean union operation example script -- but: colors lo

Posted: Thu Jan 29, 2009 9:10 pm
by bam93
bam93 wrote:I still have a problem with the script: it loses color information! Which is not the case if I use a boolean union via the GUI. Easy to test: create two IcoSpheres of different color and check for yourself. Any help greatly appreciated.
Adding to my own question, actually this seems to be a known problem/bug/feature as can be seen here: http://www.blender.org/forum/viewtopic.php?t=13642
It concerns the material in general.

Is there any known workaround?

multi boolean without colors

Posted: Tue Jun 11, 2013 5:34 pm
by milkyandy
Hi...

maby a bit late :D

...heres my solution to union-boolean many objects. The Color is only one, so it is only one object finally.

thought...

Take care! With many objects, it neeeeeeeeds time :roll:

Code: Select all


#!BPY

"""
Name: 'Muli Boolean Union'
Blender: 242
Group: 'Misc'
Tooltip: 'trying'
"""

from Blender import *
from Blender import Draw, Scene, Object
from Blender.Mathutils import Rand

.# runs through all objects and boolean:union them together
.def supb(scn, sel):
.  i = 0
.  for objss in sel:
.    if i == 0:
.      objFirst = objss
.      i = 1
.    else:	# i == 1
.      if i == 1:
.        objSecond = objss
.        i = 2
	      # adding modifier  
.        mods = objFirst.modifiers            # adding a modifier
.        mod = mods.append(Modifier.Types.BOOLEAN)	# with boolean
.        mod[Modifier.Settings.OPERATION] = 1	# and union
			      
				      # doing the modifier
.        mod[Modifier.Settings.OBJECT] = objSecond	# adding the element to the modifier
.        objFirst.makeDisplayList() 	# doing the modifier
				      
				      # deleting the old two
#				      scn.unlink( objFirst )
.        scn.unlink( objSecond )
.      else:
.        i = 3
.    Window.RedrawAll() #??


.def main():
.  scn = Scene.GetCurrent()
.  if not scn.objects.context:
.    return
	
.  sel = scn.objects.context
.  objects = [ob for ob in scn.objects.context]
.  Count = len(objects)
.  f = Count

.  for i in range(0, f):
.    supb( scn, sel )
.    sel = scn.objects.context
	    
.  Window.RedrawAll() 

	
.if __name__ == '__main__':
.  main()



have fun
[quote][/quote]