Skip to content

Commit

Permalink
Added ability to compute 'object type' of a node. Issue #72.
Browse files Browse the repository at this point in the history
Added 'getAsDagPath' utility.
  • Loading branch information
david-cattermole committed Sep 5, 2019
1 parent 823c816 commit 425deb3
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion src/mayaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#ifndef MAYA_UTILS_H
#define MAYA_UTILS_H

#include <vector>

// Utils
#include <utilities/debugUtils.h>

Expand All @@ -33,11 +35,22 @@
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MObject.h>
#include <maya/MDagPath.h>
#include <maya/MPlug.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>


#define OBJECT_TYPE_UNKNOWN (0)
#define OBJECT_TYPE_ATTRIBUTE (1)
#define OBJECT_TYPE_MARKER (2)
#define OBJECT_TYPE_BUNDLE (3)
#define OBJECT_TYPE_CAMERA (4)
#define OBJECT_TYPE_IMAGE_PLANE (5)
#define OBJECT_TYPE_MARKER_GROUP (6)
#define OBJECT_TYPE_COLLECTION (7)


static inline
MStatus getAsSelectionList(MStringArray nodeNames, MSelectionList &selList) {
MStatus status;
Expand Down Expand Up @@ -92,11 +105,91 @@ MStatus getAsObject(MString nodeName, MObject &object) {
MStatus status;
MSelectionList selList;
status = getAsSelectionList(nodeName, selList);
CHECK_MSTATUS(status);
if (selList.length() == 1) {
status = selList.getDependNode(0, object);
CHECK_MSTATUS(status);
}
return status;
}


static inline
MStatus getAsDagPath(MString nodeName, MDagPath &nodeDagPath) {
MStatus status;
MSelectionList selList;
status = getAsSelectionList(nodeName, selList);
CHECK_MSTATUS(status);
if (selList.length() == 1) {
selList.getDependNode(0, object);
status = selList.getDagPath(0, nodeDagPath);
CHECK_MSTATUS(status);
}
return status;
}


inline
bool hasAttrName(MFnDependencyNode &dependFn, MString attrName) {
MPlug plug = dependFn.findPlug(attrName, true);
return plug.isNull();
}


inline
unsigned int computeObjectType(MObject node_obj, MDagPath nodeDagPath) {
bool hasLocatorShape = false;
bool hasCameraShape = false;
bool hasImagePlaneShape = false;
MFn::Type node_tid = nodeDagPath.apiType();
std::vector<MFn::Type> shape_tids;
unsigned int num_children = nodeDagPath.childCount();
for (unsigned int i = 0; i < num_children; ++i) {
MObject child_obj = nodeDagPath.child(i);
nodeDagPath.push(child_obj);
MFn::Type shape_tid = nodeDagPath.apiType();
shape_tids.push_back(shape_tid);
if (shape_tid == MFn::kLocator) {
hasLocatorShape = true;
} else if (shape_tid == MFn::kCamera) {
hasCameraShape = true;
} else if (shape_tid == MFn::kImagePlane) {
hasImagePlaneShape = true;
}
}

MFnDependencyNode dependFn(node_obj);
unsigned int objectType = OBJECT_TYPE_UNKNOWN;
bool hasAttrEnable = hasAttrName(dependFn, MString("enable"));
bool hasAttrWeight = hasAttrName(dependFn, MString("weight"));
bool hasAttrBundle = hasAttrName(dependFn, MString("bundle"));
bool hasAttrSolverList = hasAttrName(dependFn, "solver_list");
if (node_tid == MFn::kTransform
&& hasLocatorShape
&& hasAttrEnable
&& hasAttrWeight
&& hasAttrBundle) {
objectType = OBJECT_TYPE_MARKER;
} else if (node_tid == MFn::kTransform
&& hasLocatorShape) {
objectType = OBJECT_TYPE_BUNDLE;
} else if (node_tid == MFn::kTransform
&& hasCameraShape) {
objectType = OBJECT_TYPE_CAMERA;
} else if (node_tid == MFn::kCamera) {
objectType = OBJECT_TYPE_CAMERA;
} else if (node_tid == MFn::kTransform
&& hasImagePlaneShape) {
objectType = OBJECT_TYPE_IMAGE_PLANE;
} else if (hasImagePlaneShape) {
objectType = OBJECT_TYPE_IMAGE_PLANE;
} else if (node_tid == MFn::kPluginDependNode) {
// TODO: Check specifically for 'mmMarkerGroupTransform' node type.
objectType = OBJECT_TYPE_MARKER_GROUP;
} else if (node_tid == MFn::kSet && hasAttrSolverList) {
objectType = OBJECT_TYPE_COLLECTION;
}
return objectType;
}


#endif // MAYA_UTILS_H

0 comments on commit 425deb3

Please sign in to comment.