简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM