bullet3
bullet3 copied to clipboard
How to let BulletClient not print "argv[0]=" at initialization
I have pybullet==3.2.5
, and when I try to create a PyBullet client connection via bullet_client.BulletClient(connection_mode=p.DIRECT)
, at the very beginning, it will output "argv[0]=". This could be not very pleasant if I want to build many connections. A minimal example is the following:
import pybullet as p
from pybullet_utils import bullet_client as bc
for _ in range(10):
client = bc.BulletClient(connection_mode=p.DIRECT)
This will output
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
argv[0]=
Any idea to solve this? Thanks.
Hey, I also encountered this problem and used this workaround, although its a dirty hack :(
I was unable to find a simpler way to do this.
import ctypes
import sys
class RedirectStream(object):
@staticmethod
def _flush_c_stream(stream):
streamname = stream.name[1:-1]
libc = ctypes.CDLL(None)
libc.fflush(ctypes.c_void_p.in_dll(libc, streamname))
def __init__(self, stream=sys.stdout, file=os.devnull):
self.stream = stream
self.file = file
def __enter__(self):
self.stream.flush() # ensures python stream unaffected
self.fd = open(self.file, "w+")
self.dup_stream = os.dup(self.stream.fileno())
os.dup2(self.fd.fileno(), self.stream.fileno()) # replaces stream
def __exit__(self, type, value, traceback):
RedirectStream._flush_c_stream(self.stream) # ensures C stream buffer empty
os.dup2(self.dup_stream, self.stream.fileno()) # restores stream
os.close(self.dup_stream)
self.fd.close()
And then run the code that generates argv[0]=
inside a with RedirectStream(sys.stdout):
. So in your case,
with RedirectStream(sys.stdout):
for _ in range(10):
client = bc.BulletClient(connection_mode=p.DIRECT)
If you find a simpler way let me know :)