-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenericAttributeNode.cpp
More file actions
238 lines (197 loc) · 7.15 KB
/
Copy pathgenericAttributeNode.cpp
File metadata and controls
238 lines (197 loc) · 7.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//-
// ==========================================================================
// Copyright 1995,2006,2008 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk
// license agreement provided at the time of installation or download,
// or which otherwise accompanies this software in either electronic
// or hard copy form.
// ==========================================================================
//+
//
// DESCRIPTION:
//
// Produces the dependency node "genericAttributeNode".
//
// This plug-in demonstrates how to create a MPxNode that contains generic attributes.
// Several generic attributes are added to the node and the compute() method sets its result
// based on a controlling type attribute.
//
#include <string.h>
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MPxNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnGenericAttribute.h>
#include <maya/MFnPlugin.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>
//
// Node definition
//
class genericAttributeNode : public MPxNode
{
public:
genericAttributeNode();
~genericAttributeNode() override;
MStatus compute( const MPlug& plug, MDataBlock& data ) override;
static void* creator();
static MStatus initialize();
static MStatus addComplexFloatGenericAttribute( MObject& attrObject, const char *longName, const char *shortName );
public:
// Numeric attribute
static MObject gInputInt;
// Generic attribute that can be a float, 2float, 3float
static MObject gOutputFloat_2Float_3Float;
// Type id
static MTypeId id;
};
// Static initialization
MTypeId genericAttributeNode::id( 0x81031 );
MObject genericAttributeNode::gInputInt;
MObject genericAttributeNode::gOutputFloat_2Float_3Float;
// No-op constructor and destructor
genericAttributeNode::genericAttributeNode() {}
genericAttributeNode::~genericAttributeNode() {}
//
// Compute that will update the generic attribute
// when requested.
//
MStatus genericAttributeNode::compute( const MPlug& plug, MDataBlock& data )
{
MStatus returnStatus;
if ( plug == gOutputFloat_2Float_3Float )
{
// attribute affecting generic attribute case. Based on the
// input attribute, we modify the output generic attribute
MDataHandle inputDataInt = data.inputValue( gInputInt );
int inputInt = inputDataInt.asInt();
// Get the output handle
MDataHandle outputData = data.outputValue( plug );
bool isGenericNumeric = false;
bool isGenericNull = false;
// Is the output handle generic data
if ( outputData.isGeneric( isGenericNumeric, isGenericNull ) )
{
// Based on the inputHandle, update the generic
// output handle
if ( inputInt == 1 )
outputData.setGenericBool( false, true );
else if ( inputInt == 2 )
outputData.setGenericBool( true, true );
else if ( inputInt == 3 )
outputData.setGenericChar( 127, true );
else if ( inputInt == 4 )
outputData.setGenericDouble( 3.145, true );
else if ( inputInt == 5 )
outputData.setGenericFloat( (float)9.98, true );
else if ( inputInt == 6 )
outputData.setGenericShort( 3245, true );
else if ( inputInt == 7 )
outputData.setGenericInt( 32768, true );
else if ( inputInt == 8 )
{
MFnNumericData numericData;
MObject obj = numericData.create( MFnNumericData::k2Float, &returnStatus );
CHECK_MSTATUS( returnStatus );
returnStatus = numericData.setData( (float)1.5, (float)6.7 );
CHECK_MSTATUS( returnStatus );
outputData.set( obj );
}
else if ( inputInt == 9 )
{
MFnNumericData numericData;
MObject obj = numericData.create( MFnNumericData::k3Float, &returnStatus );
CHECK_MSTATUS( returnStatus );
returnStatus = numericData.setData( (float)2.5, (float)8.7, (float)2.3345 );
CHECK_MSTATUS( returnStatus );
outputData.set( obj );
}
else if ( inputInt == 10 )
{
outputData.setGenericInt( 909, true );
}
// Mark the data clean
outputData.setClean();
data.setClean( gOutputFloat_2Float_3Float );
}
}
else
{
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
// Creator
void* genericAttributeNode::creator()
{
return new genericAttributeNode();
}
// Adds a generic attribute that accepts a float, float2, float3
MStatus genericAttributeNode::addComplexFloatGenericAttribute(
MObject& attrObject,
const char *longName, const char *shortName )
{
// Create the generic attribute and set the 3 accepts types
MFnGenericAttribute gAttr;
attrObject = gAttr.create( longName, shortName );
MStatus status = gAttr.addNumericDataAccept(MFnNumericData::kFloat);
CHECK_MSTATUS( status );
status = gAttr.addNumericDataAccept(MFnNumericData::k2Float);
CHECK_MSTATUS( status );
status = gAttr.addNumericDataAccept(MFnNumericData::k3Float);
CHECK_MSTATUS( status );
gAttr.setWritable(false);
gAttr.setStorable( false );
// Add the attribute to the node
status = addAttribute( attrObject );
CHECK_MSTATUS( status );
return MS::kSuccess;
}
// Create and add attributes
MStatus genericAttributeNode::initialize()
{
MFnNumericAttribute nAttr;
MStatus status;
// single float attribute affecting a generic attribute
gInputInt = nAttr.create( "gInputInt", "gii",
MFnNumericData::kInt, 0, &status );
nAttr.setStorable(true);
nAttr.setKeyable(true);
CHECK_MSTATUS( status );
status = addAttribute( gInputInt );
CHECK_MSTATUS( status );
if ( !addComplexFloatGenericAttribute( gOutputFloat_2Float_3Float,
"gOutputFloat_2Float_3Float", "gof2f3f" ) )
return MS::kFailure;
status = attributeAffects( gInputInt, gOutputFloat_2Float_3Float );
CHECK_MSTATUS( status );
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "7.0", "Any");
status = plugin.registerNode( "genericAttributeNode",
genericAttributeNode::id, genericAttributeNode::creator,
genericAttributeNode::initialize );
if (!status) {
status.perror("registerNode");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterNode( genericAttributeNode::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return status;
}