Difference between revisions of "BlenderBIM Add-on/BlenderBIM Add-on code examples"

From Wiki.OSArch
Line 1: Line 1:
 +
Blender can run any Python script headlessly. This allows for a lot of automation, which may run on a scheduled time. On Linux or Mac, you can combine this with cron, or use scheduled tasks on Windows. For example, to run the script <code>script.py</code>, just execute:
 +
 +
<syntaxhighlight lang="bash">
 +
blender -b -P script.py
 +
</syntaxhighlight>
 +
 
To import an IFC:
 
To import an IFC:
  

Revision as of 01:57, 7 September 2020

Blender can run any Python script headlessly. This allows for a lot of automation, which may run on a scheduled time. On Linux or Mac, you can combine this with cron, or use scheduled tasks on Windows. For example, to run the script script.py, just execute:

blender -b -P script.py

To import an IFC:

bpy.ops.import_ifc.bim(filepath='/path/to/your/file.ifc')

To export an IFC:

bpy.ops.export_ifc.bim(filepath='/path/to/your/file.ifc')

You can also bypass the operator to import or export yourself. This gives you granularity to instantiate the IFC import classes, export classes, quantity calculator class, logging class, settings, and so on. This is useful if you are writing your own script and would like to customise how the import or export works. You are also able to overload the definition of these classes to provide even more control.

To import an IFC:

import bpy
import logging
import blenderbim.bim.import_ifc
ifc_import_settings = blenderbim.bim.import_ifc.IfcImportSettings.factory(bpy.context, '/home/dion/testa.ifc', logging.getLogger('ImportIFC'))
ifc_importer = blenderbim.bim.import_ifc.IfcImporter(ifc_import_settings)
ifc_importer.execute()

To export an IFC:

import bpy
import logging
import blenderbim.bim.export_ifc
import blenderbim.bim.qto
settings = blenderbim.bim.export_ifc.IfcExportSettings.factory(bpy.context, '/home/dion/testa.ifc', logging.getLogger('ExportIFC'))
qto_calculator = blenderbim.bim.qto.QtoCalculator()
ifc_parser = blenderbim.bim.export_ifc.IfcParser(settings, qto_calculator)
ifc_exporter = blenderbim.bim.export_ifc.IfcExporter(settings, ifc_parser)
ifc_exporter.export(bpy.context.selected_objects)