bullet3 icon indicating copy to clipboard operation
bullet3 copied to clipboard

Opening and closing a matplotlib figure breaks pybullet

Open EtorArza opened this issue 11 months ago • 0 comments

While working on a project, I observed that "getAABB()" gives different output values if a matplotlib figure is opened and closed before using pybullet. This might be related to Issue #2328 although I am not sure. I opened this issue to save some time if someone encounters the same issue. The workaround is easy: do not use matplotlib when working with pybullet.

To reproduce the bug, I modified the example getAABB.py to load (or not) matplotlib and open and close a figure. The code is shown below. The bug only happens if load_matplotlib_fig is 1. I only changed the code to add the part that loads the figure.

Code to reproduce the bug

import pybullet as p
import pybullet_data



draw = 1
printtext = 1
load_matplotlib_fig = 1

if load_matplotlib_fig:
    import numpy as np
    from matplotlib import pyplot as plt
    print("We load a figure with random noise and then we close it.")    
    plt.figure()
    image_data = np.random.random((100, 100))
    plt.imshow(image_data, cmap='gray')
    plt.close()

if (draw):
  p.connect(p.GUI)
else:
  p.connect(p.DIRECT)




p.setAdditionalSearchPath(pybullet_data.getDataPath())
r2d2 = p.loadURDF("r2d2.urdf")


