これはフォーラムでの私の最初の質問です。私はそれが正しいことを願っています。 一般的な質問:変更するコンテキストやパラメータに応じて異なるデータ型の値を入力できるスクリプトを作成するときに、Pythonがエラーを返さないようにするにはどうすればよいですか? もっと具体的には:私はPythonを初めて使い、Foundry's Nukeのユーザーが同じクラスの複数のノードの値を一度に変更できるスクリプトを作成したいと考えています。変更するパラメータがチェックボックス( 'bool')で、RGBA入力( '4 float')であるかどうかによって、入力は異なるタイプでなければなりません。フォーラムを検索すると、そのタイプがタイプ()関数でチェックされ、if文でisinstance()関数と比較されました。私はそれを使って作業することができたと思う。 Gradenodeの乗算ノブはタイプ 'AColor_Knob'を返します。私はフロートのようなものを期待していた。それをisinstance()で比較しても、私が比較しているデータ型に関係なく、私には一致しません。これまでユーザーがどのようなデータ型(str、float、int、boolean ...)も入力できるようにする方法は?
Mainscript:私はこれまでのデータ型をチェックする方法
nukescripts.clear_selection_recursive()
userInput = nuke.getInput('Which type of nodes would you like to select? (!!!first char has to be capitalized!!!)',
'Shuffle')
matchingNodes = []
for each in nuke.allNodes():
if each.Class() == userInput:
matchingNodes.append(each)
else:
pass
for i in matchingNodes:
i.setSelected(True)
nuke.message(str(len(
matchingNodes)) + ' matching Nodes have been found and are now selected! (if 0 there either is no node of this type or misspelling caused an error!)')
userInput_2 = nuke.getInput('Which parameter of these nodes would you like to change? \n' +
'(!!!correct spelling can be found out by hovering over parameter in Properties Pane!!!)',
'postage_stamp')
userInput_3 = nuke.getInput('To what do you want to change the specified parameter? \n' +
'(allowed input depends on parameter type (e.g. string, int, boolean(True/False)))', 'True')
for item in matchingNodes:
item.knob(userInput_2).setValue(userInput_3)
:
selected = nuke.selectedNode()
knobsel = selected.knob('multiply')
print(type(knobsel))
#if type(knobsel) == bool:
if isinstance(knobsel, (str,bool,int,float,list)):
print('match')
else:
print('no match')
ありがとうございました。私はそれからたくさんの初心者を学びました。しかし、あなたが許可すれば、まだ2つの質問があります:私はtclに慣れていないので、何か:ノブルート{0}。{1} "{2}" 'フォーマット(node.fullName()ノブ名、ノブ_値) 'do?また、私はこのフォーマットを見たことがありません: '' new = {}(例) '' Pythonは '(例)'と何をするべきかを知っていますか? – leabum
@leabumあなたは文字列の書式を調べたいと思うでしょう、ここにいくつかのリンクがあります:formatstrings pep-3101 str.format NukeのTCLコマンドに関する情報を追加するために、上記の元の記事を編集しました。ちなみに、この投稿にはpython-3.xというタグが付いていますが、Nukeはまだ2.7(現在はバージョン10.x)で動作しています。 –