Created
January 6, 2022 20:21
-
-
Save david-rhodes/0313f42ab53910cc373b4cba3e885ef1 to your computer and use it in GitHub Desktop.
Houdini Point Cloud Decorator for Level I/O (asset: transformation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DevUtils | |
# =========================== | |
from DevUtils.OSP.PointCloudMarkup import PointCloud, PointInstance | |
# Houdini | |
# =========================== | |
from HoudiniUtils.Math.HoudiniMatrix4d import HoudiniMatrix4d | |
# 3rd Party | |
# =========================== | |
import hou | |
import os | |
class HoudiniPointCloud(object): | |
""" | |
Houdini wrapper for PointCloud to read or write from hou.Geometry. | |
Args: | |
pointCloud (PointCloud): the PointCloud instance we are decorating. | |
""" | |
def __init__(self, pointCloud): | |
self._pointCloud = pointCloud | |
# This object _is_ a PointCloud | |
def __getattr__(self, item): | |
if hasattr(self._pointCloud, item): | |
return getattr(self._pointCloud, item) | |
raise AttributeError("[{}] is missing from backing PointCloud!".format(item)) | |
@classmethod | |
def FromGeometry(cls, pointCloud, geometry): | |
""" | |
Convenience constructor to create a HoudiniPointCloud from data on hou.Geometry. | |
Args: | |
pointCloud (PointCloud): The backing PointCloud instance. | |
geometry (hou.Geometry): The geometry we will read point data from. | |
Returns: | |
(HoudiniPointCloud): HoudiniPointCloud build from geometry points and their attributes. | |
""" | |
houdiniPointCloud = cls(pointCloud) | |
points = geometry.points() | |
for point in points: | |
path = point.attribValue("path") | |
matrix = HoudiniMatrix4d.FromPointAttributs(point) | |
matrix.convert() | |
pointInstance = PointInstance(path, matrix.asArrayString()) | |
houdiniPointCloud.add(pointInstance) | |
return houdiniPointCloud | |
def toGeometry(self, geometry): | |
""" | |
Write this PointCloud to points on the provided hou.Geometry. | |
Returns: | |
geometry (hou.Geometry): geometry populated with PointCloud data. | |
""" | |
if geometry.findPointAttrib("orient") is None: | |
geometry.addAttrib(hou.attribType.Point, "orient", (1.0, 1.0, 1.0, 1.0)) | |
if geometry.findPointAttrib("scale") is None: | |
geometry.addAttrib(hou.attribType.Point, "scale", (1.0, 1.0, 1.0)) | |
if geometry.findPointAttrib("path") is None: | |
geometry.addAttrib(hou.attribType.Point, "path", "") | |
for instance in self.instances(): | |
point = geometry.createPoint() | |
matrix = HoudiniMatrix4d(instance.matrix4d()) | |
matrix.convert() | |
matrix.toPointAttributes(point) | |
# For full path access in VEX--important for setprimintrinsic | |
main = hou.getenv("ROOT") | |
path = "{}/{}".format(main, instance.path()) | |
path = os.path.normpath(path).replace("\\", "/") | |
point.setAttribValue("path", path) | |
return geometry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment