2
$\begingroup$

I wrote a script that imports and modifys two objects. It works as following: The script imports an .obj and makes some modifactions. The position of the object stays the same. After the modifications, the same .obj is imported again. Then the last imported object is selected and Boolean/Difference/Object1 used.

The Boolean works without problems, when I run the script till the object is imported again and I select the this object by hand and do the Bool.

The script runs till bpy.context.object.modifiers["Boolean"].object = bpy.data.objects[NameWOFormat].

I understand that I have a problem with my selection. There is a small Modifier symbol next to the Name "Object1" in the Outliner Window. If it would run as it should, the symbol would be behind "Object1.001". That's the reason I think it's a selection problem.

Am I using the wrong command for the selection?

import bpy
import os
import bmesh
from pathlib import Path
from mathutils import Vector, kdtree, Matrix

NameWOFormat ="Object1"

# Modifier Boolean
def ModDif():
    global NameWoFormat
    global NameSubstrObject
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.modifier_add(type='BOOLEAN')
    bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
    bpy.context.object.modifiers["Boolean"].object = bpy.data.objects[NameWOFormat]
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")

bpy.ops.import_scene.obj(C:/Object1.obj)

#===============================================================================
# Now I do some modifications on Object1. I save this block of code here.
#===============================================================================

# Change into Object-Mode
bpy.ops.object.mode_set(mode='OBJECT')

# Import Object1 again and use Boolean
bpy.ops.import_scene.obj(C:/Object1.obj)
NameSubstrObject = NameWOFormat + ".001"

# Do Boolean edit
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[NameSubstrObject].select = True
ModDif()
$\endgroup$
  • $\begingroup$ Can you post a screenshot of your modifier panel after running this script? $\endgroup$ – Rick Riggs Aug 29 '16 at 14:54
  • $\begingroup$ i.imgur.com/De7FdP2.jpg $\endgroup$ – Anokino Aug 29 '16 at 15:16
2
$\begingroup$

You can add modifiers without using operators or relying on what is selected.

import bpy

# this creates Object1
bpy.ops.import_scene.obj(filepath='Object1.obj')
# this creates Object1.001
bpy.ops.import_scene.obj(filepath='Object1.obj')

obj = bpy.data.objects['Object1.001']
m = obj.modifiers.new(name='cutting modified obj',type='BOOLEAN')
m.object = bpy.data.objects['Object1']
m.operation = 'INTERSECT'
$\endgroup$
  • $\begingroup$ This works for me. Thank you. One more question: When the Boolean prints the Error "Cannot execute boolean operation", is there a log where i can find out what the problem is? $\endgroup$ – Anokino Aug 30 '16 at 12:34
  • 1
    $\begingroup$ @Anokino you have to run blender in terminal/command line, script output and errors will be printed there. $\endgroup$ – Noidea Aug 30 '16 at 13:15
1
$\begingroup$

Operator is applied to the active object. There is always one active object in a scene, so deselect doesn't clear it and Object1 stays active. Although active object is the last selected object, call to select doesn't affect it.

You need to either set active object:

bpy.context.scene.objects.active =  bpy.data.objects[NameSubstrObject]

or create a global variable

myObject = bpy.data.objects[NameSubstrObject]

and use it instead of bpy.ops.object

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.