カスタム コマンドからの戻り値として特殊な Python の型を使用する際の制限事項

 
 
 

Python で作成したカスタム コマンドからの戻り値をセットアップする場合は、次に示すように、Python ネイティブ ディクショナリと Python カスタム クラスをそのまま渡すことはできないことに注意する必要があります。

戻り値として Python ディクショナリを使用する場合

Python ネイティブ ディクショナリの型は、Softimage がカスタム コマンドからの戻り値として許可するデータ タイプに適合していません。 解決策として、次のコード(自己インストール カスタム コマンドからの抜粋)で示すように、代わりに ActiveX スクリプト ディクショナリを使用できます。

#---------
def testdictionary_Execute(  ):
	import win32com.client
	oDict = win32com.client.Dispatch( "Scripting.Dictionary" )

	oDict[ 'key1' ] = 123
	oDict[ 'key2' ] = 45
	oDict[ 'key3' ] = 6789
	
	return oDict
#---------

戻り値として Python カスタム クラスを使用する場合

Python では、Python クラスを ActiveX オブジェクトとして戻すことができますが、次の追加の作業が必要です。

Python の例: Python クラスを戻す自己インストール コマンド

次の自己インストール プラグインは、JScript または VBScript から使用できる Python オブジェクトを戻します。

# This class is going to be exported to VB and jscript
class TestPython:
	# Declare list of exported functions:
	_public_methods_ = ['GetAnswer']
	# Declare list of exported attributes
	_public_attrs_ = ['exclamation', 'answer']
	# Declare list of exported read-only attributes:
	_readonly_attrs_ = ['answer']
	# Class init:
	def __init__(self):

		# Initialize exported attributes:
		self.exclamation = 1
		self.answer = 42
		# Perfectly legal to have other non exported attributes

	# Exported function
	def GetAnswer(self, question):
		return "The answer to " + str(question) + " is " + str(self.answer) 
			+ "!"*self.exclamation
		
	# Perfectly legal to have other non exported functions.
		
# Traditional plug-in installation:
true = 1
def XSILoadPlugin( in_reg ):
	in_reg.Author = "Command Wizard User"
	in_reg.Name = "TestPython Plug-in"
	in_reg.Major = 1
	in_reg.Minor = 0
	in_reg.RegisterCommand( "TestPython","TestPython" )
	return true

def TestPython_Init( io_Context ):
	oCmd = io_Context.Source
	Application.LogMessage( "TestPython_Init called" )
	oCmd.Description = ""
	oCmd.ToolTip = ""
	oCmd.ReturnValue = true
	return true

def TestPython_Execute(  ):
	Application.LogMessage( "TestPython_Execute called" )
	oClass = TestPython()
	import win32com.server
	# Class MUST be wrapped before being returned:
	return win32com.server.util.wrap(oClass)

これで、Python オブジェクトを使用する Script Editor 内で、このテスト用 VBScript コードを正常に実行することができます。

set a = TestPython()
'INFO : TestPython_Execute called
LogMessage a.GetAnswer("life, the universe, everything")
'INFO : The answer to life, the universe, everything is 42!
a.exclamation = 10
LogMessage a.GetAnswer("life, the universe, everything")
'INFO : The answer to life, the universe, everything is 42!!!!!!!!!!

詳細については、次の Python ソースを参照してください。

  • aspn.activestate.com/ASPN/docs/ActivePython/2.3/pywin32/html/com/win32com/HTML/QuickStartServerCom.html: COM でラップされた Python カスタム クラスに関する情報と、ポリシー属性を使用してそのメソッドとプロパティを COM に公開する方法が記載されています。

  • www.oreilly.com/catalog/pythonwin32/chapter/ch12.html: 最も信頼のおけるガイド『Python Programming on Win32』のサンプルの章(「Implementing COM Objects in Python」)です。このガイドでは、COM を使った操作についても説明されています。ただし、カスタムコ マンドが完成したオブジェクトを既に戻しているため、CLSID/ProgID に関する情報はスキップしてかまいません。