Skip to content

Instantly share code, notes, and snippets.

@serguei-k
Last active August 13, 2017 00:04
Show Gist options
  • Save serguei-k/603a0e38de04e8fde12239fdbc68da97 to your computer and use it in GitHub Desktop.
Save serguei-k/603a0e38de04e8fde12239fdbc68da97 to your computer and use it in GitHub Desktop.
// ExpressionControlCommand.h
#pragma once
#include <maya/MPxCommand.h>
class ExpressionControlCommand : public MPxCommand
{
public:
MStatus doIt(const MArgList& argList) override;
bool isUndoable() const override;
static void* create();
static MSyntax createSyntax();
};
// ExpressionControlCommand.cc
#include "ExpressionControlCommand.h"
#include <maya/MArgDatabase.h>
#include <maya/MSyntax.h>
// Include our custom widget
#include "ExpressionLineEdit.h"
#define kAttributeFlag "-a"
#define kAttributeFlagLong "-attribute"
#define kLabelFlag "-l"
#define kLabelFlagLong "-label"
MStatus ExpressionControlCommand::doIt(const MArgList& argList)
{
MStatus status;
MArgDatabase argDb(syntax(), argList);
MString name;
status = argDb.getCommandArgument(0, name);
if(!status)
{
displayError("Control name is required!");
return MS::kFailure;
}
MString nodeAttribute;
if(argDb.isFlagSet(kAttributeFlag))
{
argDb.getFlagArgument(kAttributeFlag, 0, nodeAttribute);
}
else
{
displayError("A valid attribute needs to be specified!");
return MS::kFailure;
}
MStringArray nodeAttributePair;
nodeAttribute.split('.', nodeAttributePair);
MSelectionList selList;
selList.add(nodeAttributePair[0]);
MObject node;
selList.getDependNode(0, node);
MFnDependencyNode nodeFn(node);
MPlug attribute = nodeFn.findPlug(nodeAttributePair[1]);
if(argDb.isEdit())
{
QWidget* widget = MQtUtil::findControl(name);
if(!widget)
{
displayError("Unable to find an existing control with a given name!");
return MS::kFailure;
}
// Update control to use the current attribute
ExpressionLineEdit* control = static_cast<ExpressionLineEdit*>(widget);
control->setAttribute(attribute);
return MS::kSuccess;
}
QWidget* layout = MQtUtil::getCurrentParent();
if(!layout)
{
displayError("Control must be created inside a valid layout!");
return MS::kFailure;
}
MString label;
if(argDb.isFlagSet(kLabelFlag))
{
argDb.getFlagArgument(kLabelFlag, 0, label);
}
else
{
label = attribute.partialName();
}
ExpressionLineEdit* control = new ExpressionLineEdit(attribute, MQtUtil::toQString(label));
control->setObjectName(MQtUtil::toQString(name));
MQtUtil::addWidgetToMayaLayout(control, layout);
return MS::kSuccess;
}
bool ExpressionControlCommand::isUndoable() const
{
return false;
}
void* ExpressionControlCommand::create()
{
return new CreateExpressionControlCommand();
}
MSyntax ExpressionControlCommand::createSyntax()
{
MSyntax syntax;
syntax.enableEdit();
syntax.addFlag(kAttributeFlag, kAttributeFlagLong, MSyntax::kString);
syntax.addFlag(kLabelFlag, kLabelFlagLong, MSyntax::kString);
syntax.addArg(MSyntax::kString);
return syntax;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment