How to apply a modifier using pyrhon


(Peter Rabbit) #1

Hi. I’m writing a script which removes overhangs from molds, as this prevents cast parts from demolding. My script works by:

  • Iterating through the mold mesh (a cube in my test scene).
  • Detecting every upward facing polygon.
  • Creating a prism (column) from that polygon which stretches up to a precalculated ceiling
  • Unifying those prisms into a single object (the mold negative)
  • Then subtracting the mold negative from the mold.

I’ve written pretty much every part of that code, and it’s working more or less. I’m currently stuck on the last part, Applying the booleans I’ve created. I’ve read a few tutorials and I’ve coded the process they recommended:

  • Switching out to object mode
  • Deselecting all, or deselecting the cube
  • Setting the mold negative (object with the boolean modifier) to active
  • Applying the modifier
  • Deselecting all, or deselecting the mold negative
  • Setting the cube object to active
  • Re-entering edit mode

So far my code is getting hung up on deselecting all. I’ve tried various approaches, but nothing seems to be able to convince blender to deselect that cube (my test mold object) and move on to the mold negative object. The only thing I haven’t tried is selecting them by name, but I’d like to avoid this as it could break my code under some circumstances.

Here’s the relevant code block from my script:

        #Create prism
        prismObject = makePrism(moldObject.data.polygons[index], draftAngle, direction, zBoundary)
        
        #Create new union boolean between mold negative and prism
        bool = moldNegativeObject.modifiers.new ("Bool", type="BOOLEAN")
        bool.object = prismObject
        bool.operation = "UNION"
        bool.solver = "BMESH"
        bool.double_threshold = 0.000001
        
        #Switch to object mode
        bpy.ops.object.mode_set(mode='OBJECT')
        
        #Select moldNegativeObject
        bpy.ops.object.select_all(action='DESELECT')
        moldNegativeObject.select = True
        
        #Apply modifier
        bpy.ops.object.modifier_apply (modifier = "Bool")
        
        #Reselect mold
        bpy.ops.object.select_all(action='DESELECT')
        moldObject.select = True
        
        #Return to edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        #Delete prism
        #bpy.data.objects.remove(prismObject, True)


(Peter Rabbit) #2

So I found the solution to this. The problem was that there’s a distinction between selecting objects and making them the active object (which is what is needed to apply modifiers). The working code block is below:

#Create prism
        prismObject = makePrism(moldObject.data.polygons[index], draftAngle, direction, zBoundary)
        
        #Create new union boolean between mold negative and prism
        bool = moldNegativeObject.modifiers.new ("Bool", type="BOOLEAN")
        bool.object = prismObject
        bool.operation = "UNION"
        bool.solver = "BMESH"
        bool.double_threshold = 0.000001
        
        #Switch to object mode
        bpy.ops.object.mode_set(mode='OBJECT')
        
        #Select moldNegativeObject
        bpy.context.scene.objects.active = moldNegativeObject
        
        #Apply modifier
        bpy.ops.object.modifier_apply (modifier = "Bool")
        
        #Reselect mold
        bpy.context.scene.objects.active = moldObject
        
        #Return to edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        #Delete prism
        bpy.data.objects.remove(prismObject, True)