Blender V2.61 - r43446

blendVideoTex.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of VideoTexture library
00004 
00005 Copyright (c) 2006 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00027 #include <PyObjectPlus.h>
00028 
00029 #include <RAS_GLExtensionManager.h>
00030 
00031 #include <RAS_IPolygonMaterial.h>
00032 
00033 //Old API
00034 //#include "TexPlayer.h"
00035 //#include "TexImage.h"
00036 //#include "TexFrameBuff.h"
00037 
00038 //#include "TexPlayerGL.h"
00039 
00040 #include "ImageBase.h"
00041 #include "FilterBase.h"
00042 #include "Texture.h"
00043 
00044 #include "Exception.h"
00045 
00046 
00047 // get material id
00048 static PyObject * getMaterialID (PyObject *self, PyObject *args)
00049 {
00050     // parameters - game object with video texture
00051     PyObject * obj = NULL;
00052     // material name
00053     char * matName;
00054 
00055     // get parameters
00056     if (!PyArg_ParseTuple(args, "Os:materialID", &obj, &matName))
00057         return NULL;
00058     // get material id
00059     short matID = getMaterialID(obj, matName);
00060     // if material was not found, report errot
00061     if (matID < 0)
00062     {
00063         PyErr_SetString(PyExc_RuntimeError, "VideoTexture.materialID(ob, string): Object doesn't have material with given name");
00064         return NULL;
00065     }
00066     // return material ID
00067     return Py_BuildValue("h", matID);
00068 }
00069 
00070 
00071 // get last error description
00072 static PyObject * getLastError (PyObject *self, PyObject *args)
00073 {
00074     return PyUnicode_FromString(Exception::m_lastError.c_str());
00075 }
00076 
00077 // set log file
00078 static PyObject * setLogFile (PyObject *self, PyObject *args)
00079 {
00080     // get parameters
00081     if (!PyArg_ParseTuple(args, "s:setLogFile", &Exception::m_logFile))
00082         return Py_BuildValue("i", -1);
00083     // log file was loaded
00084     return Py_BuildValue("i", 0);
00085 }
00086 
00087 
00088 // image to numpy array
00089 static PyObject * imageToArray (PyObject * self, PyObject *args)
00090 {
00091     // parameter is Image object
00092     PyObject * pyImg;
00093     char *mode = NULL;
00094     if (!PyArg_ParseTuple(args, "O|s:imageToArray", &pyImg, &mode) || !pyImageTypes.in(Py_TYPE(pyImg)))
00095     {
00096         // if object is incorect, report error
00097         PyErr_SetString(PyExc_TypeError, "VideoTexture.imageToArray(image): The value must be a image source object");
00098         return NULL;
00099     }
00100     // get image structure
00101     PyImage * img = reinterpret_cast<PyImage*>(pyImg);
00102     return Image_getImage(img, mode);
00103 }
00104 
00105 
00106 // metody modulu
00107 static PyMethodDef moduleMethods[] =
00108 {
00109     {"materialID", getMaterialID, METH_VARARGS, "Gets object's Blender Material ID"},
00110     {"getLastError", getLastError, METH_NOARGS, "Gets last error description"},
00111     {"setLogFile", setLogFile, METH_VARARGS, "Sets log file name"},
00112     {"imageToArray", imageToArray, METH_VARARGS, "get buffer from image source, color channels are selectable"},
00113     {NULL}  /* Sentinel */
00114 };
00115 
00116 #ifdef WITH_FFMPEG
00117 extern PyTypeObject VideoFFmpegType;
00118 extern PyTypeObject ImageFFmpegType;
00119 #endif
00120 extern PyTypeObject FilterBlueScreenType;
00121 extern PyTypeObject FilterGrayType;
00122 extern PyTypeObject FilterColorType;
00123 extern PyTypeObject FilterLevelType;
00124 extern PyTypeObject FilterNormalType;
00125 extern PyTypeObject FilterRGB24Type;
00126 extern PyTypeObject FilterRGBA32Type;
00127 extern PyTypeObject FilterBGR24Type;
00128 extern PyTypeObject ImageBuffType;
00129 extern PyTypeObject ImageMixType;
00130 extern PyTypeObject ImageRenderType;
00131 extern PyTypeObject ImageMirrorType;
00132 extern PyTypeObject ImageViewportType;
00133 extern PyTypeObject ImageViewportType;
00134 
00135 
00136 static void registerAllTypes(void)
00137 {
00138 #ifdef WITH_FFMPEG
00139     pyImageTypes.add(&VideoFFmpegType, "VideoFFmpeg");
00140     pyImageTypes.add(&ImageFFmpegType, "ImageFFmpeg");
00141 #endif
00142     pyImageTypes.add(&ImageBuffType, "ImageBuff");
00143     pyImageTypes.add(&ImageMixType, "ImageMix");
00144     pyImageTypes.add(&ImageRenderType, "ImageRender");
00145     pyImageTypes.add(&ImageMirrorType, "ImageMirror");
00146     pyImageTypes.add(&ImageViewportType, "ImageViewport");
00147 
00148     pyFilterTypes.add(&FilterBlueScreenType, "FilterBlueScreen");
00149     pyFilterTypes.add(&FilterGrayType, "FilterGray");
00150     pyFilterTypes.add(&FilterColorType, "FilterColor");
00151     pyFilterTypes.add(&FilterLevelType, "FilterLevel");
00152     pyFilterTypes.add(&FilterNormalType, "FilterNormal");
00153     pyFilterTypes.add(&FilterRGB24Type, "FilterRGB24");
00154     pyFilterTypes.add(&FilterRGBA32Type, "FilterRGBA32");
00155     pyFilterTypes.add(&FilterBGR24Type, "FilterBGR24");
00156 }
00157 
00158 static struct PyModuleDef VideoTexture_module_def = {
00159     {}, /* m_base */
00160     "VideoTexture",  /* m_name */
00161     "Module that allows to play video files on textures in GameBlender.",  /* m_doc */
00162     0,  /* m_size */
00163     moduleMethods,  /* m_methods */
00164     0,  /* m_reload */
00165     0,  /* m_traverse */
00166     0,  /* m_clear */
00167     0,  /* m_free */
00168 };
00169 
00170 PyObject* initVideoTexture(void) 
00171 {
00172     PyObject * m;
00173     
00174     // initialize GL extensions
00175     //bgl::InitExtensions(0);
00176 
00177     // prepare classes
00178     registerAllTypes();
00179     registerAllExceptions();
00180 
00181     if (!pyImageTypes.ready())
00182         return NULL;
00183     if (!pyFilterTypes.ready()) 
00184         return NULL;
00185     if (PyType_Ready(&TextureType) < 0) 
00186         return NULL;
00187 
00188     /* Use existing module where possible
00189      * be careful not to init any runtime vars after this */
00190     m = PyImport_ImportModule( "VideoTexture" );
00191     if(m) {
00192         Py_DECREF(m);
00193         return m;
00194     }
00195     else {
00196         PyErr_Clear();
00197     
00198         m = PyModule_Create(&VideoTexture_module_def);
00199         PyDict_SetItemString(PySys_GetObject("modules"), VideoTexture_module_def.m_name, m);
00200     }
00201     
00202     if (m == NULL) 
00203         return NULL;
00204 
00205     // initialize classes
00206     pyImageTypes.reg(m);
00207     pyFilterTypes.reg(m);
00208 
00209     Py_INCREF(&TextureType);
00210     PyModule_AddObject(m, (char*)"Texture", (PyObject*)&TextureType);
00211     
00212     // init last error description
00213     Exception::m_lastError = "";
00214     
00215     return m;
00216 }
00217 
00218 // registration to Image types, put here because of silly linker bug