3

With Blender 2.8 I have created a complex object that I wanted to split in two seperate objects.

The procedure I followed (all scripted): Create object; duplicate object; go to edit mode and bisect with '''clear_inner=True'''. (works Perfect!) Then select other (original) object; go to edit mode and bisect with '''clear_outer=True'''. Now the first object seems also to have been subject to the bisect: Only some points/faces were the bisect is remain.

I am including the code for a simple cube:

import bpy

bpy.ops.mesh.primitive_cube_add(size=4, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name="left"    

bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'})  
bpy.context.object.name="right"    

# cutting in two (bisect)

bpy.data.objects['left'].select_set(False)
bpy.data.objects['right'].select_set(True)

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.bisect(plane_co=(0, 28.5249, 5.80484), plane_no=(1, 0, 0), use_fill=True, clear_inner=True, threshold=0.0001, xstart=1042, xend=1068, ystart=647, yend=130)
bpy.ops.object.editmode_toggle()

bpy.data.objects['right'].select_set(False)
bpy.data.objects['left'].select_set(True)

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.bisect(plane_co=(0, 28.5249, 5.80484), plane_no=(1, 0, 0), use_fill=True, clear_outer=True, threshold=0.0001, xstart=1042, xend=1068, ystart=647, yend=130)
bpy.ops.object.editmode_toggle()

using bisect to seperate cube in two halfs

In the picture you see that the result of the second bisect succesfully halved the first cube ('left'). But it also splitted the duplictated cube('right') which was already halved resulting only in a face at the bisect plane.

Why doesn't it work? What am I doing wrong?

Odvkram
  • 33
  • 3

1 Answers1

1

The problem is caused by second bpy.ops.mesh.select_all(action='SELECT'). None of those is needed because the whole mesh is selected until you change that. This cause seleciton of mesh of both cubes and then aplying bisect on both of them. This is possible because Blender 2.80 allows you to edit multiple objects at once. Source of the problem is select_set(False) though. This will deselect the object but it does not change its active status. When you toggle edit mode for the second cube you are editing both because one is selected and second is active. You can try that by removing last 3 lines of your code and you will see what is going on. You have to change that by setting bpy.context.view_layer.objects.active as seen in second code.

Also you do not need to fill all parameters of bpy.ops.mesh.bisect function and plane_co is point on the cutting plane which I think should be zero but that is up to you.

Documentation: https://docs.blender.org/api/current/bpy.ops.mesh.html?highlight=mesh#bpy.ops.mesh.bisect

Edited code using bpy.ops.object.select_all(action='DESELECT'):

...

# deselect all objects
bpy.ops.object.select_all(action='DESELECT')

# select just right object
bpy.data.objects['right'].select_set(True)

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.bisect(plane_co=(0, 0, 0), plane_no=(1, 0, 0), use_fill=True, clear_inner=True)
bpy.ops.object.editmode_toggle()

# deselect all objects
bpy.ops.object.select_all(action='DESELECT')

# select just left object
bpy.data.objects['left'].select_set(True)

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.bisect(plane_co=(0, 0, 0), plane_no=(1, 0, 0), use_fill=True, clear_outer=True)
bpy.ops.object.editmode_toggle()

This one also works - using bpy.context.view_layer.objects.active:

...

bpy.data.objects['left'].select_set(False)
bpy.data.objects['right'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['right'] # set right as active

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.bisect(plane_co=(0, 0, 0), plane_no=(1, 0, 0), use_fill=True, clear_inner=True)
bpy.ops.object.editmode_toggle()

bpy.data.objects['right'].select_set(False)
bpy.data.objects['left'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['left'] # set left as active

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.bisect(plane_co=(0, 0, 0), plane_no=(1, 0, 0), use_fill=True, clear_outer=True)
bpy.ops.object.editmode_toggle()
Jan Husák
  • 331
  • 3
  • 12
  • Thanks Jan! That is indeed solving my problem. Now I know that the problem is in the active vs selected objects, I should be able to search the docs on that too. Very helpfull! – Odvkram Jan 02 '20 at 15:09