2

I've been practicing with Python and PyOpenGL, but I can't seem to open .OFF files (Object File Format) with Python.

If you're wondering, .OFF files are files that contain positions of a 3D object.

 OFF
 8 6 0
-0.500000 -0.500000 0.500000
 0.500000 -0.500000 0.500000
-0.500000 0.500000 0.500000
 0.500000 0.500000 0.500000
-0.500000 0.500000 -0.500000
 0.500000 0.500000 -0.500000
-0.500000 -0.500000 -0.500000
 0.500000 -0.500000 -0.500000
 4 0 1 3 2
 4 2 3 5 4
 4 4 5 7 6
 4 6 7 1 0
 4 1 7 5 3
 4 6 0 2 4

I want to read this file and make it appear on python.

The example should be like this: http://people.sc.fsu.edu/~jburkardt/data/off/box.png

So far what I've had to do is write every coordinate from the .OFF file manually. But the program needs to be able to read every .OFF file you give it to.

I could only make the example I gave because I made a tuple of tuples for both the verticies and the surfaces:

verticiesCube = (
   (-0.5,-0.5,0.5),
   (0.5,-0.5,0.5),
   (-0.5,0.5,0.5),
   ...
   )

And

 surfacesCube = (
   (0,1,2,3),
   (2,3,5,4),
   (4,5,7,6),
   ...
   )

To then do:

 def Read(verticies,surfaces):
   glBegin(QL_QUADS)
   for surface in surfaces:
      for vertex in surface:
        glVertex3fv(verticies[vertex])
   glEnd()

Then I run Read(verticiesCube, surfacesCube) and a Cube appears. My point is that you need to be able to do this with every .OFF file, and sometimes not all of them are equal (they have colors specified as well, or sometimes there are things written before the numbers so I don't know how to skip them. Example:)

  OFF
  #
  #  cone.off
  #
  22   40     120
  0.000000   1.000000   0.000000
  0.000000   0.000000   0.000000
  0.500000   0.000000   0.000000

(The #'s and the cone.off are the things I mentioned)

So how do I save the coordinates and surfaces of the .OFF files into similar tuples so I can apply the Read() algorithm.

1
  • 1
    Why did you delete your original question on this topic? I already gave you an answer there. Oh well, I'l just copy and paste my answer here, too. Commented Jun 30, 2015 at 9:01

1 Answer 1

8

No idea on what the last field in the second line means. The rest can be deduced easily:

def read_off(file):
    if 'OFF' != file.readline().strip():
        raise('Not a valid OFF header')
    n_verts, n_faces, n_dontknow = tuple([int(s) for s in file.readline().strip().split(' ')])
    verts = [[float(s) for s in file.readline().strip().split(' ')] for i_vert in range(n_verts)]
    faces = [[int(s) for s in file.readline().strip().split(' ')][1:] for i_face in range(n_faces)]
    return verts, faces
Sign up to request clarification or add additional context in comments.

14 Comments

@Kyle: Actually that '4' is telling you, what kind of geometrical primitive you're dealing with; kind of redundant, since that same information is also contained in the number of vertex indices referred to in that line. Anyway, dropping that first element is as simple as using Python slicing starting from the first element of the array (…[1:]) see my edit.
@Kyle: Perfectly valid. Although deriving a custom exception from Exception would be preferred. Also one may argue that the contents of the file are an invalid value and raise a ValueError.
@Kyle: Could have sliced away that field as well, but maybe that value gets important later on, and then by renaming the variable you already have it around.
@Kyle: These numbers are vital for the program to work. That lines tells how many vertex and face lines to read. Without that information you could not reliable parse the whole thing. I suggest you carefully read and try to understand each and every statement in that snippet of Python code I gave you. I guess the hardest for you to wrap your head around are the so called "list comprehensions" in lines 4, 7 and 10
@Kyle: Exercise for you: Modify the reader to ignore lines starting with a #
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.