mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-10-02 13:04:49 +00:00
IfcBlender: Ensure correct matrices on nested parenting
This commit is contained in:
@@ -59,28 +59,15 @@ bpy.types.Object.ifc_type = StringProperty(name="IFC Entity Type",
|
|||||||
description="The STEP Datatype keyword")
|
description="The STEP Datatype keyword")
|
||||||
|
|
||||||
|
|
||||||
class ImportIFC(bpy.types.Operator, ImportHelper):
|
def import_ifc(filename, use_names, process_relations):
|
||||||
bl_idname = "import_scene.ifc"
|
|
||||||
bl_label = "Import .ifc file"
|
|
||||||
|
|
||||||
filename_ext = ".ifc"
|
|
||||||
filter_glob = StringProperty(default="*.ifc", options={'HIDDEN'})
|
|
||||||
|
|
||||||
use_names = BoolProperty(name="Use entity names",
|
|
||||||
description="Use entity names rather than GlobalIds for objects",
|
|
||||||
default=True)
|
|
||||||
process_relations = BoolProperty(name="Process relations",
|
|
||||||
description="Convert containment and aggregation relations to parenting",
|
|
||||||
default=True)
|
|
||||||
|
|
||||||
def execute(self, context):
|
|
||||||
from . import IfcImport
|
from . import IfcImport
|
||||||
if not IfcImport.Init(self.filepath):
|
if not IfcImport.Init(filename):
|
||||||
return {'FINISHED'}
|
return
|
||||||
id_to_object = {}
|
id_to_object = {}
|
||||||
id_to_parent = {}
|
id_to_parent = {}
|
||||||
|
id_to_matrix = {}
|
||||||
old_progress = -1
|
old_progress = -1
|
||||||
print ("Creating geometry...")
|
print("Creating geometry...")
|
||||||
while True:
|
while True:
|
||||||
ob = IfcImport.Get()
|
ob = IfcImport.Get()
|
||||||
if ob.type != 'IfcOpeningElement':
|
if ob.type != 'IfcOpeningElement':
|
||||||
@@ -88,7 +75,7 @@ class ImportIFC(bpy.types.Operator, ImportHelper):
|
|||||||
v = ob.mesh.verts
|
v = ob.mesh.verts
|
||||||
m = ob.matrix
|
m = ob.matrix
|
||||||
t = ob.type[0:21]
|
t = ob.type[0:21]
|
||||||
nm = ob.name if len(ob.name) and self.use_names else ob.guid
|
nm = ob.name if len(ob.name) and use_names else ob.guid
|
||||||
|
|
||||||
verts = [[v[i], v[i + 1], v[i + 2]] \
|
verts = [[v[i], v[i + 1], v[i + 2]] \
|
||||||
for i in range(0, len(v), 3)]
|
for i in range(0, len(v), 3)]
|
||||||
@@ -105,10 +92,14 @@ class ImportIFC(bpy.types.Operator, ImportHelper):
|
|||||||
me.materials.append(mat)
|
me.materials.append(mat)
|
||||||
|
|
||||||
bob = bpy.data.objects.new(nm, me)
|
bob = bpy.data.objects.new(nm, me)
|
||||||
bob.matrix_world = mathutils.Matrix(([m[0], m[1], m[2], 0],
|
mat = mathutils.Matrix(([m[0], m[1], m[2], 0],
|
||||||
[m[3], m[4], m[5], 0],
|
[m[3], m[4], m[5], 0],
|
||||||
[m[6], m[7], m[8], 0],
|
[m[6], m[7], m[8], 0],
|
||||||
[m[9], m[10], m[11], 1]))
|
[m[9], m[10], m[11], 1]))
|
||||||
|
if process_relations:
|
||||||
|
id_to_matrix[ob.id] = mat
|
||||||
|
else:
|
||||||
|
bob.matrix_world = mat
|
||||||
bpy.context.scene.objects.link(bob)
|
bpy.context.scene.objects.link(bob)
|
||||||
|
|
||||||
bpy.ops.object.select_all(action='DESELECT')
|
bpy.ops.object.select_all(action='DESELECT')
|
||||||
@@ -127,15 +118,17 @@ class ImportIFC(bpy.types.Operator, ImportHelper):
|
|||||||
id_to_parent[ob.id] = ob.parent_id
|
id_to_parent[ob.id] = ob.parent_id
|
||||||
progress = IfcImport.Progress() // 2
|
progress = IfcImport.Progress() // 2
|
||||||
if progress > old_progress:
|
if progress > old_progress:
|
||||||
print ("\r[" + "=" * progress + " " * (50 - progress) + "]",end="")
|
print("\r[" + "#" * progress + " " * (50 - progress) + "]", end="")
|
||||||
old_progress = progress
|
old_progress = progress
|
||||||
if not IfcImport.Next():
|
if not IfcImport.Next():
|
||||||
break
|
break
|
||||||
|
|
||||||
print ("\rDone creating geometry"+" "*30)
|
print("\rDone creating geometry" + " " * 30)
|
||||||
|
|
||||||
while len(id_to_parent) and self.process_relations:
|
id_to_parent_temp = dict(id_to_parent)
|
||||||
id, parent_id = id_to_parent.popitem()
|
|
||||||
|
while len(id_to_parent_temp) and process_relations:
|
||||||
|
id, parent_id = id_to_parent_temp.popitem()
|
||||||
|
|
||||||
if parent_id in id_to_object:
|
if parent_id in id_to_object:
|
||||||
bob = id_to_object[parent_id]
|
bob = id_to_object[parent_id]
|
||||||
@@ -145,10 +138,11 @@ class ImportIFC(bpy.types.Operator, ImportHelper):
|
|||||||
bob = None
|
bob = None
|
||||||
else:
|
else:
|
||||||
m = parent_ob.matrix
|
m = parent_ob.matrix
|
||||||
nm = parent_ob.name if len(parent_ob.name) and self.use_names \
|
nm = parent_ob.name if len(parent_ob.name) and use_names \
|
||||||
else parent_ob.guid
|
else parent_ob.guid
|
||||||
bob = bpy.data.objects.new(nm, None)
|
bob = bpy.data.objects.new(nm, None)
|
||||||
bob.matrix_world = mathutils.Matrix(([m[0], m[1], m[2], 0],
|
id_to_matrix[parent_ob.id] = mathutils.Matrix((
|
||||||
|
[m[0], m[1], m[2], 0],
|
||||||
[m[3], m[4], m[5], 0],
|
[m[3], m[4], m[5], 0],
|
||||||
[m[6], m[7], m[8], 0],
|
[m[6], m[7], m[8], 0],
|
||||||
[m[9], m[10], m[11], 1]))
|
[m[9], m[10], m[11], 1]))
|
||||||
@@ -161,13 +155,43 @@ class ImportIFC(bpy.types.Operator, ImportHelper):
|
|||||||
|
|
||||||
if parent_ob.parent_id > 0:
|
if parent_ob.parent_id > 0:
|
||||||
id_to_parent[parent_id] = parent_ob.parent_id
|
id_to_parent[parent_id] = parent_ob.parent_id
|
||||||
|
id_to_parent_temp[parent_id] = parent_ob.parent_id
|
||||||
id_to_object[parent_id] = bob
|
id_to_object[parent_id] = bob
|
||||||
if bob:
|
if bob:
|
||||||
ob = id_to_object[id]
|
ob = id_to_object[id]
|
||||||
ob.matrix_world = bob.matrix_world.inverted() * ob.matrix_world
|
|
||||||
ob.parent = bob
|
ob.parent = bob
|
||||||
|
|
||||||
|
id_to_matrix_temp = dict(id_to_matrix)
|
||||||
|
|
||||||
|
while len(id_to_matrix_temp):
|
||||||
|
id, matrix = id_to_matrix_temp.popitem()
|
||||||
|
parent_id = id_to_parent.get(id, None)
|
||||||
|
parent_matrix = id_to_matrix.get(parent_id, None)
|
||||||
|
if parent_matrix:
|
||||||
|
id_to_object[id].matrix_local = parent_matrix.inverted() * matrix
|
||||||
|
else:
|
||||||
|
id_to_object[id].matrix_world = matrix
|
||||||
|
|
||||||
IfcImport.CleanUp()
|
IfcImport.CleanUp()
|
||||||
|
|
||||||
|
|
||||||
|
class ImportIFC(bpy.types.Operator, ImportHelper):
|
||||||
|
bl_idname = "import_scene.ifc"
|
||||||
|
bl_label = "Import .ifc file"
|
||||||
|
|
||||||
|
filename_ext = ".ifc"
|
||||||
|
filter_glob = StringProperty(default="*.ifc", options={'HIDDEN'})
|
||||||
|
|
||||||
|
use_names = BoolProperty(name="Use entity names",
|
||||||
|
description="Use entity names rather than GlobalIds for objects",
|
||||||
|
default=True)
|
||||||
|
process_relations = BoolProperty(name="Process relations",
|
||||||
|
description="Convert containment and aggregation" \
|
||||||
|
" relations to parenting",
|
||||||
|
default=True)
|
||||||
|
|
||||||
|
def execute(self, context):
|
||||||
|
import_ifc(self.filepath, self.use_names, self.process_relations)
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user