Fix Ifc modifier error after #3393064 because of legacy context.object

For some reason in the scene properties context.object returns None even if an object is active  in the viewport, so ifc modifiers would randomly throw errors. AFAIK functionally they are supposed to point to the same thing, but active_object seems to be the superior one and context.object deprecated and kept for legacy reasons. Couldn't find dev quote but I'm sure I saw it at some point
This commit is contained in:
Gorgious56
2023-07-17 14:48:01 +02:00
parent 79e67c3f12
commit 7aab0ccfd7
12 changed files with 41 additions and 41 deletions
@@ -748,7 +748,7 @@ class AngleDecorator(BaseDecorator):
arcs_color = None arcs_color = None
edges_color = UNSPECIAL_ELEMENT_COLOR edges_color = UNSPECIAL_ELEMENT_COLOR
if context.object == obj and obj.data.is_editmode: if context.active_object == obj and obj.data.is_editmode:
arcs_color = context.preferences.addons["blenderbim"].preferences.decorator_color_special arcs_color = context.preferences.addons["blenderbim"].preferences.decorator_color_special
edges_color = None edges_color = None
@@ -961,7 +961,7 @@ class SvgWriter:
p0 = points_2d[1] + dir0 * angle_radius p0 = points_2d[1] + dir0 * angle_radius
p2 = points_2d[1] + dir1 * angle_radius p2 = points_2d[1] + dir1 * angle_radius
points_chunk = [view3d_utils.region_2d_to_origin_3d(region, region_3d, p) for p in [p0, p3, p2]] points_chunk = [view3d_utils.region_2d_to_origin_3d(region, region_3d, p) for p in [p0, p3, p2]]
# points = [p.co.xyz for p in bpy.context.object.data.splines[0].points[:3]] # points = [p.co.xyz for p in bpy.context.active_object.data.splines[0].points[:3]]
bm = bmesh.new() bm = bmesh.new()
bm.verts.index_update() bm.verts.index_update()
@@ -229,7 +229,7 @@ class AnnotationToolUI:
@classmethod @classmethod
def draw_edit_object_interface(cls, context): def draw_edit_object_interface(cls, context):
if DecoratorData.get_ifc_text_data(bpy.context.object): if DecoratorData.get_ifc_text_data(bpy.context.active_object):
add_layout_hotkey_operator(cls.layout, "Edit Text", "S_E", "") add_layout_hotkey_operator(cls.layout, "Edit Text", "S_E", "")
@classmethod @classmethod
@@ -306,10 +306,10 @@ class Hotkey(bpy.types.Operator, Operator):
create_annotation() create_annotation()
def hotkey_S_E(self): def hotkey_S_E(self):
if not bpy.context.object: if not bpy.context.active_object:
return return
if DecoratorData.get_ifc_text_data(bpy.context.object): if DecoratorData.get_ifc_text_data(bpy.context.active_object):
bpy.ops.bim.edit_text_popup() bpy.ops.bim.edit_text_popup()
def hotkey_S_G(self): def hotkey_S_G(self):
@@ -276,7 +276,7 @@ def create_bm_door_lining(bm, size: Vector, thickness: list, position: Vector =
def update_door_modifier_bmesh(context): def update_door_modifier_bmesh(context):
obj = context.object obj = context.active_object
props = obj.BIMDoorProperties props = obj.BIMDoorProperties
overall_width = props.overall_width overall_width = props.overall_width
@@ -456,7 +456,7 @@ def update_door_modifier_bmesh(context):
bmesh.ops.translate(bm, vec=V(0, lining_offset, 0), verts=lining_offset_verts) bmesh.ops.translate(bm, vec=V(0, lining_offset, 0), verts=lining_offset_verts)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001) bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bmesh.update_edit_mesh(obj.data) bmesh.update_edit_mesh(obj.data)
else: else:
bm.to_mesh(obj.data) bm.to_mesh(obj.data)
@@ -475,9 +475,9 @@ class BIM_OT_add_door(bpy.types.Operator, tool.Ifc.Operator):
self.report({"ERROR"}, "You need to start IFC project first to create a door.") self.report({"ERROR"}, "You need to start IFC project first to create a door.")
return {"CANCELLED"} return {"CANCELLED"}
if context.object is not None: if context.active_object is not None:
spawn_location = context.object.location.copy() spawn_location = context.active_object.location.copy()
context.object.select_set(False) context.active_object.select_set(False)
else: else:
spawn_location = bpy.context.scene.cursor.location.copy() spawn_location = bpy.context.scene.cursor.location.copy()
@@ -120,7 +120,7 @@ def update_railing_modifier_bmesh(context):
"""before using should make sure that Data contains up-to-date information. """before using should make sure that Data contains up-to-date information.
If BBIM Pset just changed should call refresh() before updating bmesh If BBIM Pset just changed should call refresh() before updating bmesh
""" """
obj = context.object obj = context.active_object
props = obj.BIMRailingProperties props = obj.BIMRailingProperties
# NOTE: using Data since bmesh update will hapen very often # NOTE: using Data since bmesh update will hapen very often
@@ -274,9 +274,9 @@ class BIM_OT_add_railing(bpy.types.Operator, tool.Ifc.Operator):
self.report({"ERROR"}, "You need to start IFC project first to create a railing.") self.report({"ERROR"}, "You need to start IFC project first to create a railing.")
return {"CANCELLED"} return {"CANCELLED"}
if context.object is not None: if context.active_object is not None:
spawn_location = context.object.location.copy() spawn_location = context.active_object.location.copy()
context.object.select_set(False) context.active_object.select_set(False)
else: else:
spawn_location = bpy.context.scene.cursor.location.copy() spawn_location = bpy.context.scene.cursor.location.copy()
@@ -442,7 +442,7 @@ class EnableEditingRailingPath(bpy.types.Operator, tool.Ifc.Operator):
props.is_editing_path = True props.is_editing_path = True
update_railing_modifier_bmesh(context) update_railing_modifier_bmesh(context)
if bpy.context.object.mode != "EDIT": if bpy.context.active_object.mode != "EDIT":
bpy.ops.object.mode_set(mode="EDIT") bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.wm.tool_set_by_id(tool.Blender.get_viewport_context(), name="bim.cad_tool") bpy.ops.wm.tool_set_by_id(tool.Blender.get_viewport_context(), name="bim.cad_tool")
ProfileDecorator.install(context, exit_edit_mode_callback=lambda: cancel_editing_railing_path(context)) ProfileDecorator.install(context, exit_edit_mode_callback=lambda: cancel_editing_railing_path(context))
@@ -457,7 +457,7 @@ def cancel_editing_railing_path(context):
props.is_editing_path = False props.is_editing_path = False
update_railing_modifier_bmesh(context) update_railing_modifier_bmesh(context)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
return {"FINISHED"} return {"FINISHED"}
@@ -492,7 +492,7 @@ class FinishEditingRailingPath(bpy.types.Operator, tool.Ifc.Operator):
# since we know that BBIM_Railing could have changed # since we know that BBIM_Railing could have changed
refresh() refresh()
update_railing_modifier_bmesh(context) update_railing_modifier_bmesh(context)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
update_railing_modifier_ifc_data(context) update_railing_modifier_ifc_data(context)
return {"FINISHED"} return {"FINISHED"}
@@ -424,7 +424,7 @@ def update_roof_modifier_bmesh(context):
"""before using should make sure that Data contains up-to-date information. """before using should make sure that Data contains up-to-date information.
If BBIM Pset just changed should call refresh() before updating bmesh If BBIM Pset just changed should call refresh() before updating bmesh
""" """
obj = context.object obj = context.active_object
props = obj.BIMRoofProperties props = obj.BIMRoofProperties
# NOTE: using Data since bmesh update will hapen very often # NOTE: using Data since bmesh update will hapen very often
@@ -506,9 +506,9 @@ class BIM_OT_add_roof(bpy.types.Operator, tool.Ifc.Operator):
self.report({"ERROR"}, "You need to start IFC project first to create a roof.") self.report({"ERROR"}, "You need to start IFC project first to create a roof.")
return {"CANCELLED"} return {"CANCELLED"}
if context.object is not None: if context.active_object is not None:
spawn_location = context.object.location.copy() spawn_location = context.active_object.location.copy()
context.object.select_set(False) context.active_object.select_set(False)
else: else:
spawn_location = bpy.context.scene.cursor.location.copy() spawn_location = bpy.context.scene.cursor.location.copy()
@@ -654,7 +654,7 @@ class EnableEditingRoofPath(bpy.types.Operator, tool.Ifc.Operator):
props.is_editing_path = True props.is_editing_path = True
update_roof_modifier_bmesh(context) update_roof_modifier_bmesh(context)
if bpy.context.object.mode != "EDIT": if bpy.context.active_object.mode != "EDIT":
bpy.ops.object.mode_set(mode="EDIT") bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.wm.tool_set_by_id(tool.Blender.get_viewport_context(), name="bim.cad_tool") bpy.ops.wm.tool_set_by_id(tool.Blender.get_viewport_context(), name="bim.cad_tool")
@@ -706,7 +706,7 @@ def cancel_editing_roof_path(context):
props.is_editing_path = False props.is_editing_path = False
update_roof_modifier_bmesh(context) update_roof_modifier_bmesh(context)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
return {"FINISHED"} return {"FINISHED"}
@@ -747,7 +747,7 @@ class FinishEditingRoofPath(bpy.types.Operator, tool.Ifc.Operator):
update_roof_modifier_bmesh(context) update_roof_modifier_bmesh(context)
update_roof_modifier_ifc_data(context) update_roof_modifier_ifc_data(context)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
update_roof_modifier_ifc_data(context) update_roof_modifier_ifc_data(context)
return {"FINISHED"} return {"FINISHED"}
@@ -789,7 +789,7 @@ class SetGableRoofEdgeAngle(bpy.types.Operator):
# tried to avoid bmesh with foreach_get and foreach_set # tried to avoid bmesh with foreach_get and foreach_set
# but in EDIT mode it's only possible to change attributes by working with bmesh # but in EDIT mode it's only possible to change attributes by working with bmesh
me = context.object.data me = context.active_object.data
bm = tool.Blender.get_bmesh_for_mesh(me) bm = tool.Blender.get_bmesh_for_mesh(me)
# check if attribute exists or create one # check if attribute exists or create one
@@ -215,7 +215,7 @@ def update_stair_modifier(context):
props_kwargs = obj.BIMStairProperties.get_props_kwargs() props_kwargs = obj.BIMStairProperties.get_props_kwargs()
vertices, edges, faces = generate_stair_2d_profile(**props_kwargs) vertices, edges, faces = generate_stair_2d_profile(**props_kwargs)
obj = context.object obj = context.active_object
bm = bmesh.new() bm = bmesh.new()
bm.verts.index_update() bm.verts.index_update()
bm.edges.index_update() bm.edges.index_update()
@@ -234,7 +234,7 @@ def update_stair_modifier(context):
translate_verts = [v for v in extruded["geom"] if isinstance(v, BMVert)] translate_verts = [v for v in extruded["geom"] if isinstance(v, BMVert)]
bmesh.ops.translate(bm, vec=extrusion_vector, verts=translate_verts) bmesh.ops.translate(bm, vec=extrusion_vector, verts=translate_verts)
if context.object.mode == "EDIT": if context.active_object.mode == "EDIT":
bmesh.update_edit_mesh(obj.data) bmesh.update_edit_mesh(obj.data)
else: else:
bm.to_mesh(obj.data) bm.to_mesh(obj.data)
@@ -316,9 +316,9 @@ class BIM_OT_add_clever_stair(bpy.types.Operator, tool.Ifc.Operator):
self.report({"ERROR"}, "You need to start IFC project first to create a stair.") self.report({"ERROR"}, "You need to start IFC project first to create a stair.")
return {"CANCELLED"} return {"CANCELLED"}
if context.object is not None: if context.active_object is not None:
spawn_location = context.object.location.copy() spawn_location = context.active_object.location.copy()
context.object.select_set(False) context.active_object.select_set(False)
else: else:
spawn_location = bpy.context.scene.cursor.location.copy() spawn_location = bpy.context.scene.cursor.location.copy()
@@ -51,7 +51,7 @@ def update_sverchok_modifier(context):
bm.edges.index_update() bm.edges.index_update()
bm.faces.index_update() bm.faces.index_update()
if context.object.mode == "EDIT": if context.active_object.mode == "EDIT":
bmesh.update_edit_mesh(obj.data) bmesh.update_edit_mesh(obj.data)
else: else:
bm.to_mesh(obj.data) bm.to_mesh(obj.data)
@@ -292,7 +292,7 @@ def create_bm_window(
def update_window_modifier_bmesh(context): def update_window_modifier_bmesh(context):
obj = context.object obj = context.active_object
props = obj.BIMWindowProperties props = obj.BIMWindowProperties
panel_schema = DEFAULT_PANEL_SCHEMAS[props.window_type] panel_schema = DEFAULT_PANEL_SCHEMAS[props.window_type]
accumulated_height = [0] * len(panel_schema[0]) accumulated_height = [0] * len(panel_schema[0])
@@ -425,7 +425,7 @@ def update_window_modifier_bmesh(context):
bmesh.ops.translate(bm, vec=V(0, lining_offset, 0), verts=bm.verts) bmesh.ops.translate(bm, vec=V(0, lining_offset, 0), verts=bm.verts)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001) bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bmesh.update_edit_mesh(obj.data) bmesh.update_edit_mesh(obj.data)
else: else:
bm.to_mesh(obj.data) bm.to_mesh(obj.data)
@@ -444,9 +444,9 @@ class BIM_OT_add_window(bpy.types.Operator, tool.Ifc.Operator):
self.report({"ERROR"}, "You need to start IFC project first to create a window.") self.report({"ERROR"}, "You need to start IFC project first to create a window.")
return {"CANCELLED"} return {"CANCELLED"}
if context.object is not None: if context.active_object is not None:
spawn_location = context.object.location.copy() spawn_location = context.active_object.location.copy()
context.object.select_set(False) context.active_object.select_set(False)
else: else:
spawn_location = bpy.context.scene.cursor.location.copy() spawn_location = bpy.context.scene.cursor.location.copy()
+2 -2
View File
@@ -398,12 +398,12 @@ class Blender:
) )
bmesh.update_edit_mesh(mesh) bmesh.update_edit_mesh(mesh)
if not obj: if not obj:
if not bpy.context.object or bpy.context.object.data != mesh: if not bpy.context.active_object or bpy.context.active_object.data != mesh:
raise Exception( raise Exception(
"Error applying bmesh in EDIT object - object is " "Error applying bmesh in EDIT object - object is "
"not provided and can't be acquired from the context. " "not provided and can't be acquired from the context. "
) )
obj = bpy.context.object obj = bpy.context.active_object
obj.update_from_editmode() obj.update_from_editmode()
else: else:
bm.to_mesh(mesh) bm.to_mesh(mesh)
@@ -106,7 +106,7 @@ def update_geonodes_modifier():
bm.edges.index_update() bm.edges.index_update()
bm.faces.index_update() bm.faces.index_update()
if bpy.context.object.mode == "EDIT": if bpy.context.active_object.mode == "EDIT":
bmesh.update_edit_mesh(obj.data) bmesh.update_edit_mesh(obj.data)
else: else:
bm.to_mesh(obj.data) bm.to_mesh(obj.data)
+2 -2
View File
@@ -347,7 +347,7 @@ Scenario: Remove pset - multiple objects
Scenario: Edit pset length property Scenario: Edit pset length property
Given an empty IFC project Given an empty IFC project
And I press "mesh.add_clever_stair" And I press "mesh.add_clever_stair"
And the variable "pset" is "tool.Pset.get_element_pset(tool.Ifc.get_entity(bpy.context.object), 'Pset_StairFlightCommon').id()" And the variable "pset" is "tool.Pset.get_element_pset(tool.Ifc.get_entity(bpy.context.active_object), 'Pset_StairFlightCommon').id()"
And the variable "si_conversion" is "ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())" And the variable "si_conversion" is "ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())"
And I press "bim.enable_pset_editing(pset_id={pset}, obj='IfcStairFlight/StairFlight', obj_type='Object')" And I press "bim.enable_pset_editing(pset_id={pset}, obj='IfcStairFlight/StairFlight', obj_type='Object')"
@@ -380,7 +380,7 @@ Scenario: Edit qset length property
Given an empty IFC project Given an empty IFC project
And I press "mesh.add_clever_stair" And I press "mesh.add_clever_stair"
And I press "bim.calculate_all_quantities" And I press "bim.calculate_all_quantities"
And the variable "pset" is "tool.Pset.get_element_pset(tool.Ifc.get_entity(bpy.context.object), 'Qto_StairFlightBaseQuantities').id()" And the variable "pset" is "tool.Pset.get_element_pset(tool.Ifc.get_entity(bpy.context.active_object), 'Qto_StairFlightBaseQuantities').id()"
And the variable "si_conversion" is "ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())" And the variable "si_conversion" is "ifcopenshell.util.unit.calculate_unit_scale(tool.Ifc.get())"
And I press "bim.enable_pset_editing(pset_id={pset}, obj='IfcStairFlight/StairFlight', obj_type='Object')" And I press "bim.enable_pset_editing(pset_id={pset}, obj='IfcStairFlight/StairFlight', obj_type='Object')"