def drawAABB(aabb):
  f = [aabbMin[0], aabbMin[1], aabbMin[2]]
  t = [aabbMax[0], aabbMin[1], aabbMin[2]]
  p.addUserDebugLine(f, t, [1, 0, 0])
  f = [aabbMin[0], aabbMin[1], aabbMin[2]]
  t = [aabbMin[0], aabbMax[1], aabbMin[2]]
  p.addUserDebugLine(f, t, [0, 1, 0])
  f = [aabbMin[0], aabbMin[1], aabbMin[2]]
  t = [aabbMin[0], aabbMin[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [0, 0, 1])

  f = [aabbMin[0], aabbMin[1], aabbMax[2]]
  t = [aabbMin[0], aabbMax[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMin[0], aabbMin[1], aabbMax[2]]
  t = [aabbMax[0], aabbMin[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMax[0], aabbMin[1], aabbMin[2]]
  t = [aabbMax[0], aabbMin[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMax[0], aabbMin[1], aabbMin[2]]
  t = [aabbMax[0], aabbMax[1], aabbMin[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMax[0], aabbMax[1], aabbMin[2]]
  t = [aabbMin[0], aabbMax[1], aabbMin[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMin[0], aabbMax[1], aabbMin[2]]
  t = [aabbMin[0], aabbMax[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])

  f = [aabbMax[0], aabbMax[1], aabbMax[2]]
  t = [aabbMin[0], aabbMax[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1.0, 0.5, 0.5])
  f = [aabbMax[0], aabbMax[1], aabbMax[2]]
  t = [aabbMax[0], aabbMin[1], aabbMax[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])
  f = [aabbMax[0], aabbMax[1], aabbMax[2]]
  t = [aabbMax[0], aabbMax[1], aabbMin[2]]
  p.addUserDebugLine(f, t, [1, 1, 1])


aabb = p.getAABB(r2d2)
aabbMin = aabb[0]
aabbMax = aabb[1]
if (printtext):
  print(aabbMin)
  print(aabbMax)
if (draw == 1):
  drawAABB(aabb)

for i in range(p.getNumJoints(r2d2)):
  aabb = p.getAABB(r2d2, i)
  aabbMin = aabb[0]
  aabbMax = aabb[1]
  if (printtext):
    print(aabbMin)
    print(aabbMax)
  if (draw == 1):
    drawAABB(aabb)

while (1):
  a = 0
  p.stepSimulation()


The output and the visualization are also different if the figure is opened or not at the beginning. This is the output and the visualization when we do not load a figure:

Output & screenshot when *not* loading matplotlib
pybullet build time: May 20 2022 19:43:01
startThreads creating 1 threads.
starting thread 0
started thread 0 
argc=2
argv[0] = --unused
argv[1] = --start_demo_name=Physics Server
ExampleBrowserThreadFunc started
X11 functions dynamically loaded using dlopen/dlsym OK!
X11 functions dynamically loaded using dlopen/dlsym OK!
Creating context
Created GL 3.3 context
Direct GLX rendering context obtained
Making context current
GL_VENDOR=AMD
GL_RENDERER=RENOIR (renoir, LLVM 15.0.7, DRM 3.47, 5.19.0-46-generic)
GL_VERSION=4.6 (Core Profile) Mesa 22.2.5-0ubuntu0.1~22.04.3
GL_SHADING_LANGUAGE_VERSION=4.60
pthread_getconcurrency()=0
Version = 4.6 (Core Profile) Mesa 22.2.5-0ubuntu0.1~22.04.3
Vendor = AMD
Renderer = RENOIR (renoir, LLVM 15.0.7, DRM 3.47, 5.19.0-46-generic)
b3Printf: Selected demo: Physics Server
startThreads creating 1 threads.
starting thread 0
started thread 0 
MotionThreadFunc thread started
ven = AMD
ven = AMD
(-0.17200000000000001, -0.17200000000000001, -0.302)
(0.17200000000000001, 0.17200000000000001, 0.302)
(0.16898605568946856, -0.101, -0.35100236234354004)
(0.27101394431053144, 0.101, 0.25100236234354006)
(0.16999999999999993, -0.2, -0.40000000000000013)
(0.26999999999999996, 0.2, -0.30000000000000016)
(0.16799828596438987, 0.09633333333300007, -0.4720024089536297)
(0.27200171403561, 0.17033333333300008, -0.3979975910463706)
(0.16799828596438987, -0.17033333333300008, -0.4720024089536297)
(0.27200171403561, -0.09633333333300007, -0.3979975910463706)
(-0.2710139443105315, -0.101, -0.35100236234354004)
(-0.16898605568946867, 0.101, 0.25100236234354006)
(-0.27000000000000013, -0.2, -0.40000000000000013)
(-0.17000000000000015, 0.2, -0.30000000000000016)
(-0.27200171403561024, 0.09633333333300007, -0.4720024089536297)
(-0.16799828596439007, 0.17033333333300008, -0.3979975910463706)
(-0.27200171403561024, -0.17033333333300008, -0.4720024089536297)
(-0.16799828596439007, -0.09633333333300007, -0.3979975910463706)
(-0.013000138992183543, 0.1869988419428027, 0.18699527467979934)
(0.013009404351159428, 0.3930011578425803, 0.2130047253202008)
(-0.06775906337920796, 0.3788265253910151, 0.1702498897463084)
(-0.009009770394090368, 0.4874014459929719, 0.22975227464735515)
(-0.05673869938717402, 0.45277774448476804, 0.18551946571186562)
(-0.005994157175350845, 0.5155914645712579, 0.21448435550465816)
(0.00802949267915131, 0.3778229214400647, 0.16924329962117776)
(0.06878439117311044, 0.4883979349488581, 0.23075112713368154)
(-0.0038798349859614743, 0.4527752955872638, 0.18551279074176535)
(0.04686720541047727, 0.5155890157894866, 0.21448219650602468)
(-0.16, -0.16, 0.13999999999999999)
(0.16, 0.16, 0.45999999999999996)
(-0.04, 0.0814, 0.3814)
(0.04, 0.1614, 0.4614)

Screenshot without loading figure

This is the output and the visualization when we do load a figure:

Output & screenshot when loading matplotlib
pybullet build time: May 20 2022 19:43:01
We load a figure with random noise and then we close it.
startThreads creating 1 threads.
starting thread 0
started thread 0 
argc=2
argv[0] = --unused
argv[1] = --start_demo_name=Physics Server
ExampleBrowserThreadFunc started
X11 functions dynamically loaded using dlopen/dlsym OK!
X11 functions dynamically loaded using dlopen/dlsym OK!
Creating context
Created GL 3.3 context
Direct GLX rendering context obtained
Making context current
GL_VENDOR=AMD
GL_RENDERER=RENOIR (renoir, LLVM 15.0.7, DRM 3.47, 5.19.0-46-generic)
GL_VERSION=4.6 (Core Profile) Mesa 22.2.5-0ubuntu0.1~22.04.3
GL_SHADING_LANGUAGE_VERSION=4.60
pthread_getconcurrency()=0
Version = 4.6 (Core Profile) Mesa 22.2.5-0ubuntu0.1~22.04.3
Vendor = AMD
Renderer = RENOIR (renoir, LLVM 15.0.7, DRM 3.47, 5.19.0-46-generic)
b3Printf: Selected demo: Physics Server
startThreads creating 1 threads.
starting thread 0
started thread 0 
MotionThreadFunc thread started
ven = AMD
ven = AMD
(-0.002, -0.002, -0.002)
(0.002, 0.002, 0.002)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(-0.002763546581352073, -0.002, -0.002763546581352073)
(0.002763546581352073, 0.002, 0.002763546581352073)
(-0.002763546581352073, -0.002, -0.002763546581352073)
(0.002763546581352073, 0.002, 0.002763546581352073)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(-0.002763546581352073, -0.002, -0.002763546581352073)
(0.002763546581352073, 0.002, 0.002763546581352073)
(-0.002763546581352073, -0.002, -0.002763546581352073)
(0.002763546581352073, 0.002, 0.002763546581352073)
(-0.004557865850570369, -0.0047878221657851406, -0.003763546581352073)
(0.004557865850570369, 0.0047878221657851406, 0.003763546581352073)
(-0.04623250694915617, -0.015333108646533325, -0.02975011025369168)
(0.06186089572327253, 0.10776690769701987, 0.029752274647355085)
(-0.0408263505492684, -0.032911750075899304, -0.01348053428813445)
(0.03304484202212981, 0.04459446925494098, 0.013484355504658097)
(-0.02009602090087989, -0.038603424353189086, -0.03719552255361835)
(0.09733205405403186, 0.09147940986278869, 0.03200138293101714)
(-0.03283687722910239, -0.04210318636114759, -0.018696030229231435)
(0.04658944478750421, 0.039959034294695724, 0.01687742789680729)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)

Screenshot when loading a figure

Same screenshot but zoomed in:

Screenshot when loading a figure zoom

EtorArza avatar Jul 21 '23 12:07 EtorArza