Custom Objects and Classes for Scripting 2005-03-26 - By Bernard Lebel
Back Brad Friedman wrote: > What I wonder with your example, is what would happen if you returned an > instance of your Ls class from a command, and then called that command > from say... vbscript. Would you be able to call ls.HasAreaTransforms() > from vbscript, (...) > > -brad
That is actually a very good question, and the answer is yes, you can use Python classes with other langages. The same issue was brought up on a former sister mailing list, and here is what Jerry -the Softimage's Python MVP- Gamache had to say:
"""
Hello Bernard,
Python allows you to return a Python class as an ActiveX object, but extra work is needed:
1- Some attributes must be set at the class definition level to define which methods and attributes are exposed: _public_methods_ -> List of public functions _public_attrs_ -> List of public attributes (both read-only and read-write) _readonly_attrs_ -> List of read only attributes
2- The class must be wrapped before being returned: Use the win32com.server.util.wrap( ) function
The following self installable plugin returns a Python object usable from jscript and vbscript:
### ### Python script begin ###
# 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. true = 1
# Traditionnal Plugin installation: 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 end ###
This test vb script works just fine using the Python object:
''' ''' VB script begin ''' 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!!!!!!!!!! ''' ''' VB script end '''
And the killer is that a function called "unwrap" exists in win32com.server, allowing you to get back the pure Python object that was wrapped. This could be useful in some scenarios...
"""
Note that the issue was again brought up recently on another sister mailing list, and Kim promised to post something about that on CG-Soup.... we're still waiting Kim ;-)
Bernard
--- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
|
|