Thursday, January 28, 2010

Considering the Flash 10 3D API

I'm back from a much needed vacation with a new lease on life.

Currently, I am looking into a 3D treatment for a UI in production. Where 3D meets Flash, there are plenty of established third-party 3D engines available from the open source community. To name a few:

Sandy 3d
Five3d
Papervision 3d
Unity 3d
and others...

With the advent of FP 10, Adobe now has their own, not quite 3d option.

2.5D

The Flash 10 3d api is known as a 2.5d option, since it involves perceptual transformations in a 2d environment rather than a bona fide x,y,z coordinate space. Aptly enough, it is also referred to as "Postcards in Space". It is fine for menus and presentations such as the one I'm looking into, where the scope of motion is limited to fixed options.

Planning it out

Putting together a 3d UI adds complexity to an already complex process. Thus it is necessary to break the process down into manageable steps. Here are the general guidelines I am following:
  • Bitmapped planar polygons
  • in a 2d mesh
  • in a display object container
  • perspective positioned relative to mouse position.
With this in mind, I can follow a process of assembly from top-down or bottom-up. Let's look at top-down.
  1. Create xml-driven coordinate system to hold masked bitmaps as a 2d display object
  2. Add each of these 2d display objects as a plane of a 3d cube
  3. Render cube by mouse coordinates
or bottom-up
  1. create 3d cube
  2. wire up cube to mouse interaction
  3. add complex content to faces of cube
In this case, I already have a sprite object built for top-down step 1, aka bottom-up step 3. I can scratch that off the to-do list. The question remains, how well would a proposed cube face handle a complex sprite?

First of all, there are different ways to present 3d objects in Flash 10. There is the use of a display object, with its properties of
  • rotation x, y and z
  • scale x, y and z
Another way is to use the newly-introduced vector data type to add a series of points to a graphics class, thus to draw polygons using a series of point data.
  • beginShaderFill()
  • drawGraphicsData()
  • drawPath()
  • drawTriangles()
  • lineBitmapStyle()
  • lineShaderStyle()
The latter method allows for precise polygon mapping with contour details intact. Some startling geometry can be plotted with the graphics API of Flash 10. Vector data and the graphics API is expertly detailed by senocular in his Flash 10 API primer.

Since my UI is effectively a cube with complex objects inside it, the former method will be the best for this effort. Incidentally, have a look at Lee Brimelow's recent tutorial on a Flash 10 API 3d image carousel. It's a good basic intro to the technique. Plus, it mentions a critical point of the implementation, the need to address z sorting. Did somebody say quaternion?

So, containers can use x,y and z positioning data. Furthermore, they may contain nested containers. Moving forward is now possible.

Tuesday, December 22, 2009

Flexcairngorm extensions considered

Tracing the evolution of the Cairngorm framework reveals a great effort to isolate business logic. A coup of the framework has been to shift event flow from exclusively the display list towards a simple, efficient non-UIComponent propagation within an application architecture.

Specifically, the native event flow in Flex works hierarchically within the display list. Since display objects are by definition hierarchical, events flow through them from impetus to the bottom node and, if so configured, back again. This ensures full propagation of the event. A reasonable supposition on behalf of Cairngorm engineers is the difficulty of performing business logic apropos of a display hierarchy. Business logic requires a unique non-display set of delegates, services and model managers. The Cairngorm framework places events at the behest of these classes.

How then to bridge the new events and the old? This is where flexcairngorm takes the reins.

There is a very handy explanation of this, supplemental graphics and all, by Thomas Burleson on Gridlinked.info. He describes the process by which notification returns to the view following the propagation to business logic and the response back. It involves IResponders and a callback parameter. Browse gridlinked.info to learn all about it.

Once you've read it, the next step is coordinating flexcairngorm with Mate dependency injectors, using Mate EventMaps.

Flex MVC options

