Is it possible to import and display 3D objects/meshes?

2,073 views
Skip to first unread message

William Bolton

unread,
May 8, 2018, 6:27:22 AM5/8/18
to VPython-users
Hi all,

Brand new to VPython but not new to 3D objects.  I have created some 3D models elsewhere that I would like to import into VPython.  Is this even possible?

Thanks,
BB

Bruce Sherwood

unread,
May 8, 2018, 9:00:24 AM5/8/18
to VPython-users
It depends on the details of the data structures of your existing models how easy or difficult this is. Take the simplest case, of the model consisting of a mesh of vertexes and triangles (or quads). The following page in the VPython documentation explains how to use these low-level objects:


It is usually advantageous to put the triangles (and/or quads) into a list tris= [t, t, t, ....] and pass this to the compound object, c = compound(tris). Now your model is stored in GPU memory and can be used just like other VPython objects; you can move it simply by changing c.pos or reorient it by changing c.axis or change its size by changing c.size. The object will be displayed very fast due to being directly available to the GPU. You can also make clones, such as d = c.clone(), and the clones will also be displayed very fast. If you want the clones to have individual (overall) colors, make the original triangles be white, as changing the color of a compound involves multiplicative color.

Bruce

William Bolton

unread,
May 8, 2018, 1:50:45 PM5/8/18
to VPython-users
Bruce,

Thank you very much for the quick response, there is light at the end of the tunnel.

What we are trying to do is create a visual model of our robot arm using the joint position/rotation data direct from the robot (controlled by Python).

I have created 3D models of the arm in Lightwave that are photo-realistic and I can export the models into several different formats including .STL's (triangle polys) that are simplified enough to display with VPython.  It seems to me that the hard part is going to be converting the .STL's into list(s). 

Thanks,
Bill Bolton
Altius Space Machines

Bruce Sherwood

unread,
May 8, 2018, 4:16:40 PM5/8/18
to VPython-users
Since it is STL, I was able to partially update an old (VPython 6) STL converter which is now available as a zip file (with sample STL files included) at


The section for binary files needs to be rewritten; I didn't have a binary STL file to try. If you do have a binary file, perhaps you can update that section of the converter program, based on what I did for text files.

I also ignore color, as the documentation I found about binary files indicated that it's not possible for my program to distinguish between two different color codings.

I also did not attempt to assign texpos coordinates to the vertexes, so one can't assign a texture in a meaningful way. Again, this might be something you could contribute.

Bruce

Nahuel

unread,
Mar 3, 2022, 6:38:50 AM3/3/22
to VPython-users
Hello Bruce,

Thank you very much for your program!
I don't have much experience in programming languages, but I was able to import my own 3D model to Vpython thanks to your program.

Is there a new version of this program which could include the colors?

Best regards,
Nahuel

John

unread,
Mar 3, 2022, 7:53:23 PM3/3/22
to VPython-users
If your STL file contains color information then you could probably create a custom version of an STL reader to display the colors. Here is an example in webvpython.


You can then try it with the attached STL file which contains color information for each vertex. You should see a cube with different color faces.

However, normally an STL file does not include color information.
Cube_3d_printing_sample4-ascii.stl

John

unread,
Mar 3, 2022, 8:17:47 PM3/3/22
to VPython-users
I should point out that STL files don't normally contain color information. There are some non-standard ways of adding color to binary STL files. See the "Color in binary STL" section of this link


However this hasn't been implemented in vpython and you would probably need to create your own custom binary STL reader to add this capability.

John

unread,
Mar 6, 2022, 7:50:35 PM3/6/22
to VPython-users
It is also possible to load an OBJ file into vpython by means of a python package like "PyWaveFront"


which has github repository


In this repository they have an examples directory where they load the following 6 OBJ files of box objects with different properties into a 3D scene.


and produce the following result.

pywavefront.jpg



I recreated this result in vpython loading the same 6 OBJ files with color and textures.

vpython.jpg

I put this project in github if anyone wants to try it out.


You will need to install pywavefront

     pip install pywavefront

then try running the program

python vpython_load_obj_test.py

or the notebook

vpython_load_obj_file_test.ipynb

You can also try it on mybinder.


So yes it is possible to load colored 3D meshes into vpython via an OBJ file as this example shows.

John

Bruce Sherwood

unread,
Mar 6, 2022, 9:17:30 PM3/6/22
to VPython-users
Thanks much for this work, John!

Bruce

Reply all
Reply to author
Forward
0 new messages