Python’s win32com and XSI

March 4th, 2005 by Patrick Boucher - Viewed 5775 times - Popularity: 10% [?]




So I’m tinkering with Python inside XSI and I’m noticing a lot of people talking about win32com and constants. Some of these people are using these items, often puting import statements at the top of their Python files, without necessarilly understanding why. For those who wish to understand why, here’s my stab at an explanation.

1
2
import win32com.client
from win32com.client import constants as c

The code provided above is code you can safely add to everyone of your XSI Python script files and you’ll be ready for pretty much everything. But what does it do?

The import and from keywords are executable statements in Python. The keyword import assigns an entire module object to a single local name while from assigns one or more names to objects of the same name, or not, in another module. When your Python interpreter executes the first line it loads the module win32com.client and makes it available to your script through the same name. When the interpreter hits the second line it takes the name constants from the win32com.client module and makes it available to your script as c.

What is win32com.client anyway?

The com in win32com stands for Component Object Model. Microsoft’s COM is a system that was built to facilitate code reuse at the binary level. This means that a specific language can leverage objects written in another language because the COM system doesn’t require you to understand the source code, header files or object libraries. Even better, the COM model specifies methods that allow objects to expose their implementation details and allow dynamic discovery. This means that scripting languages can use COM functionality without having to do early binding… Scripting languages can use COM… Whatever. The COM system is the corner stone of other systems such as DCOM, OLE and ActiveX and has turned into the de-facto standard for application development on the win32 platform.

In basic COM automation you can be a server, exposing yourself to be played around with. You can also be a client, using other COM objects in your own environment to do usefull things. If you’ve done JScript or VBScript you’ve used a lot of COM objects because these languages don’t have large libraries and rely heavily on external objects. If you’ve coded a line in these languages that included CreateObject() you’ve used a COM automation object!

Other languages like Perl and Python don’t really need COM objects because of their large libraries. They can, through these libraries, access the filesystem, bind to network sockets and do all sorts of fancy stuff that other scripting languages rely on COM for. When these languages do need to create COM automation objects there is often a library that can handle the gory details. Python’s win32com is such a library.

XSI itself being a COM server of sorts you’ll sometimes need to create COM objects and win32com.client will come in handy. The coolest use of win32com.client though is using other applications through automation inside XSI. I’ll provide an example of this later on…

How about the constants thingy?

COM objects often define constants that you can/should use in order to interact with them. Different scripting languages take different approaches at making these constants available to you. VBScripts loads all these constants automatically into your namespace. Python exposes these constants to you through the win32com.client.constants name. The second line in the above code is written to accelerate access to these constants, you can just use c.yourConstant. One thing to know is that these constants are only available, through whatever methods your scripting language so chooses, once you’ve created an instance of the COM automation object. This is due to the fact that scripting languages dynamically discover COM objects and don’t know what the objects are about before actually using them.

For example, in XSI a VBScript to list all selected objects could be:

1
2
set allObject = SIFilter( , siObjectFilter)
LogMessage(allObject)

… but disliking VBScript I would write it in Python this way:

1
2
3
4
5
import win32com.client
from win32com.client import constants as c
 
allObject = Application.SIFilter(None, c.siObjectFilter)
Application.LogMessage(allObject)

or

1
2
3
4
from win32com.client import constants as c
 
allObject = Application.SIFilter(None, c.siObjectFilter)
Application.LogMessage(allObject)

You don’t really need the first line (import win32com.client) because you’re only really using the constants in this tiny example.

XSI constants seem to be allready loaded in the win32com.client.constants name even before you create a COM object that would expose these constants but that’s not quite true. Operating inside the XSI script editor an object called Application is allready automatically created for you and it is the creation of this object that populates the win32com.client.constants name with the values you like, and need, so much!

Stupid COM tricks!

So you kept on reading because you wanted to know what I meant by “using other applications through automation inside XSI”? Through Python’s win32com.client module you can create COM automation objects that allow you to do some fun stuff. Let’s say that you wanted to provide some XSI scene related info to some project manager through some Excel file. A script could be written that would remotely control Excel through COM automation and write some data in a worksheet. Even more useful: you could read data from a worksheet and apply it to whatever you needed to in XSI. You can figure out loads of fun stuff to do by using COM objects!

Try this Python-based COM automation “Hello World” code in XSI just for kicks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import win32com.client
from win32com.client import constants as c
 
excel = win32com.client.Dispatch("Excel.Application")
book = excel.Workbooks.Add()
sheet = book.Worksheets(1)
sheet.Range("A1").Value = "Hello World!"
sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter))
book.SaveAs("c:\myBook.xls")
 
sheet = None
book = None
excel.Quit()
excel = None

You will have created a file in your root C: drive called myBook.xls that is an Excel file containing one cell in which the words “Hello World!” are written. Please note that this COM usage is not limited to Python. You can do this in whatever language that supports COM. I’ll leave it up to the reader to learn more about the specifics of the different COM objects that are out there.

Hope it was usefull!

Popularity: 10% [?]

2 Responses to “Python’s win32com and XSI”

  1. [...] e goodness March 20th, 2005 by Patrick Boucher A little while back I spoke of Python’s win32com module and why you have to know it exists in the XSI context as well as a tip concerning Ap [...]

  2. Mustafa says:

    That`s a fine article. And if you need examples, visit http://win32com.goermezer.de . There are ~150 Python example scripts about win32com.

Leave a Reply