2
$\begingroup$

I just copied and pasted from https://docs.blender.org/api/current/gpu.html#d-image:

import bpy
import gpu
import bgl
from gpu_extras.batch import batch_for_shader

IMAGE_NAME = "Untitled"
image = bpy.data.images[IMAGE_NAME]

shader = gpu.shader.from_builtin('2D_IMAGE')
batch = batch_for_shader(
    shader, 'TRI_FAN',
    {
        "pos": ((100, 100), (200, 100), (200, 200), (100, 200)),
        "texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
    },
)

if image.gl_load():
    raise Exception()


def draw():
    bgl.glActiveTexture(bgl.GL_TEXTURE0)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode)

    shader.bind()
    shader.uniform_int("image", 0)
    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')

But it rendered in black and white colors.

So,

  1. How to draw colored image?
  2. How to draw with alpha?
  3. It doesn't render it immediately - only when I hover over some view_3d interface widget. How can I force to draw it immediately?

The problem seems to be SOLVED.

I just can't understand why the image I've tested first time rendered noncolored.

Here is some Screenshots.

enter image description here

Second time I add image it was drawn RGB. enter image description here

But why it doesn't render immediately? Is there some refresh function operator or etc.?

$\endgroup$
2
  • $\begingroup$ Sometimes the blender documentation links change so I have added the relevant code to the question. $\endgroup$ Mar 6, 2020 at 15:45
  • 1
    $\begingroup$ This code works and displays colors if the image is itself colored. Could you show some screen captures of the "Untitled" image and the result? $\endgroup$
    – lemon
    Mar 6, 2020 at 15:51

1 Answer 1

3
$\begingroup$

The 3D view does not know about this script, so it won't draw immediately since some change occurs for it.

But you can force updates in 3D views using this at the end of your script:

for area in bpy.context.window.screen.areas:
    if area.type == 'VIEW_3D':
        area.tag_redraw()
$\endgroup$

You must log in to answer this question.

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