Creating user sandbox folders without needing the user to work on the task

If you have set up user sandboxes in your config’s schema, by default they only get created when a user goes and saves a scene in that context. However, there can be times where you want to create them manually in a one-off step. For example

  • If you would like to create them up front without the user needing to create a new scene file in workfiles on the task.
  • You had to unregister existing user folders and now you want to re-register them, you will need to use a script to do this.

Here is an example of how this can be achieved:

  1. Take over and modify the core hook. Then add the following into the beginning of the execute method:

    folder_user = os.environ.get("FOLDERUSER")
    if folder_user:    
        return folder_user
  2. Then use a script like this to create the folders:

    import sgtk
    from sgtk.util import login
    from tank_vendor.shotgun_authentication import ShotgunAuthenticator
    # For this to work we must use script authentication, otherwise the 
    # hook won't be called, as it will extract the user details
    # from the currently authenticated user.
    cdm = sgtk.util.CoreDefaultsManager()
    authenticator = ShotgunAuthenticator(cdm)
    # replace script user and key with your actual script user and key.
    user = authenticator.create_script_user(
    # create an sgtk api instance
    project_id = 137
    tk = sgtk.sgtk_from_entity("Project",project_id)
    # Now we are authenticated with a script user, we can move onto creating the folders
    entities = # Grab a list of entities that you wish to create folders for.
    # A list of user login names, this could be pulled from Shotgun
    users = ["philip"] 
    for a_user in users:    
        os.environ["FOLDERUSER"] = a_user    
        for entity in entities:
            # A little bit ugly but we need to clear the current user cache so 
            # we can create the folders for other users
            login.g_shotgun_current_user_cache = "unknown"
            # Actual engine name not important, 
            # but an engine name does need to be supplied so that                 
            # the user folder will get created        
            tk.create_filesystem_structure(entity['type'], entity['id'], engine="blah")