教學課程:使用 Azure Batch 轉譯場景Tutorial: Render a scene with Azure Batch

Azure Batch 提供了按使用次數付費的雲端規模轉譯功能。Azure Batch provides cloud-scale rendering capabilities on a pay-per-use basis. Azure Batch 支援 Autodesk Maya、3ds Max、Arnold 與 V-Ray 等應用程式的轉譯。Azure Batch supports rendering apps including Autodesk Maya, 3ds Max, Arnold, and V-Ray. 本教學課程會示範使用 Azure 命令列介面透過 Batch 轉譯小型場景的步驟。This tutorial shows you the steps to render a small scene with Batch using the Azure Command-Line Interface. 您會了解如何:You learn how to:

  • 將場景上傳至 Azure 儲存體Upload a scene to Azure storage
  • 建立 Batch 集區以供轉譯Create a Batch pool for rendering
  • 轉譯單一框架場景Render a single-frame scene
  • 調整集區,以及轉譯多框架場景Scale the pool, and render a multi-frame scene
  • 下載已轉譯的輸出Download rendered output

在本教學課程中,您會使用 Arnold 光線追蹤轉譯器,透過 Batch 轉譯 3ds Max 場景。In this tutorial, you render a 3ds Max scene with Batch using the Arnold ray-tracing renderer. Batch 集區會使用 Azure Marketplace 映像,其中包含提供即用即付授權的預先安裝圖表與轉譯應用程式。The Batch pool uses an Azure Marketplace image with pre-installed graphics and rendering applications that provide pay-per-use licensing.

必要條件Prerequisites

您需要隨用隨付訂用帳戶或其他 Azure 購買選項,以按使用量付費的方式,在 Batch 中使用轉譯應用程式。You need a pay-as-you-go subscription or other Azure purchase option to use rendering applications in Batch on a pay-per-use basis. 如果您使用提供信用額度金額的免費 Azure 方案,則不支援按使用量付費授權。Pay-per-use licensing isn't supported if you use a free Azure offer that provides a monetary credit.

此教學課程中的範例 3ds Max 場景 (連同範例 Bash 指令碼和 JSON 設定檔) 位於 GitHub 上。The sample 3ds Max scene for this tutorial is on GitHub, along with a sample Bash script and JSON configuration files. 3ds Max 場景來自 Autodesk 3ds Max 範例檔案The 3ds Max scene is from the Autodesk 3ds Max sample files. (在 Creative Commons Attribution-NonCommercial-Share Alike 授權之下可取得 Autodesk 3ds Max 範例檔案。(Autodesk 3ds Max sample files are available under a Creative Commons Attribution-NonCommercial-Share Alike license. Copyright © Autodesk, Inc.)Copyright © Autodesk, Inc.)

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell 可讓您使用 bashPowerShell 以與 Azure 服務搭配使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要啟動 Azure Cloud Shell:To launch Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 開啟 Cloud Shell。Open Cloud Shell.
  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上按 Ctrl+Shift+V;或在 macOS 上按 Cmd+Shift+V,將程式碼貼到 Cloud Shell工作階段中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. 按下 Enter 鍵執行程式碼。Press Enter to run the code.

如果您選擇在本機安裝和使用 CLI,本教學課程會要求您執行 Azure CLI 2.0.20 版或更新版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.20 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

建立批次帳戶:Create a Batch account

如果您還沒有 Batch 帳戶,請在您的訂用帳戶中建立資源群組、Batch 帳戶,以及已連結的儲存體帳戶。If you haven't already, create a resource group, a Batch account, and a linked storage account in your subscription.

使用 az group create 命令來建立資源群組。Create a resource group with the az group create command. 下列範例會在 eastus2 位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the eastus2 location.

az group create \
    --name myResourceGroup \
    --location eastus2

使用 az storage account create 命令在資源群組中建立 Azure 儲存體帳戶。Create an Azure Storage account in your resource group with the az storage account create command. 在本教學課程中,您會使用此儲存體帳戶來存放輸入 3ds Max 場景和已轉譯的輸出。For this tutorial, you use the storage account to store an input 3ds Max scene and the rendered output.

az storage account create \
    --resource-group myResourceGroup \
    --name mystorageaccount \
    --location eastus2 \
    --sku Standard_LRS

