bash-j / mikey_nodes

comfy nodes from mikey
MIT License
109 stars 15 forks source link

Can you add this to your node library? #23

Closed michaelLochlann closed 3 months ago

michaelLochlann commented 7 months ago

Request: The code is already written and it works, if you could just peer review it and add it to your library, that would be cool. I couldn't find a node to do this anywhere on the internet.

Background: I wanted a node to give me subdirectories for a base directory, so I can pick a random one. For a random image type thing without wildcards and with a folder structure.

I was able to do it by taking one of your nodes and editing. But I'm don't know how to/am not interested in running a github or node library. I am also a total python noob and custom node noob, so I don't know what I'm doing there either. But I thought this could be useful to others so I wanted it to be made public somehow.

Input: base directory path output: text list of folders in that path my usage: i can use "random text line" on the output to get a random subdirectory.
& by doing this multiple times I can get a very flexible but also very structured randomizer around various compositions without having to manage a wildcard file in addition to the folders.

Code: Feel free to edit whatever, take credit for it, etc etc. I couldn't have done it without copy pasting your code


import os

class GetSubDirectories:
    """
    A GetSubDirectories node

    Class methods
    -------------
    INPUT_TYPES (dict): 
        Tell the main program input parameters of nodes.
    IS_CHANGED:
        optional method to control when the node is re executed.

    Attributes
    ----------
    RETURN_TYPES (`tuple`): 
        The type of each element in the output tulple.
    RETURN_NAMES (`tuple`):
        Optional: The name of each output in the output tulple.
    FUNCTION (`str`):
        The name of the entry-point method. For GetSubDirectories, if `FUNCTION = "execute"` then it will run GetSubDirectories().execute()
    OUTPUT_NODE ([`bool`]):
        If this node is an output node that outputs a result/image from the graph. The SaveImage node is an GetSubDirectories.
        The backend iterates on these output nodes and tries to execute all their parents if their parent graph is properly connected.
        Assumed to be False if not present.
    CATEGORY (`str`):
        The category the node should appear in the UI.
    execute(s) -> tuple || None:
        The entry point method. The name of this method must be the same as the value of property `FUNCTION`.
        For GetSubDirectories, if `FUNCTION = "execute"` then this method's name must be `execute`, if `FUNCTION = "foo"` then it must be `foo`.
    """
    def __init__(self):
        pass

    @classmethod
    def INPUT_TYPES(s):
        return {"required": {"base_directory": ("STRING", {"multiline": False, "placeholder": "base Directory"}) }}

    RETURN_TYPES = ('STRING',)
    RETURN_NAMES = ('string',)
    FUNCTION = 'batch'    
    OUTPUT_IS_LIST = (True, )

    CATEGORY = "GetSubDirectories"

    def batch(self, base_directory):
        if not os.path.exists(base_directory):
            raise Exception(f"base directory {base_directory} does not exist")

        strings = []

        dirs = [entry.path for entry in os.scandir(base_directory) if entry.is_dir()]

        return (dirs,)

    """
        The node will always be re executed if any of the inputs change but
        this method can be used to force the node to execute again even when the inputs don't change.
        You can make this node return a number or a string. This value will be compared to the one returned the last time the node was
        executed, if it is different the node will be executed again.
        This method is used in the core repo for the LoadImage node where they return the image hash as a string, if the image hash
        changes between executions the LoadImage node is executed again.
    """
    #@classmethod
    #def IS_CHANGED(s, image, string_field, int_field, float_field, print_to_screen):
    #    return ""

# Set the web directory, any .js file in that directory will be loaded by the frontend as a frontend extension
# WEB_DIRECTORY = "./somejs"

# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
    "GetSubDirectories": GetSubDirectories
}

# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
    "GetSubDirectories": "GetSubDirectories"
}
bash-j commented 6 months ago

Hi @michaelLochlann

Thanks for the suggestion. I have just added it to the node pack.