16
$\begingroup$

I am trying to set up an operator to use the BoolTool after I set a position and modifier on Selected object and a different one on the Active object. I basically need to move the selected object up in Z and add a solidify modifier at .3 thickness, and to the Active I need to add a solidify at .1 thickness and remain in original position.

I thought I understood how to get this to work, but all I get is both modifiers on the Active object. Any hint as to where to go from here?

class SolidfyDifference(bpy.types.Operator):
"""Solidify and Difference Mask"""
bl_idname = "object.solidfy_difference"
bl_label = "Add Solidy and Difference Bool"
bl_options = { 'REGISTER','UNDO' }

def execute(self, context):
    scene = context.scene

    sel = bpy.context.selected_objects
    act = bpy.context.active_object



    for obj in sel:
        if obj != act:
            bpy.ops.object.modifier_add(type='SOLIDIFY')
            bpy.context.object.modifiers["Solidify"].thickness = 0.1



    return {'FINISHED'}
$\endgroup$

2 Answers 2

24
$\begingroup$

I'd suggest not using modifier add operator and use modifiers.new(name, type) instead. Avoids hassle of changing the active object and also if using the op to add a second modifier of the same type, would need to access it via obj.modifiers[-1] as it will be named "Solidify.001" or somesuch.

Result of Running Script on 2 default planes Result of Running Script on 2 default planes

import bpy
context = bpy.context

obj = context.active_object
# add a solidify modifier on active object
mod = obj.modifiers.new("Solidify", 'SOLIDIFY')
# set modifier properties
mod.thickness = 0.3
obj.location.z += 0.15
for o in context.selected_objects:
    if o == obj:
        continue
    # see if there is already a modifier named "SelectedSolidify" and use it
    mod = o.modifiers.get("SelectedSolidify")
    if mod is None:
        # otherwise add a modifier to selected object
        mod = o.modifiers.new("SelectedSolidify", 'SOLIDIFY')
    mod.thickness = 0.1
    o.location.z = 0.05
    # add a boolean mod
    boolmod = obj.modifiers.new("Bool", 'BOOLEAN')
    boolmod.object = o
    boolmod.solver = 'CARVE'
    boolmod.operation = 'DIFFERENCE'
$\endgroup$
2
  • $\begingroup$ Main purpose I am trying to achieve: two planes selected, one is active - add a solidify modifier to both, but the non-active needs to be at a different thickness so a boolean difference can be added as a modifier and return a solid result. $\endgroup$ Jul 26, 2016 at 20:26
  • 1
    $\begingroup$ @CraigDJones I added a boolean modifier to the active object that carves out the difference bool to each selected object. Produces an "empty sandwich" for two def planes at same location. Hardcoded in 0.3 and 0.1 etc, these could be passed to an operator as properties. $\endgroup$
    – batFINGER
    Jul 27, 2016 at 11:35
12
$\begingroup$

Try something like that :

sel = bpy.context.selected_objects
act = bpy.context.active_object

for obj in sel:
 if obj != act:
    bpy.context.view_layer.objects.active = obj #sets the obj accessible to bpy.ops
    bpy.ops.object.modifier_add(type='SOLIDIFY')
    bpy.context.object.modifiers["Solidify"].thickness = 0.1

bpy.context.view_layer.objects.active = act
$\endgroup$
2
  • $\begingroup$ That was pretty much the answer - by assigning the status of Active, I can swap to the other selection by naming it as active and giving it a new modifier with different settings. $\endgroup$ Jul 26, 2016 at 18:51
  • $\begingroup$ This option is not possible now with the change in API for post 2.8 series $\endgroup$ Mar 28, 2022 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .