pymel 讓 optionVar 用起來看起來,更像 dictionary

Feb 4 2009

animationpython

Maya 裏頭有個非常 high level 的 mel function: optionVar,有別於其它的 functions 的簡單與立即反應,它的目的在提供一個跟 Maya session 的設定檔的存取。換個說法,你可以在一次執行 Maya 的時候,存一個自訂的變數 IamSoHandsome,然後在下一次執行時取出來用。所以你可以把它想成是 Windows 上頭的 registry、Unix-like 下的 .ooxx 個人設定檔、或是會永續存在下去的 environment variables。

optionVar 另一個優秀的地方在於,它允許你隨時更改存下來的變數的 data type。所以你可以今天把 IamSoHandsome 宣告成一個只有 0 與 1 的 boolean,然後明天改成 “yes” 與 “no” 的 string,只要直接指定給它一個新的 data type 就做到了。如下:

optionVar -iv "IamSoHandsome" 1;
...
...
optionVar -sv "IamSoHandsome" "yes";
...
...

optionVar -sv:

creates a new variable named using the first string with value given by the second string. If a variable already exists with this name, it is overridden in favour of the new value (even if the type is different).

pymel 更酷了,它讓整個 optionVar 變成了一個 dictionary-like global object(singleton class),所以你可以非常迅速地做到如下幾件事:

print optionVar["RecentFilesList"]

myVar = optionVar.get("IamSoHandsome", 1) # default to 1 if it is not in optionVar

optionVar["IamSoHandsome"] = 1
optionVar["IamSoHandsome"] = "yes"
optionVar["IamSoHandsome"] = [1, 2, 3, 4] # quickly change its value and data type without any hesitate
optionVar["IamSoHandsome"] = optionVar["IamSoHandsome"][1:] # [1,2,3,4] => [2,3,4]

我愈來愈愛用 optionVar 來提供更好的 maya GUI scripts 了 :)

comments powered by Disqus