Repository navigation
Expand file tree
/
Copy pathAutoRenameCmd.cpp
More file actions
67 lines (57 loc) · 1.86 KB
/
Copy pathAutoRenameCmd.cpp
File metadata and controls
67 lines (57 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <maya/MPxCommand.h>
#include <maya/MArgList.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MObject.h>
#include <maya/MFnDagNode.h>
#include <maya/MStatus.h>
class AutoRenameCmd : public MPxCommand {
public:
AutoRenameCmd() {}
virtual MStatus doIt(const MArgList& args) override;
static void* creator() { return new AutoRenameCmd(); }
};
// Command Execution
MStatus AutoRenameCmd::doIt(const MArgList& args) {
MStatus status;
// Get the prefix from arguments or set a default
MString prefix = "object";
if (args.length() > 0) {
prefix = args.asString(0, &status);
if (!status) {
MGlobal::displayError("Invalid prefix argument.");
return status;
}
}
// Get the current selection
MSelectionList selection;
MGlobal::getActiveSelectionList(selection);
if (selection.isEmpty()) {
MGlobal::displayWarning("No objects selected!");
return MS::kFailure;
}
// Iterate through selected objects and rename them
for (unsigned int i = 0; i < selection.length(); ++i) {
MObject obj;
selection.getDependNode(i, obj);
if (!obj.isNull()) {
MFnDagNode dagNode(obj, &status);
if (status == MS::kSuccess) {
MString newName = prefix + "_" + MString(std::to_string(i + 1).c_str());
dagNode.setName(newName);
}
}
}
MGlobal::displayInfo("Objects renamed successfully.");
return MS::kSuccess;
}
// Plugin Registration
#include <maya/MFnPlugin.h>
MStatus initializePlugin(MObject obj) {
MFnPlugin plugin(obj, "YourName", "1.0", "Any");
return plugin.registerCommand("autoRename", AutoRenameCmd::creator);
}
MStatus uninitializePlugin(MObject obj) {
MFnPlugin plugin(obj);
return plugin.deregisterCommand("autoRename");
}