Houdini custom LiDAR importer in Python using Laspy Module

524 Views Asked by At

I am trying to build a custom LiDAR importer for Houdini with Python. The Laspy Module (https://pypi.org/project/laspy) does so far a good and quick job by reading and writing *.*las files inside Houdini and also filtering the classifications.

But I have to read and write the *.*las file and import again instead of instantly getting the points inside Houdini.

Now I wonder, if I can fetch the LiDAR points xyz position, to feed them on points inside Houdini.

I was trying to find useful information in the Laspy manual, but couldn't find any example or function.

I made something similar with a *.*csv file, that has the xyz positions to build a simple GPS reader to output the positions as points in Houdini (with the csv module).

I attach a screenshot with the original .las (grey) and the filtered output.las (red roofs) and the script example from the Laspy manual.

Maybe instead of Laspy there is a more elegant Solution? I am using Python 3 in Houdini, but 2.7 works too.

Update, answer from here works almost perfect https://forums.odforce.net/topic/46475-custom-lidar-importer-with-python/?tab=comments#comment-217104 :

from laspy.file import File
import numpy as np

node = hou.pwd()
geo = node.geometry()

file_path = geo.attribValue("file_path")
inFile = File(file_path, mode='r')

# --- load point position
coords = np.vstack((inFile.x, inFile.y, inFile.z)).transpose()
scale = np.array(inFile.header.scale)
offset = np.array(inFile.header.offset) # there is no offset in simple.las example from laspy library
# offset = np.array([1000, 20000, 100000])  # just for testing that offset works

# geo.setPointFloatAttribValues("P", np.concatenate(coords))    # same as Lidar Import SOP - seems that it ignores scale (and offset?)
geo.setPointFloatAttribValues("P", np.concatenate(coords*scale+offset))

# --- load color
color = np.vstack((inFile.red, inFile.green, inFile.blue)).transpose()
geo.addAttrib(hou.attribType.Point, "Cd", (1.0,1.0,1.0), False, False)  # add color atttribute
geo.setPointFloatAttribValues("Cd", np.concatenate(color / 255.0)) # transform from 1-255 to 0.0-1.0 range)

THe only thing that doesnt work yet is the inFile.classifications == x That is crashing Houdini.

1

There are 1 best solutions below

0
On

Petr from Odforce finished the LiDAR Importer. https://forums.odforce.net/topic/46475-custom-lidar-importer-with-python/

To load and read the points:

from laspy.file import File
node = hou.pwd()
geo = node.geometry()
geo.createPoint()   # dummy point for point generate

classification = hou.evalParm("classification")
file_path = hou.evalParm("lidar_file")
inFile = File(file_path, mode='r')
# store how many points lidar attribute has
geo.addAttrib(hou.attribType.Global, "nb_of_points", 0, False, False)
geo.setGlobalAttribValue("nb_of_points", len(inFile.points[inFile.Classification == classification]))

# store file path
geo.addAttrib(hou.attribType.Global, "file_path", "", False, False)
geo.setGlobalAttribValue("file_path", file_path)

# store classification
geo.addAttrib(hou.attribType.Global, "classification", 0, False, False)
geo.setGlobalAttribValue("classification", classification)

# find availible information
pointformat = inFile.point_format
for spec in inFile.point_format:
    print(spec.name)

and to load the attributes:

from laspy.file import File
import numpy as np

node = hou.pwd()
geo = node.geometry()

file_path = geo.attribValue("file_path")
inFile = File(file_path, mode='r')
classification = geo.attribValue("classification")

selection = inFile.Classification == classification
points = inFile.points[selection]



# --- load point position
coords = np.vstack((inFile.x, inFile.y, inFile.z)).transpose()
scale = np.array(inFile.header.scale)
offset = np.array(inFile.header.offset) # there is no offset in simple.las example from laspy library
# offset = np.array([1000, 20000, 100000])  # just for testing that offset works

# geo.setPointFloatAttribValues("P", np.concatenate(coords))    # same as Lidar Import SOP - seems that it ignores scale (and offset?)
geo.setPointFloatAttribValues("P", np.concatenate(coords[selection]*scale+offset))

# --- load color
missing_color = ["red", "green", "blue"]
for spec in inFile.point_format:
    if spec.name in missing_color:
        missing_color.remove(spec.name)

if not missing_color:       
    color = np.vstack((inFile.red, inFile.green, inFile.blue)).transpose()
    geo.addAttrib(hou.attribType.Point, "Cd", (1.0,1.0,1.0), False, False)  # add color atttribute
    geo.setPointFloatAttribValues("Cd", np.concatenate(color[selection] / 255.0)) # transform from 1-255 to 0.0-1.0 range)

# --- load intensity
geo.addAttrib(hou.attribType.Point, "intensity", 0.0, False, False)  # add intensity atttribute
geo.setPointFloatAttribValues("intensity", inFile.intensity[selection] / 512.0) # transform from 1-255 to 0.0-1.0 range)