教程:使用 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

本教程使用 Batch,通过 Arnold 光线跟踪渲染器来渲染 3ds Max 场景。In this tutorial, you render a 3ds Max scene with Batch using the Arnold ray-tracing renderer. Batch 池使用一个 Azure 市场映像,该映像中预安装了提供按使用付费的许可的图形和渲染应用程序。The Batch pool uses an Azure Marketplace image with pre-installed graphics and rendering applications that provide pay-per-use licensing.

先决条件Prerequisites

  • 若要以“按使用付费”模式使用 Batch 中的渲染应用程序,需要有一个标准预付费套餐订阅或其他 Azure 购买选项。You need a Standard Pay-in-Advance Offer 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.

  • GitHub 上提供了本教程的示例 3ds Max 场景,以及示例 Bash 脚本和 JSON 配置文件。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. (提供的 Autodesk 3ds Max 示例文件已获得 Creative Commons Attribution-NonCommercial-Share Alike 许可。(Autodesk 3ds Max sample files are available under a Creative Commons Attribution-NonCommercial-Share Alike license. 版权所有 © Autodesk, Inc.)Copyright © Autodesk, Inc.)

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.
    • 如果使用的是本地安装,请通过 Azure CLI 使用 az login 命令登录。If you're using a local install, sign in with Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅使用 Azure CLI 登录See Sign in with Azure CLI for additional sign-in options.
    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with Azure CLI.
    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本教程需要 Azure CLI 版本 2.0.20 或更高版本。This tutorial requires version 2.0.20 or later of the Azure CLI.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

提示

可以在 Azure Batch 扩展模板 GitHub 存储库中查看 Arnold 作业模板You can view Arnold job templates in the Azure Batch Extension Templates GitHub repository.

创建批处理帐户Create a Batch account

在订阅中创建资源组、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. 以下示例在“chinaeast2”位置创建名为“myResourceGroup”的资源组。The following example creates a resource group named myResourceGroup in the chinaeast2 location.

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

使用 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 chinaeast2 \
    --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 chinaeast2

若要创建和管理计算池和作业,需使用 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

将场景 MotionBlur-Dragon-Flying.maxGitHub 下载到本地工作目录。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.8"
    },
    "nodeAgentSKUId": "batch.node.windows amd64"
  },
  "targetDedicatedNodes": 0,
  "targetLowPriorityNodes": 1,
  "enableAutoScale": false,
  "applicationLicenses":[
         "3dsmax",
         "arnold"
      ],
  "enableInterNodeCommunication": false 
}

Batch 支持专用节点。Batch supports dedicated nodes. 专用节点为池保留。Dedicated nodes are reserved for your pool.

指定的池包含单个运行 Windows Server 映像的专用节点,所装软件适用于 Batch 渲染服务。The pool specified contains a single dedicated 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 容器写入内容,该令牌在 2020 年 11 月 15 日过期:This example creates a token to write to any blob container in the account, and the token expires on November 15, 2020:

az storage account generate-sas \
    --permissions w \
    --resource-types co \
    --services b \
    --expiry 2020-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=2020-11-15&sp=rw&sv=2019-09-24&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.chinacloudapi.cn/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": [
    {
        "httpUrl": "https://mystorageaccount.blob.core.chinacloudapi.cn/scenefiles/MotionBlur-DragonFlying.max",
        "filePath": "MotionBlur-DragonFlying.max"
    }
  ],
    "outputFiles": [
        {
            "filePattern": "dragon*.jpg",
            "destination": {
                "container": {
                    "containerUrl": "https://mystorageaccount.blob.core.chinacloudapi.cn/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.jpgOpen 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 dedicated nodes in the pool to 6:

az batch pool resize --pool-id myrenderpool --target-dedicated-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

如果不再需要资源组、Batch 帐户、池和所有相关的资源,则可以使用 az group delete 命令将其删除。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.