The set of instructions in rich applications tends to get quite complex. Thus, RIA code needs to be divided up into smaller components to manage complexity. Design patterns such as MVC are loose methodologies for doing this, with limitless implementations in practice. In Flex, MVC are often referred to as microarchitectures. They manage business logic for a client application, allowing decoupling between logic and UI.

The prominent first generation of mature MVC framework for Flash and Flex is called Cairngorm. It is now up to 2.2 with version 3 hovering indefinitely in beta. Cairngorm introduces a new system of event management called the CairngormEvent. It is more analog than extension of a Flex Event, since it has unique dispatch and propagation attributes.

The pros and cons of isolated event management, MVC and FlexEvent, have been largely considered in the developer community, resulting in some rethinking of Cairngorm event methodology. A prominent advance is UniversalMind's UM Cairngorm Extensions. This represents first and foremost an enrichment of the Cairngorm framework. If you plan on going down this path, UM Extensions may allow you to do more with it.

Otherwise, Flex is reaching the second iteration of third party frameworks, as Cairngorm gets more competitors. Mate and Swiz lead the pack, with retooled and reconsidered architectures for events and business logic structures. In some cases, microarchitectures are fully MXML in the second iteration.

By no means is the solution one-sided, as many groups have invested heavily in their extant codebase. In any case, all the options deserve a thorough look see. Some orientation will allow you to consider the best for your project.

Thursday, December 3, 2009

Multiple AMF Django apps share a common gateway

A while back I wrote about a library called django-flashpolicies for providing a flexible remoting environment. Essentially, it frees up the root of your django project for whatever content you like while providing a crossdomain policy file there for remoting applications.

That is still one way to go, but as an advance on that, have a look at the Django-amf-gateway library.

The PyAMF team offers info on it as a way to structure a django project such that you can heap on all the apps you want and allow them to share a common http gateway. By simplifying gateway addresses on the server side django-amf-gateway simplifies resource management on the client side.

Works great in applicable cases.

Tuesday, December 1, 2009

More on mod_wsgi issues

When you add wsgi to Apache it is so you can run Python on Apache. Thus you need, at a minimum, Python and Apache.

Python

If you use a utility such as virtualenv to compartmentalize several domain development environments on one server account, installing mod_wsgi may not and probably will not be associated with the correct Python library. Will not, that is, unless you explicitly point it out in the installation process. Be sure to plan in advance which Python install you are developing for, and be sure to flag it when installing mod_wsgi:

./configure --with-python=/home/django/domains/mydomain.com/bin/python2.5* ***

*** UPDATE (1/31/10): Please read the helpful comment and link from Graham Dumpleton, author of mod_wsgi, below. Per his direction, it is best to install mod_wsgi against the complete base Python installation and add references to the virtual environments from your Apache config or from the WSGI script file. Thanks for taking the time to clarify, and for making mod_wsgi available for general use.

That said, there are many ways to install mod_wsgi, from manual to managed by aptitude, get-apt or the like. The downside to using a managed install is the dependency on the vintage of their managed version. At last check, the aptitude repository of mod_wsgi is still at 1.3 while the current stable version on googlecode is 2.8. Thus, you may want to look into another method, perhaps a manual installation.

Apache

When manually installing mod_wsgi, be sure you have the appropriate development library for apache, in the case of ubuntu hardy heron, apache2_threaded_dev will allow you to install mod_wsgi. Furthermore, if you are using aptitude to install apache2_threaded_dev, go ahead and update the distro as a whole first by running sudo aptitude update. If you do not, you may find the install doesn't work, and there will be no indication as to what went wrong.

Finally, with these measures in place, try ./configure with the appropriate flags and see if you can get the correct library in place. Good luck.

*your location will be different from this example

Monday, November 30, 2009

ORM options for Python server to Flex client

