1
$\begingroup$

I have an Add-On which resides in the N-Panel of the View_3D window. From this Add-on, I would like to manipulate the current view with buttons.

As there are many View_3D view ports in Blender and all of them has my Add-on in their respective N-Panel, I need to find out in which of those View_3D windows was the button pressed so I can manipulate the view matrix of that view port.

Is there a simple way to get that screen without fiddling with the location of the mouse pointer?

$\endgroup$

1 Answer 1

1
$\begingroup$

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()
$\endgroup$
2
  • $\begingroup$ Thank you. Now I stumbled on to the next problem. I wanted to apply the matrix from a camera to the view_matrix but obviously they are not "interchangeable" as the view is 90° turned (X axis) and mirrored on the Y axis. How do the Matrix of a camera relate to the region_3d.view_matrix? $\endgroup$
    – Steve
    Commented Mar 4, 2023 at 14:42
  • 1
    $\begingroup$ I figured it out. The camera matrix needs to be inverted. ( view_matrix.inverted() ) $\endgroup$
    – Steve
    Commented Mar 4, 2023 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .