Search code examples
pythonpython-3.xdirectoryvpython

VPython 7 Texture not loading from custom directory


I am working on a small project in VPython 7; Python 3.6, where textures need to be applied to my 3D objects. However, when I try loading the texture in, the object does not appear, until I place the texture in the Lib\site packages\vpython\vpython_data folder, where it is loaded perfectly with no problems.

However, for my project, I need it to be in my chosen directory for easy organisation.

Let's call the directory C:\Project with my texture Tex /Tex.jpg

textures.customTex= {'file':":Tex.jpg"} self.3DObject= sphere(pos=vector(0,0,0),radius = 1, texture=textures.Tex)

The above will work if the texture is the the /vpython_data directory.

However, when I try to load the same texture but in my directory:

textures.customTex= {'file':":C:\Project\Tex.jpg"} self.3DObject= sphere(pos=vector(0,0,0),radius = 1, texture=textures.Tex)

The above will not work.

My question is whether there if I am loading it in wrong, or whether there is simply no workaround this problem.

Thank you in advance


Solution

  • I don't quite understand your use of colons, but there is a problem. I did the experiment of putting an image at C:\cabinet.jpg and executing

    box(texture='C:\cabinet.jpg')

    This fails, and if you turn on the browser debugger you'll see this error message:

    Not allowed to load local resource: file:///C:/cabinet.jpg

    This is related to CORS issues, Cross-origin resource sharing.

    On the other hand, it is possible to get an image from a "CORS-enabled" site. This works:

    box(texture='https://s3.amazonaws.com/glowscript/textures/flower_texture.jpg')

    Thanks for reporting this, though, as I think I may see a way to make local files work.

    Bruce Sherwood

    P.S. It's better to post VPython questions to the VPython forum, where there are many more VPython users than are likely to see stackoverflow questions.