Wednesday 21 March 2012

Using the Maya API with Qt Creator

I've just started writing next terms Maya API lectures and decided it would be a good idea to use Qt Creator as the ide for development as it's our main IDE. It also has the advantage of automatically creating the make file required for the projects and helping to make multi platform builds easier to manage.

Qt Creator Project file
qmake and Qt Creator both use the .pro file for the projects. In this case I'm going to create builds for both Linux 64bit and Mac OSX. qmake uses the pre-fix values linux-g+-64 for all flags relating to linux 64 bit and macx for Mac OSX. 

The following .pro file shows how this is done.
# This file is split into Three sections
# The first configures Qt and the source files for all platforms
# The second is the linux build
# The third the mac build
# (if your using windows you will need to add a fourth one!)
# first lets remove Qt core and gui not going to need it
QT       -= core gui
# set the variable for the Maya Location, this will be different depending
# upon the platform / version of maya used
TARGET = HelloMaya
# for for mac we need a bundle so change the name
macx:TARGET=HelloMaya.bundle
# here we add the source files (and headers if required)
SOURCES+=HelloMaya.cpp
# these are defines required by Maya to re-define some C++
# stuff, we will add some more later to tell what platform
# we are on as well
DEFINES+=REQUIRE_IOSTREAM \
         _BOOL
# These are the maya libs we need to link to, this will change depending
# upon which maya framework we use, just add them to the end of
# this list as required and they will be added to the build
MAYALIBS=-lOpenMaya \
        -lFoundation

# now tell linux we need to build a lib
linux-g++-64:TEMPLATE = lib

# this tells qmake where maya is
linux-g++-64:MAYALOCATION=/usr/autodesk/maya2011-x64/
# under linux we need to use the version of g++ used to build maya
# in this case g++412
linux-g++-64:QMAKE_CXX = g++412
# set the include path for linux
linux-g++-64:INCLUDEPATH += $$MAYALOCATION/include \
                        /usr/X11R6/include
# set which libs we need to include
linux-g++-64:LIBS += -L$$MAYALOCATION/lib \
                   $$MAYALIBS
# tell maya we're building for linux
linux:DEFINES+=linux

# tell maya we're building for Mac
macx:DEFINES+=OSMac_
macx:MAYALOCATION=/Applications/Autodesk/maya2011
macx:CONFIG -= app_bundle
macx:INCLUDEPATH+=$$MAYALOCATION/devkit/include
# under mac we need to build a bundle, to do this use
# the -bundle flag but we also need to not use -dynamic lib so
# remove this
macx:LIBS +=-bundle
mac:LIBS -=-dynamiclib

macx:LIBS += -L$$MAYALOCATION/Maya.app/Contents/MacOS \
             $$MAYALIBS
There are several sections to the .pro file above, mainly these are to make it easier to add to the project when needed. The two main distinctions between the linux and the Mac OSX version are how the plugin should be generated. Under linux the plugin is a normal shared object file (.so), however the mac version requires a bundle. To do this the -bundle flag is used and we also need to ensure that the extension .bundle is added to the Target.

Finally we need to add some defines to the build, depending upon the platform we need to either use -DOSMac_ or -Dlinux as well as the two flags REQUIRE_IOSTREAM which includes the correct iostream library for using std::cout and _BOOL to re-define bool.

Test Program
The following test program will send a message to both the shell maya was run from and the maya command window.

#include <maya/MSimple.h>
#include <maya/MIOStream.h>
#include <maya/MGlobal.h>

// This is a macro to create a simple command
// the compiler expands it to a bunch of code

DeclareSimpleCommand( HelloMaya , "NCCA", "Maya 2011"); 

MStatus HelloMaya::doIt( const MArgList& )
{
  std::cout<<"This should come from the shell\n";
  MGlobal::displayInfo("Hello Maya in the maya command shell");
  return MS::kSuccess;
}
Once this program is build depending upon the platform there should be a plugin ready to load in the maya plugin manager. The image below show this and the info set from the Command above
We now have two options to execute the loaded command. In the mel window we can invoke the command as follows, For the mel version
And for the python version we need to import the maya.cmds module first.


