Skip to content

Commit

Permalink
Python API via SWIG (WIP)
Browse files Browse the repository at this point in the history
WIP:
	* working but incomplete
	* need some cleanup (e.g. move testRun.py to samples)
  • Loading branch information
rpaciorek committed Feb 10, 2022
1 parent f8f5946 commit 49342b0
Show file tree
Hide file tree
Showing 9 changed files with 601 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMake/FeatureSummary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ endif ()
if (OGRE_BUILD_COMPONENT_PROPERTY)
set(_components "${_components} + Property\n")
endif ()
if (OGRE_BUILD_COMPONENT_PYTHON)
set(_components "${_components} + Python [BETA]\n")
endif ()
if (OGRE_BUILD_COMPONENT_SCENE_FORMAT)
set(_components "${_components} + SceneFormat\n")
endif ()
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ cmake_dependent_option(OGRE_BUILD_COMPONENT_MESHLODGENERATOR "Build MeshLodGener
cmake_dependent_option(OGRE_BUILD_COMPONENT_VOLUME "Build Volume component" FALSE "" FALSE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_PROPERTY "Build Property component" FALSE "Boost_FOUND" FALSE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_OVERLAY "Build Overlay component" TRUE "FREETYPE_FOUND OR OGRE_BUILD_PLATFORM_WINRT OR OGRE_BUILD_PLATFORM_WINDOWS_PHONE" FALSE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_PYTHON "Build Python bindings with SWIG" TRUE "NOT OGRE_STATIC" FALSE)
cmake_dependent_option(OGRE_BUILD_LIBS_AS_FRAMEWORKS "Build frameworks for libraries on OS X." TRUE "APPLE;NOT OGRE_BUILD_PLATFORM_APPLE_IOS" FALSE)

option(OGRE_BUILD_SAMPLES2 "Build Ogre new demos (2.1)" TRUE)
Expand Down
4 changes: 4 additions & 0 deletions Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ endif ()
if (OGRE_BUILD_COMPONENT_OVERLAY)
add_subdirectory(Overlay)
endif ()

if (OGRE_BUILD_COMPONENT_PYTHON)
add_subdirectory(Python)
endif()
58 changes: 58 additions & 0 deletions Components/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
set(Python_ADDITIONAL_VERSIONS 3.4) # allows using python3 on Ubuntu 14.04
find_package(PythonInterp)
find_package(PythonLibs)
macro_log_feature(PYTHONLIBS_FOUND "Python" "Language bindings to use OGRE from Python" "http://www.python.org/" FALSE "" "")

find_package(SWIG 3.0.8)

if(NOT PYTHONLIBS_FOUND OR NOT SWIG_FOUND)
set(OGRE_BUILD_COMPONENT_PYTHON OFF CACHE BOOL "" FORCE)
message(WARNING "Python ${PYTHONLIBS_FOUND} ${SWIG_FOUND}")
message(WARNING "Python Component disabled because SWIG or Python was not found")
return()
endif()

include_directories(${PYTHON_INCLUDE_PATH})
include_directories("${PROJECT_BINARY_DIR}/include" "${PROJECT_SOURCE_DIR}/OgreMain/include" "${PROJECT_SOURCE_DIR}/Components/Hlms/Common/include" "${PROJECT_SOURCE_DIR}/Components/Hlms/Pbs/include" "${PROJECT_SOURCE_DIR}/Components/Hlms/Unlit/include")
include(${SWIG_USE_FILE})

set(PYTHON_SITE_PACKAGES lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/dist-packages/)

set(CMAKE_SWIG_FLAGS -builtin)

if (APPLE AND NOT ANDROID)
# we are using libc++ on OSX
list(APPEND CMAKE_SWIG_FLAGS -D_LIBCPP_VERSION)
endif()

macro(ogre_python_module target)
set_target_properties(${SWIG_MODULE_${target}_REAL_NAME} PROPERTIES
DEBUG_POSTFIX "")
install(TARGETS ${SWIG_MODULE_${target}_REAL_NAME} LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES})
install(FILES ${PROJECT_BINARY_DIR}/Components/Python/${target}.py DESTINATION ${PYTHON_SITE_PACKAGES})
endmacro()

