File: remote_rendering_client_sample.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (217 lines) | stat: -rw-r--r-- 10,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: remote_rendering_client_sample.py
DESCRIPTION:
    These samples demonstrate creating a remote rendering client, converting an asset into the format used in rendering
    sessions, listing created asset conversions, starting a rendering session, extending the lifetime of a rendering
    session, stopping a rendering session and listing rendering sessions.

USAGE:
    python remote_rendering_client_sample.py
    Set the environment variables with your own values before running the sample:
    ARR_SERVICE_ENDPOINT - the endpoint of the Azure Remote Rendering service in the desired region.
        e.g. "https://remoterendering.eastus.mixedreality.azure.com" for the East US region
        Supported regions can be found at https://docs.microsoft.com/en-us/azure/remote-rendering/reference/regions
    ARR_ACCOUNT_DOMAIN - the Remote Rendering account domain. e.g. "eastus.mixedreality.azure.com"
    ARR_ACCOUNT_ID - the Remote Rendering account identifier.
    ARR_ACCOUNT_KEY - the Remote Rendering account primary or secondary key.
"""

import logging
import os
import sys
from typing import Optional, Union, cast
import uuid
from datetime import datetime

from azure.core.credentials import AzureKeyCredential, AccessToken, TokenCredential
from azure.core.pipeline.policies import NetworkTraceLoggingPolicy
from azure.mixedreality.authentication import MixedRealityStsClient
from azure.mixedreality.remoterendering import (AssetConversion,
                                                AssetConversionInputSettings,
                                                AssetConversionOutputSettings,
                                                AssetConversionStatus,
                                                RemoteRenderingClient,
                                                RenderingSessionSize,
                                                RenderingSessionStatus,
                                                AssetConversionOutput)

# Create a logger for the 'azure' SDK
logger = logging.getLogger("azure")
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

# Enable network trace logging. This will be logged at DEBUG level.
# By default, logging is disabled.
logging_policy_enabled = os.environ.get("ARR_LOGGING_ENABLED", None)

logging_policy = None
if logging_policy_enabled:
    logging_policy = NetworkTraceLoggingPolicy()
    logging_policy.enable_http_logger = True

arr_endpoint = os.environ.get("ARR_SERVICE_ENDPOINT", None)
if not arr_endpoint:
    raise ValueError("Set ARR_SERVICE_ENDPOINT env before run this sample.")

account_id = os.environ.get("ARR_ACCOUNT_ID", None)
if not account_id:
    raise ValueError("Set ARR_ACCOUNT_ID env before run this sample.")

account_domain = os.environ.get("ARR_ACCOUNT_DOMAIN", None)
if not account_domain:
    raise ValueError("Set ARR_ACCOUNT_DOMAIN env before run this sample.")

account_key = os.environ.get("ARR_ACCOUNT_KEY", None)
if not account_key:
    raise ValueError("Set ARR_ACCOUNT_KEY env before run this sample.")

storage_container_uri_optional: Optional[str] = os.environ.get("ARR_STORAGE_CONTAINER_URI", None)
if not storage_container_uri_optional:
    raise ValueError("Set ARR_STORAGE_CONTAINER_URI env before run this sample.")
storage_container_uri: str = storage_container_uri_optional

input_blob_prefix = os.environ.get("ARR_STORAGE_INPUT_BLOB_PREFIX", None)
# if no input_blob_prefix is specified the whole content of the storage container will be retrieved for conversions
# this is not recommended since copying lots of unneeded files will slow down conversions

relative_input_asset_path_optional: Optional[str] = os.environ.get("ARR_STORAGE_INPUT_ASSET_PATH", None)
if not relative_input_asset_path_optional:
    raise ValueError("Set ARR_STORAGE_INPUT_ASSET_PATH env before run this sample.")
relative_input_asset_path: str = relative_input_asset_path_optional

# use AzureKeyCredentials to authenticate to the service - other auth options include AAD and getting
# STS token using the mixed reality STS client
key_credential: Union[AzureKeyCredential, TokenCredential, AccessToken] = AzureKeyCredential(account_key)

client = RemoteRenderingClient(
    endpoint=arr_endpoint,
    account_id=account_id,
    account_domain=account_domain,
    credential=key_credential,
    logging_policy=logging_policy
)


def perform_asset_conversion():
    try:
        # a UUID is a good conversion ID - guaranteed to be unique on an account
        conversion_id = str(uuid.uuid4())

        # In order to convert a model the input model needs to be retrieved from blob storage and the result of the
        # conversion process will be written back to blob storage
        # The subset of files which will be retrieved from the given input storage container is controlled by the
        # input_settings blob_prefix more details at:
        # https://docs.microsoft.com/en-us/azure/remote-rendering/resources/troubleshoot#conversion-file-download-errors
        input_settings = AssetConversionInputSettings(
            storage_container_uri=storage_container_uri,
            blob_prefix=input_blob_prefix,  # if not specified all files from the input container will be retrieved
            relative_input_asset_path=relative_input_asset_path,
            # container_read_list_sas #if storage is not linked with the ARR account provide a SAS here to grant access.
        )

        output_settings = AssetConversionOutputSettings(
            storage_container_uri=storage_container_uri,  # Note: different input/output containers can be specified
            blob_prefix="output/"+conversion_id,
            # output_asset_filename= convertedAsset.arrAsset # if not specified the output will be "<inputfile>.arrAsset".
            # container_write_sas  #if storage is not linked with the ARR account provide a SAS here to grant access.
        )

        conversion_poller = client.begin_asset_conversion(conversion_id=conversion_id,
                                                          input_settings=input_settings,
                                                          output_settings=output_settings)

        print("conversion with id:", conversion_id, "created. Waiting for completion.")
        conversion = conversion_poller.result()
        print("conversion with id:", conversion_id, "finished with result:", conversion.status)
        if conversion.output is not None:
            print(conversion.output.asset_uri)
        else:
            print("conversion had no output asset_uri")

        # a poller can also be acquired by id
        # id_poller = await client.get_asset_conversion_poller(conversion_id=conversion_id)
        # conversion = await id_poller.result()

        # we can also get the status of an individual asset conversion like this:
        conversion = client.get_asset_conversion(conversion_id)
        print("individual conversion retrieved with id:", conversion.id)
        print("\tconversion status:", conversion.status)

    except Exception as e:
        print("An error occurred: ", e)


def list_all_asset_conversions():
    print("listing conversions for remote rendering account: ", account_id)
    print("conversions:")
    conversions = client.list_asset_conversions()
    for c in conversions:
        created_on = c.created_on.strftime("%m/%d/%Y, %H:%M:%S")
        print("\t conversion:  id:", c.id, "status:", c.status, "created on:", created_on)
        if c.status == AssetConversionStatus.SUCCEEDED:
            if c.output is not None:
                print("\t\tconversion result URI:", c.output.asset_uri)
            else:
                print("conversion result returned with empty URI")


def demonstrate_rendering_session_lifecycle():
    try:
        # a UUID is a good session ID - guaranteed to be unique on an account
        session_id = str(uuid.uuid4())
        print("starting rendering session with id:", session_id)
        session_poller = client.begin_rendering_session(
            session_id=session_id, size=RenderingSessionSize.STANDARD, lease_time_minutes=5)
        print("rendering session with id:", session_id, "created. Waiting for session to be ready.",)

        session = session_poller.result()
        print("session with id:", session.id, "is ready. lease_time_minutes:", session.lease_time_minutes,)

        # a poller can also be acquired by a id if a session already exists
        # id_poller = client.get_rendering_session_poller(session_id=session_id)
        # session = id_poller.result()

        # one can now connect to the rendering session using the runtime SDK on a Hololens 2
        print(session)

        # we can also get the properties of an individual session by id:
        session = client.get_rendering_session(session_id)
        print(session)

        # if the session should run longer than initially requested we can extend the lifetime of the session
        session = client.get_rendering_session(session_id)
        if cast(int, session.lease_time_minutes) - cast(int, session.elapsed_time_minutes) < 2:
            session = client.update_rendering_session(
                session_id=session_id, lease_time_minutes=cast(int, session.lease_time_minutes) + 10)
            print("session with id:", session.id, "updated. New lease time:", session.lease_time_minutes, "minutes",)

        # once we do not need the session anymore we can stop the session
        client.stop_rendering_session(session_id)
        print("session with id:", session_id, "stopped")
    except Exception as e:
        print("An error occurred: ", e)


def list_all_rendering_sessions():
    print("listing sessions for account:", account_id)
    print("sessions:")
    rendering_sessions = client.list_rendering_sessions()
    for session in rendering_sessions:
        created_on = cast(datetime, session.created_on).strftime("%m/%d/%Y, %H:%M:%S")
        print("\t session:  id:", session.id, "status:", session.status, "created on:", created_on,)


if __name__ == "__main__":
    perform_asset_conversion()
    # list_all_asset_conversions()
    demonstrate_rendering_session_lifecycle()
    list_all_rendering_sessions()