simple graphics for python - python

I am making a simulation in python that needs visualization. I am looking for the most simple graphics library python has. Is there anything that can let me do something along the lines of:
setWindow(10,10)
setCube(0,0,1,1,1)#windowX, windowY, width, length, hight,
updateList = simUpdate
for update in updateList:
removeShape(0,0)
setCube(0,0,1,1,1)
Is there anything that simple? 3d is not a must but it would be nice.
I'm working in python 3.3 and pygames hasn't been updated for that on a mac as far as I can tell. I've been working with tkinter but would like something a little easier.

For simple graphics, you can use graphics.py.
It's not included with Python, so you should save it as a Python file (preferably named graphics.py) where Python can see it --- on your sys.path.
Note: it is also available using pip install graphics.py see link
It's very easy to learn and has various shapes already built-in. There are no native 3D graphics (none are in there) but it's easy to do so: for a cube, draw one square and another one to the side, and connect one corner of a square to the corresponding corner in the other square.
Example using graphics.py to create a square:
from graphics import *
win = GraphWin(width = 200, height = 200) # create a window
win.setCoords(0, 0, 10, 10) # set the coordinates of the window; bottom left is (0, 0) and top right is (10, 10)
mySquare = Rectangle(Point(1, 1), Point(9, 9)) # create a rectangle from (1, 1) to (9, 9)
mySquare.draw(win) # draw it to the window
win.getMouse() # pause before closing

pygame does work with 3.x, and with Mac OS X. There are no binary installers for any recent Mac version, but that's not much of a loss, because the binary installers were never very good. You will basically have to follow the Unix install directions, and figure out for yourself how to install all the prereqs (if you use Homebrew it's just brew install sdl sdl_ttf … jpeg libpng), then figure out how to tell the setup how to find all those prereqs. You may want to look at (or maybe even use) this recipe, adapted to your own particular Python installation.
As far as I know, the other major alternatives for 3D graphics are PyOpenGL and pyglet. The former is a pretty direct wrapper around OpenGL, which is great if you think in GL terms. The latter is a higher-level library, somewhat similar to pygame but built directly on native OpenGL and native windows on each platform, instead of on SDL.
You also might want to look at Python 3D Software Collection, which is maintained by the main author of PyOpenGL. I have no idea how up-to-date it is, but he's still linking to it from the PyOpenGL site and docs.
If you're looking for something mathematically-oriented, you may want to look at matplotlib, mplot3d and friends. If you're ultimately trying to graph complex geometric shapes and transform/intersect/etc. them, this is your friend. If you're trying to build a simple game or quasi-CAD program, it's not.

Visual Python, from www.vpython.org
Designed for people who aren't OpenGL hackers. It has a retained scene graph model rather than immediate, some built in controls, and there are quite a lot of scientific / engineering simulation examples floating around the Internet.

Related

Graphics library to draw images to screen for python

