1
$\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
    May 4 '14 at 3:25
  • $\begingroup$ I'd try looking at PyVRML or ScientificPython to start. $\endgroup$ May 4 '14 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
    May 4 '14 at 10:56
4
$\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
    Aug 21 '14 at 11:50
  • $\begingroup$ @SAAD: #VRML V2.0 utf8 $\endgroup$ Aug 21 '14 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
    Aug 21 '14 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$ Aug 22 '14 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
    Aug 22 '14 at 22:22

Your Answer

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

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