Blender Hack Blog

オープンソースの総合3DCGソフトウェアのBlenderのコード解析や開発を記録していきます。

Hello Arnold Python API

はじめに

今回は、Blenderから外れますが、ArnoldのPython APIを扱うメモです。 シンプルな球体をレンダリングし、JPEGで出力する所まで動かします。

コード

こちらの記事にあるコードを使います。

arnoldsupport.com

from arnold import *
 
AiBegin()
AiMsgSetConsoleFlags(AI_LOG_INFO)
AiMsgInfo('Hello World')
AiEnd()

実行

環境変数PYTHONPATHにArnold SDKPython APIのパスをセットしておきます。 それから上記Pythonコードを実行します。 詳細なログが出力され、「Hello World」も出力されました。

% export PYTHONPATH=/Applications/Autodesk/Arnold-6.0.4.1-darwin/python
% python hello_world.py 
dyld: warning, LC_RPATH @executable_path/. in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/libai.dylib being ignored in restricted program because of @executable_path (Codesign main executable with Library Validation to allow @ paths)
dyld: warning, LC_RPATH @rpath in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/./libAdpSDKWrapper-arnold.dylib being ignored in restricted program because it is a relative path
dyld: warning, LC_RPATH @rpath in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/./libAdpSDKWrapper-arnold.dylib being ignored in restricted program because it is a relative path
dyld: warning, LC_RPATH @rpath in /Users/yoshinori_sano/Library/Application Support/Autodesk/ADPSDK/bin/AdpSDKCore.bundle/Contents/MacOS/AdpSDKCore being ignored in restricted program because it is a relative path
00:00:00    66MB         | log started Tue Mar  9 09:12:33 2021
00:00:00    66MB         | Arnold 6.0.4.1 [e641c6ed] darwin clang-9.0.1 oiio-2.2.1 osl-1.11.6 vdb-4.0.0 clm-1.1.2.132 rlm-12.4.2 2020/09/25 14:42:01
00:00:00    66MB         | running on pc.local, pid=13396
00:00:00    66MB         |  1 x Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (6 cores, 12 logical) with 32768MB
00:00:00    66MB         |  macOS 10.16.0, Darwin kernel 20.2.0
00:00:00    66MB         |  soft limit for open files raised from 2560 to 10238
00:00:00    66MB         |  
00:00:00    66MB         | loading plugins from /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/../plugins ...
dyld: warning, LC_RPATH @executable_path/. in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/libai.dylib being ignored in restricted program because of @executable_path (Codesign main executable with Library Validation to allow @ paths)
00:00:00    66MB         |  cryptomatte.dylib: cryptomatte uses Arnold 6.0.4.1
00:00:00    66MB         |  cryptomatte.dylib: cryptomatte_filter uses Arnold 6.0.4.1
00:00:00    66MB         |  cryptomatte.dylib: cryptomatte_manifest_driver uses Arnold 6.0.4.1
dyld: warning, LC_RPATH @executable_path/. in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/libai.dylib being ignored in restricted program because of @executable_path (Codesign main executable with Library Validation to allow @ paths)
00:00:00    68MB         |  usd_proc.dylib: usd uses Arnold 6.0.4.1
dyld: warning, LC_RPATH @executable_path/. in /Applications/Autodesk/Arnold-6.0.4.1-darwin/bin/libai.dylib being ignored in restricted program because of @executable_path (Codesign main executable with Library Validation to allow @ paths)
00:00:00    68MB         |  alembic_proc.dylib: alembic uses Arnold 6.0.4.1
00:00:00    68MB         | loaded 5 plugins from 3 lib(s) in 0:00.02
        | Hello World
        |  
        | releasing resources
        | Arnold shutdown

BtoA初期のコードから移植し球体をレンダリング

前回書いた記事でBtoAを紹介しました。

blender-hack.hatenablog.com

BtoA初期のコードに球体をレンダリングするコードがあるので、その部分を抽出してレンダリングしてみます。 少し修正して、グリーンの球体になるようにしてみます。

参考にしたコード: https://github.com/lunadigital/btoa/blob/ece49981dfd55b8ae0e2ccb2cb9bbb43f1ffb00b/engine/__init__.py

import arnold

arnold.AiBegin()

sphere = arnold.AiNode("sphere")
arnold.AiNodeSetStr(sphere, "name", "mysphere")
arnold.AiNodeSetVec(sphere, "center", 0, 4, 0)
arnold.AiNodeSetFlt(sphere, "radius", 4)

shader = arnold.AiNode("standard_surface")
arnold.AiNodeSetStr(shader, "name", "redShader")
arnold.AiNodeSetRGB(shader, "base_color", 0.0, 1.00, 0.0) # 修正
arnold.AiNodeSetFlt(shader, "specular", 0.05)

arnold.AiNodeSetPtr(sphere, "shader", shader)

camera = arnold.AiNode("persp_camera")
arnold.AiNodeSetStr(camera, "name", "Camera")
arnold.AiNodeSetVec(camera, "position", 0, 10, 35)
arnold.AiNodeSetVec(camera, "look_at", 0, 3, 0)
arnold.AiNodeSetFlt(camera, "fov", 45)

light = arnold.AiNode("point_light")
arnold.AiNodeSetStr(light, "name", "pointLight")
arnold.AiNodeSetVec(light, "position", 15, 30, 15)
arnold.AiNodeSetFlt(light, "intensity", 4500)
arnold.AiNodeSetFlt(light, "radius", 4)

options = arnold.AiUniverseGetOptions()
arnold.AiNodeSetInt(options, "AA_samples", 8)
arnold.AiNodeSetInt(options, "xres", 1920) # 修正
arnold.AiNodeSetInt(options, "yres", 1080) # 修正
arnold.AiNodeSetInt(options, "GI_diffuse_depth", 4)
arnold.AiNodeSetPtr(options, "camera", camera)

driver = arnold.AiNode("driver_jpeg")
arnold.AiNodeSetStr(driver, "name", "jpegDriver")
arnold.AiNodeSetStr(driver, "filename", "myFirstRender.jpg")

filter = arnold.AiNode("gaussian_filter")
arnold.AiNodeSetStr(filter, "name", "gaussianFilter")

outputs = arnold.AiArrayAllocate(1, 1, arnold.AI_TYPE_STRING)
arnold.AiArraySetStr(outputs, 0, "RGBA RGBA gaussianFilter jpegDriver")
arnold.AiNodeSetArray(options, "outputs", outputs)

arnold.AiRender(arnold.AI_RENDER_MODE_CAMERA)

arnold.AiEnd()

コードを実行すると、以下の球体がレンダリングされました。

f:id:fixme:20210309093427j:plain
Arnold Python APIレンダリングした結果