if(MSVC)
# TODO: Set MSVC flags
add_definitions(-DMS_NO_COREDLL)
else()
add_definitions(-Wno-cast-qual -Wno-shadow -Wno-maybe-uninitialized -Wno-unused-label -Wno-unused-but-set-variable -Wno-missing-declarations)
endif()
set_source_files_properties(Ogre.i PROPERTIES CPLUSPLUS ON)
swig_add_module(Ogre python Ogre.i)
swig_link_libraries(Ogre OgreMain OgreHlmsPbs OgreHlmsUnlit ${PYTHON_LIBRARIES})
ogre_python_module(Ogre)

# if(OGRE_BUILD_COMPONENT_RTSHADERSYSTEM)
# set_source_files_properties(../RTShaderSystem/include/OgreRTShader.i PROPERTIES CPLUSPLUS ON)
# swig_add_module(OgreRTShader python ../RTShaderSystem/include/OgreRTShader.i)
# swig_link_libraries(OgreRTShader OgreMain OgreRTShaderSystem ${PYTHON_LIBRARIES})
# ogre_python_module(OgreRTShader)
# endif()
#
# if(OGRE_BUILD_COMPONENT_OVERLAY)
# set_source_files_properties(../Overlay/include/OgreOverlay.i PROPERTIES CPLUSPLUS ON)
# swig_add_module(OgreOverlay python ../Overlay/include/OgreOverlay.i)
# swig_link_libraries(OgreOverlay OgreMain OgreOverlay ${PYTHON_LIBRARIES})
# ogre_python_module(OgreOverlay)
# endif()
212 changes: 212 additions & 0 deletions Components/Python/Ogre.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
%module(directors="1") Ogre

// SWIG setting for expose Python API
%include PythonAPI.i

// Global rename politics:
// - rename all nested classes "Ogre::A::B" into "A_B"
%rename("%(regex:/^(Ogre)?(::)?([^:]+)(::)?(.*?)$/\\3_\\5/)s", fullname=1, %$isnested, regextarget=1) "^Ogre::[^:]+::";
// - rename elements from Ogre::XX namespace to XX_{element_name},
%rename("%(regex:/^(Ogre)?(::)?([^:]+)(::)?(.*?)$/\\3_\\5/)s", fullname=1, %$not %$ismember, %$not %$isconstructor, %$not %$isdestructor, regextarget=1) "^Ogre::[^:<]+::[^:<]+";


// STD
%include std_shared_ptr.i
%include std_string.i
%include std_pair.i
%include std_deque.i
%include std_list.i
%include std_map.i
%include std_set.i
%include std_multimap.i
%include std_unordered_map.i
%include std_unordered_set.i
%include std_vector.i
%include exception.i
%include typemaps.i
%include stdint.i

// Ogre STD
%include ogrestd/deque.h
%include ogrestd/list.h
%include ogrestd/map.h
%include ogrestd/set.h
%include ogrestd/unordered_map.h
%include ogrestd/unordered_set.h
%include ogrestd/vector.h


// define some macros (to make possible parsing headers via %include)
%include "OgrePlatform.h"
#define _OgreExport
#define _OgrePrivate
#define OGRE_DEPRECATED
#undef FORCEINLINE
#define FORCEINLINE
#define final
#define noexcept(a)

// add headers and some definition to output C++ file
%{
#include "Ogre.h"
using namespace Ogre;

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wcast-function-type"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-label"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif

#include <sstream>
%}


// basic Ogre types
%include "OgreBasic.i"

// node, scene node, movable object

%include "OgreNode.h"
%include "OgreSceneNode.h"
%ignore Ogre::MovableObject::calculateCameraDistance; // is not defined (FIXME why public member is 'static inline'?)
%include "OgreMovableObject.h"
%include "OgreUserObjectBindings.h"

// scene manager

%{
#include "OgreSceneManager.h"
typedef SceneManager::RenderContext RenderContext;
%}
%ignore Ogre::SceneManager::updateAllBounds;
%ignore Ogre::SceneManager::getCameras; // FIXME protected typedef for CameraList
%ignore Ogre::SceneManager::getCameraIterator;
%ignore Ogre::SceneManager::getAnimations;
%ignore Ogre::SceneManager::getAnimationIterator;
%ignore Ogre::SceneManager::getAnimationStateIterator;
%include "OgreSceneManager.h"

// compositor

%include "initHLMS.i" // TODO see comment in this file

%{
#include <Compositor/OgreCompositorManager2.h>
#include <Compositor/OgreCompositorWorkspace.h>
#include <Compositor/OgreCompositorNode.h>
%}
%ignore Ogre::CompositorManager2::connectOutput; // FIXME is not defined (only declaration in OgreCompositorManager2.h file)
%include "Compositor/OgreCompositorManager2.h"
%include "Compositor/OgreCompositorWorkspace.h"
%include "Compositor/OgreCompositorNode.h"