使用 az batch account create 命令建立 Batch 帳戶。Create a Batch account with the az batch account create command. 下列範例會在 myResourceGroup 中建立名為 mybatchaccount 的 Batch 帳戶,並連結您所建立的儲存體帳戶。The following example creates a Batch account named mybatchaccount in myResourceGroup, and links the storage account you created.

az batch account create \
    --name mybatchaccount \
    --storage-account mystorageaccount \
    --resource-group myResourceGroup \
    --location eastus2

若要建立及管理計算集區和作業,您需要向 Batch 進行驗證。To create and manage compute pools and jobs, you need to authenticate with Batch. 使用 az batch account login 命令登入帳戶。Log in to the account with the az batch account login command. 登入之後,az batch 命令會使用此帳戶內容。After you log in, your az batch commands use this account context. 下列範例會根據 Batch 帳戶名稱和金鑰,使用共用金鑰驗證。The following example uses shared key authentication, based on the Batch account name and key. Batch 也支援透過 Azure Active Directory 進行驗證,以便驗證個別使用者或自動執行的應用程式。Batch also supports authentication through Azure Active Directory, to authenticate individual users or an unattended application.

az batch account login \
    --name mybatchaccount \
    --resource-group myResourceGroup \
    --shared-key-auth

將場景上傳至儲存體Upload a scene to storage

若要將輸入場景上傳至儲存體,您必須先存取儲存體帳戶,並建立 blob 的目的地容器。To upload the input scene to storage, you first need to access the storage account and create a destination container for the blobs. 若要存取 Azure 儲存體帳戶,請匯出 AZURE_STORAGE_KEYAZURE_STORAGE_ACCOUNT 環境變數。To access the Azure storage account, export the AZURE_STORAGE_KEY and AZURE_STORAGE_ACCOUNT environment variables. 第一個 Bash Shell 命令會使用 az storage account keys list 命令來取得第一個帳戶金鑰。The first Bash shell command uses the az storage account keys list command to get the first account key. 設定這些環境變數之後,您的儲存體命令就會使用此帳戶內容。After you set these environment variables, your storage commands use this account context.

export AZURE_STORAGE_KEY=$(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup -o tsv --query [0].value)

export AZURE_STORAGE_ACCOUNT=mystorageaccount

現在,在儲存體帳戶中建立場景檔案的 Blob 容器。Now, create a blob container in the storage account for the scene files. 下列範例會使用 az storage container create 命令建立名為 scenefiles 且允許公用讀取存取的 blob 容器。The following example uses the az storage container create command to create a blob container named scenefiles that allows public read access.

az storage container create \
    --public-access blob \
    --name scenefiles

GitHubMotionBlur-Dragon-Flying.max 場景下載到本機工作目錄。Download the scene MotionBlur-Dragon-Flying.max from GitHub to a local working directory. 例如︰For example:

wget -O MotionBlur-DragonFlying.max https://github.com/Azure/azure-docs-cli-python-samples/raw/master/batch/render-scene/MotionBlur-DragonFlying.max

將本機工作目錄中的場景檔案上傳到 blob 容器。Upload the scene file from your local working directory to the blob container. 下列範例會使用 az storage blob upload-batch 命令,該命令可以上傳多個檔案:The following example uses the az storage blob upload-batch command, which can upload multiple files:

az storage blob upload-batch \
    --destination scenefiles \
    --source ./

建立轉譯集區Create a rendering pool

使用 az batch pool create 命令建立 Batch 集區以供轉譯。Create a Batch pool for rendering using the az batch pool create command. 在此範例中,您會在 JSON 檔案中指定集區設定。In this example, you specify the pool settings in a JSON file. 在您目前的 Shell 中,建立名為 mypool.json 的檔案,然後複製並貼上下列內容。Within your current shell, create a file name mypool.json, then copy and paste the following contents. 請務必正確地複製所有文字。Be sure all the text copies correctly. (您可以從 GitHub 下載此檔案。)(You can download the file from GitHub.)

