From 60b60ef9fd2517ad6287c7a9da075b0088eb2d35 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Mon, 14 Aug 2017 17:02:20 -0700 Subject: [PATCH] add pybullet.connect(pybullet.GUI_SERVER) option. This allows shared memory connections, acting as a physics server. You can connect using SHARED_MEMORY to this GUI_SERVER. --- .../InProcessExampleBrowser.cpp | 9 ++++---- .../ExampleBrowser/InProcessExampleBrowser.h | 4 ++-- .../PhysicsClientSharedMemory.cpp | 18 ++++++++++----- .../SharedMemoryInProcessPhysicsC_API.cpp | 22 +++++++++---------- .../SharedMemoryInProcessPhysicsC_API.h | 4 ++-- examples/SharedMemory/SharedMemoryPublic.h | 1 + examples/pybullet/pybullet.c | 18 +++++++++++++-- 7 files changed, 49 insertions(+), 27 deletions(-) diff --git a/examples/ExampleBrowser/InProcessExampleBrowser.cpp b/examples/ExampleBrowser/InProcessExampleBrowser.cpp index 9c0572626..7b42da5be 100644 --- a/examples/ExampleBrowser/InProcessExampleBrowser.cpp +++ b/examples/ExampleBrowser/InProcessExampleBrowser.cpp @@ -316,11 +316,12 @@ struct btInProcessExampleBrowserInternalData -btInProcessExampleBrowserInternalData* btCreateInProcessExampleBrowser(int argc,char** argv2) +btInProcessExampleBrowserInternalData* btCreateInProcessExampleBrowser(int argc,char** argv2, bool useInProcessMemory) { btInProcessExampleBrowserInternalData* data = new btInProcessExampleBrowserInternalData; - data->m_sharedMem = new InProcessMemory; + + data->m_sharedMem = useInProcessMemory ? new InProcessMemory : 0; int numThreads = 1; int i; @@ -410,12 +411,12 @@ struct btInProcessExampleBrowserMainThreadInternalData b3Clock m_clock; }; -btInProcessExampleBrowserMainThreadInternalData* btCreateInProcessExampleBrowserMainThread(int argc,char** argv) +btInProcessExampleBrowserMainThreadInternalData* btCreateInProcessExampleBrowserMainThread(int argc,char** argv, bool useInProcessMemory) { btInProcessExampleBrowserMainThreadInternalData* data = new btInProcessExampleBrowserMainThreadInternalData; data->m_examples.initExampleEntries(); data->m_exampleBrowser = new DefaultBrowser(&data->m_examples); - data->m_sharedMem = new InProcessMemory; + data->m_sharedMem = useInProcessMemory ? new InProcessMemory : 0; data->m_exampleBrowser->setSharedMemoryInterface(data->m_sharedMem ); bool init; init = data->m_exampleBrowser->init(argc,argv); diff --git a/examples/ExampleBrowser/InProcessExampleBrowser.h b/examples/ExampleBrowser/InProcessExampleBrowser.h index e01d3fd91..32c2c7106 100644 --- a/examples/ExampleBrowser/InProcessExampleBrowser.h +++ b/examples/ExampleBrowser/InProcessExampleBrowser.h @@ -3,7 +3,7 @@ struct btInProcessExampleBrowserInternalData; -btInProcessExampleBrowserInternalData* btCreateInProcessExampleBrowser(int argc,char** argv2); +btInProcessExampleBrowserInternalData* btCreateInProcessExampleBrowser(int argc,char** argv2, bool useInProcessMemory); bool btIsExampleBrowserTerminated(btInProcessExampleBrowserInternalData* data); @@ -17,7 +17,7 @@ class SharedMemoryInterface* btGetSharedMemoryInterface(btInProcessExampleBrowse struct btInProcessExampleBrowserMainThreadInternalData; -btInProcessExampleBrowserMainThreadInternalData* btCreateInProcessExampleBrowserMainThread(int argc,char** argv2); +btInProcessExampleBrowserMainThreadInternalData* btCreateInProcessExampleBrowserMainThread(int argc,char** argv2, bool useInProcessMemory); bool btIsExampleBrowserMainThreadTerminated(btInProcessExampleBrowserMainThreadInternalData* data); diff --git a/examples/SharedMemory/PhysicsClientSharedMemory.cpp b/examples/SharedMemory/PhysicsClientSharedMemory.cpp index 12585e13b..5e297d28f 100644 --- a/examples/SharedMemory/PhysicsClientSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsClientSharedMemory.cpp @@ -251,17 +251,23 @@ void PhysicsClientSharedMemory::resetData() m_data->m_userConstraintInfoMap.clear(); } -void PhysicsClientSharedMemory::setSharedMemoryKey(int key) { m_data->m_sharedMemoryKey = key; } +void PhysicsClientSharedMemory::setSharedMemoryKey(int key) +{ + m_data->m_sharedMemoryKey = key; +} void PhysicsClientSharedMemory::setSharedMemoryInterface(class SharedMemoryInterface* sharedMem) { - if (m_data->m_sharedMemory && m_data->m_ownsSharedMemory) + if (sharedMem) { - delete m_data->m_sharedMemory; - } - m_data->m_ownsSharedMemory = false; - m_data->m_sharedMemory = sharedMem; + if (m_data->m_sharedMemory && m_data->m_ownsSharedMemory) + { + delete m_data->m_sharedMemory; + } + m_data->m_ownsSharedMemory = false; + m_data->m_sharedMemory = sharedMem; + }; } void PhysicsClientSharedMemory::disconnectSharedMemory() { diff --git a/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.cpp b/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.cpp index 142d42a30..2def80e1c 100644 --- a/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.cpp +++ b/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.cpp @@ -5,7 +5,7 @@ #include "PhysicsClientSharedMemory.h" #include"../ExampleBrowser/InProcessExampleBrowser.h" #include - +#include #include "PhysicsServerExampleBullet2.h" #include "../CommonInterfaces/CommonExampleInterface.h" @@ -19,7 +19,7 @@ class InProcessPhysicsClientSharedMemoryMainThread : public PhysicsClientSharedM public: - InProcessPhysicsClientSharedMemoryMainThread(int argc, char* argv[]) + InProcessPhysicsClientSharedMemoryMainThread(int argc, char* argv[], bool useInProcessMemory) { int newargc = argc+2; char** newargv = (char**)malloc(sizeof(void*)*newargc); @@ -30,7 +30,7 @@ public: char* t1 = (char*)"--start_demo_name=Physics Server"; newargv[argc] = t0; newargv[argc+1] = t1; - m_data = btCreateInProcessExampleBrowserMainThread(newargc,newargv); + m_data = btCreateInProcessExampleBrowserMainThread(newargc,newargv, useInProcessMemory); SharedMemoryInterface* shMem = btGetSharedMemoryInterfaceMainThread(m_data); setSharedMemoryInterface(shMem); @@ -83,9 +83,9 @@ public: }; -b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[]) +b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[], int useInProcessMemory) { - InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv); + InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv, useInProcessMemory); cl->setSharedMemoryKey(SHARED_MEMORY_KEY); cl->connect(); return (b3PhysicsClientHandle ) cl; @@ -98,7 +98,7 @@ class InProcessPhysicsClientSharedMemory : public PhysicsClientSharedMemory public: - InProcessPhysicsClientSharedMemory(int argc, char* argv[]) + InProcessPhysicsClientSharedMemory(int argc, char* argv[], bool useInProcessMemory) { int newargc = argc+2; m_newargv = (char**)malloc(sizeof(void*)*newargc); @@ -109,7 +109,7 @@ public: char* t1 = (char*)"--start_demo_name=Physics Server"; m_newargv[argc] = t0; m_newargv[argc+1] = t1; - m_data = btCreateInProcessExampleBrowser(newargc,m_newargv); + m_data = btCreateInProcessExampleBrowser(newargc,m_newargv, useInProcessMemory); SharedMemoryInterface* shMem = btGetSharedMemoryInterface(m_data); setSharedMemoryInterface(shMem); } @@ -123,11 +123,11 @@ public: }; -b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[]) -{ +b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[], int useInProcessMemory) +{ - InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv); - cl->setSharedMemoryKey(SHARED_MEMORY_KEY); + InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv, useInProcessMemory); + cl->setSharedMemoryKey(SHARED_MEMORY_KEY+1); cl->connect(); return (b3PhysicsClientHandle ) cl; } diff --git a/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.h b/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.h index 7ce6e024a..0bad1283a 100644 --- a/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.h +++ b/examples/SharedMemory/SharedMemoryInProcessPhysicsC_API.h @@ -10,9 +10,9 @@ extern "C" { ///think more about naming. The b3ConnectPhysicsLoopback -b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[]); +b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[], int useInProcessMemory); -b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[]); +b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[], int useInProcessMemory); b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect(void* guiHelperPtr); diff --git a/examples/SharedMemory/SharedMemoryPublic.h b/examples/SharedMemory/SharedMemoryPublic.h index 55de32c57..4b61142d4 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -577,6 +577,7 @@ enum eCONNECT_METHOD { eCONNECT_UDP = 4, eCONNECT_TCP = 5, eCONNECT_EXISTING_EXAMPLE_BROWSER=6, + eCONNECT_GUI_SERVER=7, }; enum eURDF_Flags diff --git a/examples/pybullet/pybullet.c b/examples/pybullet/pybullet.c index 2b8acac81..02e3a5d42 100644 --- a/examples/pybullet/pybullet.c +++ b/examples/pybullet/pybullet.c @@ -294,6 +294,7 @@ static PyObject* pybullet_connectPhysicsServer(PyObject* self, PyObject* args, P int method = eCONNECT_GUI; int i; char* options=""; + b3PhysicsClientHandle sm = 0; if (sNumPhysicsClients >= MAX_PHYSICS_CLIENTS) @@ -354,9 +355,21 @@ static PyObject* pybullet_connectPhysicsServer(PyObject* self, PyObject* args, P char* argv[2] = {"unused",options}; #ifdef __APPLE__ - sm = b3CreateInProcessPhysicsServerAndConnectMainThread(argc, argv); + sm = b3CreateInProcessPhysicsServerAndConnectMainThread(argc, argv,1); #else - sm = b3CreateInProcessPhysicsServerAndConnect(argc, argv); + sm = b3CreateInProcessPhysicsServerAndConnect(argc, argv,1); +#endif + break; + } + case eCONNECT_GUI_SERVER: + { + int argc = 2; + char* argv[2] = {"unused",options}; + +#ifdef __APPLE__ + sm = b3CreateInProcessPhysicsServerAndConnectMainThread(argc, argv,0); +#else + sm = b3CreateInProcessPhysicsServerAndConnect(argc, argv,0); #endif break; } @@ -7261,6 +7274,7 @@ initpybullet(void) PyModule_AddIntConstant(m, "GUI", eCONNECT_GUI); // user read PyModule_AddIntConstant(m, "UDP", eCONNECT_UDP); // user read PyModule_AddIntConstant(m, "TCP", eCONNECT_TCP); // user read + PyModule_AddIntConstant(m, "GUI_SERVER", eCONNECT_GUI_SERVER); // user read PyModule_AddIntConstant(m, "JOINT_REVOLUTE", eRevoluteType); // user read PyModule_AddIntConstant(m, "JOINT_PRISMATIC", ePrismaticType); // user read