Steelbird
Steelbird

Reputation: 53

How to represent a 3D .obj object as a 3D array?

Is there any way by which 3D models can be represented as 3D arrays? Are there any libraries that take .obj or .blend files as input and give an array representation of the same?

I thought that I would slice object and export the slices to an image. I would then use those images in opencv to build arrays for each slice. In the end I would combine all the arrays of all the slices to finally get a 3D array representation of my .obj file. But I gave up halfway through because it is a painfully long process to get the image slices aligned to each other.

Is there any other index based representation I could use to represent 3D models in code?

A 3D array would be very convenient for my purposes.

Upvotes: 5

Views: 4670

Answers (3)

akhilmd
akhilmd

Reputation: 182

Binvox can be used to generate a voxel representation of a 3D model from a .obj file.

Binvox-rw-py can be used to read and parse the .binvox file generated above.

Here's what I used to a get a numpy array:

>>> import binvox_rw
>>> with open("chair.binvox","rb") as f:
...   ml = binvox_rw.read_as_3d_array(f)
... 
>>> type(ml.data)
<type 'numpy.ndarray'>
>>> ml.data.shape
(32, 32, 32)

Upvotes: 2

Hugh Fisher
Hugh Fisher

Reputation: 2516

If I understand correctly, you want to create a voxel representation of 3D models? Something like the visible human displays?

I would use one of the OBJ file loaders recommended above to import the model into an OpenGL program. Rotate and scale to whatever alignment you want along XYZ.

Then draw the object with a fragment shader that discards any pixel with Z < 0.001 or Z >= 0.002 (or whatever resolution works - I'm just trying to explain the method). This gives you the first image slice, which you store or save. Clear and draw again this time discarding Z < 0.002 or Z >= 0.003 … Because it's the same model in the same position, all your slices will be aligned.

However, are you aware that OBJ (and nearly all other 3D formats) are surface descriptions, not solid? They're hollow inside like origami models. So your 3D array representation will be mostly empty.

Hope this helps.

Upvotes: 1

Dan
Dan

Reputation: 1227

PyGame has an OBJFileLoader class. PyWavefront has a 3D object model.

Upvotes: 1

Related Questions