{
  "id": "myrenderpool",
  "vmSize": "standard_d2_v2",
  "virtualMachineConfiguration": {
    "imageReference": {
      "publisher": "batch",
      "offer": "rendering-windows2016",
      "sku": "rendering",
      "version": "1.3.2"
    },
    "nodeAgentSKUId": "batch.node.windows amd64"
  },
  "targetDedicatedNodes": 0,
  "targetLowPriorityNodes": 1,
  "enableAutoScale": false,
  "applicationLicenses":[
         "3dsmax",
         "arnold"
      ],
  "enableInterNodeCommunication": false 
}

Batch 支援專用節點和低優先順序節點,而您可以在集區中使用其中一種或同時使用兩種。Batch supports dedicated nodes and low-priority nodes, and you can use either or both in your pools. 專用節點會保留給您的集區使用。Dedicated nodes are reserved for your pool. 低優先順序節點則會以較低的價格從 Azure 中的剩餘容量提供。Low-priority nodes are offered at a reduced price from surplus VM capacity in Azure. 如果 Azure 沒有足夠的容量,便無法使用低優先順序節點。Low-priority nodes become unavailable if Azure does not have enough capacity.

指定的集區包含一個執行 Windows Server 映像 (內含 Batch 轉譯服務軟體) 的低優先順序節點。The pool specified contains a single low-priority node running a Windows Server image with software for the Batch Rendering service. 此集區已獲得授權可透過 3ds Max 和 Arnold 進行轉譯。This pool is licensed to render with 3ds Max and Arnold. 在後面的步驟中,您可將集區調整為更多節點。In a later step, you scale the pool to a larger number of nodes.

藉由將 JSON 檔案傳遞至 az batch pool create 命令來建立集區:Create the pool by passing the JSON file to the az batch pool create command:

az batch pool create \
    --json-file mypool.json

佈建集區需要幾分鐘的時間。It takes a few minutes to provision the pool. 若要查看集區的狀態,請執行 az batch pool show 命令。To see the status of the pool, run the az batch pool show command. 下列命令可取得集區的配置狀態:The following command gets the allocation state of the pool:

az batch pool show \
    --pool-id myrenderpool \
    --query "allocationState"

繼續執行下列步驟以建立一個作業和數項工作,然而集區狀態一直在改變。Continue the following steps to create a job and tasks while the pool state is changing. 當配置狀態為 steady 且節點正在執行時,便已完全佈建集區。The pool is completely provisioned when the allocation state is steady and the nodes are running.

建立輸入的 Blob 容器Create a blob container for output

在本教學課程的範例中,轉譯作業中的每項工作都會建立輸出檔案。In the examples in this tutorial, every task in the rendering job creates an output file. 在排程作業之前,請在您的儲存體帳戶中建立 blob 容器作為輸出檔案的目的地。Before scheduling the job, create a blob container in your storage account as the destination for the output files. 下列範例會使用 az storage container create 命令建立具有公用讀取存取權的 job-myrenderjob 容器。The following example uses the az storage container create command to create the job-myrenderjob container with public read access.

az storage container create \
    --public-access blob \
    --name job-myrenderjob

為了將輸出檔案寫入容器,Batch 需要使用共用存取簽章 (SAS) 權杖。To write output files to the container, Batch needs to use a Shared Access Signature (SAS) token. 使用 az storage account generate-sas 命令建立權杖。Create the token with the az storage account generate-sas command. 此範例會建立可寫入帳戶中任何 blob 容器的權杖,而該權杖會在 2018 年 11 月 15 日到期:This example creates a token to write to any blob container in the account, and the token expires on November 15, 2018:

az storage account generate-sas \
    --permissions w \
    --resource-types co \
    --services b \
    --expiry 2019-11-15

請記下命令所傳回的權杖,如下所示。Take note of the token returned by the command, which looks similar to the following. 您會在後面的步驟中使用此權杖。You use this token in a later step.

se=2018-11-15&sp=rw&sv=2017-04-17&ss=b&srt=co&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

轉譯單一框架場景Render a single-frame scene

建立工作Create a job

使用 az batch job create 命令建立要在集區上執行的轉譯作業。Create a rendering job to run on the pool by using the az batch job create command. 一開始作業沒有任何工作。Initially the job has no tasks.

az batch job create \
    --id myrenderjob \
    --pool-id myrenderpool

建立工作Create a task

