2
$\begingroup$

I would like to import (or parse ??) a WRL/VRML file format in python in order to work the data with Matplotlib. Is there any way of converting this file format into plottable numpy arrays ?

I attached a photo a WRL file visualized with the FreeWRL viewer enter image description here

$\endgroup$
3
  • $\begingroup$ I am not really sure if this question is more suitable for stackoverflow.com $\endgroup$
    – SAAD
    Commented May 4, 2014 at 3:25
  • $\begingroup$ I'd try looking at PyVRML or ScientificPython to start. $\endgroup$ Commented May 4, 2014 at 4:50
  • $\begingroup$ I stumbled on it while searching but I couldnt find a decent documentation not to mention that the Github repository links are broken ! $\endgroup$
    – SAAD
    Commented May 4, 2014 at 10:56

2 Answers 2

5
$\begingroup$

{Assuming you still need the code} Let A1.wrl is your wrl file.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import re
holder = []
with open("A1.wrl", "rb") as vrml:
    for lines in vrml:
        a = re.findall("[-0-9]{1,3}.[0-9]{6}", lines)
        if len(a) == 3:
            holder.append(map(float, a))

holder_array = np.array(holder) #if you want numpy array

#3D Plotting
x,y,z = zip(*holder)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(x,y,z)
plt.show()

My A.wrl: http://pastebin.com/PeVXbR5C

File generate:

enter image description here

$\endgroup$
5
  • $\begingroup$ what version of wrl files are you using ? I tested it but no regular expression are found, and the numpy array are empty. $\endgroup$
    – SAAD
    Commented Aug 21, 2014 at 11:50
  • $\begingroup$ @SAAD: #VRML V2.0 utf8 $\endgroup$ Commented Aug 21, 2014 at 12:53
  • $\begingroup$ your A.wrl file works, but I think your python script doesnt work with my file B.wrl (pastebin.com/HCVjqETa) $\endgroup$
    – SAAD
    Commented Aug 21, 2014 at 14:10
  • $\begingroup$ @SAAD: My code is just an example. You can just change the regex according to what you want to get and it will do it. In your files there are more than one place to get the coordinates and the numbers are in 2E-01 format, your code have to just catch that. $\endgroup$ Commented Aug 22, 2014 at 4:12
  • 1
    $\begingroup$ Actually i am very familiarbwith re module, nonethless i ll take a look at yourr wrl file to see how you did it. For this, you have also my up vote also. $\endgroup$
    – SAAD
    Commented Aug 22, 2014 at 22:22
0
$\begingroup$

For the people who end up here. I typically use trimesh to handle and visualize meshes and since trimesh does not support WRL/VRML...

import trimesh

def read_wrl(file_path):

    with open(file_path, 'r') as f:
        s = f.read()

    s2 = re.sub(r'\n|\r|,', ' ', s)
    s3 = re.sub('\s+', ' ', s2)
    
    l1 = re.search(r'point \[(.*?)\]', s3).group(1).strip().split()
    points = np.float32(l1).reshape((-1,3))

    l2 = re.search(r'coordIndex \[(.*?)\]', s3).group(1).strip().split()
    coordIndexs = np.int32(l2).reshape((-1,4))

    mesh = trimesh.Trimesh(vertices=points, faces=coordIndexs[:,:3])

    return mesh
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.