ana maria
ana maria

Reputation: 373

Running Blender python script outside of blender error message calculate_object_volumes.poll() failed, context is incorrect

I try to run a python script outside blender headless via blender -b --python import_ifc_model.py command

I have installed Blender 4.2.1, the add-on Bonsai (new name before it was blenderbim) and the idea is to import an ifc file and calculate the volume of all the objects. (source code https://github.com/IfcOpenShell/IfcOpenShell/blob/f0502c123ea61c5574a7cb0d8e293afc94c4ec1e/src/blenderbim/blenderbim/bim/module/qto/operator.py#L73)

import time
import bpy
import ifcopenshell
import ifcopenshell.api
import bonsai.tool as tool
import bonsai.core.qto as core
from bonsai.bim.ifc import IfcStore
from bonsai.bim.module.qto import helper
from bonsai.bim.module.qto import operator

# blender -b --python import_ifc_model.py

bpy.ops.bim.load_project(filepath="E:\\model.ifc", use_relative_path=False, should_start_fresh_session=True)

time.sleep(30) 
bpy.ops.bim.load_project_elements() 
bpy.ops.object.select_all(action='DESELECT')
# go to 'edit' mode >>> bpy.ops.object.editmode_toggle()
#contexte_scene = bpy.ops.object.select_all(action='SELECT')

#result = helper.calculate_volumes([o for o in bpy.context.selected_objects if o.type == "MESH"], bpy.context)
#print(str(round(result, 3)))  
#operator.CalculateObjectVolumes(bpy.types.Operator)
#bpy.ops.bim.calculate_object_volumes()

contexte_scene = bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active
#bpy.context.active_object.data
for o in bpy.context.selected_objects:
#for idx in range(len(bpy.data.objects)):
    o.select_set(True)
    #bpy.context.scene.objects.active = bpy.data.objects[idx]
bpy.ops.bim.calculate_object_volumes()

When running I have the following error:

 Traceback (most recent call last):   File
 "C:\Users\Downloads\blender-4.2.1-windows-x64\import_ifc_model.py",
 line 33, in <module>
     bpy.ops.bim.calculate_object_volumes()   File "C:\Users\Downloads\blender-4.2.1-windows-x64\4.2\scripts\modules\bpy\ops.py",
 line 109, in __call__
     ret = _op_call(self.idname_py(), kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Operator bpy.ops.bim.calculate_object_volumes.poll() failed, context is
 incorrect

Do you have an idea how to fix it?

Upvotes: 1

Views: 95

Answers (0)

Related Questions