4

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?

jossefaz
  • 3,312
  • 4
  • 17
  • 40
FR_MPI
  • 111
  • 1
  • 9
  • 1
    `p` is not a variable in the traditional sense; it's a module, so it's the same for every `Foo` instance. – afghanimah Apr 29 '20 at 14:55
  • When you tried out what you describe, what behaviour did you observe? – khelwood Apr 29 '20 at 14:58
  • what do you mean by "affect p in foo1 and foo2 or just foo1?" ? do you mean that if you call setGravity() on foo1 will this affect foo2 gravity ? – jossefaz Apr 29 '20 at 15:11
  • By the way : in "setGravity" method, you forget to write `self` like so : `def setGravity(self)` – jossefaz Apr 29 '20 at 15:15
  • @yAzou yes if I call it in one foo[x] will it affect gravity in every foo[_] @ afghanimah is there a way to make it specific to one instance? – FR_MPI Apr 29 '20 at 15:28
  • From the official documentation you can make use of `pybullet_utils.bullet_client` to make different instance....i post an answer with your example – jossefaz Apr 29 '20 at 15:45

1 Answers1

7

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

jossefaz
  • 3,312
  • 4
  • 17
  • 40