Archive for the 'Python' Category

Ming and version numbers

May 3, 2006

Ming has an obscure and not very well-documented function Ming_useSWFVersion() which can come in handy. In Ming 0.4.0, it defaults to Flash 5 SWF. This means if you want to add Flash 6+ functionality to your created SWF (e.g. you want to incorporate an onRollOver event function on an SWFSprite) from externally, it will not react. However, you can use the above function to set the version, e.g. (in Python, as always):

import ming
ming.Ming_useSWFVersion(6)

However, this creates what surely must be a bug. If you try adding code to a SWFSprite that uses the keyword this then the Ming ActionScript compiler throws an error, saying it does not recognise it. For me, this code does not work:

my_mc.add(SWFAction("this.onRelease = function() { trace('foo'); };"))

Luckily, I think you can miss out the this without too much worry, as Flash’s scoping usually picks up local variables first. If you really need it, you can always use eval to do:

my_mc.add(SWFAction(
  "t = eval('this'); t.onRelease = function() { trace('foo'); };"
  ))

and this compiles satisfactorily.

Compiling Ming for Python on Windows

April 26, 2006

Ming is a highly useful open-source Flash SWF writer that I am using in creating a Flash mapping application. However, it is only available as C source code and not Windows binaries, which is a pain if (like me) you work with an exclusively Windows-only environment and you have no Linux resources to hand. Trying to compile it can be a pain; the Windows environment is demanding enough, and it can be especially for Windows regulars who aren’t used to mucking around with Linux. To make it worse, there is precious little documentation around for it. So, for the benefit of anyone else interested in the same, here is how I managed to get the Ming modules for Python to work in Windows

1. Download & install Python 2.4 for Windows in e.g. c:/python
2. Download & install Cygwin in e.g. c:/cygwin. This provides you with a shell and other tools. When it comes to picking the modules and applications to use, add in autoconf, bison and flex. Do not install gcc or make – these are taken care of in the next step.
3. Download & install MinGW in e.g. c:/mingw. MinGW is not Ming – MinGW is an entirely different application takes care of the compilation.
4. Download the MinGW-zlib library. Copy the two .h files in usr/include/mingw to c:/mingw/include
5. Copy the file libpython24.a from c:/python/libs to c:/mingw/lib.
6. Download & install the Ming source code, in e.g. c:/ming
7. Download the source code for zlib. Copy the files zlib.h and zconf.h into c:/ming/src
8. Fire up Cygwin and go into the ming directory (e.g. cd /cygdrive/c/ming)
9. Type the following commands:

  $ autoconfig
  $ ./configure
  $ cd src
  $ mingw32-make static
  $ cd ..
  $ ls libming.a

10. A file called libming.a should now exist. Type cd py_ext and press ENTER.
11. In this folder, there should be a file setup.py. Open it, delete the existing code and replace it with:

from distutils.core import setup, Extension

setup(name = "mingc",
      version = "0.4",
      description = "A library for creating Flash files",
      py_modules = ['ming', 'mingc'],
      ext_modules = [Extension(name="_mingc",
                               sources=["ming_wrap.c"],
                               include_dirs=['/usr/local/include', '../src'],
                               library_dirs=['/usr/local/lib', '..'],
                               libraries=['ming'])])

12. Run the following command:

$ ../../python/python setup.py build --compiler=mingw32

(The path to your python instance may be different from the above)
13. In the folder build/lib.win32-2.4 there should now be a spanking new file _mingc.pyd.
14. You can test this by moving the file _mingc.pyd up two levels, in the same folder as shape.py, then running:

$ ../../Python/python shape.py

15. This should have created an SWF called test.swf, which consists of a red quarter-circle with a grey border.

References: When coming up with the above, I found this newsgroup post and this MinGW introduction page useful resources.