使用 az batch task create 命令在作業中建立轉譯工作。Use the az batch task create command to create a rendering task in the job. 在此範例中,您會在 JSON 檔案中指定工作設定。In this example, you specify the task settings in a JSON file. 在您目前的 Shell 中,建立名為 myrendertask.json 的檔案,然後複製並貼上下列內容。Within your current shell, create a file named myrendertask.json, then copy and paste the following contents. 請務必正確地複製所有文字。Be sure all the text copies correctly. (您可以從 GitHub 下載此檔案。)(You can download the file from GitHub.)

此工作會指定 3ds Max 命令,以轉譯 MotionBlur-DragonFlying.max 場景的單一框架。The task specifies a 3ds Max command to render a single frame of the scene MotionBlur-DragonFlying.max.

修改 JSON 檔案中的 blobSourcecontainerURL 元素,讓它們包含您的儲存體帳戶名稱和 SAS 權杖。Modify the blobSource and containerURL elements in the JSON file so that they include the name of your storage account and your SAS token.

提示

您的 containerURL 會以 SAS 權杖結尾,如下所示:Your containerURL ends with your SAS token and is similar to:

https://mystorageaccount.blob.core.windows.net/job-myrenderjob/$TaskOutput?se=2018-11-15&sp=rw&sv=2017-04-17&ss=b&srt=co&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{
  "id": "myrendertask",
  "commandLine": "cmd /c \"%3DSMAX_2018%3dsmaxcmdio.exe -secure off -v:5 -rfw:0 -start:1 -end:1 -outputName:\"dragon.jpg\" -w 400 -h 300 MotionBlur-DragonFlying.max\"",
  "resourceFiles": [
    {
        "blobSource": "https://mystorageaccount.blob.core.windows.net/scenefiles/MotionBlur-DragonFlying.max",
        "filePath": "MotionBlur-DragonFlying.max"
    }
  ],
    "outputFiles": [
        {
            "filePattern": "dragon*.jpg",
            "destination": {
                "container": {
                    "containerUrl": "https://mystorageaccount.blob.core.windows.net/job-myrenderjob/myrendertask/$TaskOutput?Add_Your_SAS_Token_Here"
                }
            },
            "uploadOptions": {
                "uploadCondition": "TaskSuccess"
            }
        }
    ],
  "userIdentity": {
    "autoUser": {
      "scope": "task",
      "elevationLevel": "nonAdmin"
    }
  }
}

請使用下列命令將工作新增至作業:Add the task to the job with the following command:

az batch task create \
    --job-id myrenderjob \
    --json-file myrendertask.json

Batch 會排程工作,而集區中的節點一旦可用,此工作就會執行。Batch schedules the task, and the task runs as soon as a node in the pool is available.

檢視工作輸出View task output

此工作需花費數分鐘的時間來執行。The task takes a few minutes to run. 使用 az batch task show 命令來檢視工作的詳細資訊。Use the az batch task show command to view details about the task.

az batch task show \
    --job-id myrenderjob \
    --task-id myrendertask

此工作會在計算節點上產生 dragon0001.jpg ,並將它上傳至您儲存體帳戶中的 job-myrenderjob 容器。The task generates dragon0001.jpg on the compute node and uploads it to the job-myrenderjob container in your storage account. 若要檢視輸出,請使用 az storage blob download 命令,將此檔案從儲存體下載到您的本機電腦。To view the output, download the file from storage to your local computer using the az storage blob download command.

az storage blob download \
    --container-name job-myrenderjob \
    --file dragon.jpg \
    --name dragon0001.jpg

在電腦上開啟 dragon.jpg 。Open dragon.jpg on your computer. 已轉譯的影像如下所示:The rendered image looks similar to the following:

已轉譯的飛龍框架 1

調整集區Scale the pool

立即修改集區,以準備進行較大型的轉譯作業(具有多個框架)。Now modify the pool to prepare for a larger rendering job, with multiple frames. Batch 提供數種方式來調整計算資源,包括自動調整,此種方式會隨著工作需求變化來新增或移除節點。Batch provides a number of ways to scale the compute resources, including autoscaling which adds or removes nodes as task demands change. 在此基本範例中,使用 az batch pool resize 命令,將集區中的低優先順序節點數目增加至 6 :For this basic example, use the az batch pool resize command to increase the number of low-priority nodes in the pool to 6:

