Search code examples
pythonblenderpoint-clouds

How to reformat a text document from a point cloud?


I'm trying to convert a list of points from my 3D scanner. The scanner outputs the points in the format of "X Y Z Intensity R G B" Example:

-20.909 -7.091 -1.204 -1466 119 102 90
-20.910 -7.088 -1.203 -1306 121 103 80
-20.910 -7.090 -1.204 -1456 123 102 89

I would like to convert it to this (just dropping the intensity and color data while adding commas and parentheses)

desired output:

(-20.909, -7.091, -1.204),
(-20.910, -7.088, -1.203),
(-20.910, -7.090, -1.204),
(-20.910, -7.088, -1.204),
(-20.909, -7.088, -1.204),
(-20.910, -7.088, -1.203),
(-20.909, -7.090, -1.202),
(-20.905, -7.091, -1.204),
(-20.907, -7.090, -1.204),
(-20.907, -7.090, -1.204)

I'm trying to do this so I can import the point cloud data into a script inside of Blender3D. Any help would be appreciated.

Edit: typos.


Solution

  • def convert_line(line):
        parts = line.split(maxsplit=3)
        return f'({parts[0]}, {parts[1]}, {parts[2]})'
    
    with open('data.txt') as in_:
        lines = in_.readlines()
    
    converted = [convert_line(x) for x in lines]
    
    with open('output.txt', 'w') as out_:
        out_.write(',\n'.join(converted))
    

    This is a very basic script but it should do just what you need, assuming no data is corrupt. Change data.txt and output.txt with appropriate file names (paths).