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?

6
  • 1
    p is not a variable in the traditional sense; it's a module, so it's the same for every Foo instance. Commented Apr 29, 2020 at 14:55
  • When you tried out what you describe, what behaviour did you observe? Commented Apr 29, 2020 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 ? Commented Apr 29, 2020 at 15:11
  • By the way : in "setGravity" method, you forget to write self like so : def setGravity(self) Commented Apr 29, 2020 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? Commented Apr 29, 2020 at 15:28

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.