// resources

%include "OgreResourceGroupManager.h"
%include "OgreResourceManager.h"
//%include "OgreResourceBackgroundQueue.h"
%{
#include "OgreResourceManager.h"
typedef Ogre::ResourceManager::ResourceCreateOrRetrieveResult ResourceCreateOrRetrieveResult;
#include "OgreArchiveFactory.h"
%}
%include "OgreArchive.h"
%include "OgreArchiveFactory.h"
%include "OgreArchiveManager.h"
%{
#include "OgreZip.h"
%}
%include "OgreZip.h"
%{
#include "OgreSerializer.h"
typedef Ogre::Serializer::Endian Endian;
%}
%include "OgreSerializer.h"

%include "OgreResource.h"

%include "OgreMaterialManager.h"
%include "OgreRenderable.h"

// textures

%{
#include <OgreTextureGpu.h>
#include <OgreTextureGpuManager.h>
#include <OgreTextureBox.h>
%}
%ignore Ogre::GpuResidency::toString;
%include "OgreGpuResource.h"
%ignore Ogre::TextureGpuManager::taskLoadToSysRamOrResident; // FIXME ScheduledTasks (argument type of public member) is protected within this context
%ignore Ogre::TextureGpuManager::taskToUnloadOrDestroy; // FIXME ScheduledTasks (argument type of public member) is protected within this context
%ignore Ogre::TextureGpuManager::executeTask; // FIXME ScheduledTasks (argument type of public member) is protected within this context
%include "OgreTextureGpu.h"
%include "OgreTextureGpuManager.h"
%include "OgreTextureGpuListener.h"

%include "OgreImage2.h"
%include "OgreBlendMode.h"

// item and other movable object derived class

%{
#include "OgreItem.h"
%}
%ignore Ogre::Item::clone; // FIXME is not defined (disabled by #if 0 in OgreItem.cpp, but not in OgreItem.h)
%include "OgreItem.h"
%include "OgreSubItem.h"

%ignore Ogre::Light::isInLightRange; // FIXME is not defined (only declaration in OgreLight.h file)
%ignore Ogre::Light::getTypeFlags; // FIXME is not defined (only declaration in OgreLight.h file and use in disabled by #ifdef ENABLE_INCOMPATIBLE_OGRE_2_0 part of OgreDefaultSceneQueries.cpp)
%include "OgreLight.h"
%include "OgreFrustum.h"
%include "OgreCamera.h"

%{
typedef Ogre::v1::Billboard Billboard;
typedef Ogre::v1::BillboardSet BillboardSet;
typedef Ogre::v1::RenderOperation RenderOperation;
%}
%include "OgreBillboard.h"
%include "OgreBillboardChain.h"
%include "OgreRibbonTrail.h"
%include "OgreBillboardSet.h"

%{
typedef Ogre::v1::SubEntity SubEntity;
typedef Ogre::v1::VertexData VertexData;
typedef Ogre::v1::EdgeData EdgeData;
typedef Ogre::v1::AnimationStateSet AnimationStateSet;
typedef Ogre::v1::AnimationState AnimationState;
typedef Ogre::v1::OldSkeletonInstance OldSkeletonInstance;
%}
%ignore Ogre::v1::Entity::getTypeFlags; // FIXME is not defined (only declaration in OgreEntity.h file and use in disabled by #ifdef ENABLE_INCOMPATIBLE_OGRE_2_0 part of OgreDefaultSceneQueries.cpp)
%include "OgreEntity.h"
%include "OgreSubEntity.h"


// Listeners and Utils

%include "OgreFrameListener.h"
%include "OgreLodListener.h"
%include "OgreController.h"
%{
typedef Ogre::ControllerFunction< Ogre::Real > ControllerFunction_Real;
#include "OgrePredefinedControllers.h"
%}
%template(ControllerFunction_Real) Ogre::ControllerFunction< Ogre::Real >;
%template(ControllerValue_Real) Ogre::ControllerValue< Ogre::Real >;
%include "OgrePredefinedControllers.h"

// Root, Window and RenderSystem

%ignore Ogre::Root::getSceneManagerMetaDataIterator;
%ignore Ogre::Root::getSceneManagerIterator;
%include "OgreRoot.h"
%{
#include <OgreWindow.h>
%}
%include "OgreWindow.h"
%include "OgreRenderSystem.h"
Loading

0 comments on commit 49342b0

Please sign in to comment.