3
$\begingroup$

I wrote below scripts to render texture already loaded by blender.

However, no images are rendered on 3D View. This code works correctly when I run this script on blender 2.70. But, this script doesn't work on blender 2.72 or higher.

Is there any ideas about this problem?

bl_idname = "uv.tp_texture_renderer"
bl_label = "Texture renderer"

__handle = None

@staticmethod
def handle_add(self, context):
    TPTextureRenderer.__handle = bpy.types.SpaceView3D.draw_handler_add(
        TPTextureRenderer.draw_texture,
        (self, context), 'WINDOW', 'POST_PIXEL')

@staticmethod
def handle_remove(self, context):
    if TPTextureRenderer.__handle is not None:
        bpy.types.SpaceView3D.draw_handler_remove(
            TPTextureRenderer.__handle, 'WINDOW')
        TPTextureRenderer.__handle = None

@staticmethod
def draw_texture(self, context):
    wm = context.window_manager
    sc = context.scene

    # no texture is selected
    if sc.tex_image == "None":
        return

    # setup rendering region
    rect = get_canvas(context, sc.tex_magnitude)
    positions = [
        [rect.x0, rect.y0],
        [rect.x0, rect.y1],
        [rect.x1, rect.y1],
        [rect.x1, rect.y0]
        ]
    tex_coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]

    # get texture to be renderred
    img = bpy.data.images[sc.tex_image]

    # OpenGL configuration
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    if img.bindcode:
        bind = img.bindcode
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, bind)
        bgl.glTexParameteri(
            bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
        bgl.glTexParameteri(
            bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
        bgl.glTexEnvi(
            bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE, bgl.GL_MODULATE)

    # render texture
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glColor4f(1.0, 1.0, 1.0, sc.tex_transparency)
    for (v1, v2), (u, v) in zip(positions, tex_coords):
        bgl.glTexCoord2f(u, v)
        bgl.glVertex2f(v1, v2)
    bgl.glEnd()
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.