Deleting objects after boolean difference in python

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

techmoose007
Posts: 5
Joined: Mon Mar 16, 2009 5:36 pm
Location: Colorado, USA

Deleting objects after boolean difference in python

Postby techmoose007 » Tue Jul 10, 2012 8:03 pm

I have a puzzler that is leaving me stumped. :?: The following is a snipet of code I am writing in Blender 2.63.13-r48794 with Python:

Code: Select all

 # Search comments for tyre flags
            if tyre_string in comments[ (each_element - 1) ]:
                print(comments[ (each_element - 1) ] + comments[ each_element ] + "\n")
                ascii_num += 1
               
                # Set width of tyre
                depth = position_m[ (each_element) ] - position_m[ (each_element - 1) ]
               
                # Generate inner diameter cylinder for boolean difference
                bpy.ops.mesh.primitive_cyli:?: nder_add(vertices=32, radius=Rad, depth=(depth + 1.0),
                 end_fill_type='NGON', view_align=False, enter_editmode=False,
                 location=(0, ((position_m[ each_element ] + position_m[each_element - 1])/2.0), 0),
                 rotation=((pi/2),0,0), layers=(True, False, False, False, False, False, False, False,
                 False, False, False, False, False, False, False, False, False, False, False, False))
               
                # Assign active object to a variable for later identification
                cyl1 = bpy.context.active_object
                cyl1_name = bpy.context.active_object.name
               
                # Diagnostic print to console
                print(cyl1)
               
                # Generate outer diameter cylinder for boolean difference
                bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=(Rad + (Rad * 0.50
                )),
                 depth=(depth), end_fill_type='NGON', view_align=False, enter_editmode=False,
                 location=(0, ((position_m[ each_element ] + position_m[ (each_element - 1) ])/2.0), 0),
                 rotation=((pi/2.0),0,0), layers=(True, False, False, False, False, False, False, False,
                 False, False, False, False, False, False, False, False, False, False, False, False))
               
                # Assign active object to a variable for later identification
                cyl2 = bpy.context.active_object
               
                # Diagnostic print
                print(cyl2)
               
                # Apply material to outer tyre
                bpy.context.object.data.materials.append(tyre_mat)
               
                # Conduct boolean difference to generate tyre
                bpy.ops.object.select_all(action='DESELECT')
                # Set active object to outer cylinder
                bpy.context.scene.objects.active=cyl2
                # Create modifier
                bpy.ops.object.modifier_add(type='BOOLEAN')
                # Select modifier object to inner diameter cylinder
                cyl2.modifiers[0].object = cyl1
                # Set operation
                cyl2.modifiers[0].operation = 'DIFFERENCE'
                # Conduct boolean
                bpy.ops.object.modifier_apply(apply_as='DATA', modifier=cyl2.modifiers[0].name)
               
                # Remove inner diameter cylinder after boolean
                bpy.ops.object.select_all(action='DESELECT')
                # Select inner diameter cylinder as active
                bpy.context.scene.objects.active=cyl1
               
                # Diagnostic information for console
                temp=bpy.context.active_object.name
                print("Active object is: " + temp)
               
                # Delete active object
                bpy.ops.object.delete(use_global=True)
               
            else:
                print ('Tyre ID not found at %i \n' % (each_element))




This code builds a piece of machinery from data in a CSV file. The "tyres" are constructed from a boolean difference of two cylinders. The goal is to create an automated script to generate this machinery and its animation without user intervention. The user starts the tool from the panel, selects their input file, and Blender does the rest. My problem is that the object deletion is not working within the script itself.


I can delete the inner cylinder within the interface in the 3D View context. I can assign the inner cylinder to variable in the console, make it active, and then run the bpy.ops.object.delete(use_global=True) and the object is deleted. In the script, however, I cannot seem to get it to work. I receive no errors on the console...the boolean happens correctly, but the inner cylinders still exist.


How may I delete these inner cylinders from within the python script? Am I calling the wrong delete operation? What additional information may you all need from me to look into this?

Thanks,
-- Techmoose007
"Freedom to create is truly living free."

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Tue Jul 10, 2012 8:47 pm

you deselect everything, then you make the inner cylinder active:

bpy.ops.object.select_all(action='DESELECT')
bpy.context.scene.objects.active=cyl1

but you don't select it!

there is a difference between selected and active object, operators mostly require a selection

so you should select cyl1 rather than making it active object:

bpy.ops.object.select_all(action='DESELECT')
cyl.select = True
# delete
I'm sitting, waiting, wishing, building Blender in superstition...

techmoose007
Posts: 5
Joined: Mon Mar 16, 2009 5:36 pm
Location: Colorado, USA

Potential Fix

Postby techmoose007 » Tue Jul 10, 2012 8:50 pm

I found a way to get the results I want, but I am not sure this is an acceptable method.

After the delete operation:

Code: Select all

 bpy.ops.object.delete(use_global=True)


I unlinked the objects from the scene:

Code: Select all

 bpy.context.scene.objects.unlink(cyl1)


I was under the assumption that the use_global flag on the delete was to remove the object from all scenes AND unlink...but I can see why that may not be the actual behavior...
-- Techmoose007

"Freedom to create is truly living free."

techmoose007
Posts: 5
Joined: Mon Mar 16, 2009 5:36 pm
Location: Colorado, USA

Postby techmoose007 » Tue Jul 10, 2012 8:53 pm

CoDEmanX wrote:you deselect everything, then you make the inner cylinder active:


there is a difference between selected and active object, operators mostly require a selection

so you should select cyl1 rather than making it active object:

bpy.ops.object.select_all(action='DESELECT')
cyl.select = True
# delete


Thanks...I was not aware the mouse actions in 3D view selected and made the object active upon click... I like this approach more than the unlink I did earlier.
-- Techmoose007

"Freedom to create is truly living free."


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest