在 python class 中创建多个 pybullet 客户端实例

Create multiple instances of pybullet client within a python class

我在 python class 中使用 pybullet。我将其导入为 import pybullet as p。 当我有多个使用 pybullet 的 class 实例时,每个实例的 class p 是相同的还是每个实例的 "variable" p 都是唯一的?

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)

和main.py

from foo import Foo

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

setGravity() 会影响 foo1 和 foo2 中的 p 还是只影响 foo1?

您可以使用bullet_client 来获取两个不同的实例。像这样:

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))

输出:

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>

如您所见:您有两个不同的实例,每个都存储在不同的地址中

请参阅官方存储库中的以下示例: https://github.com/bulletphysics/bullet3/blob/master/examples/pybullet/gym/pybullet_utils/examples/multipleScenes.py