From cb6541675a233d290c88af30b4829a5c8c5bc5c8 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 21 May 2016 20:10:11 -0400 Subject: [PATCH] Change to Blender 2.77 style bpy.types.Image.bindcode. This idiotic change was introduced in stealth mode from 2.76 to 2.77. It has something to do with 3d textures, supposedly. I think the person who made this change is simply an asshat... --- korlib/texture.cpp | 29 ++++++++++++++++++++++++++--- korman/__init__.py | 4 ++-- korman/exporter/material.py | 3 ++- korman/korlib/texture.py | 6 +++--- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/korlib/texture.cpp b/korlib/texture.cpp index 85a12f4..a69aae5 100644 --- a/korlib/texture.cpp +++ b/korlib/texture.cpp @@ -30,6 +30,8 @@ # define GL_GENERATE_MIPMAP 0x8191 #endif // GL_GENERATE_MIPMAP +#define TEXTARGET_TEXTURE_2D 0 + extern "C" { typedef struct { @@ -80,8 +82,16 @@ static int pyGLTexture___init__(pyGLTexture* self, PyObject* args, PyObject* kwd static PyObject* pyGLTexture__enter__(pyGLTexture* self) { // Is the image already loaded? PyObjectRef bindcode = PyObject_GetAttrString(self->m_blenderImage, "bindcode"); + + // bindcode changed to a sequence in 2.77. We want the first element for a 2D texture. + // Why did we make this change, exactly? + if (PySequence_Check(bindcode)) { + bindcode = PySequence_GetItem(bindcode, TEXTARGET_TEXTURE_2D); + } + + // Now we should have a GLuint... if (!PyLong_Check(bindcode)) { - PyErr_SetString(PyExc_RuntimeError, "Image bindcode isn't a long?"); + PyErr_SetString(PyExc_TypeError, "Image bindcode isn't a long?"); return NULL; } @@ -92,11 +102,24 @@ static PyObject* pyGLTexture__enter__(pyGLTexture* self) { // Load image into GL if (self->m_ownIt) { PyObjectRef new_bind = PyObject_CallMethod(self->m_blenderImage, "gl_load", NULL); - if (PyLong_AsSize_t(new_bind) != 0) { - PyErr_SetString(PyExc_RuntimeError, "failed to load image into GL"); + if (!PyLong_Check(new_bind)) { + PyErr_SetString(PyExc_TypeError, "gl_load() did not return a long"); + return NULL; + } + ssize_t result = PyLong_AsSize_t(new_bind); + if (result != GL_NO_ERROR) { + PyErr_Format(PyExc_RuntimeError, "gl_load() error: %d", result); return NULL; } bindcode = PyObject_GetAttrString(self->m_blenderImage, "bindcode"); + if (PySequence_Check(bindcode)) { + bindcode = PySequence_GetItem(bindcode, TEXTARGET_TEXTURE_2D); + } + // Now we should have a GLuint... + if (!PyLong_Check(bindcode)) { + PyErr_SetString(PyExc_TypeError, "Image bindcode isn't a long?"); + return NULL; + } image_bindcode = PyLong_AsUnsignedLong(bindcode); } diff --git a/korman/__init__.py b/korman/__init__.py index cd51a9c..552a43f 100644 --- a/korman/__init__.py +++ b/korman/__init__.py @@ -22,10 +22,10 @@ from . import operators bl_info = { "name": "Korman", "author": "Guild of Writers", - "blender": (2, 74, 0), # I can't be bothered to support old stuff + "blender": (2, 77, 0), "location": "File > Import-Export", "description": "Exporter for Cyan Worlds' Plasma Engine", - "warning": "alpha", + "warning": "beta", "category": "System", # Eventually, we will hide some of the default # Blender panels (think materials) } diff --git a/korman/exporter/material.py b/korman/exporter/material.py index 29886a8..84361c6 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -572,9 +572,10 @@ class MaterialConverter: def _resize_image(self, image, width, height): image.scale(width, height) + image.update() # If the image is already loaded into OpenGL, we need to refresh it to get the scaling. - if image.bindcode != 0: + if image.bindcode[0] != 0: image.gl_free() image.gl_load() diff --git a/korman/korlib/texture.py b/korman/korlib/texture.py index ff4468c..16e2080 100644 --- a/korman/korlib/texture.py +++ b/korman/korlib/texture.py @@ -22,7 +22,7 @@ bgl.GL_BGRA = 0x80E1 class GLTexture: def __init__(self, blimg): - self._ownit = (blimg.bindcode == 0) + self._ownit = (blimg.bindcode[0] == 0) self._blimg = blimg def __enter__(self): @@ -32,9 +32,9 @@ class GLTexture: raise RuntimeError("failed to load image") self._previous_texture = self._get_integer(bgl.GL_TEXTURE_BINDING_2D) - self._changed_state = (self._previous_texture != self._blimg.bindcode) + self._changed_state = (self._previous_texture != self._blimg.bindcode[0]) if self._changed_state: - bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._blimg.bindcode) + bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._blimg.bindcode[0]) return self def __exit__(self, type, value, traceback):