0
\$\begingroup\$

I want to open an .obj file using retcave. Here is the code I am using:

import pyglet
import ratcave as rc

window = pyglet.window.Window(800, 520,caption='3D Face', resizable=False)

def update(dt):
    pass
pyglet.clock.schedule(update)

# Insert filename into WavefrontReader.
obj_filename = "./ouput/abdul_majid_mesh.obj"
obj_reader = rc.WavefrontReader(obj_filename)

# Check which meshes can be found inside the Wavefront file, and extract it into a Mesh object for rendering.
print(obj_reader.bodies.keys())

mesh = obj_reader.get_mesh("./ouput/abdul_majid_mesh.obj")
mesh.position.xyz = 0, 0, -2

scene = rc.Scene(meshes=[mesh])
@window.event
def on_draw():
    with rc.default_shader:
        scene.draw()

pyglet.app.run()

After running the code I get following error:

dict_keys(['./ouput/abdul_majid_mesh.obj'])
Traceback (most recent call last):
  File "show_3d.py", line 18, in <module>
    mesh = obj_reader.get_mesh("./ouput/abdul_majid_mesh.obj")
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/ratcave/wavefront.py", line 36, in get_mesh
    mesh = Mesh.from_incomplete_data(vertices=vertices, normals=normals, texcoords=texcoords, **kwargs)
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/ratcave/mesh.py", line 171, in from_incomplete_data
    normals = normals if hasattr(texcoords, '__iter__') and len(normals) else calculate_normals(vertices)
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/ratcave/mesh.py", line 21, in calculate_normals
    normal = np.cross(*vecs)  # normal is the cross products of vectors.
  File "<__array_function__ internals>", line 6, in cross
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/numpy/core/numeric.py", line 1580, in cross
    raise ValueError(msg)
ValueError: incompatible dimensions for cross product
(dimension must be 2 or 3)

I am able to view the same file in MeshLab but not using the code. I get that the .obj file doesn't have normal information. So how to open a file without normals. This is the file I want to open.

This is the function that saves the vertex and faces information as .obj file which i later have to open:

# save 3D face to obj file
def save_obj(path,v,f,c):
    with open(path,'w') as file:
        for i in range(len(v)):
            file.write('v %f %f %f %f %f %f\n'%(v[i,0],v[i,1],v[i,2],c[i,0],c[i,1],c[i,2]))

        file.write('\n')

        for i in range(len(f)):
            file.write('f %d %d %d\n'%(f[i,0],f[i,1],f[i,2]))

    file.close()

UPDATE 0: After making changes to save_obj function this is how I save .obj file:

# save 3D face to obj file
def save_obj(path,v,f,c):
    with open(path,'w') as file:
        for i in range(len(v)):
            file.write('v %f %f %f\n'%(v[i,0],v[i,1],v[i,2]))

        file.write('\n')
        for i in range(len(c)):
            file.write('vt %f %f %f\n'%(c[i,0],c[i,1],c[i,2]))
        file.write('\n')
        for i in range(len(f)):
            file.write('f %d/%d %d/%d %d/%d\n'%(f[i,0], f[i,0], f[i,1],f[i,1], f[i,2],f[i,2]))

    file.close()

UPDATE 1: I modified the save_obj function as:

# save 3D face to obj file
def save_obj(path,v,f,c):
    with open(path,'w') as file:
        for i in range(len(v)):
            file.write('v %f %f %f\n'%(v[i,0],v[i,1],v[i,2]))

        file.write('\n')
        for i in range(len(c)):
            file.write('vt %f %f %f\n'%(c[i,0],c[i,1],c[i,2]))
        file.write('\n')
        for i in range(len(f)):
            file.write('f %d %d %d\n'%(f[i,0],f[i,1],f[i,2]))

    file.close()

Now I am getting error:

Traceback (most recent call last):
  File "show_3d.py", line 13, in <module>
    obj_reader = rc.WavefrontReader(obj_filename)
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/ratcave/wavefront.py", line 27, in __init__
    self.bodies = read_wavefront(file_name)
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/wavefront_reader/reading.py", line 101, in read_wavefront
    geoms = read_objfile(fname_obj)
  File "/home/minhaj/anaconda3/envs/deep/lib/python3.6/site-packages/wavefront_reader/reading.py", line 58, in read_objfile
    obj[vertname] = verts[vertname][obj['f'][idx].flatten() - 1, :]
AttributeError: 'tuple' object has no attribute 'flatten'

Now when I open this newly generated .obj file in meshlab. Only shape is shown and no color is displayed.

\$\endgroup\$
3
  • \$\begingroup\$ @DMGregory Kindly check the updated question. \$\endgroup\$ Sep 23 at 20:04
  • \$\begingroup\$ How have you set up your shader to read the 3-dimensional texture coordinates you've loaded and render them as colours? Also, please don't just append "Update n" to your question. Either answer your old question and post a new one about the new problem, or edit your question to ask just about your current problem, not the full history of problems you've solved until now. \$\endgroup\$
    – DMGregory
    Sep 23 at 20:32
  • \$\begingroup\$ Keep in mind that you have access to the source code of the library you're using. You can open the box and adapt the function to your own needs. \$\endgroup\$
    – Vaillancourt
    Sep 24 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.