[BGL] loading an image without adding it to blender data

Hey guys I have this code

<b>import </b>bpy
<b>import </b>bgl
<b>import </b>blf





<b>class </b><b>CyclopsModalDrawOperator</b>(bpy.types.Operator):
    <i>"""Draw a line with the mouse"""
</i><i>    </i>bl_idname = 'view3d.cyclops'
    bl_label = 'Cyclops'
    texture = 0

    <b>def </b><b>draw_callback_px</b>(self, context, event):
        print("mouse points", len(self.mouse_path))
        x = self.mouse_path[-1][0]
        y = self.mouse_path[-1][1]
        font_id = 0  # XXX, need to find out how best to get this.

        print(" bindcode: ", self.texture.bindcode[0])

        # draw some text
        blf.position(font_id, 15, 30, 0)
        blf.size(font_id, 20, 72)
        blf.draw(font_id, "mouse path :" + str(self.mouse_path[-1]))
        blf.position(font_id, 15, 60, 0)
        blf.draw(font_id, "bindcode : " + str(self.texture.bindcode[0]))

        self.texture.gl_load()
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture.bindcode[0])
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)

        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)



        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_TEXTURE_2D)




        # 50% alpha, 2 pixel width line

        bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
        bgl.glLineWidth(2)

        bgl.glBegin(bgl.GL_QUADS)
        # for x, y in self.mouse_path:

        # bgl.glRecti(10, 10,x ,y )
        bgl.glTexCoord2i(1, 0)
        bgl.glVertex2i(10, 10)
        bgl.glTexCoord2i(1, 1)
        bgl.glVertex2i(10, y)
        bgl.glTexCoord2i(0, 1)
        bgl.glVertex2i(x, y)
        bgl.glTexCoord2i(0, 0)
        bgl.glVertex2i(x, 10)

        bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)

        bgl.glDisable(bgl.GL_TEXTURE_2D)
        bgl.glDisable(bgl.GL_BLEND)
        self.texture.gl_free()
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

    <b>def </b><b>modal</b>(self, context, event):
        context.area.tag_redraw()

        <b>if </b>event.type == 'MOUSEMOVE':
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))

        <b>elif </b>event.type == 'LEFTMOUSE':
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            <b>return </b>{'FINISHED'}

        <b>elif </b>event.type <b>in </b>{'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            <b>return </b>{'CANCELLED'}

        <b>return </b>{'RUNNING_MODAL'}

    <b>def </b><b>invoke</b>(self, context, event):
        <b>if </b>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(self.draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
            self.texture =bpy.data.images.load("/Users/kilon/Pictures/rocket.jpg")
            self.mouse_path = []


            context.window_manager.modal_handler_add(self)
            <b>return </b>{'RUNNING_MODAL'}
        <b>else</b>:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            <b>return </b>{'CANCELLED'}


<b>def </b><b>register</b>():
    bpy.utils.register_class(CyclopsModalDrawOperator)


<b>def </b><b>unregister</b>():
    bpy.utils.unregister_class(CyclopsModalDrawOperator)

<b>if </b>__name__ == "__main__":
    register()

It works fine, it draws a box with the loaded image as texture. My problem is that it adds the image in the image list of the image editor and I do not want that. The image is intended to be used only for bgl so I have no reason to add it inside blender.

Basically I am trying to make a custom gui inside the viewport using bgl and custom made images.

Is there a way to load the image without adding it to blender ?

Hello :slight_smile:

2 years larer, I am having the same issue, did you find a solution ?
Someone else ?

See you :slight_smile: ++
Tricotou

Image buffers were implemented as an extenstion to the Blender Python API, so now yes its possible but I have began my own commercial project based on Blender source so I use my own way to load images directly to blender very similarly to how image buffers do it. I have also been a contributor to image buffer implementation so yes it works.