Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple instances of pybullet client within a python class

I am using pybullet in a python class. I import it as import pybullet as p. When I have several instances of the class using pybullet, is the class p the same for each instance or is the "variable" p unique for each instance?

foo.py

import pybullet as p

class Foo:
    def __init__(self, counter):
        physicsClient = p.connect(p.DIRECT)
    def setGravity(self):
        p.setGravity(0, 0, -9.81)
(more code)

and main.py

from foo import Foo

foo1 = Foo(1)
foo2 = Foo(2)
foo1.setGravity()

will setGravity() affect p in foo1 and foo2 or just foo1?

like image 432
FR_MPI Avatar asked Mar 03 '23 12:03

FR_MPI


1 Answers

You can use bullet_client to get two different instance. like so :

import pybullet as p
import pybullet_utils.bullet_client as bc


class Foo:
    def __init__(self, counter):
        self.physicsClient = bc.BulletClient(connection_mode=p.DIRECT)

    def setGravity(self):
        self.physicsClient.setGravity(0, 0, -9.81)


foo1 = Foo(1)
foo2 = Foo(2)
foo1.setGravity()
foo2.setGravity()

print("Adress of  foo1 bullet client 1 : " + str(foo1.physicsClient))
print("Adress of foo2 bullet client 2  : " + str(foo2.physicsClient))

Output :

Adress of  foo1 bullet client 1 : 
<pybullet_utils.bullet_client.BulletClient object at 0x7f8c25f12460>
Adress of foo2 bullet client 2  : 
<pybullet_utils.bullet_client.BulletClient object at 0x7f8c0ed5a4c0>

As you can see here : you got two different instance, each one stored in diferrent adress

See the bellow examples from the official repository : https://github.com/bulletphysics/bullet3/blob/master/examples/pybullet/gym/pybullet_utils/examples/multipleScenes.py

like image 103
jossefaz Avatar answered Mar 05 '23 17:03

jossefaz