PyODE Tutorial 3

PyODE head family tutorial 3 was carried out

Original PyODE site

"Tutorial 3" is a sample of contact judgment. The visualization is PyOpenGL. -PyOpenGL setup is required. -Since it is OpenGL, there are many visualization code descriptions because it is 3D.

PyOpenGL setup

It was a little difficult. ・ Https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl I got "PyOpenGL-3.1.5-cp38-cp38-win_amd64.whl" from and installed it with pip. → Tutorial 3 works normally.

result

"Tutorial 3" has moved. I added an image saving routine to make a GIF movie. (Use Pillow) tutorial3.gif

・ The value of the viewpoint has been changed from the original code.

Image storage routine

Outputs a PNG image group and one GIF video as a file. Only the additional part will be placed on ↓.

How to use: -Install "Pillow". -Create two current folders "img" and "gif" in advance before execution. -Call the additional routine "capture ()" after the original line 231 "glutSwapBuffers ()". ・ Similarly, on line 220, call "export_movie ()" before "sys.exit (0)".

Generate snapshot from PyOpenGL/Save


from PIL import Image
from PIL import ImageOps

step=-1
intvl = 10
imgs = []
def capture():
  global step
  step += 1

  if step % intvl != 0:
      return

  pad_step = '{0:04d}'.format(step)
  print( pad_step )
  savepath = "img/tutorial3_"+pad_step+".png "
 
  width = glutGet(GLUT_WINDOW_WIDTH)
  height = glutGet(GLUT_WINDOW_HEIGHT)
 
  glReadBuffer(GL_FRONT)
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
  data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
 
  #image = Image.fromstring("RGBA", (width, height), data)
  image = Image.frombytes("RGBA", (width, height), data)
  image = ImageOps.flip(image)
  image.save( savepath )
  imgs.append( image )

GIF video save routine



def export_movie():
    if not imgs:
        return
    imgs[0].save('gif/tutorial3.gif'
               , save_all=True
               , append_images=imgs[1:]
               , optimize=False
               , duration=100 #40
               , loop=0)

Recommended Posts

PyODE Tutorial 2
PyODE Tutorial 1
PyODE Tutorial 3
Python tutorial
TensorFlow tutorial tutorial
Quantopian Tutorial LESSON 10
RabbitMQ Tutorial 5 (Topic)
Quantopian Tutorial LESSON 8
Quantopian Tutorial LESSON 6
Python Django Tutorial (5)
Python Django Tutorial (2)
Python tutorial summary
RabbitMQ Tutorial 6 (RPC)
django tutorial memo
Ryu tutorial Addendum
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Quantopian Tutorial LESSON 9
Quantopian Tutorial LESSON 5
Quantopian Tutorial LESSON 3
Quantopian Tutorial LESSON 7
5 minute gRPC tutorial
Python Django Tutorial (7)
Python Django Tutorial (1)
Quantopian Tutorial LESSON 4
Python Django tutorial tutorial
Try using PyODE
Quantopian Tutorial LESSON 11
Python Django Tutorial (3)
RabbitMQ Tutorial 4 (Routing)
zipline Beginner Tutorial
[Translation] hyperopt tutorial
Python Django Tutorial (4)