Drawing an image with the gpu module in scene and viewport space

Using the GPU module from the Blender Python API, we can draw images in the scene or viewport space. Images can be drawn in the scene coordinate system for intuitive mesh interaction, or in the viewport coordinate system to create custom UI elements.

Make the simplest example of drawing an image on the screen with the Blender Python API GPU module.

First, let’s draw the image in the scene space coordinate system.

Define a texture for drawing the image:

Create a shader to draw this texture:

and a batch for rendering the shader:

Pay attention to which coordinates are specified in the “pos” parameter. In this case, we want to display the image in scene space, so we specify the coordinates like we’re adding a simple plane to the scene and placing its corners at points (0,0), (2,0), (2,2), (0,2) in the XY scene plane.

Define a function for rendering the shader.

And add it to the draw handler to display the shader in the 3D viewport area:

Pay attention to the value of the fourth parameter – “POST_VIEW”. It determines that the image is displayed in the scene coordinate space.

When we need to display an image in the viewport coordinate space, we have to change the value of this parameter to the “POST_PIXEL”.

We also need to change the coordinate values ​​in the “pos” parameter when defining the batch to values ​​in the viewport coordinate system:

Here we specify the coordinates in pixels, from the lower left corner of the 3D Viewport area.

The rest of the code remains the same.

To stop drawing and remove our image from the screen, we need to remove our draw function from the draw handler of the SpaceView3D.

 

5 2 votes
Article Rating
Subscribe
Notify of
guest

4 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
lynchon
lynchon
1 year ago

can we use gifs or other image formats?

Constan
Constan
1 year ago

how can i remove that image visualized?