Three ways to get the active SpaceView3D link
context.space_data
context.area.spaces.active
context.area.spaces[0]
This works because all sidebars are part of an area View3D. No need to override the context here. For matrix you need datas from View3D.region_3d
[RegionView3D] link
import bpy
### UI
class TEST_PT_Panel(bpy.types.Panel):
bl_idname = 'TEST_PT_Panel'
bl_label = "Test Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "TEST"
def draw(self, context):
tool = context.scene.tool
layout = self.layout
layout.prop(tool, 'bool')
layout.operator('test.operator', text="Test Operator")
### OPERATOR
class TEST_OT_Operator(bpy.types.Operator):
bl_idname = 'test.operator'
bl_label = "Test Operator"
bl_description = "...testing"
def execute(self, context):
# get active space:
activeA = context.space_data
activeA.overlay.show_overlays = not activeA.overlay.show_overlays
activeB = context.area.spaces.active
activeB.show_gizmo = not activeB.show_gizmo
activeC = context.area.spaces[0]
activeC.show_region_toolbar = not activeC.show_region_toolbar
# get matrix example
r3d = activeA.region_3d
view_mat = r3d.view_matrix
print("view matrix: ", view_mat)
return {'FINISHED'}
### PROPERTIES
class Test_Properties(bpy.types.PropertyGroup):
bool : bpy.props.BoolProperty(default=False)
### REGISTRY
classes = (
TEST_PT_Panel,
TEST_OT_Operator,
Test_Properties,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.tool = bpy.props.PointerProperty(type=Test_Properties)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
bpy.types.Scene.tool
if __name__ == "__main__":
register()