我在一个python类中使用了pybullet。我将其导入为import pybullet as p。当我有几个使用pybullet的类实例时,每个实例的p类是相同的还是每个实例的“变量”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?
发布于 2020-04-29 23:43:39
您可以使用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
https://stackoverflow.com/questions/61504794
复制相似问题