8
$\begingroup$

I'd like to know if it's possible to display text, with blf module, in the viewport with 2 colors ? For exemple, i want to draw "Ngon: 5".

"Ngon:" should be red and "5" should be blue.

Did i have to draw my text separatly or is it possible to define the color in the string ?

$\endgroup$

1 Answer 1

8
$\begingroup$

yeah, you have to draw by setting a bgl.color** before each new differently coloured text. It's not like a terminal print command, though you could write really simple functions that take

(x, y, [(str, col),...])

Because you can query the x,y dimensions of a string, you can easily generate the offset coordinates.

Something like this:

def draw_string(x, y, packed_strings):
    font_id = 0
    blf.size(font_id, 18, 72) 
    x_offset = 0
    for pstr, pcol in packed_strings:
        bgl.glColor4f(*pcol)
        text_width, text_height = blf.dimensions(font_id, pstr)
        blf.position(font_id, (x + x_offset), y, 0)
        blf.draw(font_id, pstr)
        x_offset += text_width

x = 60
y = 100
RED = (1, 0, 0, 1)
GREEN = (0, 1, 0, 1)
BLUE = (0, 0, 1, 1)
ps = [("Blue ", RED),("Yellow ", BLUE),("White ", GREEN)]
draw_string(x, y, ps)

enter image description here

$\endgroup$
2
  • $\begingroup$ Just on more question. Is it possible to draw other line under the previous one with an array fonction, still conserved the colors ? $\endgroup$
    – pistiwique
    Commented May 31, 2015 at 19:43
  • $\begingroup$ please ask a new separate question. $\endgroup$
    – zeffii
    Commented May 31, 2015 at 19:44

You must log in to answer this question.

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