Ooops
January 21, 2021, 04:24:35 AM

Author Topic: Openb3d wrapper for Python  (Read 346 times)

Offline sprotz

  • Jr. Member
  • **
  • Posts: 7
Openb3d wrapper for Python
« on: November 02, 2020, 11:07:12 AM »
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:

Code: [Select]
#!/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.

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 480
Re: Openb3d wrapper for Python
« Reply #1 on: November 02, 2020, 12:16:21 PM »
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].

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 480
Re: Openb3d wrapper for Python
« Reply #2 on: November 02, 2020, 12:38:29 PM »
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

Offline sprotz

  • Jr. Member
  • **
  • Posts: 7
Re: Openb3d wrapper for Python
« Reply #3 on: November 02, 2020, 01:04:36 PM »
The only changes I made to the wrapper are:
Code: [Select]
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.

Offline sprotz

  • Jr. Member
  • **
  • Posts: 7
Re: Openb3d wrapper for Python
« Reply #4 on: November 02, 2020, 06:05:50 PM »
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.

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 480
Re: Openb3d wrapper for Python
« Reply #5 on: November 04, 2020, 12:55:38 PM »
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?

Offline sprotz

  • Jr. Member
  • **
  • Posts: 7
Re: Openb3d wrapper for Python
« Reply #6 on: November 05, 2020, 11:40:28 AM »
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.

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 480
Re: Openb3d wrapper for Python
« Reply #7 on: November 06, 2020, 10:36:06 AM »
Okay thanks, I was just wondering but yes it's obviously best to use the latest Python 3.9, and thanks for your suggestion.

Offline sprotz

  • Jr. Member
  • **
  • Posts: 7
Re: Openb3d wrapper for Python
« Reply #8 on: November 06, 2020, 05:23:15 PM »
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.  ;)

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 480
Re: Openb3d wrapper for Python
« Reply #9 on: November 07, 2020, 03:35:27 PM »
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.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal