
hohehohe2's OpenMaya tutorial
kotamuraa@yahoo.co.jp
4. Hello World
In this chapter we will make a hello plug-in.
When Maya load the plug-in, a new MEL command "hello" is created.
And When this "hello" command is executed, a string
Hello! Hello!
is displayed in the Output window. (the window that appears when
you render a scene)
A plug-in that create a new MEL command is called a "command plug-in"
Setting up a project
First, we will set up a project. In this chapter, we will
use Maya Plug-in Wizard
and skip complicated settings. Again, this process is for VC++6.0,
not for VC++.NET.
- Start VC++
- File -> new -> "Project" tab
- Select Maya Plug-in Wizard
Read
here before you start screaming "I Can't find it!"
- Type "helloCmd" in the project
name field
- Type the folder name
where helloCmd project folder will be created in
the "Location" field.
- "OK"
- Maya Plug-in Wizard step 1/3
- Please enter version....
Leave unchanged(4.0)
Select other versions if you want to build
a plug-in for other versions
- Location of the developer kit
Leave unchanged (Use installed copy)
- What is the vender name...
Your name maybe.
- Verbose comments in code
Leave unchanged
- "Next"
- Maya Plug-in Wizard step 2/3
- What type of plug-in would
you like to create?
Select "MEL command with
Undo/Redo"
This selection is very important. I will
describe it briefly.
"Empty"
Empty
project. Not used frequently.
"MEL command"
Make a project
for a command plug-in. If you select this, the source code will be
very simple,
but you can't create a command with Undo / Redo. You cannot
make a command
which changes the scene for this restriction (described later).
Changing the scene includes making/moving/deforming/deleting a scene object,
and modifying the
dependency graph.
"MEL command with Undo/Redo"
Make a project
for a command plug-in of any kind. The source code gets a
little bit longer.
"Dependency graph node"
Make a project
for a dependency node plug-in.
If you are making
a shader plug-in, select this.
- Maya type name...
Type "hello".
The name of the new MEL command will become
"hello".
- "Next"
- Maya Plug-in Wizard step 3/3
- Name of plug-in
Leave unchanged(hello.mll).
The name of the plug-in making.
- What libraries would you...
Leave unchanged (Foundation
and openMaya are selected)
This is also an important one. Libraries
have codes to implement there own functions.
For example, you need to use MFnAnimCurve
class to make a key frame. This class is in
"openMayaAnim", so if you don't select
"openMayaAnim" here, you will see a build error.
What library you need is described
in the API manual. When you see a description of
"MRenderUtil" class, you
can see,
Static class providing common API
rendering functions (OpenMayaRender)
Then you know it needs OpenMayRender.
program
A plug-in starts working by registering a new class
defining the behaviour of the plug-in.
In the case of command plug-in, the class is made by deriving "MPxCommand"
class.
"MPxCommand" class is prepared in the API.
Look at the classes made by the AppWizard in the File view.
- helloCmd.cpp
This is a file you write a plug-in
code mainly.
helloCmd.cpp and helloCmd.h defines
a class "hello" that defines the
plug-in's behaviour.
- helloCmd.h
It's header file. You can see that
the class "hello" is derived from "MPxCommand".
This file also may be modified.
- pluginMain.cpp
This has the trick to register the
plug-in to Maya.
This file is not often modified, but
when you change the name of the new MEL command
or Maya version
the plug-in works under, you need to edit this.
Look at the file "helloCmd.h", Class "hello" has the following methods.
- hello();
- ~hello();
- doIt( const MArgList& );
- redoIt();
- undoIt();
- isUndoable() const;
- void* creator();
Important ones among these are,
- doIt( const MArgList& );
- redoIt();
- undoIt();
( creator() method is very important to make this class recognized as
a plug-in,
but so far we will not go into detail.)
They are,
- doIt( const MArgList& );
A method that is executed when the
new MEL command is executed.
MArgList passes a parameter(s) if
any. For example, if you make a command plug-in
"makeSpheres" that makes a number
of spheres, you will type the command
like
makeSpheres 100;
MArgList passes the parameter 100
to your plug-in code. See
Arguments of
a command plug-in for details
- redoIt();
A method that is
executed when you redo the command
- undoIt();
A method that is
executed when you undo the command
This time the only thing the plug-in does is to display a string to
the Output Window,
so we need to edit only doIt().
Edit the doIt() method like this.
---------------------------------------------------------
MStatus hello::doIt( const MArgList& ){
std::cout << "Hello!
Hello!"; //C++ way of printing out strings
std::cout.flush();
MStatus stat = MS::kSuccess;
return stat;
}
---------------------------------------------------------
To use std::cout, add
#include <iostream>
just before #include <maya/MGlobal.h> line
in the beginning of helloCmd.cpp.
Build it by pressing F7 key.
Load the "helloCmd.mll" newly created, and type "hello" in the
command line,
and look at the Output Window. You will see "Hello! Hello!" now.
supplement
1:
When you output a string to the standard output, the output
string will be displayed
in the Output Window. But it will not be displayed for buffering.
To make it displayed
immediately, write
std::cout.flush();
after the
std::cout << ***
line.
2:
In the last part of hello::doIt() method, it returns MS::kSuccess
to Maya.
MStatus stat = MS::kSuccess;
return stat;
"MS::kSuccess" is a value that means "method successfully finished".
If the method fails, doIt() method must return a value other than
"MS::kSuccess"
(such as MS::kFailure�AMS::kInsufficientMemory).
Prev Contents Next
Maya is a registered
trademark of Autodesk
Copyright ©
2003, Koichi Tamura. All Rights Reserved.