1 /* 2 * pybox2d -- http://pybox2d.googlecode.com 3 * 4 * Copyright (c) 2010 Ken Lauer / sirkne at gmail dot com 5 * 6 * This software is provided 'as-is', without any express or implied 7 * warranty. In no event will the authors be held liable for any damages 8 * arising from the use of this software. 9 * Permission is granted to anyone to use this software for any purpose, 10 * including commercial applications, and to alter it and redistribute it 11 * freely, subject to the following restrictions: 12 * 1. The origin of this software must not be misrepresented; you must not 13 * claim that you wrote the original software. If you use this software 14 * in a product, an acknowledgment in the product documentation would be 15 * appreciated but is not required. 16 * 2. Altered source versions must be plainly marked as such, and must not be 17 * misrepresented as being the original software. 18 * 3. This notice may not be removed or altered from any source distribution. 19 */ 20 21 /* 22 * This is the main Python SWIG interface file. 23 */ 24 25 %module(directors="1") Box2D 26 %{ 27 #include "Box2D/Box2D.h" 28 %} 29 30 /*note: 31 swig generated names: _Box2D.<class>_<name> 32 python obfuscated names: __<class>_<name> 33 */ 34 35 #ifdef SWIGPYTHON 36 /* To disable assertions->exceptions, comment out the two lines that define 37 USE_EXCEPTIONS. One is here, one is in Common/b2Settings.h */ 38 #define USE_EXCEPTIONS 39 #ifdef USE_EXCEPTIONS 40 /* See Common/b2Settings.h also. It defines b2Assert to instead throw 41 an exception if USE_EXCEPTIONS is defined. */ 42 %include "exception.i" 43 44 %exception { 45 try { 46 $action catch(b2AssertException)47 } catch(b2AssertException) { 48 // error already set, pass it on to python 49 SWIG_fail; 50 } 51 if (PyErr_Occurred()) { 52 // This is if we set the error inside a function; report it to swig 53 SWIG_fail; 54 } 55 } 56 #endif 57 58 /* Director-exceptions are a result of callbacks that happen as a result to 59 the physics step or debug draw, usually. So, catch those errors and report 60 them back to Python. 61 62 Example: 63 If there is a typo in your b2Draw instance's DrawPolygon (in 64 Python), when you call world.DrawDebugData(), callbacks will be made 65 to such functions as that. Being Python code called from the C++ module, 66 they turn into director exceptions then and will crash the application 67 unless handled in C++ (try/catch) and then passed to Python (SWIG_fail). 68 */ 69 %exception b2World::Step { 70 try { $action } catch(Swig::DirectorException)71 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)72 catch (b2AssertException) { SWIG_fail; } 73 } 74 %exception b2World::DrawDebugData { 75 try { $action } catch(Swig::DirectorException)76 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)77 catch (b2AssertException) { SWIG_fail; } 78 } 79 %exception b2World::QueryAABB { 80 try { $action } catch(Swig::DirectorException)81 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)82 catch (b2AssertException) { SWIG_fail; } 83 } 84 %exception b2World::RayCast { 85 try { $action } catch(Swig::DirectorException)86 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)87 catch (b2AssertException) { SWIG_fail; } 88 } 89 %exception b2World::DestroyJoint { 90 try { $action } catch(Swig::DirectorException)91 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)92 catch (b2AssertException) { SWIG_fail; } 93 } 94 %exception b2World::DestroyBody { 95 try { $action } catch(Swig::DirectorException)96 catch (Swig::DirectorException) { SWIG_fail; } catch(b2AssertException)97 catch (b2AssertException) { SWIG_fail; } 98 } 99 100 #pragma SWIG nowarn=314 101 102 /* ---- classes to ignore ---- */ 103 /*Most of these are just internal structures, so there is no need to have them 104 accessible by Python. You can safely comment out any %ignore if you for some reason 105 do need them. Ignoring shrinks the library by a small amount. */ 106 //%ignore b2ContactManager; // TODO 107 %ignore b2Chunk; 108 %ignore b2DynamicTree; 109 %ignore b2DynamicTreeNode; 110 %ignore b2Island; 111 %ignore b2Position; 112 %ignore b2Velocity; 113 %ignore b2TimeStep; 114 %ignore b2Simplex; 115 %ignore b2SimplexVertex; 116 %ignore b2SimplexCache; 117 %ignore b2StackAllocator; 118 %ignore b2StackEntry; 119 %ignore b2ContactRegister; 120 %ignore b2BlockAllocator; 121 %ignore b2Timer; 122 123 /* ---- features ---- */ 124 /* Autodoc puts the basic docstrings for each function */ 125 %feature("autodoc", "1"); 126 127 /* Add callback support for the following classes */ 128 %feature("director") b2ContactListener; 129 %feature("director") b2ContactFilter; 130 %feature("director") b2DestructionListener; 131 %feature("director") b2Draw; 132 %feature("director") b2DrawExtended; 133 %feature("director") b2QueryCallback; 134 %feature("director") b2RayCastCallback; 135 136 /* ---- includes ---- */ 137 /* The order of these is important. */ 138 139 /* Doxygen-generated docstrings. Can safely be commented out. */ 140 %include "Box2D/Box2D_doxygen.i" 141 142 /* __dir__ replacement. Can safely be commented out. */ 143 %include "Box2D/Box2D_dir.i" 144 145 /* __init__ replacement allowing kwargs. Can safely be commented out, but tests will fail. */ 146 %include "Box2D/Box2D_kwargs.i" 147 148 /* __repr__ replacement -- pretty printing. Can safely be commented out. */ 149 %include "Box2D/Box2D_printing.i" 150 151 /* Miscellaneous inline code. */ 152 %include "Box2D/Box2D_inline.i" 153 154 /* Miscellaneous extended classes: b2Color, b2Version, b2DistanceProxy, b2BroadPhase */ 155 %include "Box2D/Box2D_misc.i" 156 157 /* Typemaps that allow for tuples to be used in place of vectors, 158 the removal of getAsType, etc. */ 159 %include "Box2D/Box2D_typemaps.i" 160 161 /* Contact-related classes (b2Contact, b2Manifold, etc.) */ 162 %include "Box2D/Box2D_contact.i" 163 164 /* b2Vec2, b2Vec3, b2Mat22, b2Transform, b2AABB and related extensions. */ 165 %include "Box2D/Box2D_math.i" 166 167 /* Allows for userData to be used. Also modifies CreateBody/Joint. */ 168 %include "Box2D/Box2D_userdata.i" 169 170 /* b2World only. */ 171 %include "Box2D/Box2D_world.i" 172 173 /* b2Body, b2Fixture, and related definitions. */ 174 %include "Box2D/Box2D_bodyfixture.i" 175 176 /* b2Shape, b2CircleShape, b2PolygonShape. */ 177 %include "Box2D/Box2D_shapes.i" 178 179 /* All joints and definitions. Defines b2JointTypes dict. */ 180 %include "Box2D/Box2D_joints.i" 181 182 /* Extending the debug draw class. */ 183 %include "Box2D/Box2D_debugdraw.i" 184 185 /* Include everything from the C++ library now */ 186 %include "Box2D/Box2D.h" 187 188 /* And finally tag on the secondary namespace code to the end of Box2D.py */ 189 %pythoncode %{ 190 # Backward-compatibility 191 b2LoopShape = b2ChainShape 192 193 # Initialize the alternative namespace b2.*, and clean-up the 194 # dir listing of Box2D by removing *_swigregister. 195 # 196 # To see what this is, try import Box2D; print(dir(Box2D.b2)) 197 from . import b2 198 199 s=None 200 to_remove=[] 201 for s in locals(): 202 if s.endswith('_swigregister'): 203 to_remove.append(s) 204 elif s!='b2' and s.startswith('b2'): 205 if s[2]=='_': # Covers b2_* 206 setattr(b2, s[3].lower() + s[4:], locals()[s]) 207 else: # The other b2* 208 if s[3].isupper(): 209 setattr(b2, s[2:], locals()[s]) 210 else: 211 setattr(b2, s[2].lower() + s[3:], locals()[s]) 212 for s in to_remove: 213 del locals()[s] 214 215 del s 216 del to_remove 217 %} 218 219 #endif 220 221