/src/pyglue/PyMain.cpp

http://github.com/imageworks/OpenColorIO · C++ · 205 lines · 145 code · 27 blank · 33 comment · 5 complexity · 791d6bb0136e9af4a6c2480d4c99a069 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <Python.h>
  28. #include <OpenColorIO/OpenColorIO.h>
  29. namespace OCIO = OCIO_NAMESPACE;
  30. #include "PyUtil.h"
  31. #include "PyDoc.h"
  32. namespace
  33. {
  34. PyObject * PyOCIO_ClearAllCaches(PyObject * /* self */)
  35. {
  36. OCIO_PYTRY_ENTER()
  37. OCIO::ClearAllCaches();
  38. Py_RETURN_NONE;
  39. OCIO_PYTRY_EXIT(NULL)
  40. }
  41. PyObject * PyOCIO_GetLoggingLevel(PyObject * /* self */)
  42. {
  43. OCIO_PYTRY_ENTER()
  44. return PyString_FromString(
  45. OCIO::LoggingLevelToString(OCIO::GetLoggingLevel()));
  46. OCIO_PYTRY_EXIT(NULL)
  47. }
  48. PyObject * PyOCIO_SetLoggingLevel(PyObject * /*self*/, PyObject * args)
  49. {
  50. OCIO_PYTRY_ENTER()
  51. PyObject* pylevel;
  52. if (!PyArg_ParseTuple(args, "O:SetLoggingLevel",
  53. &pylevel)) return NULL;
  54. // We explicitly cast to a str to handle both the str and int cases.
  55. PyObject* pystr = PyObject_Str(pylevel);
  56. if(!pystr) throw OCIO::Exception("Fist argument must be a LOGGING_LEVEL");
  57. OCIO::LoggingLevel level = OCIO::LoggingLevelFromString(PyString_AsString(pystr));
  58. OCIO::SetLoggingLevel(level);
  59. Py_DECREF(pystr);
  60. Py_RETURN_NONE;
  61. OCIO_PYTRY_EXIT(NULL)
  62. }
  63. PyObject * PyOCIO_GetCurrentConfig(PyObject * /* self */)
  64. {
  65. OCIO_PYTRY_ENTER()
  66. return OCIO::BuildConstPyConfig(OCIO::GetCurrentConfig());
  67. OCIO_PYTRY_EXIT(NULL)
  68. }
  69. PyObject * PyOCIO_SetCurrentConfig(PyObject * /*self*/, PyObject * args)
  70. {
  71. OCIO_PYTRY_ENTER()
  72. PyObject * pyconfig;
  73. if (!PyArg_ParseTuple(args, "O!:SetCurrentConfig",
  74. &OCIO::PyOCIO_ConfigType, &pyconfig)) return NULL;
  75. OCIO::ConstConfigRcPtr c = OCIO::GetConstConfig(pyconfig, true);
  76. OCIO::SetCurrentConfig(c);
  77. Py_RETURN_NONE;
  78. OCIO_PYTRY_EXIT(NULL)
  79. }
  80. PyMethodDef PyOCIO_methods[] = {
  81. { "ClearAllCaches",
  82. (PyCFunction) PyOCIO_ClearAllCaches, METH_NOARGS, OCIO::OPENCOLORIO_CLEARALLCACHES__DOC__ },
  83. { "GetLoggingLevel",
  84. (PyCFunction) PyOCIO_GetLoggingLevel, METH_NOARGS, OCIO::OPENCOLORIO_GETLOGGINGLEVEL__DOC__ },
  85. { "SetLoggingLevel",
  86. (PyCFunction) PyOCIO_SetLoggingLevel, METH_VARARGS, OCIO::OPENCOLORIO_SETLOGGINGLEVEL__DOC__ },
  87. { "GetCurrentConfig",
  88. (PyCFunction) PyOCIO_GetCurrentConfig, METH_NOARGS, OCIO::OPENCOLORIO_GETCURRENTCONFIG__DOC__ },
  89. { "SetCurrentConfig",
  90. (PyCFunction) PyOCIO_SetCurrentConfig, METH_VARARGS, OCIO::OPENCOLORIO_SETCURRENTCONFIG__DOC__ },
  91. { NULL, NULL, 0, NULL } /* Sentinel */
  92. };
  93. }
  94. OCIO_NAMESPACE_ENTER
  95. {
  96. namespace
  97. {
  98. PyObject * g_exceptionType = NULL;
  99. PyObject * g_exceptionMissingFileType = NULL;
  100. }
  101. // These are explicitly initialized in the init function
  102. // to make sure they're not initialized until after the module is
  103. PyObject * GetExceptionPyType()
  104. {
  105. return g_exceptionType;
  106. }
  107. void SetExceptionPyType(PyObject * pytypeobj)
  108. {
  109. g_exceptionType = pytypeobj;
  110. }
  111. PyObject * GetExceptionMissingFilePyType()
  112. {
  113. return g_exceptionMissingFileType;
  114. }
  115. void SetExceptionMissingFilePyType(PyObject * pytypeobj)
  116. {
  117. g_exceptionMissingFileType = pytypeobj;
  118. }
  119. inline bool AddObjectToModule(PyTypeObject& o, const char* n, PyObject* m)
  120. {
  121. o.tp_new = PyType_GenericNew;
  122. if(PyType_Ready(&o) < 0) return false;
  123. Py_INCREF(&o);
  124. PyModule_AddObject(m, n, (PyObject *)&o);
  125. return true;
  126. }
  127. // fwd declare
  128. void AddConstantsModule(PyObject *enclosingModule);
  129. }
  130. OCIO_NAMESPACE_EXIT
  131. MOD_INIT(PyOpenColorIO)
  132. {
  133. PyObject * m;
  134. MOD_DEF(m, "PyOpenColorIO", OCIO::OPENCOLORIO__DOC__, PyOCIO_methods);
  135. PyModule_AddStringConstant(m, "version", OCIO::GetVersion());
  136. PyModule_AddIntConstant(m, "hexversion", OCIO::GetVersionHex());
  137. // Create Exceptions, and add to the module
  138. char Exception[] = "PyOpenColorIO.Exception";
  139. char ExceptionMissingFile[] = "PyOpenColorIO.ExceptionMissingFile";
  140. #if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 7
  141. OCIO::SetExceptionPyType(PyErr_NewExceptionWithDoc(Exception,
  142. (char*)OCIO::EXCEPTION__DOC__, OCIO::GetExceptionPyType(), NULL));
  143. OCIO::SetExceptionMissingFilePyType(PyErr_NewExceptionWithDoc(ExceptionMissingFile,
  144. (char*)OCIO::EXCEPTIONMISSINGFILE__DOC__, OCIO::GetExceptionMissingFilePyType(), NULL));
  145. #else
  146. OCIO::SetExceptionPyType(PyErr_NewException(Exception,
  147. OCIO::GetExceptionPyType(), NULL));
  148. OCIO::SetExceptionMissingFilePyType(PyErr_NewException(ExceptionMissingFile,
  149. OCIO::GetExceptionMissingFilePyType(), NULL));
  150. #endif
  151. PyModule_AddObject(m, "Exception", OCIO::GetExceptionPyType());
  152. PyModule_AddObject(m, "ExceptionMissingFile", OCIO::GetExceptionMissingFilePyType());
  153. // Register Classes
  154. OCIO::AddObjectToModule(OCIO::PyOCIO_ColorSpaceType, "ColorSpace", m);
  155. OCIO::AddObjectToModule(OCIO::PyOCIO_ConfigType, "Config", m);
  156. OCIO::AddConstantsModule(m);
  157. OCIO::AddObjectToModule(OCIO::PyOCIO_ContextType, "Context", m);
  158. OCIO::AddObjectToModule(OCIO::PyOCIO_LookType, "Look", m);
  159. OCIO::AddObjectToModule(OCIO::PyOCIO_ProcessorType, "Processor", m);
  160. OCIO::AddObjectToModule(OCIO::PyOCIO_ProcessorMetadataType, "ProcessorMetadata", m);
  161. OCIO::AddObjectToModule(OCIO::PyOCIO_GpuShaderDescType, "GpuShaderDesc", m);
  162. OCIO::AddObjectToModule(OCIO::PyOCIO_BakerType, "Baker", m);
  163. OCIO::AddObjectToModule(OCIO::PyOCIO_TransformType, "Transform", m);
  164. {
  165. OCIO::AddObjectToModule(OCIO::PyOCIO_AllocationTransformType, "AllocationTransform", m);
  166. OCIO::AddObjectToModule(OCIO::PyOCIO_CDLTransformType, "CDLTransform", m);
  167. OCIO::AddObjectToModule(OCIO::PyOCIO_ColorSpaceTransformType, "ColorSpaceTransform", m);
  168. OCIO::AddObjectToModule(OCIO::PyOCIO_DisplayTransformType, "DisplayTransform", m);
  169. OCIO::AddObjectToModule(OCIO::PyOCIO_ExponentTransformType, "ExponentTransform", m);
  170. OCIO::AddObjectToModule(OCIO::PyOCIO_FileTransformType, "FileTransform", m);
  171. OCIO::AddObjectToModule(OCIO::PyOCIO_GroupTransformType, "GroupTransform", m);
  172. OCIO::AddObjectToModule(OCIO::PyOCIO_LogTransformType, "LogTransform", m);
  173. OCIO::AddObjectToModule(OCIO::PyOCIO_LookTransformType, "LookTransform", m);
  174. OCIO::AddObjectToModule(OCIO::PyOCIO_MatrixTransformType, "MatrixTransform", m);
  175. }
  176. return MOD_SUCCESS_VAL(m);
  177. }