Editing BlenderBIM Add-on/BlenderBIM Add-on code examples

From Wiki.OSArch

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
{{Start coding}}
+
To import an IFC:
 
 
==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 <code>script.py</code>, just execute:
 
 
 
<syntaxhighlight lang="bash">
 
blender -b -P script.py
 
</syntaxhighlight>
 
  
==Import an IFC==
 
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
bpy.ops.import_ifc.bim(filepath='/path/to/your/file.ifc')
 
bpy.ops.import_ifc.bim(filepath='/path/to/your/file.ifc')
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Export an IFC==
+
To export an IFC:
 +
 
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
bpy.ops.export_ifc.bim(filepath='/path/to/your/file.ifc')
 
bpy.ops.export_ifc.bim(filepath='/path/to/your/file.ifc')
Line 44: Line 37:
 
ifc_exporter.export(bpy.context.selected_objects)
 
ifc_exporter.export(bpy.context.selected_objects)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=Access IFC objects=
 
Access multiple objects of specific criteria.
 
==Access Elements==
 
<syntaxhighlight lang="python">
 
import bpy
 
 
walls = [obj for obj in bpy.context.scene.objects if obj.name.startswith("IfcWall")]
 
voids = [obj for obj in bpy.context.scene.objects if obj.name.startswith("IfcBuildingElementProxy/ProvisionForVoid")]
 
</syntaxhighlight>
 
 
=Read object IFC data=
 
Examples affect active object only.
 
 
==Element IFC Guid / GlobalId==
 
<syntaxhighlight lang="python">
 
import bpy
 
from blenderbim.bim.ifc import IfcStore
 
 
obj = bpy.context.active_object
 
IfcStore.get_file().by_id(obj.BIMObjectProperties.ifc_definition_id).GlobalId
 
</syntaxhighlight>
 
 
==Element Storey==
 
<syntaxhighlight lang="python">
 
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]))
 
</syntaxhighlight>
 
 
=Manipulate object IFC data=
 
Examples affect active object only.
 
==PropertySet (Pset)==
 
<syntaxhighlight lang="python">
 
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 !"
 
</syntaxhighlight>
 
[https://docs.blender.org/api/blender_python_api_current/bpy.props.html Available value types]
 
==QuantityTakeOff (Qto)==
 
<syntaxhighlight lang="python">
 
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"
 
</syntaxhighlight>
 
=Manipulate material IFC data=
 
==PropertySet (Pset)==
 
<syntaxhighlight lang="python">
 
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 !"
 
</syntaxhighlight>
 
 
=Clash Detection=
 
==Clash Detection on 2 sets of elements==
 
Clash Detection on 3 cubes in two sets in this example:
 
<syntaxhighlight lang="python">
 
import bpy
 
import collision
 
import numpy as np
 
import bmesh
 
import ifcclash
 
 
 
def get_collision_results(set_a=None, set_b=None):
 
    a_cm = collision.CollisionManager()
 
    b_cm = collision.CollisionManager()
 
    add_to_cm(a_cm, set_a)
 
    add_to_cm(b_cm, set_b)
 
    return a_cm.in_collision_other(b_cm, return_data=True)
 
 
 
def add_to_cm(cm, object_names):
 
    for object_name in object_names:
 
        name = object_name.name
 
        obj = bpy.data.objects[name]
 
        triangulated_mesh = triangulate_mesh(obj)
 
        mat = np.array(obj.matrix_world)
 
        mesh = ifcclash.Mesh()
 
        mesh.vertices = np.array([tuple(v.co) for v in triangulated_mesh.vertices])
 
        mesh.faces = np.array([tuple(p.vertices) for p in triangulated_mesh.polygons])
 
        cm.add_object(name, mesh, mat)
 
 
 
def triangulate_mesh(obj):
 
    mesh = obj.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh()
 
    bm = bmesh.new()
 
    bm.from_mesh(mesh)
 
    bmesh.ops.triangulate(bm, faces=bm.faces)
 
    bm.to_mesh(mesh)
 
    bm.free()
 
    del bm
 
    return mesh
 
 
 
set_a = [
 
    bpy.data.scenes['Scene'].objects['Cube'],
 
]
 
set_b = [
 
    bpy.data.scenes['Scene'].objects['Cube.001'],
 
    bpy.data.scenes['Scene'].objects['Cube.002'],
 
]
 
 
 
err, results = get_collision_results(set_a=set_a, set_b=set_b)
 
 
seen_pairs = set()
 
for result in results:
 
    result_pair = result.names
 
    result_names_str = str(result_pair)
 
    if result_names_str in seen_pairs:
 
        continue
 
    seen_pairs.add(str(result_pair))
 
    print(35 * "-")
 
    print(result_names_str)
 
</syntaxhighlight>
 
 
 
[[Category:Blender]] [[Category:BlenderBIM Add-on]]
 

Please note that all contributions to Wiki.OSArch are considered to be released under the Creative Commons Attribution-ShareAlike (see Wiki.OSArch:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Template used on this page: