import os
import json
import hou

def import_latest_caches(show_unpublished=False):
    
    # metadata path
    metadata_path = 'C:/vfx/test_metadata.json'
    # Check if metadata file exists
    if not os.path.exists(metadata_path):
        hou.ui.displayMessage("Metadata file not found at: {}".format(metadata_path))
        return None

    # Load metadata
    with open(metadata_path, 'r') as f:
        metadata = json.load(f)

    # Create lists for UI selection
    sim_choices = sorted(list(metadata.keys()))
    if not sim_choices:
        hou.ui.displayMessage("No simulations found in metadata file.")
        return None

    # Let user select simulation(s)
    sim_indices = hou.ui.selectFromList(
        sim_choices,
        exclusive=False,
        title="Select Simulation(s)",
        message="Choose simulation(s) to import latest version:"
    )

    if not sim_indices:
        return None

    imported_nodes = []
    summary_info = []

    # Process each selected simulation
    for sim_idx in sim_indices:
        sim_name = sim_choices[sim_idx]

        # Find the latest version automatically
        versions = sorted(list(metadata[sim_name].keys()))
        if not versions:
            hou.ui.displayMessage("No versions found for {}".format(sim_name))
            continue

        latest_version = versions[-1]

        # Get all caches for this version
        version_data = metadata[sim_name][latest_version]
        caches = version_data["caches"]

        
        # Filter out unpublished caches if needed
        if not show_unpublished:
            caches = [cache for cache in caches if cache.get("published", True)]

        obj_context = hou.node("/obj")
        geo = obj_context.createNode("geo", sim_name)
        
        for cache in caches:
            file_node = geo.createNode("file", "{}_import".format(cache["name"]))
            file_node.parm("file").set(cache["full_path"])

            imported_nodes.append(file_node)

            # Add to summary
            summary_info.append(cache["name"]+" with " + "geo_node")
        
        # Add a merge node if there are multiple caches
        if len(caches) > 1:
            merge = geo.createNode("merge", "merge_all_caches")
    
            # Connect all file nodes to the merge
            for idx, node in enumerate(geo.children()):
                if node.type().name() == "file":
                    merge.setInput(idx, node)
    
            # Position merge node below all others
            merge.setDisplayFlag(True)
            merge.setRenderFlag(True)
    
        # Move geo node to good position
        geo.moveToGoodPosition()
        geo.layoutChildren()
    
        
import_latest_caches(show_unpublished=False)
def import_latest_caches(show_unpublished=False):
    # test metadata structure
    metadata = {
        "sphere_test": {
            "v01": {
                "caches": [
                    {
                        "name": "sphere_cache",
                        "extension": "bgeo.sc",
                        "full_path": "C:/path/to/sphere_cache.$F.bgeo.sc",
                        "is_animated": True,
                        "published": True
                    }
                ]
            },
            "v02": {
                "caches": [
                    {
                        "name": "sphere_cache",
                        "extension": "bgeo.sc",
                        "full_path": "C:/path/to/sphere_cache_improved.$F.bgeo.sc",
                        "is_animated": True,
                        "published": True
                    }
                ]
            }
        },
        "fluid_sim": {
            "v01": {
                "caches": [
                    {
                        "name": "fluid_particles",
                        "extension": "bgeo.sc",
                        "full_path": "C:/path/to/fluid_particles.$F.bgeo.sc",
                        "is_animated": True,
                        "published": True
                    },
                    {
                        "name": "fluid_mesh",
                        "extension": "bgeo.sc",
                        "full_path": "C:/path/to/fluid_mesh.$F.bgeo.sc",
                        "is_animated": True,
                        "published": True
                    }
                ]
            }
        }
    }

First see if the metadata file exists and then load it

useos.path.exists to see if the file exists and if not set message and return from the function

  # metadata path
  metadata_path = 'C:/vfx/test_metadata.json'
  # Check if metadata file exists
  if not os.path.exists(metadata_path):
      hou.ui.displayMessage("Metadata file not found at: %s" % metadata_path)

  # Load metadata
  with open(metadata_path, 'r') as f:
      metadata = json.load(f)

UI Selection for Simulations

sim_choices = sorted(list(metadata.keys())) put all they keys in a list

sim_indices = hou.ui.selectFromList( Create a ui to chose the sims. This will return a integer value ``

# Create lists for UI selection
    sim_choices = sorted(list(metadata.keys()))
    if not sim_choices:
        hou.ui.displayMessage("No simulations found in metadata file.")
        return None

    # Let user select simulation(s)
    sim_indices = hou.ui.selectFromList(
        sim_choices,
        exclusive=False,
        title="Select Simulation(s)",
        message="Choose simulation(s) to import latest version:"
    )

    if not sim_indices:
        return None

Processing Selected Simulations

looping through the sim choices and then using that number to check the sim from the sim sim_choices list

version_data = metadata[sim_name][latest_version] Getting all the version data in the sim and latest version

caches = version_data["caches"] storing the caches

# Process each selected simulation
    for sim_idx in sim_indices:
        sim_name = sim_choices[sim_idx]

        # Find the latest version automatically
        versions = sorted(list(metadata[sim_name].keys()))
        if not versions:
            hou.ui.displayMessage("No versions found for {}".format(sim_name))
            continue

        latest_version = versions[-1]

        # Get all caches for this version
        version_data = metadata[sim_name][latest_version]
        caches = version_data["caches"]

Filtering Unpublished Caches

Updating the caches list Filter out unpublished caches

# Filter out unpublished caches if needed
        if not show_unpublished:
            caches = [cache for cache in caches if cache.get("published", True)]

Creating Nodes

Create a geo node for the sim version

create a file node for each sim and set the file parm to the full path