• Kenny Lammers

Houdini 18 - Python Projects - Creating a Generic JSON Exporter


Howdy guys!


So I wanted to follow up the JSON Exporting vids with a more generic way of exporting point clouds that might have different sets of point attributes. In this video we walk through the process of creating this generic JSON exporter and how to use multi-parm lists to make this possible.

Code:


def export_points(kwargs):
    import os
    import json
    
    ##Get the base information
    parent = kwargs['node']
    null_node = parent.node('geo_null')
    dir = parent.parm('file_dir').eval()
    filename = parent.parm('filename').eval() + '.json'
    file_path = dir + filename
    
    ##Check to make sure we can export
    if not os.path.isdir(dir):
        os.mkdir(dir)
    
    ##get all the params from multiparm
    multiparm = parent.parm('attrs')
    instances = multiparm.multiParmInstances()
    
    ##Get all user specified attrs
    attrs = []
    for x in instances:
        attrs.append(x.eval())
        
    
    ##Create the Data Structure
    data = {
        'points' : []
    }
    
    geo = null_node.geometry()
    points = geo.points()
    
    ##Loop through each point and extract the data
    for point in points:
        id = point.number()
        pos = point.position()
        pos_array = [pos.x(), pos.y(), pos.z()]
        
        point_data = {
           'id' : id,
            'pos' : pos_array,
        }
        
        user_attrs = {}
        for x in attrs:
            if geo.findPointAttrib(x) is not None:
                
                attr = geo.findPointAttrib(x)
                attr.isArrayType()
                
                point_data[x] = point.attribValue(x)

            
        data['points'].append(point_data)
    

    json_obj = json.dumps(data, indent=4, sort_keys=True)
    with open(file_path, "w") as outfile: 
        outfile.write(json_obj)
        outfile.close()




If you like this content then you can find more on my Patreon page! I would really appreciate the support!


Thanks!

Kenny

Indie-Pixel

52 views0 comments