def find_nodes_with_plug(node, plug_name, type_names=None, results=None):
    """
    Recursively finds nodes with plugs with a specified name within the
    hierarchy of a given a root node. Optionally we can specify a list of node
    types to filter by.
    """

    if results is None:
        results = []

    if node.has_plug(plug_name):
        # if types were specified filter by them
        if (type_names and node.type_name() in type_names) or type_names is None:
            results.append(node)

    for child_node in node.children():
        find_nodes_with_plug(
            child_node, plug_name, type_names=type_names, results=results
        )

    return results