Let's say Python is your solution of choice for backend development and Flash technology is your preferred client. If your project uses a database, you will need a good ORM solution at some point. The options are many and here are a few of them:
  • WebORB by MidnightCoders
  • Django built-in ORM
  • SQLAlchemy ORM
There are surely other options (roll your own, anybody?) but these come to mind readily. Now, how to choose which one? Let's look at the capabilities of each:

WebORB

Pros: MegaOptions, from RTMP Server to Secure Transactions and lots more, turnkey CRUD, GUI admin, GUI performance, plug and play, one-click ORM generator, ORM testing suite. The list goes on...

Cons: Costs a pretty penny last I checked. Could be considered bloaty. I would have to hear some performance testers weigh in on this.

Verdict: Great at limiting server-side development to a minimum. Great full-service solution. Would be a strong contender in a rich media studio or design shop.

====================================

Django built-in ORM

I'll use it when the time comes. Methods to replace Django's ORM outright with SQLAlchemy have been detailed on some intrepid developer blogs. I would rather not customize the framework too much, as it introduces potential troubleshooting down the line. You can use Django ORM and an addition ORM library like SQLAlchemy interchangeably, so that would be my first choice if going this route.

=====================================

SQLAlchemy

A very robust open source option with a surfeit of detailed documentation, this one regularly gets my vote. It is sufficiently explicit to require familiarity to use, but not too steep a curve to get started quickly. Extensions like Elixir simplify the code at the expense of greater abstraction. This is a case of using the right tool for the right project, so feel free to learn more about both.

Sunday, November 29, 2009

MAMP with mod_wsgi

Once you get a database set up, sqlite or otherwise, you can use it with Django applications. You can serve Django directly from a command line or through Apache. In either case you will need Django to be installed. From a command line, simply run the django server and browse to it:

django-admin.py runserver

If your production server is going to be Apache, your best bet is to run Django under Apache locally. To do this you need a helper library for Apache to use. The de facto solution is mod_wsgi.

mod_wsgi

To get started with mod_wsgi, typically you download mod_wsgi from Graham Dumpleton's Google Code repository, you go to your command line, you untar it, you configure it, you make it and you make install it. This makes the code library available to wsgi-enabled services, in effect allowing you to run Django on Apache.

When you make install mod_wsgi, it places the libraries in your /lib directory. Then it links to your Apache directory where OSX keeps it, /usr/libexec/apache2. Installation includes automatic detection, or you can use some optional flags to specify resource paths, detailed in the wsgi installation guidelines.

Now your OSX Apache has modwsgi installed, but the Apache version in MAMP does not. To verify this, try making a Django app and serving it via MAMP. First, make a new django app in an appropriate directory:

django-admin.py startproject myproject

Next, open and modify the config for your MAMP Apache app, located in /Applications/Mamp/conf/apache/httpd.conf. For that matter, have a look at what your OSX Apache instance has in its config file. It is private/etc/apache2/httpd.conf. Searching for wsgi in this file yields:

LoadModule wsgi_module libexec/apache2/mod_wsgi.so

Looks good even if the relative directory location may be an issue. Insert this info into the MAMP httpd.conf and test it. Actually, there is no need because it will not work. This is due to the structure of MAMP and its proprietary use of Apache. Unlike a full install of Apache, the folders in MAMP have a different structure and are optimized to run modularly. Consequently, you cannot append a flag to your configure command in this manner:

./configure --with-apxs=/usr/sbin/apxs2-worker

because the structure available to Apache is not in MAMP, so detection will fail.

This leaves a couple of options. Either reconstruct some of the MAMP Apache folders so detection will succeed or look for another solution. Considering the evident lack of general success from many Google searches, I am letting this fish off the hook. Feel free to leave a comment if you know a way to make it work.

As a final consideration, mod_python appears to be a lot quicker to set up with MAMP. That could be a candidate if you are in a pinch. Otherwise, if serving Python via Apache is your goal, I recommend using the resources included with OS X and skipping MAMP altogether.