Thursday 15 March 2012

Using Flex with Qt

I've got a large project to work on which will require quite a bit of parsing / scanning of files so I've decided to teach myself flex and bison. So far I've read the first chapter of the excellent book Flex and Bison and the first example is as follows
/* from the flex and bison book do a word count */

%{
int chars=0;
int words=0;
int lines=0;
%}

%%

[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n    { chars++; lines++; }
.     { chars++; }

%%
int main(int argc, char **argv)
{
  yylex();
  printf("Word count %8d %8d %8d\n",lines,words,chars);
  return 0;
}

To build and compile this we use the following command line
flex -o wc.c wc.l
gcc -o WordCount wc.c -lfl
This works fine, but as I use QtCreator as my ide I really want to configure Qt to do this all for me. QtCreator does have some limited lex and yacc support however I really wanted to use flex and bison, so need to add my own compiler to the qmake build system via the Qt project. To do this we use the QMAKE_EXTRA_COMPILERS flag in the project and build our own compile chain, the following extract shows how this works
# the is the name of the exe we want
TARGET=WordCount
# this is the source fed to the flex syste
FLEXSOURCES+=wc.l
# we then add this to the other files for editing
OTHER_FILES+=$$FLEXSOURCES

###########################################################################################
# now we are going to create a Qt compiler ojbect for the EXTRA_COMPILERS FLAG
# this basically has a number of attributes which we need to set as outlined below
###########################################################################################
# this flag list the sources we wish to input to this custom compiler, in this case listed above
# in the FLEXSOURCES variable
flex.input = FLEXSOURCES
# now we need to say what files to output, for flex we want to create the .c
flex.output = ${QMAKE_FILE_BASE}.c
# this is the actual compile command we wish to call in this case it wil be
# flex -o (output).c input.l
flex.commands = flex  -o ${QMAKE_FILE_IN_BASE}.c ${QMAKE_FILE_IN}
# once this compiler has been run we need to add the output files to the linker stage in this case
# as we are generating .c files we just add it to the SOURCES variable and it will be compiled
flex.variable_out = SOURCES
# this flag ensures that the extra compiler is run before the main c one (as we need to have the .c files
# generated by flex before we use the gcc compiler)
flex.CONFIG += target_predeps
# this tells the make clean command what files to remove
flex.clean =  ${QMAKE_FILE_IN_BASE}.c
# this is just the name of the extra compiler used in the make file (make flex for example)
flex.name = flex
# finally add this to the qmake project so it will have the makefile generated
QMAKE_EXTRA_COMPILERS += flex
# we need the fl lib for flex
LIBS+=-lfl
# tell qt we usign gcc
QMAKE_LINK=gcc
# this just cleans out some things for mac
CONFIG += console
CONFIG -= app_bundle

And that is about it, now I've got to read the rest of the book, but at least it makes the coding easier!

Tuesday 6 March 2012

Setting OpenGL Formats in Qt

Got asked how to enable multi-sampling in my demos the other day, and realised that I hadn't shared this information in any of my lectures so I thought I would write it up here.

OpenGL has a number of extensions which allow a number of different rendering features to be enabled. For example we can do stereo, accumulation  multisampling and much more.

In Qt we do this by using the QGLFormat class and we can enable it for a specific QGLWidget, or for all widgets we create. For this example I will generate a default format and then create a widget to use the format. This code would be put in main.cpp in my demos (will update them soon) before the creation of the MainWindow class.

QGLFormat glf = QGLFormat::defaultFormat();
glf.setSampleBuffers(true);
glf.setSamples(4);
QGLFormat::setDefaultFormat(glf);
Now when we create the GLWindow in the ngl:: demos this will be used for all windows created. Finally we need to enable GL_MULTISAMPLE when rendering which can be done using the following code
glEnable(GL_MULTISAMPLE);
This method can also be used to enable things such as the new OpenGL core profile (under linux and windows for Qt 4.7 and with Qt 4.8 this will also work on the mac eventually, as long as you have Lion). You should be able to see from the documentation all the other features which can be enabled in this way.