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

From Wiki.OSArch
(add snippet to read obj_storey)
(add filter for IfcBuildingStorey)
Line 49: Line 49:
 
import bpy
 
import bpy
  
obj_storey = bpy.context.active_object.users_collection
+
users_collections = bpy.context.active_object.users_collection
 +
obj_storey = next(iter([coll for coll in users_collections if "IfcBuildingStorey" in coll.name]))
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 09:29, 3 December 2020

Run a script in blender from command line

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

Import an IFC

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

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)

Read object IFC data

Examples affect active object only.

Element Storey

import bpy

users_collections = bpy.context.active_object.users_collection
obj_storey = next(iter([coll for coll in users_collections if "IfcBuildingStorey" in coll.name]))

Manipulate object IFC data

Examples affect active object only.

PropertySet (Pset)

import bpy
# Adding a Pset
pset = bpy.context.active_object.BIMObjectProperties.psets.add()
pset.name = "MyPset"
# Adding a property of type string_value
prop = pset.properties.add()
prop.name = "MyProperty"
prop.string_value = "Hello World !"

Available value types

QuantityTakeOff (Qto)

import bpy
# Adding a Qto
qto = bpy.context.active_object.BIMObjectProperties.qtos.add()
qto.name = "MyQto"
# Adding a property of type string_value
prop = qto.properties.add()
prop.name = "MyQuantity"
prop.string_value = "10"

Manipulate material IFC data

PropertySet (Pset)

import bpy
# Adding a Pset
pset = bpy.context.object.active_material.BIMMaterialProperties.psets.add()
pset.name = "MyPset"
# Adding a property of type string_value
prop = pset.properties.add()
prop.name = "MyProperty"
prop.string_value = "Hello World !"