The code I posted determines the region, space and quad index in get_quadview_index(). This is basically what you need, but it is based on mouse location - but you want to determine it from inside your draw function as I understood.
You donât need to check mouse location, just find the right quadview from the context region. This can be achieved using region.id to determine the position of the region in Area.regions - the nth WINDOW-type region equals the nth quadview in SpaceView3D.region_quadviews.
import bpy
import bgl
import blf
from bpy_extras.view3d_utils import location_3d_to_region_2d
def draw_callback_px(self, context):
print("mouse points", len(self.mouse_path))
# only quadview mode supported
if not context.space_data.region_quadviews:
return
# should be true as long as operator was started from 3D View
assert context.area.type == 'VIEW_3D' and context.space_data.type == 'VIEW_3D'
i = -1
for region in context.area.regions:
if region.type == 'WINDOW':
i += 1
if context.region.id == region.id:
break
else:
return
rv3d = context.space_data.region_quadviews[i]
point3d = context.scene.cursor_location
loc = location_3d_to_region_2d(region, rv3d, point3d)
font_id = 0 # XXX, need to find out how best to get this.
# draw some text
blf.position(font_id, 15, 30, 0)
blf.size(font_id, 20, 72)
blf.draw(font_id, "Region {}, Quad {}".format(context.region.id, i))
blf.position(font_id, 15, 60, 0)
blf.size(font_id, 14, 72)
blf.draw(font_id, "Cursor 2D {}".format(tuple(map(lambda x: round(x, 2), loc))))
blf.position(font_id, 15, 80, 0)
blf.size(font_id, 14, 72)
x, y = self.mouse_path[-1]
blf.draw(font_id, "Mouse: {} {}".format(x - context.region.x, y - context.region.y))
# 50% alpha, 2 pixel width line
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(0.0, 0.0, 0.0, 0.3)
bgl.glLineWidth(2)
bgl.glBegin(bgl.GL_LINE_STRIP)
for x, y in self.mouse_path:
bgl.glVertex2i(x - context.region.x, y - context.region.y)
bgl.glEnd()
bgl.glColor4f(1.0, 0.6, 0.0, 1.0)
bgl.glPointSize(7)
bgl.glEnable(bgl.GL_POINT_SMOOTH)
bgl.glBegin(bgl.GL_POINTS)
bgl.glVertex2f(*loc)
bgl.glEnd()
# restore opengl defaults
bgl.glLineWidth(1)
bgl.glDisable(bgl.GL_BLEND)
bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
class ModalDrawOperator(bpy.types.Operator):
"""Draw a line with the mouse"""
bl_idname = "view3d.modal_operator"
bl_label = "Simple Modal View3D Operator"
def modal(self, context, event):
context.area.tag_redraw()
if event.type == 'MOUSEMOVE':
#self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
self.mouse_path.append((event.mouse_x, event.mouse_y))
elif event.type in {'RIGHTMOUSE', 'ESC'}:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
return {'CANCELLED'}
return {'PASS_THROUGH'}
def invoke(self, context, event):
if context.area.type == 'VIEW_3D':
# the arguments we pass the the callback
args = (self, context)
# Add the region OpenGL drawing callback
# draw in view space with 'POST_VIEW' and 'PRE_VIEW'
self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
self.mouse_path = []
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "View3D not found, cannot run operator")
return {'CANCELLED'}
def register():
bpy.utils.register_class(ModalDrawOperator)
def unregister():
bpy.utils.unregister_class(ModalDrawOperator)
if __name__ == "__main__":
register()