az batch pool resize --pool-id myrenderpool --target-dedicated-nodes 0 --target-low-priority-nodes 6

此集區需花費數分鐘的時間來調整大小。The pool takes a few minutes to resize. 進行該程序時,請將後續工作設定為在現有的轉譯作業中執行。While that process takes place, set up the next tasks to run in the existing rendering job.

轉譯多框架場景Render a multiframe scene

如同在單一框架的範例中,使用 az batch task create 命令,在名為 myrenderjob 的作業中建立轉譯工作。As in the single-frame example, use the az batch task create command to create rendering tasks in the job named myrenderjob. 在此,請在名為 myrendertask_multi.json 的 JSON 檔案中指定工作設定。Here, specify the task settings in a JSON file called myrendertask_multi.json. (您可以從 GitHub 下載此檔案。)這六項工作會各自指定 Arnold 命令列,以轉譯 3ds Max 場景 MotionBlur-DragonFlying.max 的一個框架。(You can download the file from GitHub.) Each of the six tasks specifies an Arnold command line to render one frame of the 3ds Max scene MotionBlur-DragonFlying.max.

在您目前的 Shell 中,建立名為 myrendertask_multi.json 的檔案,然後複製並貼上所下載檔案的內容。Create a file in your current shell named myrendertask_multi.json, and copy and paste the contents from the downloaded file. 修改 JSON 檔案中的 blobSourcecontainerURL 元素,使其包含您的儲存體帳戶名稱和 SAS 權杖。Modify the blobSource and containerURL elements in the JSON file to include the name of your storage account and your SAS token. 請務必變更這六項工作各自的設定。Be sure to change the settings for each of the six tasks. 儲存檔案,然後執行下列命令以將工作排入佇列:Save the file, and run the following command to queue the tasks:

az batch task create --job-id myrenderjob --json-file myrendertask_multi.json

檢視工作輸出View task output

此工作需花費數分鐘的時間來執行。The task takes a few minutes to run. 使用 az batch task list 命令來檢視工作的狀態。Use the az batch task list command to view the state of the tasks. 例如︰For example:

az batch task list \
    --job-id myrenderjob \
    --output table

使用 az batch task show 命令來檢視個別工作的詳細資訊。Use the az batch task show command to view details about individual tasks. 例如︰For example:

az batch task show \
    --job-id myrenderjob \
    --task-id mymultitask1

此工作會在計算節點上產生名為 dragon0002.jpg - dragon0007.jpg 的輸出檔案,並將它們上傳至您儲存體帳戶中的 job-myrenderjob 容器。The tasks generate output files named dragon0002.jpg - dragon0007.jpg on the compute nodes and upload them to the job-myrenderjob container in your storage account. 若要檢視輸出,請使用 az storage blob download-batch 命令,將檔案下載到本機電腦上的資料夾。To view the output, download the files to a folder on your local computer using the az storage blob download-batch command. 例如︰For example:

az storage blob download-batch \
    --source job-myrenderjob \
    --destination .

開啟您電腦上的其中一個檔案。Open one of the files on your computer. 已轉譯的框架 6 大致如下:Rendered frame 6 looks similar to the following:

已轉譯的飛龍框架 6

清除資源Clean up resources

若不再需要,您可以使用 az group delete 命令將資源群組、Batch 帳戶、集區和所有相關資源移除。When no longer needed, you can use the az group delete command to remove the resource group, Batch account, pools, and all related resources. 刪除資源,如下所示:Delete the resources as follows:

az group delete --name myResourceGroup

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned about how to:

  • 將場景上傳至 Azure 儲存體Upload scenes to Azure storage
  • 建立 Batch 集區以供轉譯Create a Batch pool for rendering
  • 使用 Arnold 轉譯單一框架場景Render a single-frame scene with Arnold
  • 調整集區,以及轉譯多框架場景Scale the pool, and render a multi-frame scene
  • 下載已轉譯的輸出Download rendered output

若要深入了解雲端規模轉譯,請參閱 Batch 轉譯服務的選項。To learn more about cloud-scale rendering, see the options for the Batch Rendering service.