1
$\begingroup$

I have been trying read up a lot on opengl drawing using python and have been trying to create a script which display a text and a 2d shape on the viewport (a rectangle or square). From reading the blender docs I could only find little to no information about it and only got me to display text. The 2d shape drawing is still out of my knowledge. In this code I'm trying to draw a text and a draw a 2d quad in a modal operator but unfortunately the quad doesn't show up but the text is working properly.

How do I draw a 2d shape in a modal operator using bgl ?

import bpy
import blf
import bgl

def draw_textandbox(self, context):

    font_id = 0  
    x_offset = 0
    y_offset = 0

    #The Text-------------------------
    bgl.glColor4f(1,1,1,0.8)
    blf.position(font_id, 80, 500, 0)
    blf.size(font_id, 27, 70)
    blf.draw(font_id, "LEVELS:" )

    #The Box, This is the part i wanted to work but unfortunately it doesnt
    bgl.glColor4f(1,0,0,0.8)  
    bgl.glBegin(bgl.GL_QUADS)

    bgl.glVertex2f(200, 400)
    bgl.glEnd()
$\endgroup$
2
$\begingroup$

It's a quadrilateral, you need to define 4 verts.

Test method, x and y are pixel coordinates of bottom left corner of rectangle, h and w the height and width (in pixels) respectively.

def draw_box(self, x, y, w, h, color=(0.0, 0.0, 0.0, 1.0)):
    #bgl.glDepthRange (0.1, 1.0)
    bgl.glColor4f(*color)
    bgl.glBegin(bgl.GL_QUADS)

    bgl.glVertex2f(x + w, y + h)
    bgl.glVertex2f(x, y + h)
    bgl.glVertex2f(x, y)
    bgl.glVertex2f(x + w, y)
    bgl.glEnd()

Run-able Example

$\endgroup$
  • $\begingroup$ So the x, y, w, and h are vertex coordinates ? $\endgroup$ – Retrax Oct 23 '17 at 14:09
  • $\begingroup$ Note that repeated calls to draw_box() to draw multiple quads in one frame is inefficient. It'll be better to add more calls to glVertex2f() between the glBegin() and glEnd() calls. $\endgroup$ – dr. Sybren Oct 23 '17 at 14:10
  • $\begingroup$ @dr.Sybren any news on bgl support (or its repacement) in 2.8 $\endgroup$ – batFINGER Oct 23 '17 at 14:12
  • $\begingroup$ Sorry I don't understand, are the x, y, w, h to be replaced with vertex coordinates ? $\endgroup$ – Retrax Oct 23 '17 at 14:32
  • $\begingroup$ @batfinger I wouldn't know yet. $\endgroup$ – dr. Sybren Oct 23 '17 at 21:10
1
$\begingroup$

So I looked up on samblers example script in the question that batfinger linked, and I worked something out, I would seem that using bg.GL_POLYGON is better than using bl.GL_QUADS because you can as much as vertices as you want. Nevertheless, it got my job done done of drawing a 2d rectangle/square.

def draw_poly(points):
  for i in range(len(points)):
        bgl.glVertex2f(points[i][0],points[i][1])

def draw_callback_px(self, context):
  panel_points = [[10.0, 550.0],  #[x, y]
                  [10.0, 685.0],  #[x, y]
                  [150.0, 685.0], #[x, y]
                  [150.0, 550.0], #[x, y]
                  ]

  # draw poly
  bgl.glColor4f(1.0, 0.085, 0.0, 0.2)
  bgl.glEnable(bgl.GL_BLEND)
  bgl.glBegin(bgl.GL_POLYGON)
  draw_poly(panel_points)
  bgl.glEnd()

  # restore opengl defaults
  bgl.glLineWidth(1)
  bgl.glDisable(bgl.GL_BLEND)
  bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.