I would like to know if there is a good graphics library for python that will allow me to write a console/terminal application in which I can draw images and or shapes similar to libraries that are available in C++. My applications will be developed for linux primarily but will likely also be made available for windows but it ultimately not necessary.
I already used the following libraries that offer this type of resource:
Tkinter
wxPython (https://www.wxpython.org/) - you can use it to draw shapes or images and there are a lot of built ins resources to create GUI
Pygame (https://www.pygame.org/news) - it's very useful to do physics simulations, you can use built in function to draw circles, blit images into the screen, access keys inputs, etc.
Matplotlib (https://matplotlib.org/) - this is good to mathematical purposes to draw graphs, but you can animate the graphs too
The Pygame help and docs is not user friendly (my think), but the learning curve is fast.

Render 3D model orthographically in Python

I want to write a tool in Python that will help me create isometric tiles from 3D-models. You see, I'm not a very proficient artist and free 3D-models are plentisome, and creating something like a table or chair is much easer in 3D than in painting.
This script will load a 3D model in orthographic projection and take pictures from four directions so it can be used in a game. I've tried this in Blender, but the results are inconsistent, very difficult to control and take very long time to create simple sprites.
Rolling my own script will probably let me do neat things too, especially batch-genetration, maybe on texture changes, shading, etc. The game itself will probably be made in Python tpp, so maybe I could generate on the fly. (Edit: and automatically creat cut out see-through walls for when they face camera)
Now my question, what Python libraries can do something like this? I've checked both Pyglet and Panda3D, but I haven't even been able to load a model, let alone set it to orthographic projection.
I found this code:
www.pygame.org/wiki/OBJFileLoader
It let me load and display an .obj file of a cube from Blender with ease. It runs PyOpenGL so it should let me do everything OpenGL can. Never knew OpenGL was so low-level, didn't realize I'd have to write my own loaders and everything.
Anyway, I'm pretty sure I can modify this to project isometrically, rotate the object and grab shots and combine them into sprites. Thanks you guys!
Since you looked at Panda3D - if you can convert your model to the 'egg' format (which blender/maya may do), then you could be able to import it.
https://www.panda3d.org/manual/index.php/Loading_Models
https://www.panda3d.org/manual/index.php/Models_and_Actors
http://www.panda3d.org/manual/index.php/Converting_from_Blender
Note: sources of this was 'python 3d mesh loader' in a popular search engine - this looks viable to me. I now need to try installing it and some code...

What happened to Python's rect class?

On a Google Search, I found this article:
http://docs.python.org/release/1.4/lib/node201.html
Which showed examples of using the rect class, to perform union/intersections/checking if points are inside rect. Importing rect fails in Python 2.7. Is this class in another package?
I assume the question isn't really "what happened to it?", but "where can I find a class like this that I can use?".
Most GUI libraries have a class like this. For example: wx.Rect, QRect/QRectF, gtk.gdk.Rectangle, PyGame rect.
If you want a generic rectangle class without the overhead of a GUI library, I think your only option is Sympy, which has a Geometry module that may suit your needs.
Finally, you may just choose to roll your own. It's not very hard to make such a class.
It's part of the Standard Windowing interface. To quote from the documentation you yourself linked to:
Using STDWIN is not recommended for
new applications. It has never been
ported to Microsoft Windows or Windows
NT, and for X11 or the Macintosh it
lacks important functionality -- in
particular, it has no tools for the
construction of dialogs.
The stdwin module containing rect class was obsolete since version 1.6 (see these notes) and I believe it was removed in some later version.
I think the rest of the answers have covered you. However I was in your shoes, and made a generic module to benefit more people. You can find the project here. The module works with both negative and positive numbers on a screen coordinate system (y grows downwards of screen, x grows to the right).
Some methods supported:
Distance from one rectangle to an other
See if rectangles overlap on x, y or both axis (default)
See if a point is inside a rectangle
Align rectangles

Basic Python 2D graphics?

I'm looking for a Python 2D graphics library that can basically do the following and not necessarily anything more:
Create a window of specified width and height
Set the RGB of pixel X, Y on the back buffer.
Swap buffers
...and that's it basically. I can't find anything that doesn't come with a massive amount of complex baggage.
I recommend PyQt for this - it's a GUI library/framework but it has very good drawing capabilities. If you look at the examples coming with PyQt, focusing on the graphics & drawing samples, it's quite amazing what you can do with very few lines of code.
Oh, and it does the double-buffering you mention automatically so you don't have to worry about it.
Alternatively, you can use PyGame - a library wrapping SDL, used for game development. Naturally it has very strong 2D graphics capabilities.

Can 3D OpenGL game written in Python look good and run fast?

I am planning to write an simple 3d(isometric view) game in Java using jMonkeyEngine - nothing to fancy, I just want to learn something about OpenGL and writing efficient algorithms (random map generating ones).
When I was planning what to do, I started wondering about switching to Python. I know that Python didn't come into existence to be a tool to write 3d games, but is it possible to write good looking games with this language?
I have in mind 3d graphics, nice effects and free CPU time to power to rest of game engine? I had seen good looking java games - and too be honest, I was rather shocked when I saw level of detail achieved in Runescape HD.
On the other hand, pygame.org has only 2d games, with some starting 3d projects. Are there any efficient 3d game engines for python? Is pyopengl the only alternative? Good looking games in python aren't popular or possible to achieve?
I would be grateful for any information / feedback.
If you are worried about 3D performance: Most of the performance-critical parts will be handled by OpenGL (in a C library or even in hardware), so the language you use to drive it should not matter too much.
To really find out if performance is a problem, you'd have to try it. But there is no reason why it cannot work in principle.
At any rate, you could still optimize the critical parts, either in Python or by dropping to C. You still gain Python's benefit for most of the game engine which is less performance-critical.
Yes. Eve Online does it.
http://support.eve-online.com/Pages/KB/Article.aspx?id=128
I did a EuroPython talk about my amateur attempts to drive OpenGL from Python:
http://pyvideo.org/video/381/pycon-2011--algorithmic-generation-of-opengl-geom
The latest version of the code I'm talking about is here:
https://github.com/tartley/gloopy
It's billed as a 'library', but that was naive of me: It's a bunch of personal experimental code.
Nevertheless, it demonstrates that you can move around hundreds of bits of geometry at 60fps from Python.
Although the demo above is fairly bare-bones in that it uses simply geometry and untextured faces, one thing I found is that more detailed geometry, texture mapping or other more modern graphics effects don't substantially affect the framerate. Or at least they don't affect it any worse than using the same effects in a C program. These are run on the GPU, so it doesn't make any difference at all if your program is written in Python.
One thing that is performance-sensitive from Python is if you are creating dynamic geometry on the CPU side, e.g. moving individual vertices within a shape, by bending or melting the shape. Doing this sort of per-vertex calculation in Python, then constructing a new ctypes array from the result, then shunting this geometry to the GPU, every frame, will be slow. Instead you should probably be doing this in a vertex shader.
On the other hand, if you just want affine transformations (moving objects around, rotating them, opening chests of drawers, rotating car wheels, bending a jointed robot arm) then all of this can be done by the GPU and the fact your program is written in Python makes little difference to the performance.
You might want to check out Python-Ogre. I just messed with it myself, nothing serious, but seems pretty good.
I would recommend pyglet which is a similar system to pygame, but with full bindings to OpenGL. You can start with simple 2D games to get the hang of the system and work up to 3D later. It is a more modern system than PyGame which is built around SDL which itself is a bit long in the tooth these days.
Perhaps a wee bit off topic but, if your goal is to learn Python, how about creating a game using IronPython and XNA? XNA is not OpenGL though, yet I find it an extremely simple 2D/3D engine which is fast and supports Shader Model 3.0.
Check out the Frets on Fire project -- an open source Guitar Hero alternative. It's written in Python and has decent 3D graphics in OpenGL. I would suggest checking out its sources for hints on libraries etc.
There was a Vampires game out a few years ago where most if not all of the code was in Python. Not sure if the 3D routines were in them, but it worked fine.

Categories