Import any 3D OBJ or STL file into vpython

590 views
Skip to first unread message

Gudimetla Kowshik

unread,
Oct 25, 2023, 10:25:00 AM10/25/23
to VPython-users
Hi, 

I have a 3D obj file as attached and I am trying to import it into my existing vpython program but honestly I tried many methods and failed. 

If there's any already tried working method please shed some light as I am stuck. 

Thanks,
G Kowshik
milk.obj
cup.obj

John

unread,
Oct 25, 2023, 12:53:38 PM10/25/23
to VPython-users
I tinkered with reading OBJ and STL files into webvpython and Vpython 7 a couple of years ago. Here are some example webvython demo programs for which I was able to load your milk.obj and cup.obj files.


You can view the code by clicking on the "Edit this program" link.

I also had a few webvpython demo programs that could read STL and binary STL files into web vpython.





These work for smaller OBJ and STL files but for large files you should use some python package in VPython 7 that can load STL files fast like this one

and then write some code to display it in a vpython scene.

John

Bruce Sherwood

unread,
Oct 25, 2023, 2:10:58 PM10/25/23
to VPython-users
From the "Frequently asked questions" at vpython.org:

A: For converting 3D models in STL text format, download this zip file and unzip it. It includes the program convert_stl.py and two STL text format models. See the end of convert_stl.py for an example of how to use this module. Currently the program does not handle STL binary files, and it ignores color information. The progam does not attempt to assign texpos coordinates to the vertex objects, so one can't assign a texture in a meaningful way.

However, I'm guessing that John's tools are more powerful and up to date.

Bruce

Gudimetla Kowshik

unread,
Oct 26, 2023, 2:36:30 PM10/26/23
to VPython-users
Hi Bruce and John,

Thanks for your reply. I've tried out your method and they work fine. However, I want to import the colour data of these objects as well. I have the relevant texture, mtl files. Can I know how I can import those as well instead of importing just the obj file which renders just the outlines?

Thanks,
G Kowshik

John

unread,
Oct 26, 2023, 4:03:50 PM10/26/23
to VPython-users
Try the OBJ-Color demo to see if it gives you color.


I imported a colored ladybug.obj file and it output a colored ladybug .

ladybug.jpg

Attached is the ladybug-obj.zip file which contains all the materials and colors for this object so you can try it yourself in the above demo to see if it works for you.

John
ladybug-obj.zip

John

unread,
Oct 26, 2023, 4:39:29 PM10/26/23
to VPython-users
If you are looking for an STL viewer that can load large colored STL files then you can try this one which is free.


You can then compare the result with the demo programs for STL that I provided earlier.

John

unread,
Oct 26, 2023, 9:03:07 PM10/26/23
to VPython-users
Also have a look at this response from last year on this forum on March 6, 2022  for a demo of loading OBJ files with color and materials into VPython 7 with sample code on github.


John

John

unread,
Oct 27, 2023, 10:48:00 PM10/27/23
to VPython-users
I changed the first line in the webvpython demo programs to 

Web VPython 3.2

from

GlowScript 3.1 VPython

and I also commented out the code for a 64,000 limit of compound objects because in webgl the limit doesn't exist anymore according to this post.


#                if vertices > 64000:
#                    print(vertices)
#                    ret.append(compound(tris))
#                    tris = []
#                    vertices = 0

John

Gudimetla Kowshik

unread,
Oct 30, 2023, 9:12:50 AM10/30/23
to VPython-users
Hi John,

Much thanks for the reply. I have one quick doubt, in case I am designing or wanting to import a custom 3D component what files do I need to export from blender? Is there a possibility to read the ".blend" files directly through vpython?

Thanks,
G Kowshik

Gudimetla Kowshik

unread,
Oct 30, 2023, 9:24:42 AM10/30/23
to vpytho...@googlegroups.com
Also, I tried to import this model into the Color OBJ example you shared and I am able not able to load the color data even through I had the mtl layers. Where am I going wrong?? 

Thanks,
G Kowshik

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/f958081d-f01e-4141-a914-15024e5623bcn%40googlegroups.com.


