Openb3d wrapper for Python

Started by sprotz, November 02, 2020, 11:07:12

Previous topic - Next topic

sprotz

I've got an openb3d wrapper for python working on Windows. I managed to display a window with a rotating cube. Bust still no input handling. Can anyone help me implement mouse and keyboard input in this? Here is the window I managed to make:


And I used this wrapper library here: https://mojolabs.nz/posts.php?topic=107263
I tweaked that wrapper to load Openb3d.dll and SDL.dll libs
And here is the code:

#!/usr/bin/env python
from OpenB3D import *
import msvcrt
import time
import sys

#import asyncio

angle = 0
keyhit = 0

Graphics3D(800,600,16,2)

#SetBuffer BackBuffer()

camera = CreateCamera(0)
CameraViewport(camera, 0, 0, 800, 600)

light = CreateLight( 1 ,0)
cube = CreateCube(0)
PositionEntity(cube ,-2, -2, 10,0)


while keyhit==0:
       

        global angle 
        RotateEntity(cube,0,angle,0,0)
        UpdateWorld(1)
        RenderWorld(0)
        angle +=1
        Flip()
         
        if msvcrt.kbhit()==1 and ord(msvcrt.getch()) == 27:  #escape key
          exit()
         #raise SystemExit
         #end
         #break
         
        time.sleep(0.03)



However, the keyboard input is not working using the msvcrt module or any other standard input modules. I noticed the wrapper used SDL 1.2, but I don't know how to handle SDL input on Python.

markcwm

#1
Oh nice, well done!

Could you post the tweaks you had to make to the wrapper please?

You should be able to use SDL for user input, see the section Handling the keyboard, [sorry that's not in python].

markcwm

#2
There's a python sdl wrapper pySDL2, see https://pysdl2.readthedocs.io/en/rel_0_9_4/modules/sdl2.html

This is usage of pysdl2 where there's a demo of keyboard input, using a flag to know when key_down=True, https://stackoverflow.com/questions/47211810/how-can-sdl2-sdl-getkeyboardstate-be-used-correctly

sprotz

The only changes I made to the wrapper are:
sdl=cdll.LoadLibrary("SDL.dll")
openb3d=cdll.LoadLibrary("./OpenB3D-64.dll")


I'm going to try to use this with pysdl2, thanks for the links.

sprotz

Success! I've got it working now with PySDL2 !. The program can now handle keyboard commands and Blitz3d/Openb3d can now be programmed in Python.
Here is the wrapper and example script for convenience: 
http://www.mediafire.com/file/edep6g6ahaixowx/PyOpenb3d.zip/file

Next I'm going to work on integrating the Newton physics engine, but that can very easily be integrated anyway.

markcwm

#5
Great job Sprotz and thanks for the zip! I'll have a try at it when I get the time.

I have python 2.7 installed here, would that work or would it have to be 3?

I need to follow something like the Python Crash Course to get familiar with it, there's a lot of tutorials on the Python wiki as well, any suggestions?

sprotz

Well, PySDL2 is only for Python 3, however Python 2 should be able to load an SDL2 DLL, in which case you may need to change the code in the script, and the DLL type should match the Python type, for example 64 bit Python can only handle 64 bit DLL, and 32 bit Python can only handle 32 bit DLL.
Python is easy to learn, and it is powerful and popular as well. Any web resource can be used to learn Python, but I would suggest Tutorialspoint.com.

markcwm

Okay thanks, I was just wondering but yes it's obviously best to use the latest Python 3.9, and thanks for your suggestion.

sprotz

I have now added a Newton Physics wrapper to the above files. OpenB3D has a working physics engine now! so next I'm going to try port all this to Android.  ;)

markcwm

Oh well done! Well I tried to compile to Android but it couldn't find standard C++ classes like list/string but that was for a static library in Blitzmax, so maybe you won't have this problem with a DLL. Anyway, good luck.