I'm picking up an older thread cause this still could use some
clarification (at least for me)...
So, you can wrap python objects (see code below), pass them to
functions, have them as return values for functions etc.
BUT: COM doesnt seem to be able to handle pythons dictionaries (it does
handle regular types as strings, even lists, though)
Before I continue with another problem, have a look at the code below:
Can it be confirmed that: >> there is no way to wrap a python object
with a dictionary attribute through COM?
-----------------------------------------------------------
import win32com.client
import win32com.client.dynamic
import win32com.server
from win32com.client import constants as c
class DictionaryThroughCOM():
_public_methods_ = []
_public_attrs_ = [ 'sStringTest','lListTest','dDictTest']
def __init__(self):
self.sStringTest = "xsi"
self.lListTest = ["uno", "dos"]
self.dDictTest = {}
self.dDictTest["one"] = "uno"
self.dDictTest["two"] = "dos"
oTest = DictionaryThroughCOM()
oWrapped = win32com.server.util.wrap(oTest)
oDispatched = win32com.client.dynamic.Dispatch(oWrapped)
Application.Logmessage(oDispatched.sStringTest)
Application.Logmessage(oDispatched.lListTest.index("dos"))
Application.Logmessage(oDispatched.dDictTest["two"])
-----------------------------------------------------------
# INFO : xsi
# INFO : 1
# COM Error: Unexpected Python Error: TypeError: Objects of type 'dict'
can not be converted to a COM VARIANT
-----------------------------------------------------------
So, lets continue [ thanx for staying with me :) ]
Let's say we forget about the dictionaries and stick to the types COM
can handle (lists, strings etc.)
We have a python object that wraps and dispatches OK.
We want to store this object in a PPG so that some info (stored in list
attributes of this python object) is accessible in the various callbacks
of the PPG to avoid retrieving the info over and over again.
How could this be done?
Continuing from the above example code I've tried something like this:
(after removing the evil dictionary from the class)
-----------------------------------------------------------
oTest = DictionaryThroughCOM()
oWrapped = win32com.server.util.wrap(oTest)
oDispatched = win32com.client.dynamic.Dispatch(oWrapped)
log("dispatched OK " + oDispatched.sStringTest)
oPPG.AddParameter3( "oTest", c.siDispatch, oDispatched )
or: oPPG.AddParameter3( "oTest", c.siDispatch, oWrapped )
or: oPPG.AddParameter3( "oTest", c.siUnknown, oDispatched )
or: oPPG.AddParameter3( "oTest", c.siUnknown, oWrapped )
-----------------------------------------------------------
# INFO : dispatched OK xsi
# File "<COMObject <unknown>>", line 5, in AddParameter3
-----------------------------------------------------------
Any insights on this? Anyone gone through this and wants to share?
Like always: A big thanx in advance
Philipp
var oGridData = XSIFactory.CreateGridData();
- or -
oCustomProperty.AddGridParameter( "mygriddata" );
Just be sure to define the number of rows and columns, using RowCount
and ColumnCount respectively, before dumping data into it. If your data
is coming from something in the scene, such as a clusterproperty, you
can more quickly do the dump using the .Data property:
// create grid data object
var oUVWCoords = XSIFactory.CreateGridData();
// find all texture spaces in current cluster
var oClusterProperties = oCluster.Properties.Filter( "uvspace"
);
if ( oClusterProperties.Count > 0 ) {
// get first texture space in cluster
var oUVSpace = oClusterProperties(0);
// dump UV coordinates into griddata object
oUVWCoords.Data = oUVSpace.Elements.Array;
}
Matt
var oGridData = XSIFactory.CreateGridData();
- or -
oCustomProperty.AddGridParameter( "mygriddata" );
Matt
So please, anyone successfully added a python object to a PPG? Is it
possible? ( can we please get rid of COM? :) )
Greetz
Philipp