#include
<MNamespace.h>
Overview
The MNamespace class contains methods for efficiently accessing Maya's namespace functionality. In Maya, namespaces form a packaging scheme for defining collections of objects. You can also have a namespace contain other namespaces and thus define a hierarchy. In Maya, namespaces are used by default with file referencing to collect all the objects from the given referenced file. Namespaces are also used for naming UI elements, and of course the user can create namespaces on their own to collect objects together in a logical fashion.
Here are some important definitions:
o Root Namespace: The unnamed namespace at the root of the namespace hierarchy.
o Current Namespace: The currently selected namespace. On startup and after file open and new, Maya assigns the root namespace to be current.
o Absolute Name: An object name that is fully specified from the root downwards (i.e. starts with a leading colon).
o Relative Name: An object name that is relative to the current namespace. Can be enabled via MNamespace::setRelativeNames().
o Root-Relative Name: When relative name lookup is turned off, virtually all name lookups in Maya are relative to the root of the namespace hierarchy.
About naming rules
When specifying the name of an object in a namespace, the namespace name should prefix the name, separated by a colon character (":"). There is a special namespace, the root namespace, which lives at the root of the namespace tree. In effect, the name of the root namespace is the empty string. For example, if we have the object "sphere" and it exists in the root namespace, we could use ":sphere" to identify it.
Since namespaces can be hierarchical, when completely specifying a name one could have several colons and namespaces names in the pathname. If we have the namespace ":a:b:c" that contains the object "sphere", the full namepath for the object would be thus:
:a:b:c:sphere
The leading colon specifies that the namespace lookup occur from the root-level downwards. In effect, the namespace "a" is a child of the root namespace, and "b" is a child of "a", and "c" a child of "b".
The current namespace
Maya has the concept of a "current" namespace. On startup, this defaults to the root namespace. Some operations within Maya are based off the current namespace. For example, if the 'parent' parameter is not specified to MNamespace::addNamespace() then the newly created namespace will be a child of the current namespace.
Relative name lookup
Maya actually has the concept of name lookup based off the current namespace as opposed to lookup based off the root-level namespace which is the default behaviour. In relative mode, if we had the object ":a:b:c:sphere" and the current namespace were ":a:b", we would refer to the sphere as "c:sphere". Similarly, by setting the current namespace to be ":a:b:c" we could then refer directly to sphere in an operation such as a "setAttr", e.g.
setAttr sphere.translateX 10;
If relative mode is off, names are root-relative, thus we would need to refer to the sphere as follows. Note that when relative mode is off, Maya assumes the root namespace to base name lookups on:
setAttr a:b:c:sphere.translateX 10;
Also, we can always specify an absolute namepath (one that starts with a leading colon). An absolute namepath will work with relative name lookup enabled or disabled and is thus an easy way of referring to objects in a way that is name lookup-independent.
The relative mode feature is designed to allow users to write namespace-independent code which is intended to operate upon whatever namespace is current. The method MNamespace::currentNamespace() allows you to query the name of the current namespace.
Relative mode can ge toggled using either MNamespace::setRelativeNames() or the "namespace" command. Turning relative mode "off" causes name lookup to be based off the root namespace. Continuing our example where the current namespace is ":a:b" to refer to the sphere we would now need to use:
setAttr a:b:c:sphere.translateX 10;
Using namespaces with the MNamespace class
Maya provides the "namespace" and "namespaceInfo" commands for dealing with namespaces from the command level, and there are also UI features which control and edit namespaces. All of these work harmoniously with the MNamespace class.
It is important that any user code which exercises MNamespace methods that modify (rather than query) handle undo. For example, if writing a plug-in command that creates a new namespace, the plug-in needs to handle removal of the namespace on undo.
The MNamespace class contains methods to create and remove namespaces, set and query the current namespace, and edit and query the contents of namespaces.
To add individual objects to a namespace, use MSelectionList::add() which is well suited for handling multiple objects having the same name as well as hierarchical namespaces.
Static Public Member Functions |
|
static MStatus | addNamespace (const MString &name, const MString *parent=NULL) |
static MString | currentNamespace (MStatus *ReturnStatus=NULL) |
static MStatus | setCurrentNamespace (const MString &name) |
static MStringArray | getNamespaces (const bool recurse=false, MStatus *ReturnStatus=NULL) |
static MStringArray | getNamespaces (const MString &parentNamespace, const bool recurse=false, MStatus *ReturnStatus=NULL) |
static bool | namespaceExists (const MString &name, MStatus *ReturnStatus=NULL) |
static MString | parentNamespace (MStatus *ReturnStatus=NULL) |
static MStatus | removeNamespace (const MString &name) |
static MStatus | renameNamespace (const MString &oldName, const MString &newName, const MString *parent=NULL) |
static MObjectArray | getNamespaceObjects (const MString &parentNamespace, const bool recurse=false, MStatus *ReturnStatus=NULL) |
static MStatus | moveNamespace (const MString &src, const MString &dst, const bool force=false) |
static MString | rootNamespace (MStatus *ReturnStatus=NULL) |
static bool | relativeNames (MStatus *ReturnStatus=NULL) |
static MStatus | setRelativeNames (bool newState) |
static MString | makeNamepathAbsolute (const MString &, MStatus *ReturnStatus=NULL) |
Create the namespace `name'. If the `parent' namespace is given the new namespace will be a child of `parent', otherwise the new namespace will be a child of the current namespace. The new namespace is added, but not made current. To make the new namespace be current use MNamespace::setCurrentNamespace(). Note that adding a namespace changes the scene, so any code that calls this method needs to handle undo.
[in] | name | the name of the new namespace to create. This must be an unqualified name (e.g. "c" rather than "a:b:c"): use the `parent' option to specify the parent namespace instead. |
[in] | parent | the fully qualified name of the namespace to be the parent of the new namespace. If not specified, the current namespace will be the parent. |
Get the name of the current namespace. This name is returned as an absolute namepath (i.e. fully qualfied from the root namespace downwards, ":a:b:c").
[out] | ReturnStatus | optionally returns the status code. |
Set the specified namespace to be the current namespace. The `name' parameter you specify is relative to whatever namespace is current, but any namespace can be specified by passing an absolute name (e.g. :a:b:c). Note that making a namespace current changes the scene, so any code that calls this method needs to handle undo.
To make the root namespace become current, use:
status=MNamespacesetCurrentNamespace(MNamespace::rootNamespace());
[in] | name | the name of the namespace to make current. |
MStringArray MNamespace::getNamespaces | ( | const bool | recurse = false , |
|
MStatus * | ReturnStatus =
NULL |
|||
) | [static] |
Return a list of all namespaces in the current namespace.
Notes: 1) Names returned are always absolute (e.g. :a:b:sphere). 2) The list returned is just the child namespaces (and descendents if `recurse' is true). It thus never contains the root namespace in the list returned.
[in] | recurse | optional parameter to control whether all namespaces or just top-level namespaces are returned. A value of false (the default if unspecified) causes only the top-level namespaces to be returned. If true, all namespaces will be listed. |
[out] | ReturnStatus | optionally returns the status code. |
MStringArray MNamespace::getNamespaces | ( | const MString & | parentNamespace, | |
const bool | recurse = false , |
|||
MStatus * | ReturnStatus =
NULL |
|||
) | [static] |
Return a list of all child namespaces of `parentNamespace', with the option to list only direct children versus all descendents. To list ALL namespaces in Maya, from the root downwards, specify the root namespace, i.e.:
MStringArray namespaces; namespaces = MNamepsace::getNamespaces( MNamespace::rootNamespace() );
Note: names returned are always absolute (e.g. :a:b:sphere).
[in] | parentNamespace | the namespace to query. |
[in] | recurse | optional parameter to control whether all descendents or just the direct children are returned. A value of false (the default if unspecified) causes only the direct children of `parentNamespace' to be listed. A value of true causes all descendent namespaces to be returned. |
[out] | ReturnStatus | optionally returns the status code. |
Check if a given namespace exists.
[in] | name | the name of the namespace to search for. |
[out] | ReturnStatus | optionally returns the status code. |
Get the name of the current namespace's parent. This name is returned as an absolute namepath (i.e. fully qualfied from the root namespace downwards, ":a:b"). If the root namespace is current, this method returns an error.
[out] | ReturnStatus | optionally returns the status code. |
Remove the specified namespace. Note that removing a namespace changes the scene, so any code that calls this method needs to handle undo.
[in] | name | specifies the namespace to remove. |
MStatus MNamespace::renameNamespace | ( | const MString & | oldName, | |
const MString & | newName, | |||
const MString * | parent = NULL |
|||
) | [static] |
Rename the specified namespace to a new name with optional parent name. Note that removing a namespace changes the scene, so any code that calls this method needs to handle undo.
[in] | oldName | the name of the namespace to rename. |
[in] | newName | the new name for the namespace. |
[in] | parent | optionally provides the name of the new parent. |
MObjectArray MNamespace::getNamespaceObjects | ( | const MString & | parentNamespace, | |
const bool | recurse = false , |
|||
MStatus * | ReturnStatus =
NULL |
|||
) | [static] |
Return an array of MObjects representing the object contained within the specified namespace. To query the current namespace, call this method thusly:
MObjectArray objs; objs = MNamespace::getNamespaceObjects( MNamespace::currentNamespace() )
[in] | parentNamespace | namepath of namespace to query. |
[in] | recurse | optional parameter: false (the default if unspecified) causes only the direct children of `parentNamespace' to be listed. true causes all descendents to be listed as well. |
[out] | ReturnStatus | optionally returns the status code. |
MStatus MNamespace::moveNamespace | ( | const MString & | src, | |
const MString & | dst, | |||
const bool | force = false |
|||
) | [static] |
Move the contents of the namespace `src' into the namespace `dst'. Note that moving namespace contents changes the scene, so any code that calls this method needs to handle undo.
[in] | src | source namespace from which objects will be moved. |
[in] | dst | destination namespace to which objects will be moved. |
[in] | force | optional parameter which if true forces the move even if name clashes occur, in which case nodes are renamed to ensure uniqueness. If false, the move will not happen if there are clashes. The default value is false. |
Get the name of the root namespace. This name is an absolute namepath (i.e. prefixed by a ":"). An example use of this method would be to set the current namespace to the root namespace, and this could be done as follows:
status=MNamespacesetCurrentNamespace(MNamespace::rootNamespace());
Another example would be to test if the current namespace is the root, e.g.
if ( MNamespace::currentNamespace() == MNamespace::rootNamespace() ) {
[out] | ReturnStatus | optionally returns the status code from the operation. |
bool MNamespace::relativeNames | ( | MStatus * | ReturnStatus =
NULL |
) | [static] |
Query Maya's current "relative name lookup" state. Relative name lookup causes lookups to be relative to the current namespace. By default, relative name lookup in Maya is off, which causes name lookups to be relative to the root namespace. For example, if you have the object :a:b:sphere, and the current namespace is ":a:b", in relative name lookup mode you can issue a command like
setAttr sphere.translateX 10;
If relative name lookup is off, you need to specify the full namepath, e.g.
setAttr a:b:sphere.translateX 10;
[out] | ReturnStatus | returns the status of the query. |
MStatus MNamespace::setRelativeNames | ( | bool | newState | ) | [static] |
Set relative name lookup mode. Note that turning on or off relativeNames mode can change the scene, so any code that calls this method needs to handle undo. See MNamespace::relativeNames() for details on relative name lookup.
Note: relative name lookup mode is intended for bracketing user code which needs to be namespace-independent. Leaving relative name lookup enabled outside of your specific code could cause functionality such as 3rd-party plugins that assume absolute name lookup to fail.
[in] | newState | true to turn on relative name lookup, false to turn it off. Maya's default setting is false. |
MString MNamespace::makeNamepathAbsolute | ( | const MString & | namepath, | |
MStatus * | ReturnStatus =
NULL |
|||
) | [static] |
Make a namepath which is relative to the root into an absolute namepath. For example, given the namepath "a:sphere" this method returns ":a:sphere". It also culls out duplicate and trailing separators, e.g. "a:b::c:" will return ":a:b:c".
NOTE: there is no testing to ensure that the name actually refers to an object that exists.
[in] | namepath | the root-relative namepath. |
[out] | ReturnStatus | returns the status of the operation. |
Autodesk® Maya® 2011 © 1997-2010 Autodesk, Inc. All rights reserved. | Generated with 1.5.6 |