tool.Geometry method for adding a data to an empty object

This commit is contained in:
Andrej730
2024-07-03 12:48:35 +05:00
parent e72f2758c6
commit 3f2703ca21
4 changed files with 21 additions and 12 deletions
+1 -1
View File
@@ -400,7 +400,7 @@ class Geometry:
def record_object_position(cls, obj): pass def record_object_position(cls, obj): pass
def remove_connection(cls, connection): pass def remove_connection(cls, connection): pass
def rename_object(cls, obj, name): pass def rename_object(cls, obj, name): pass
def replace_object_with_empty(cls, obj): pass def recreate_object_with_data(cls, obj, data): pass
def replace_object_data_globally(cls, old_data, new_data): pass def replace_object_data_globally(cls, old_data, new_data): pass
def resolve_mapped_representation(cls, representation): pass def resolve_mapped_representation(cls, representation): pass
def run_geometry_update_representation(cls, obj=None): pass def run_geometry_update_representation(cls, obj=None): pass
+14 -6
View File
@@ -690,13 +690,20 @@ class Geometry(blenderbim.core.tool.Geometry):
obj.name = name obj.name = name
@classmethod @classmethod
def replace_object_with_empty(cls, obj: bpy.types.Object) -> None: def recreate_object_with_data(cls, obj: bpy.types.Object, data: Union[bpy.types.ID, None]) -> bpy.types.Object:
"""Recreate a Blender object as an empty object. """Recreate a Blender object with the provided `data`.
This method is useful when an object should no longer have associated This method is useful when an object should no longer have associated
data (in Blender, you cannot simply assign .data to None). Note that the data (in Blender, you cannot simply assign .data to None).
object's original data is not handled by this method and should be Or if object is an empty and should now have a data.
The object's original data is not handled by this method and should be
processed separately to avoid leaving orphan data. processed separately to avoid leaving orphan data.
Original `obj` is deleted and becomes invalid and should be replaced
with an object returned by this method.
:return: The newly recreated object.
""" """
element = tool.Ifc.get_entity(obj) element = tool.Ifc.get_entity(obj)
name = obj.name name = obj.name
@@ -704,7 +711,7 @@ class Geometry(blenderbim.core.tool.Geometry):
tool.Ifc.unlink(element=element) tool.Ifc.unlink(element=element)
obj.name = ifcopenshell.guid.new() obj.name = ifcopenshell.guid.new()
new_obj = bpy.data.objects.new(name, None) new_obj = bpy.data.objects.new(name, data)
if element: if element:
tool.Ifc.link(element, new_obj) tool.Ifc.link(element, new_obj)
@@ -712,6 +719,7 @@ class Geometry(blenderbim.core.tool.Geometry):
collection.objects.link(new_obj) collection.objects.link(new_obj)
new_obj.matrix_world = obj.matrix_world new_obj.matrix_world = obj.matrix_world
bpy.data.objects.remove(obj) bpy.data.objects.remove(obj)
return new_obj
@classmethod @classmethod
def resolve_mapped_representation( def resolve_mapped_representation(
@@ -885,7 +893,7 @@ class Geometry(blenderbim.core.tool.Geometry):
# `representation` is the only representation for object. # `representation` is the only representation for object.
if new_representation is None: if new_representation is None:
cls.replace_object_with_empty(obj) cls.recreate_object_with_data(obj, None)
return return
blenderbim.core.geometry.switch_representation( blenderbim.core.geometry.switch_representation(
+4 -4
View File
@@ -321,9 +321,9 @@ class TestRemoveRepresentation:
geometry.has_data_users("data").should_be_called().will_return(True) geometry.has_data_users("data").should_be_called().will_return(True)
geometry.get_elements_of_type("type").should_be_called().will_return(["element"]) geometry.get_elements_of_type("type").should_be_called().will_return(["element"])
ifc.get_object("element").should_be_called().will_return("obj") ifc.get_object("element").should_be_called().will_return("obj")
geometry.replace_object_with_empty("obj").should_be_called() geometry.recreate_object_with_data("obj").should_be_called()
ifc.get_object("type").should_be_called().will_return("type_obj") ifc.get_object("type").should_be_called().will_return("type_obj")
geometry.replace_object_with_empty("type_obj").should_be_called() geometry.recreate_object_with_data("type_obj").should_be_called()
ifc.run("geometry.unassign_representation", product="type", representation="representation").should_be_called() ifc.run("geometry.unassign_representation", product="type", representation="representation").should_be_called()
ifc.run("geometry.remove_representation", representation="representation").should_be_called() ifc.run("geometry.remove_representation", representation="representation").should_be_called()
geometry.delete_data("data").should_be_called() geometry.delete_data("data").should_be_called()
@@ -344,7 +344,7 @@ class TestRemoveRepresentation:
geometry.get_element_type("element").should_be_called().will_return(None) geometry.get_element_type("element").should_be_called().will_return(None)
geometry.get_representation_data("representation").should_be_called().will_return("data") geometry.get_representation_data("representation").should_be_called().will_return("data")
geometry.has_data_users("data").should_be_called().will_return(True) geometry.has_data_users("data").should_be_called().will_return(True)
geometry.replace_object_with_empty("obj").should_be_called() geometry.recreate_object_with_data("obj").should_be_called()
ifc.run( ifc.run(
"geometry.unassign_representation", product="element", representation="representation" "geometry.unassign_representation", product="element", representation="representation"
).should_be_called() ).should_be_called()
@@ -359,7 +359,7 @@ class TestRemoveRepresentation:
geometry.is_type_product("element").should_be_called().will_return(False) geometry.is_type_product("element").should_be_called().will_return(False)
geometry.get_representation_data("representation").should_be_called().will_return("data") geometry.get_representation_data("representation").should_be_called().will_return("data")
geometry.has_data_users("data").should_be_called().will_return(True) geometry.has_data_users("data").should_be_called().will_return(True)
geometry.replace_object_with_empty("obj").should_be_called() geometry.recreate_object_with_data("obj").should_be_called()
ifc.run( ifc.run(
"geometry.unassign_representation", product="element", representation="representation" "geometry.unassign_representation", product="element", representation="representation"
).should_be_called() ).should_be_called()
+2 -1
View File
@@ -375,6 +375,7 @@ class TestRenameObject(NewFile):
assert obj.name == "name" assert obj.name == "name"
# TODO: add a test
class TestReplaceObjectWithEmpty(NewFile): class TestReplaceObjectWithEmpty(NewFile):
def test_run(self): def test_run(self):
ifc = ifcopenshell.file() ifc = ifcopenshell.file()
@@ -384,7 +385,7 @@ class TestReplaceObjectWithEmpty(NewFile):
bpy.context.scene.collection.objects.link(obj) bpy.context.scene.collection.objects.link(obj)
element = ifc.createIfcWall() element = ifc.createIfcWall()
tool.Ifc.link(element, obj) tool.Ifc.link(element, obj)
subject.replace_object_with_empty(obj) subject.recreate_object_with_data(obj, None)
obj = bpy.data.objects.get("Object") obj = bpy.data.objects.get("Object")
assert obj.users_collection[0] == bpy.context.scene.collection assert obj.users_collection[0] == bpy.context.scene.collection
assert tool.Ifc.get_entity(obj) == element assert tool.Ifc.get_entity(obj) == element