RE: Python Com and Overloaded methods problem

Date : Thu, 18 Jan 2007 09:44:08 -0000
To : <XSI(at)Softimage.COM>
From : "Daniele N" <daniele(at)passion-pictures.com>
Subject : RE: Python Com and Overloaded methods problem
I think this is turned into a very interesting post. 

So we know COM has some problems, it gives us the possibility to use
different languages but we pay that with less flexibility when we use
python... now, how could wrapping the entire SDK  to python help us?
Imagine we have it, what are the advantages and disadvantages. Is it
something we should desire or we can easily ignore it?   

 

Daniele ( Dan-ee-yel-eh )

________________________________

From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf
Of Aloys Baillet
Sent: 17 January 2007 22:39
To: XSI(at)Softimage.COM
Subject: Re: Python Com and Overloaded methods problem

 

Hi Andre,

I'm just starting to use SWIG to wrap C++ librairies in Python, Perl and
others. It's kind of old-fashion but it does work well. Boos.Python is
quite complex, but was used in the first Python for maya implementation
by cgkit ( http://cgkit.sourceforge.net/).
There's a post that compares the two:
http://retrograde-orbit.blogspot.com/2006/12/boost.html 

Cheers,

Aloys

On 1/18/07, Andre DeAngelis <andre.deangelis(at)ubisoft.com> wrote: 

Aloys,

 

I was reading what you wrote about Swig, but I was curious what your
experience was with using Boost Python.  What are the respective
advantages/disadvantages of each?

 

Andre

 

________________________________

From: owner-xsi(at)Softimage.COM [mailto: owner-xsi(at)Softimage.COM
<mailto:owner-xsi(at)Softimage.COM> ] On Behalf Of Aloys Baillet
Sent: January 17, 2007 5:50 AM


To: XSI(at)Softimage.COM
Subject: Re: Python Com and Overloaded methods problem

 

I'm not saying don't do it!
You can still manipulate things around with COM, you just loose the ease
of use of Python like operator overloading and easy iterations.
We had to use COM anyway because of our JScript legacy, we could rewrite
things in Python using a quarter of the time and the line count... 
Python support in XSI 6 being far better than in 4.2, and Maya
supporting Python as well, it's the right time to concentrate on one
language only!
I don't think .Net will bring you more than pythoncom objects, as the
limitations come more from Jscript itself (and VBScript even more). 

Good luck!

Aloys

On 1/17/07, Daniele N <daniele(at)passion-pictures.com> wrote: 

Sorry no good news here... Just stay away from COM unless you really
need it, or your company is bought by Microsoft:-)

 

Hahaha, no, not yet  ;)

Thank you again Aloys, this is all I needed to know. That's a shame
anyway, I liked the idea of use other supported languages for manipulate
objects created in python or vice versa... Maybe with .NET?

 

Cheers ,

Daniele ( Dan-ee-yel-eh )

________________________________

From: owner-xsi(at)Softimage.COM [mailto: owner-xsi(at)Softimage.COM
<mailto:owner-xsi(at)Softimage.COM> ] On Behalf Of Aloys Baillet
Sent: 16 January 2007 22:30


To: XSI(at)Softimage.COM
Subject: Re: Python Com and Overloaded methods problem

 

Hi Daniele,

A lot of limitations in here... the bigger one is JScript itself. I
don't think you can use operator overloading in JScript anyway, so the
__add__ method will not be understood by JScript anyway. I'm not even
sure you can do that in a standard COM object... 
Regarding the tuple vs list problem, the SafeArray COM object is the
same as a tuple: it's not modifiable. That's why pythoncom converts any
list to a tuple, it's a COM limitation again, not a pythoncom one. 
The bad thing here is that COM and JScript are not even close friends...
the fact that you have to use the VBArray.toArray method to use standard
JScript arrays from COM returned VBArray (aka SafeArray) is already a
source of headache... 

Sorry no good news here... Just stay away from COM unless you really
need it, or your company is bought by Microsoft:-)


Cheers,

Aloys

On 1/15/07, Daniele N <daniele(at)passion-pictures.com> wrote:

Hi Aloys,

I see... Unfortunately it seems impossible call overloaded special
methods like __add__ or __iter__, once the class is wrapped, doesn't
matter if it is called by a Jscript, VBScript or Python code.

 

For example exposing that class in a command:

 

from win32com.server.util import wrap

from win32com.server.util import unwrap

from win32com.client.dynamic import Dispatch 

import win32com.client

from win32com.client import constants

 

null = None

false = 0

true = 1

 

class MyClass:

    _public_methods_ = ['__add__', 'Method']

    _public_attrs_ = ['ClassArray']

    

    ClassArray = []

    

    def __add__(self, other):

        Application.LogMessage('porca miseria')

        ReturnClass = MyClass()

 

        for element in self.ClassArray:

            ReturnClass.ClassArray.append(element)

        for element in other.ClassArray:

            ReturnClass.ClassArray.append(element)

        

        return wrap(ReturnClass)

    

    def __init__(self):

        pass

        

    def Method(self):

        pass

 

def XSILoadPlugin( in_reg ):

    in_reg.Author = "daniele"

    in_reg.Name = "DispatchTestPlugin"

    in_reg.Email = ""

    in_reg.URL = ""

    in_reg.Major = 1

    in_reg.Minor = 0

 

    in_reg.RegisterCommand("DispatchTest","DispatchTest")

    #RegistrationInsertionPoint - do not remove this line

 

    return true

 

def XSIUnloadPlugin( in_reg ):

    strPluginName = in_reg.Name

    Application.LogMessage(str(strPluginName) + str(" has been
unloaded."))

    return true

 

def DispatchTest_Init( ctxt ):

    oCmd = ctxt.Source

    oCmd.Description = ""

    oCmd.ReturnValue = true

 

    return true

 

def DispatchTest_Execute():

    oClass = MyClass()

    return wrap(oClass)

 

 

And now launching this simple Jscript code:

 

Test1 = DispatchTest()

Test2 = DispatchTest()

 

Test1.ClassArray = new Array(0, 1, 2, 3)

Test2.ClassArray = new Array("Hello", "World")

 

Test3 = Test1+Test2

LogMessage(Test3.ClassArray)

 

I still not able to get correct this line Test3 = Test1+Test2. Test3 is
simply "null" making the line after fail: 

//ERROR : 'Test3.ClassArray' is null or not an object.

 

And by the way, if the class is wrapped and I'm used it from Python
code, if I pass a list it will be converted in a tuple... this is very
unwanted, don't you think?

 

Everything works fine with unwrap, but probably is a better idea don't
expose python classes to the com if it isn't really really necessary.

I'm developing a quite articulated framework for create rigs in python
an I thought to expose everything to the com for make it usable from
Jscript, but I have big problem now because of this idea.

 

Thank you for the answer anyway,

Daniele ( Dan-ee-yel-eh )

________________________________

From: owner-xsi(at)Softimage.COM [mailto:owner-xsi(at)Softimage.COM] On Behalf
Of Aloys Baillet
Sent: 14 January 2007 21:28
To: XSI(at)Softimage.COM
Subject: Re: Python Com and Overloaded methods problem

 

Hi Daniele,

Once your class instance is COM-wrapped, it's not available for regular
Python use: the wrap is COM-ready and you can return this object from a
python custom command and use it from JScript for example, but if you
want to access the original python object, you need to call the
pythoncom.UnwrapObject if I remember correctly.

Regarding the SWIG thing, we just need someone really courageous to
start this big big task... 

Cheers,

Aloys

On 1/13/07, Daniele N <daniele(at)passion-pictures.com> wrote:

One more thing.


Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.