--
Thanks,
G Kowshik
milk_blender.obj
milk_blender.mtl

John

unread,
Oct 30, 2023, 10:25:03 AM10/30/23
to VPython-users
I am guessing that  since you mentioned blender that you looked at this example that I posed.

since the example files like box-C3F_V3F.obj start with lines that mention blender and the contents looks like 


If you read the PyWavefront package description on pypi.org 


It mentions how to generate the files from Blender.

Generating a Wavefront file with Blender

The following presumes you are using Blender to generate your mesh:

  • Using Blender, create a mesh with a UV-mapped texture. The UV-mapping is important! If it is working properly, you will see the texture applied within Blender's 3d view.
  • Export the mesh from Blender using the Wavefront format, including normals.
  • Reference your *.obj file as in the pywavefront example above.

All I did in my demo on github was reproduce the result that they generated with different box color and material options. 

You can try out this demo yourself by cloning this repository and executing the command

python vpython_load_obj_test.py

You should read through the file vpython_load_obj_test.py to see how it converts these different OBJ files with different material and color options into VPython output.


You will need to write your own code to parse OBJ files to display the result in VPython.  Support for reading OBJ and STL files is NOT provided by the VPython package, this is code that you will need to create yourself. I have provided several examples of how it can be done but you will need to write your own version of the code to get it to do what you are looking to do. 

John

John

unread,
Oct 30, 2023, 10:49:23 AM10/30/23
to VPython-users
You are going to need to write your own code to read in and parse these OBJ and MTL files. Support for loading OBJ and STL files into VPython is not provided natively by the VPython. The demo programs I provided are just examples of how it could be done but you will need to write your own version of the software to read in OBJ files and display them in VPython. You can read the code of the OBJ_Color demo


to see what it is doing and then create your own version of this code to get it to do what you want. For VPython 7 using you can look at this example.

For loading colors and materials of box shapes generated by blender.

John

John

unread,
Oct 31, 2023, 1:50:25 AM10/31/23
to VPython-users
You milk_blender.obj file looks similar to the demo file


Which contains vertices, normals and texture elements.

v -1.000000 1.000000 -1.000000
vt 0.0000 0.0000
vn 0.0000 -1.0000 0.0000

and that box with the texture is displayed in the demo program.


So you should compare the milk_blender.mtl file with the box.mtl file contents.


and see if you can get your milk_blender.obj file to load with the demo program


try modifying this by adding the line

box7 = Wavefront('milk_blender.obj')

and also add lines at the end

boxes.append(obj_to_triangles(box7))
boxes[-1].visible = True
boxes[-1].up = vec(1,1,1)
boxes[-1].pos = vec(8,-2,0)

Then run the file

python vpython_load_obj_test.py

and see what it produces.  You might need to modify your milk_blender.mtl file to have the same fields as box.mtl in the example. 

For instance box.mtl has a line

map_Kd box.png

but your mtl file doesn't have a line like this.

John



 
On Monday, October 30, 2023 at 6:24:42 AM UTC-7 kowsh...@gmail.com wrote:

Horst JENS

unread,
Dec 28, 2023, 3:52:49 PM12/28/23
to VPython-users
I have troubles importing obj (and mtl files) into vpython using the script at https://github.com/jcoady/vpython-load-obj-file. While the cubes from the demo program work fine, as soon as I try to import some free obj (and mtl) objects from sites like https://www.kenney.nl/assets/city-kit-suburban the program complains with this error:

line 48, in obj_to_triangles
curtexture = str(material.texture._path)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '_path'

After I chanted the offending code line into:
if material.texture is not None: 
      curtexture = str(material.texture._path)

the demo works (the cubes are spinning) and i can even see my imported object (it's a list of vp.compounds), but only in white, without any colors/textures

What must I do so that i can import the object AND the textures ?

also, vpython complains about: "Unimplemented OBJ format statement 'g' on line 'g tree_small'", is this important?
tree_small.obj
vpython_load_obj_test.py
tree_small.mtl
Reply all
Reply to author
Forward
0 new messages