Search code examples
pythongraphics3dsimulation

Simple 3D Graphics in Python


I am working in a project where 3D visualizations are important to see what is happening during the setup stage and perhaps for visual validation by making a short videos of what is happening.

The problem that I have is that 3D visualizations in Python are too sophisticated, and complicated to learn for what I need. I find that Mathematica is the perfect kind of software...but it is not portable and is very expensive.

Question

Is there any Python package similar to Mathematica?

Clarification

I don't want a "plotting" program, since plotting is not what I am looking for. I want to generate simple geometric shapes like spheres and cubes that can move around, this is more than enough. Give some coordinates, perhaps a rotation, and the program just shows the desired image(s) to export as a .png or make a quick video; as in Mathematica.

Packages like Pygame, Panda3D, Pyglet, etc., look too complicated and an overkill for what I need, as well as software like Blender, etc. Jupyter notebooks are similar, but they don't have the 3D graphics capabilities. I found a Python module named Fresnel, but it looks too sophisticated for what I need.

I have read several answers to this question here in Stack Overflow, but they seem outdated and not really what I am looking for.

Further Clarification

To draw spheres in Mathematica you do:
coordinates = Flatten[Table[Table[Table[ {i,j,k}, {k,1,10}], {j,1,10}], {i,1,10}],1]
spheres= Flatten[Table[Graphics3D[{Sphere[coordinates[[i]],0.5]}],{i,1,Length[coordinates]}]]
Show[{spheres}]

This is a simple quick and easy way of displaying a group of spheres. To use any program in Python to do the same, it seems like you must be an expert in 3D graphics to do this simple thing.

Programs that have capabilities of using Python scripts, like Blender, make it difficult to use the interface in a straight forward way (try doing the same in Blender, it will take a while just to learn the basics!).


Solution

  • I know several other user-friendly plotting libraries than matplotlib, but not a lot provide an interactive view. There is of course the well known vtk but it's not for end-user

    plotly

    For usage in a notebook, like jupyter and mathematica, you probably would go for plotly It's using a browser-based interface with plots very similar to mathematica

    plotly 3D plot example

    pymadcad

    If you need a more offline version and what you are looking for is some view you can rotate/zoom/pan to look on your geometry by different sides, you can take a look at pymadcad It even works with touchscreens. It is not centered on 3D visualization, so it's a bit overkill to use it only for it, but for 3D curves, 3D surfaces, spheres and cubes as you said, it can do the job

    simple plots with pymadcad:

    from madcad import *
    from madcad.rendering import Displayable
    from madcad.displays import GridDisplay
    
    # create a wire from custom points
    s = 100
    mycurve = Wire([  vec3(sin(t/s), cos(t/s), 0.1*t/s)
                      for t in range(int(s*6*pi)) ])
    
    # create a sphere
    mysphere = uvsphere(vec3(0), 0.5)
    
    # display in a separated window
    show([
           mycurve,   # displaying the curve
           mysphere,  # displaying the sphere
           Displayable(GridDisplay, vec3(0)),   # this is to have a 2D grid centered on origin
           ])
    

    result: result window (The window is dark because so is my system theme, but likely it will adapt to yours)