Best way to authenticate in a headless python environment

Hello,

I’m running omniverse in a container headlessly and I want to be able to obtain a file that is gotten from an external server. When I try to load the file, I get an error about the file failing to open. Is there any easy way, using the python API, to authenticate and get my file from my server? The docs don’t seem to mention an easy way to login.

Thanks in advance!

To access a file, externally from a container, you have to set up a storage folder that the container can access, with the correct permissions.

I will check with our containers experts, but in the meantime, this is what I pulled up with ChatGPT

You can use Python’s `requests` module to handle authentication and file retrieval.

Here’s a general approach:

1. **Use `requests` to authenticate and download the file**
If your server requires authentication, you can pass credentials via headers, cookies, or an API token.
2. **Save the file locally**
Once the file is downloaded, save it to a directory that Omniverse can access.
3. **Load the file into Omniverse**
Use Omniverse’s Python API to open the file after downloading.

For Downloading

import requests

# Set your authentication details
url = "https://yourserver.com/path/to/your/file.usd"
headers = {
    "Authorization": "Bearer YOUR_ACCESS_TOKEN"  # Replace with your token
}

# Download the file
response = requests.get(url, headers=headers)
if response.status_code == 200:
    file_path = "/path/to/save/file.usd"
    with open(file_path, "wb") as file:
        file.write(response.content)
    print(f"File downloaded successfully: {file_path}")
else:
    print(f"Failed to download file: {response.status_code} - {response.text}")

For Uploading

import omni.usd

stage = omni.usd.get_context().open_stage(file_path)
if stage:
    print("File loaded successfully in Omniverse.")
else:
    print("Failed to open file in Omniverse.")

If your server uses Basic Authentication, use:

response = requests.get(url, auth=("username", "password"))

Hello,

Thanks for the response Richard. One of the issues I’m having that is preventing me from doing this local download method is the usd has some assets from the external omniverse server, so when trying to load the usd locally via open stage I’m getting errors about not being able to find assets.

Ah well that is a different issue, but luckily easy to solve :-)

For that, you need to open your scene fully in normal Composer and then use the FILE > COLLECT tool to collect everything down locally, will all the S3 Amazon Bucket assets. Then take that whole local folder structure and copy that up instead. If you want to make it even “neater” you can use the “Flat” option, which puts everything you need it one local subfolder. All the usds, all the materials, all the textures.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.