I used MEL for our existing Maya pipeline and have decided to slowly move to Python. Python is more powerful than MEL and I think it's more organized. It also has a lot of useful standard libraries. I don't see any reason to use MEL anymore.

Anyway, there are several things that I did to setup Python for Maya:
  1. Decide where to put all my python scripts
    I don't want to put it in Maya's default python scripts directory in order to consolidate all the engine stuff in one place. Subsequently, I added the path to PYTHONPATH environment variable. This ensures that Maya can import my modules.
  2. Python Initialization
    There are some stuff that you need to do when Maya first loaded. In order to do that, I created UserScript.py in my python scripts folder. Maya will automatically execute UserScript.py in PYTHONPATH.
  3. Add Shelf Button to Execute Python Script
    In order to "source" Python script conveniently, you can check out the following link http://www.rtrowbridge.com/blog/2008/11/maya-python-import-scripts/. You can then just do this to source a python script (in PYTHONPATH) python("pySource('')");
Another useful link to understand Maya Path: http://david.buttress.me.uk/blog/2009/05/maya-paths/

In order to debug Maya Environment Variable, you can use a script found here: http://david.buttress.me.uk/blog/2009/05/envbrowser-environment-variable-browser-mel/

Finally, I can now click a shelf button and it will execute a python script! It took me sometime to figure out the steps above. But now, I can start working with python :)
Missing the flexibility of C/C++ way in interpreting bits? In C/C++, you can reinterpret the bits by simply casting it. For example if you have an int and you want to reinterpret it as float, you can do the following:
int   i = 1234;
float f = *( (float*) &i );
The question is how can we do this in C#? It turns out there's a class in C# to do this. Meet BitConverter class.
int   i = 1234;
float f = BitConverter.ToSingle( BitConverter.GetBytes(i), 0 );
With this class, you can reinterpret bits from primitive data types. Which should be enough. The only problem is that the class is not suitable to be called every frames. Why? Using Reflector.NET, you can easily tell that BitConverter.GetBytes() is actually allocating memory!
public static unsafe byte[] GetBytes(int value)
{
    byte[] buffer = new byte[4];
    fixed (byte* numRef = buffer)
    {
        *((int*) numRef) = value;
    }
    return buffer;
}
One solution is just to keep in mind not to call this every frame. If you are willing to compile your C# project with /unsafe option, you can actually do this:
public static unsafe float Int32BitsToSingle(int value)
{
    return *(((float*)&value));
}
The solution above (and including the C/C++ solution) is actually not a cross-platform solution. If you are writing XNA game for Xbox 360, you might already know that Xbox360 uses big endian and Intel PC uses little endian. You might actually screw things up!

I think the best solution is to write a class that mimic GetBytes() in BitConverter, but make sure we don't allocate memory. Here's my solution to the problem:
using System;

namespace CastTest
{
    public static class Cast
    {
        private static byte[] tempBuffer = new byte[8];

        public static unsafe void GetBytesTo(int value, byte[] buffer)
        {
            fixed (byte* numRef = buffer)
                *((int*)numRef) = value;
        }

        public static float ReinterpretToSingle(int value)
        {
            GetBytesTo(value, tempBuffer);
            return BitConverter.ToSingle(tempBuffer, 0);
        }
    }
}
I am very excited to find a new baby in Visual Studio 2008! It is called T4: Text Template Transformation Toolkit. Before I start talking about T4, let me share the background story.

Basically, I was trying to find a way to enable C/C++ preprocessor to C#. It is possible to use "cl /E" command line to pre-process C/C++ file. I thought C# is close enough to C/C++, so surely I can somehow utilize C/C++ preprocessor.

There are several problems to this:
  • Integration to Visual Studio
    I wanted the process work seamlessly with Visual Studio. User should just add preprocessor directives to their C# files without worrying about anything else or doing additional steps. I thought that I could somehow use custom task/hack/hijack the msbuild system  to make this work. However, I have limited knowledge of msbuild system and I was unable to find an easy way to work around this.
  • C# Directives Incompatibility
    C# has #region/#endregion directives that's incompatible with C/C++ preprocessor.
I was about to give up when I found T4!

Text Template Transformation Toolkit, a.k.a T4, is a code generation utility that's built right in Visual Studio! I believe it's available starting from Visual Studio 2008. I saw somewhere on the net that it can be made to work with VS2005. Note that it's not available within the Express Editions of Visual Studio.

I am still learning about T4. However, it enables me to use C# to generate some other code and it integrates with Visual Studio IDE seamlessly. Basically, the way it works is that you just add a new file with "tt" extension (it will prompt you about some warning). I believe if you are in C# by default the "tt" file will generate "cs" file. You can specify within the tt file to generate other extension if you like want.

So excited~~!! This can be potentially used for generating shader files/shader permutation.

Adding T4 file to Visual Studio:










TT file:






















The generated CS file:

Believe it or not, I was an actor before :P. Although, I should admit, I wasn't a real actor. When I was in college, I needed to create special effects video and it happened that I became the main actor. Anyway, enjoy the video:



We used Maya + Real Flow for create the sandman. Very nostalgic!