Re: Passing python object into a python self installed plugin

30 views
Skip to first unread message

Philipp Oeser

unread,
Sep 29, 2009, 11:43:58 AM9/29/09
to XSI List
Hi list,

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

Matt Lind

unread,
Sep 29, 2009, 1:52:38 PM9/29/09
to soft...@listproc.autodesk.com
Use XSI's GridData object which is essentially a 2D array, but each
index can hold different data types if necessary. It has an
accompanying GridWidget object from PPGLayout which can be used to
display the data in a PPG.

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

Matt Lind

unread,
Sep 29, 2009, 1:23:57 PM9/29/09
to soft...@listproc.autodesk.com

var oGridData = XSIFactory.CreateGridData();

- or -

oCustomProperty.AddGridParameter( "mygriddata" );

Matt

Philipp Oeser

unread,
Sep 30, 2009, 9:49:43 AM9/30/09
to soft...@listproc.autodesk.com
Thanx for your answer Matt, really appreciate it,
I have worked with GridData in the past but I wanted to avoid it because
of cross-application use of the classes. (as a sidenote: I dont need to
display the data but I also remember the GridWidget to be unreliable
with bigger datasets...)

So please, anyone successfully added a python object to a PPG? Is it
possible? ( can we please get rid of COM? :) )

Greetz
Philipp

Aloys Baillet

unread,
Sep 30, 2009, 10:06:50 PM9/30/09
to philip...@web.de, soft...@listproc.autodesk.com
Hi,

Here's some Python code I wrote a long time ago that can recursively wrap virtually any python class and any object returned by methods of those classes.
It was working fine for us in most cases, and allowed complex python objects to be used by Jscript plugins with no modification.
To "wrap", return the result of the getComWrappedObject function to your command.
To "unwrap" from Python, get the _obj of the returned object to get the real python object.

Hope this helps.

Cheers,

Aloys
--
Aloys Baillet
Research & Development - Animal Logic
--
comwrapper.py

Philipp Oeser

unread,
Oct 4, 2009, 10:14:01 AM10/4/09
to soft...@listproc.autodesk.com
Hi Aloys,
sorry for answering so late (was away a few days), but thanx a million!
dont have XSI in front of me right now, but this sure will be handy...

[have you ever tried adding such an object to a PPG via
"AddParameter3"?]

Greetz
Philipp

Aloys Baillet

unread,
Oct 5, 2009, 5:45:34 AM10/5/09
to philip...@web.de, soft...@listproc.autodesk.com
Not really, but in that case I would rather use the very handy pickle module, and pass/store around a serialized version of your python objects. Strings will be easier to debug than complex COM wrapped objects!

Cheers,

Aloys

Philipp Oeser

unread,
Oct 5, 2009, 11:34:37 AM10/5/09
to Aloys Baillet, soft...@listproc.autodesk.com
Great suggestion Aloys! Will try asap. Sounds like I can even keep the
dictionaries this way :)
Reply all
Reply to author
Forward
0 new messages