Skip to content

Instantly share code, notes, and snippets.

@sortofsleepy
Created June 13, 2021 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sortofsleepy/6b8ac9f5048fd6d8538cfd9596937e1e to your computer and use it in GitHub Desktop.
Save sortofsleepy/6b8ac9f5048fd6d8538cfd9596937e1e to your computer and use it in GitHub Desktop.
Basic way to export props of a geo node in Houdini into Json.
import json, os
# adapted from here with minor tweaks
# https://github.com/sideeffects/GameDevelopmentToolset/blob/Development/otls/rop_csv_exporter.hda/gamedev_8_8Driver_1rop__csv__exporter/PythonModule#L43
# needs a bit more work to make it better but works well enough for now.
node = hou.pwd()
geo = node.geometry()
filename = "./tester.json"
separator = ","
def export(node):
# Gather some data from the parameters
node_name = "scatter1"
csv_path = filename
# determines whether or not to write each component separately or
# to group components together into a string form
# ie for example writing out x,y,z,w vs writing out "(x,y,z,w)"
separate_components = True;
# not foolproof but helps to append and x,y,z value to attributes like P
suffix_x = "x"
suffix_y = "y"
suffix_z = "z"
suffix_w = "w"
component_suffixes = [suffix_x, suffix_y, suffix_z, suffix_w]
if not os.path.exists(os.path.dirname(csv_path)):
os.makedirs(os.path.dirname(csv_path))
ExportAttributes = [x.name() for x in geo.pointAttribs()]
with open(csv_path, 'wb') as csvfile:
attributes = []
# setup attributes buffer with names of all properties.
for point_attr_name in ExportAttributes:
point_attr = None
if geo.findPointAttrib(point_attr_name) != None:
point_attr = geo.findPointAttrib(point_attr_name)
attributes.append({
"attrib":point_attr.name(),
"data":[]
})
# append data to the appropriate attributes
for point in geo.points():
# build an array of data for each attribute
point_data = []
for point_attr_name in ExportAttributes:
point_attr = None
if geo.findPointAttrib(point_attr_name) != None:
point_attr = geo.findPointAttrib(point_attr_name)
for attr in attributes:
if point_attr.name() == attr["attrib"]:
if separate_components and point_attr.size()>1:
for i in range(point_attr.size()):
attr["data"].append(point.attribValue(point_attr)[i])
else:
attr["data"].append(point.attribValue(point_attr))
json.dump(attributes,csvfile)
export(node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment