
hohehohe2's OpenMaya tutorial
kotamuraa@yahoo.co.jp
7. Handling data
In this chapter I will explain how you can
handle lots of data Maya has.
This is a very important part.
A sample program is shown in the next chapter.
Function sets
A plug-in can play its role by accessing to Maya's various
internal data
(proper Maya API manual term is "Maya object").
(The following part is a bit difficult to explain, if it difficult
to understand, see
"how to access Maya's internal data" below.)
Maya's internal data is completely hidden and you can't touch
them directly.
It is hidden on purpose so that you can't access to them easily
and corrupt data (?).
So You use special classes to access to them indirectly.
API has the classes.
The classes to access to Maya's internal data is called
"function sets", which
class names start with "MFn".
For example MFnNurbsSurface is a class to access to nurbs
surface data,
and MFnAmbientLight is a class to access to ambient light
data.
To use a function set, it needs to know which internal data
to access.
For example, "MFnNurbsSurface" has a method setCV() that changes
CV positions
of a nurbs surface. If there are two nurbs surfaces in the
scene, "MFnNurbsSurface"
class instance must know CV's of which nurbs surface to change.
The next sections descries how to do it.
MObject
Like you can identify each file using a file handle,
FILE* fp1; //File handle
fp1 = fopen("file1.txt",
"w");
......
fprintf(fp1, "hohe");
you can identify each internal data with a handle in your
plug-in code.
This handle is called "MObject".
An instance of MObject class can be a handle of one internal
data.
As you can see in the API manual, MObject can be a handle
of lots
of lots of types of internal data.
You can make a handle of all the following types.
DG node, attribute, deformer, motion path, expression, component,
data flows in the dependency graph, ....
If you can get an MObject of an internal data, you can
tell a function set
(i.e. MFn*** class instance) "what internal data I am going
to access" with
the MObject.
How to access maya's internal data
Previous example.
There are two nurbs surface in the scene. How can I change
values of
one surface CV positions and not the other, using MFnNurbsSurface
class instance.
The process is as follows,
1) Get an MObject of the nurbs surface which you want to change
CV positions.
(assume the name of this MObject class instance
is "objNsurf1")
2) Make a class instance of MFnNurbsSurface (function set)
which can access to a nurbs surface.
MFnNurbsSurface fnSurf;
3) Tell the MFnNurbsSurface instance which nurbs surface you
are dealing with.
fnSurf.setObject(objNsurf1);
4) Change CV positions.
fnSurf.setCV(...);
(Note) You can do 2) and 3) at the same time by
MFnNurbsSurface fnSurf(objNsurf1);
The remaining problem is, how to 1) Get an MObject of the nurbs
surface which
you want to change CV positions.
API has a lot of methods to ask Maya,
"Give me (an) MObject(s) of (an) internal
data that satisfies this condition"
So you can ask Maya
"Give me an MObject of the scene object
(DAG node) which name is ****".
The process is a little bit complicated so I will describe
how to do it later.
(See chapter 9)
There are cases you don't have to use MObject.
For example, MFnNurbsSurface has a method to create a new nurbs
surface in the scene.
MFnNurbsSurface fnSurf;
fnSurf.create(...);
//Parameters are CV position, and so on
When you create a nurbs surface in the scene using fnSurf,
fnSurf is already
connected to the newly created surface. You don't have to use MObject
here.
There are other cases you don't have to use MObject. (Such
as using MDagPath. described later)
It is just a general rule to identify
an internal data using MObject.
(HELP English:Does "general rule" implies there are exceptions?
I mean it.)
Combination of MObject and function set
In the above case I attached an MObject of a nurbs surface
to MFnNurbsSurface
instance with setObject() method. What if you attach
an MObject of
a nurbs surface (objNsurf1) to MFnNurbsCurve
function set?
MFnNurbsCurve fnCurve;
fnCurve.setObject(objNsurf1);
Nothing happens. setObject() Just returns an error.
When you attach an MObject to a function set, there are types
of function sets you
can attach and you cannot attach. Manual "Node and attribute
reference" tells
what function sets you can attach to an MObject of a dependency
node.
I cannot find the documentation of this combination except
dependency nodes MObjects,
but it's not very difficult to know it. (Somehow you can know
it as you code a plug-in.)
Depending on what internal data an MObject represents, it may
be attached to two or more
function set types.
For example an MObject for an ambientLight can be attached
to both MFnLight and MFnAmbientLight.
MFnAmbientLight is derived from MFnLight,
class MFnAmbientLight : public MFnLight {
...
};
So if you attach MFnAmbientLight to it, you can use all MFnLight
methods.
Contrary, if you attach MFnLight to
it, you cannot use MFnAmbientLight specific
methods.
For example, MFnAmbientLight::setIntensity() is a method
from MFnLight class, but
MFnAmbientLight::setAmbientShade()
is not. So,
MFnAmbientLight fnAmbLight();
MFnLight fnlight();
fnAmbLight.setObject(objLight);
//OK
fnlight.setObject(objLight);
//OK
fnAmbLight.setIntensity(0.1)
//OK
fnlight.setIntensity(0.1);
//OK These two are the same.
fnAmbLight.setAmbientShade(0.1)
//OK
fnlight.setAmbientShade(0.1);
//Error. No such method.
Here, objLight is an MObject for an ambient light node.
MFnLight function set is
good to use when
- You know an MObject is
a light, but don't know what type of light it is.
- You know an MObject is an ambient light, but only MFnlight
methods are needed here.
Another light type MObject may be used in the future.
supplement(important!)
Once the execution of the plug-in finish and go back
to Maya,
You don't know what is attached to MObject when the execution
comes back to the plug-in again. You cannot use the MObject
without
initializing again. It's the same with a function set.
The same thing happens when you change current time in
the plug-in.
You should reinitialize all MObjects and function sets. (?)
(On the contrary you don't have to reinitialize MDagPath. it is
described later.)
Prev Contents Next
Maya is a registered
trademark of Autodesk
Copyright ©
2003, Koichi Tamura. All Rights Reserved.