整数インデックスを使用してコレクションの項目にアクセスするには、次の例に示すように [] シンタックスを使用します。
oObj = Application.ActiveSceneRoot.AddGeometry("Cube","MeshSurface")
oEdges = oObj.ActivePrimitive.Geometry.Edges
try:
oEdge = oEdges.Item( 10 )
except:
Application.LogMessage( "You cannot call .Item() method explicitly" ) ;
# Instead do this
oEdge = oEdges[10]
Application.LogMessage( oEdge.Vertices.Count )
XSIUtils.Environment["MYVAR"] = "bar" ; Application.LogMessage( XSIUtils.Environment["MYVAR"] ) ;
次の例では、ランタイムにスクリプティング オブジェクトのメソッドを取得する方法を示します。
def GetFunctions( dynDisp ):
"""Returns a sorted and unique list of all functions defined in a dynamic dispatch"""
dict = {}
try:
for iTI in xrange(0,dynDisp._oleobj_.GetTypeInfoCount()):
typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI)
typeAttr = typeInfo.GetTypeAttr()
for iFun in xrange(0,typeAttr.cFuncs):
funDesc = typeInfo.GetFuncDesc(iFun)
name = typeInfo.GetNames(funDesc.memid)[0]
dict[name] = 1
except:
pass
ret = dict.keys()
ret.sort()
return ret
import pprint
funcs = GetFunctions(Application)
Application.LogMessage(pprint.pformat(funcs))
funcs = GetFunctions(Application.ActiveSceneRoot)
Application.LogMessage(pprint.pformat(funcs))また、次のように、Python により公開された情報からいくつかの基本的なオブジェクト モデル ドキュメントを生成することもできます。
def FormatDocumentation(typeInfo, funDesc):
nameAndParms = typeInfo.GetNames(funDesc.memid)
docum = typeInfo.GetDocumentation(funDesc.memid)
import pythoncom
# This differentiates between "functions" and "accessors"
if funDesc.invkind == pythoncom.INVOKE_FUNC:
docStr = nameAndParms[0] + "(" + ",".join(nameAndParms[1:]) + ")\n"
else:
docStr = nameAndParms[0] + "\n"
if docum[1]:
docStr += "\t" + docum[1]
return docStr
def GetDocumentation( dynDisp, funcName = None ):
allTypeInfoDoc = []
for iTI in xrange(0,dynDisp._oleobj_.GetTypeInfoCount()):
typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI)
typeAttr = typeInfo.GetTypeAttr()
if funcName:
for iFun in xrange(0,typeAttr.cFuncs):
funDesc = typeInfo.GetFuncDesc(iFun)
name = typeInfo.GetNames(funDesc.memid)[0]
if name.upper() == funcName.upper():
return FormatDocumentation(typeInfo, funDesc)
else:
dict = {}
className = dynDisp._oleobj_.GetTypeInfo(0).GetDocumentation(-1)
classDoc = "class %s \n\t%s\n\n"%(className[0],className[1])
allFuncDoc = []
for iFun in xrange(0,typeAttr.cFuncs):
funDesc = typeInfo.GetFuncDesc(iFun)
name = typeInfo.GetNames(funDesc.memid)[0]
if not dict.has_key(name):
dict[name] = 1
allFuncDoc.append(FormatDocumentation(typeInfo, funDesc))
allFuncDoc.sort()
allTypeInfoDoc.append(classDoc + "\n".join(allFuncDoc))
if funcName:
return "Documentation not found for %s"%(funcName,)
else:
return "\n".join(allTypeInfoDoc)
# There are two ways to call this: without a function name and with a function name
# Without a function name, it retrieves documentation for all functions
Application.LogMessage(GetDocumentation(Application))
# And with a function name, it tries to find the documentation for that function
Application.LogMessage(GetDocumentation(Application, "StatusBar"))
Application.LogMessage(GetDocumentation(Application, "NotThere"))次の例のように getattr を使用して、選択したオブジェクトが Properties プロパティをサポートしているかを特定することができます。
oObj = Application.Selection(0)
if getattr(oObj, "Properties", None):
Application.LogMessage("Yes")
else:
Application.LogMessage("No")カスタム コマンドに基づく Python の戻り値は常に ActiveX セーフ オブジェクト タイプに変換されます。
別の Python スクリプトから Python に基づくコマンドを呼び出す場合は、win32com.server の unwrap 関数を使用してそのままの Python オブジェクトを取得することができます。
Python カスタム コマンドは、関数と属性を VBScript と JScript がメソッドおよびプロパティとして使用できる Python オブジェクトを返すことができます。「 カスタム コマンドから Python タイプを返す」を参照してください。
引数を指定しない場合、vars() 関数は現在のローカル シンボル テーブルに対応するディクショナリを返します。モジュール、クラス、またはクラス インスタンス オブジェクト(または、その他の __dict__ 属性を持つもの)を引数に指定すると、オブジェクトのシンボル テーブルに対応するディクショナリを返します。
次の例で、LogMessage 呼び出しを参照してください。
# Set up an SIVector3 math object v3 = XSIMath.CreateVector3() v3.Set( 10.0, 20.0, 30.0) v3.ScaleInPlace(2) # Initialize the variables (you need to initialize # variables to be used as input to SIVector3.Get) x=0 y=0 z=0 # Retrieve the three values from the SIVector3.Get method # (use the same variables as input) x, y, z = v3.Get(x, y, z) # Write the results to the Script Editor Application.LogMessage( '%(x).2f %(y).2f %(z).2f' % vars() )
ここでは、vars() は x、y、z を持つディクショナリを返します。
x = 10.0 y = 20.0 z = 30.0 Application.LogMessage( '%(x).2f %(y).2f %(z).2f' % vars() )
my_dict = { 'x':10.0, 'y' : 20.0, 'z' : 30.0 }
Application.LogMessage( '%(x).2f %(y).2f %(z).2f' % my_dict )