forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalveMaya.h
672 lines (540 loc) · 19.3 KB
/
valveMaya.h
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//======= Copyright © 1996-2006, Valve Corporation, All rights reserved. ======
//
// Purpose:
//
//=============================================================================
#ifndef VALVEMAYA_H
#define VALVEMAYA_H
#if defined( _WIN32 )
#pragma once
#endif
// Maya Includes
#include <maya/MAngle.h>
#include <maya/MDagModifier.h>
#include <maya/MEulerRotation.h>
#include <maya/MFloatVector.h>
#include <maya/MGlobal.h>
#include <maya/MIOStream.h>
#include <maya/MMatrix.h>
#include <maya/MObject.h>
#include <maya/MQuaternion.h>
#include <maya/MSyntax.h>
#include <maya/MString.h>
#include <maya/MDagPath.h>
#include <maya/MVector.h>
// Valve Includes
#include "tier1/stringpool.h"
#include "tier1/utlstring.h"
#include "tier1/utlstringmap.h"
#include "tier1/utlvector.h"
#include "tier1/interface.h"
#include "mathlib/mathlib.h"
#include "ValveMaya/Undo.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class IMayaVGui;
//-----------------------------------------------------------------------------
// Maya-specific library singletons
//-----------------------------------------------------------------------------
extern IMayaVGui *g_pMayaVGui;
//-----------------------------------------------------------------------------
//
// Purpose: Group a bunch of functions into the Valve Maya Namespace
//
//-----------------------------------------------------------------------------
namespace ValveMaya
{
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CUndo;
//-----------------------------------------------------------------------------
// Statics
//-----------------------------------------------------------------------------
extern const MQuaternion v2mQuat; // Valve Engine -> Maya Quaternion
extern const MQuaternion m2vQuat; // Maya -> Valve Engine Quaternion
extern const MMatrix v2mMat; // Valve Engine -> Maya Matrix
extern const MMatrix m2vMat; // Maya -> Valve Engine Matrix
extern const MQuaternion vc2mcQuat; // Valve Engine Camera -> Maya Camera Quaternion
extern const MQuaternion mc2vcQuat; // Maya Camera -> Valve Camera Engine Quaternion
extern const MMatrix vc2mcMat; // Valve Engine Camera -> Maya Camera Quaternion
extern const MMatrix mc2vcMat; // Maya Camera -> Valve Camera Engine Quaternion
inline MQuaternion ValveToMaya( const MQuaternion &q ) { return q * v2mQuat; }
inline MQuaternion ValveCameraToMayaCamera( const MQuaternion &q ) { return vc2mcQuat * q * v2mQuat; }
inline MQuaternion MayaToValve( const MQuaternion &q ) { return q * m2vQuat; }
inline MQuaternion MayaCameraToValveCamera( const MQuaternion &q ) { return mc2vcQuat * q * m2vQuat; }
inline MVector ValveToMaya( const MVector &p ) { return p.rotateBy( v2mQuat ); }
inline MVector ValveCameraToMayaCamera( const MVector &p ) { return p.rotateBy( v2mQuat ); }
inline MVector MayaToValve( const MVector &p ) { return p.rotateBy( m2vQuat ); }
inline MVector MayaCameraToValveCamera( const MVector &p ) { return p.rotateBy( m2vQuat ); }
//-----------------------------------------------------------------------------
// Connect, disconnect
//-----------------------------------------------------------------------------
bool ConnectLibraries( CreateInterfaceFn factory );
void DisconnectLibraries();
MStatus CreateDagNode(
const char *const i_nodeType,
const char *const i_transformName = NULL,
const MObject &i_parentObj = MObject::kNullObj,
MObject *o_pTransformObj = NULL,
MObject *o_pShapeObj = NULL,
MDagModifier *i_mDagModifier = NULL);
inline MStatus CreateDagNode(
CUndo &undo,
const char *const i_nodeType,
const char *const i_transformName = NULL,
const MObject &i_parentObj = MObject::kNullObj,
MObject *o_pTransformObj = NULL,
MObject *o_pShapeObj = NULL )
{
return CreateDagNode( i_nodeType, i_transformName, i_parentObj, o_pTransformObj, o_pShapeObj, &undo.DagModifier() );
}
bool IsNodeVisible( const MDagPath &mDagPath, bool bTemplateAsInvisible = true );
bool IsPathVisible( MDagPath mDagPath, bool bTemplateAsInvisible = true );
class CMSyntaxHelp
{
public:
CMSyntaxHelp()
: m_groupedHelp( false ) // Make case sensitive
, m_helpCount( 0 )
, m_shortNameLength( 0 )
{}
void Clear();
MStatus AddFlag(
MSyntax &i_mSyntax,
const char *i_shortName,
const char *i_longName,
const char *i_group,
const char *i_help,
const MSyntax::MArgType i_argType1 = MSyntax::kNoArg,
const MSyntax::MArgType i_argType2 = MSyntax::kNoArg,
const MSyntax::MArgType i_argType3 = MSyntax::kNoArg,
const MSyntax::MArgType i_argType4 = MSyntax::kNoArg,
const MSyntax::MArgType i_argType5 = MSyntax::kNoArg,
const MSyntax::MArgType i_argType6 = MSyntax::kNoArg);
void PrintHelp(
const char *const i_cmdName,
const char *const i_cmdDesc,
int i_lineLength = 0U);
void PrintHelp(
const MString &i_cmdName,
const MString &i_cmdDesc,
int i_lineLength = 0U)
{
PrintHelp( i_cmdName.asChar(), i_cmdDesc.asChar(), i_lineLength );
}
protected:
public:
protected:
CCountedStringPool m_stringPool;
struct HelpData_t
{
const char *m_shortName;
const char *m_longName;
const char *m_help;
CUtlVector<MSyntax::MArgType> m_argTypes;
};
CUtlVector<const char *> m_groupOrder;
CUtlStringMap<CUtlVector<HelpData_t> > m_groupedHelp;
int m_helpCount;
int m_shortNameLength;
};
MString RemoveNameSpace( const MString &mString );
MString &BackslashToSlash( MString &mString );
template < class T_t > bool IsDefault( T_t &, const MPlug & );
bool IsDefault( const MPlug &aPlug );
uint NextAvailable( MPlug &mPlug );
MPlug NextAvailablePlug( MPlug &mPlug );
MObject AddColorSetToMesh(
const MString &colorSetName,
const MDagPath &mDagPath,
MDagModifier &mDagModifier );
MString GetMaterialPath(
const MObject &shadingGroupObj,
MObject *pSurfaceShaderObj,
MObject *pFileObj,
MObject *pPlace2dTextureObj,
MObject *pVmtObj,
bool *pbTransparent,
MString *pDebugWhy );
// Returns the first node that is connected to the specified plug as input or output
MObject FindConnectedNode( const MPlug &mPlug );
// Returns the plug connected to the specified plug as an input, a NULL plug if no plug is connected
MPlug FindInputPlug( const MPlug &dstPlug );
MPlug FindInputPlug( const MObject &dstNodeObj, const MString &dstPlugName );
MObject FindInputNode( const MPlug &dstPlug );
MObject FindInputNode( const MObject &dstNodeObj, const MString &dstPlugName );
MObject FindInputAttr( const MPlug &dstPlug );
MObject FindInputAttr( const MObject &dstNodeObj, const MString &dstPlugName );
// Returns the first found node MObject of the specified type in the history of the specified node
MObject FindInputNodeOfType( const MObject &dstNodeObj, const MString &typeName, const MString &dstPlugName );
MObject FindInputNodeOfType( const MObject &dstNodeObj, const MString &typeName, MSelectionList &doneList );
// Creates a deformer of the specified type and deforms the specified shape with an optional component
MObject DeformShape( ValveMaya::CUndo &undo, const MString &deformerType, const MDagPath &inOrigDagPath, MObject &origCompObj = MObject::kNullObj );
bool Substitute( MString &str, const MString &searchStr, const MString &replaceStr );
bool SubstituteCaseInsensitive( MString &str, const MString &searchStr, const MString &replaceStr );
bool SubstituteAll( MString &str, const MString &searchStr, const MString &replaceStr );
bool SubstituteAllCaseInsensitive( MString &str, const MString &searchStr, const MString &replaceStr );
void FixSlashes( MString &str, char correctPathSeparator = '/' );
//-----------------------------------------------------------------------------
// Converts a Maya MMatrix to a Valve matrix3x4_t (transpose)
//-----------------------------------------------------------------------------
inline void MatrixMayaToValve( matrix3x4_t &mValve, const MMatrix &mMaya )
{
mValve[0][0] = mMaya[0][0]; mValve[0][1] = mMaya[1][0]; mValve[0][2] = mMaya[2][0]; mValve[0][3] = mMaya[3][0];
mValve[1][0] = mMaya[0][1]; mValve[1][1] = mMaya[1][1]; mValve[1][2] = mMaya[2][1]; mValve[1][3] = mMaya[3][1];
mValve[2][0] = mMaya[0][2]; mValve[2][1] = mMaya[1][2]; mValve[2][2] = mMaya[2][2]; mValve[2][3] = mMaya[3][2];
}
//-----------------------------------------------------------------------------
// Converts a Valve matrix3x4_t to a Maya MMatrix (transpose)
//-----------------------------------------------------------------------------
inline void MatrixValveToMaya( MMatrix &mMaya, const matrix3x4_t &mValve )
{
mMaya[0][0] = mValve[0][0]; mMaya[0][1] = mValve[1][0]; mMaya[0][2] = mValve[2][0]; mMaya[3][0] = 0.0;
mMaya[1][0] = mValve[0][1]; mMaya[1][1] = mValve[1][1]; mMaya[1][2] = mValve[2][1]; mMaya[3][1] = 0.0;
mMaya[2][0] = mValve[0][2]; mMaya[2][1] = mValve[1][2]; mMaya[2][2] = mValve[2][2]; mMaya[3][2] = 0.0;
mMaya[3][0] = mValve[0][3]; mMaya[3][1] = mValve[1][3]; mMaya[3][2] = mValve[2][3]; mMaya[3][3] = 1.0;
}
//-----------------------------------------------------------------------------
// Converts a Maya MVector to a Valve Vector
//-----------------------------------------------------------------------------
inline void VectorMayaToValve( Vector &vValve, const MVector &vMaya )
{
vValve.x = vMaya.x;
vValve.y = vMaya.y;
vValve.z = vMaya.z;
}
//-----------------------------------------------------------------------------
// Converts a Valve Vector to a Maya MVector
//-----------------------------------------------------------------------------
inline void VectorValveToMaya( MVector &vMaya, const Vector &vValve )
{
vMaya.x = vValve.x;
vMaya.y = vValve.y;
vMaya.z = vValve.z;
}
//-----------------------------------------------------------------------------
// Converts a Maya MQuaternion to a Valve Quaternion
//-----------------------------------------------------------------------------
inline void QuaternionMayaToValve( Quaternion &qValve, const MQuaternion &qMaya )
{
qValve.x = qMaya.x;
qValve.y = qMaya.y;
qValve.z = qMaya.z;
qValve.w = qMaya.w;
}
//-----------------------------------------------------------------------------
// Converts a Maya MQuaternion to a Valve Quaternion
//-----------------------------------------------------------------------------
inline void QuaternionMayaToValve( Quaternion &qValve, const MEulerRotation &eMaya )
{
const MQuaternion qMaya = eMaya.asQuaternion();
qValve.x = qMaya.x;
qValve.y = qMaya.y;
qValve.z = qMaya.z;
qValve.w = qMaya.w;
}
//-----------------------------------------------------------------------------
// Converts a Valve Quaternion to a Maya MQuaternion
//-----------------------------------------------------------------------------
inline void QuaternionValveToMaya( MQuaternion &qMaya, const Quaternion &qValve )
{
qMaya.x = qValve.x;
qMaya.y = qValve.y;
qMaya.z = qValve.z;
qMaya.w = qValve.w;
}
//-----------------------------------------------------------------------------
// Converts a Valve Quaternion to a Maya MQuaternion
//-----------------------------------------------------------------------------
inline void QuaternionValveToMaya( MEulerRotation &eMaya, const Quaternion &qValve )
{
MQuaternion qMaya;
QuaternionValveToMaya( qMaya, qValve );
const MEulerRotation::RotationOrder roTmp = eMaya.order;
eMaya = qMaya;
if ( eMaya.order != roTmp )
{
eMaya.reorder( roTmp );
}
}
//-----------------------------------------------------------------------------
// Converts a Maya MEulerRotation to a Valve RadianEuler
//-----------------------------------------------------------------------------
inline void RadianEulerMayaToValve( RadianEuler &eValve, const MEulerRotation &eMaya )
{
if ( eMaya.order == MEulerRotation::kXYZ )
{
eValve.x = eMaya.x;
eValve.y = eMaya.y;
eValve.z = eMaya.z;
}
else
{
MEulerRotation eTmp = eMaya;
eTmp.reorder( MEulerRotation::kXYZ );
eValve.x = eTmp.x;
eValve.y = eTmp.y;
eValve.z = eTmp.z;
}
}
//-----------------------------------------------------------------------------
// Converts a Valve RadianEuler to a Maya MEulerRotation
//-----------------------------------------------------------------------------
inline void RadianEulerValveToMaya( MEulerRotation &eMaya, const RadianEuler &eValve )
{
const MEulerRotation::RotationOrder roTmp = eMaya.order;
if ( roTmp == MEulerRotation::kXYZ )
{
eMaya.x = eValve.x;
eMaya.y = eValve.y;
eMaya.z = eValve.z;
}
else
{
eMaya.reorder( MEulerRotation::kXYZ );
eMaya.x = eValve.x;
eMaya.y = eValve.y;
eMaya.z = eValve.z;
eMaya.reorder( roTmp );
}
}
} // end namespace ValveMaya
// Make an alias for the ValveMaya namespace
namespace vm = ValveMaya;
//-----------------------------------------------------------------------------
// A simple stream class for printing information to the Maya script editor
//-----------------------------------------------------------------------------
class CMayaStream
{
public:
enum StreamType { kInfo, kWarning, kError };
CMayaStream( const StreamType i_streamType = kInfo )
: m_streamType( i_streamType )
{}
template < class T_t >
CMayaStream &operator<<( const T_t &v );
// This is a hack so CMayaStream << std::endl works as expected
CMayaStream &operator<<( std::ostream &(*StdEndl_t)( std::ostream & ) )
{
return flush();
}
CMayaStream &flush() { return outputString(); }
protected:
CMayaStream &outputString()
{
// Always ensure it's terminated with a newline
if ( *( m_string.asChar() + m_string.length() - 1 ) != '\n' )
{
m_string += "\n";
}
const char *pBegin = m_string.asChar();
const char *const pEnd = pBegin + m_string.length();
const char *pCurr = pBegin;
while ( *pCurr && pCurr < pEnd )
{
if ( *pCurr == '\n' )
{
switch ( m_streamType )
{
case kWarning:
MGlobal::displayWarning( MString( pBegin, pCurr - pBegin ) );
break;
case kError:
MGlobal::displayError( MString( pBegin, pCurr - pBegin ) );
break;
default:
MGlobal::displayInfo( MString( pBegin, pCurr - pBegin ) );
break;
}
++pCurr;
pBegin = pCurr;
}
else
{
++pCurr;
}
}
m_string.clear();
return *this;
}
CMayaStream &checkForFlush()
{
const char *pCurr = m_string.asChar();
const char *pEnd = pCurr + m_string.length();
while ( *pCurr && pCurr != pEnd )
{
if ( *pCurr == '\n' )
{
return outputString();
}
++pCurr;
}
return *this;
}
StreamType m_streamType;
MString m_string;
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CMayaWarnStream : public CMayaStream
{
public:
CMayaWarnStream()
: CMayaStream( CMayaStream::kWarning )
{}
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CMayaErrStream : public CMayaStream
{
public:
CMayaErrStream()
: CMayaStream( CMayaStream::kError )
{}
};
//-----------------------------------------------------------------------------
// Specialization for std::string
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const std::string &v )
{
*this << v.c_str();
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for char
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const char &v )
{
m_string += MString( &v, 1 );
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for MVector
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const MVector &v )
{
m_string += v.x;
m_string += " ";
m_string += v.y;
m_string += " ";
m_string += v.z;
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for MFloatVector
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const MFloatVector &v )
{
m_string += v.x;
m_string += " ";
m_string += v.y;
m_string += " ";
m_string += v.z;
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for MEulerRotation
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const MEulerRotation &e )
{
m_string += MAngle( e.x, MAngle::kRadians ).asDegrees();
m_string += " ";
m_string += MAngle( e.y, MAngle::kRadians ).asDegrees();
m_string += " ";
m_string += MAngle( e.z, MAngle::kRadians ).asDegrees();
switch ( e.order )
{
case MEulerRotation::kXYZ:
m_string += " (XYZ)";
break;
case MEulerRotation::kXZY:
m_string += " (XZY)";
break;
case MEulerRotation::kYXZ:
m_string += " (YXZ)";
break;
case MEulerRotation::kYZX:
m_string += " (YZX)";
break;
case MEulerRotation::kZXY:
m_string += " (ZXY)";
break;
case MEulerRotation::kZYX:
m_string += " (ZYX)";
break;
default:
m_string += " (Unknown)";
break;
}
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for MQuaternion
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const MQuaternion &q )
{
m_string += q.x;
m_string += " ";
m_string += q.y;
m_string += " ";
m_string += q.z;
m_string += " ";
m_string += q.w;
m_string += " (";
operator<<( q.asEulerRotation() );
m_string += ")";
return *this;
}
//-----------------------------------------------------------------------------
// Specialization for Quaternion
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const Quaternion &q )
{
return operator<<( MQuaternion( q.x, q.y, q.z, q.w ) );
}
//-----------------------------------------------------------------------------
// Specialization for RadianEuler
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const RadianEuler &e )
{
return operator<<( MEulerRotation( e.x, e.y, e.z ) );
}
//-----------------------------------------------------------------------------
// Specialization for RadianEuler
//-----------------------------------------------------------------------------
template <>
inline CMayaStream &CMayaStream::operator<<( const Vector &v )
{
return operator<<( MVector( v.x, v.y, v.z ) );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
template < class T_t >
inline CMayaStream &CMayaStream::operator<<( const T_t &v )
{
m_string += v;
return checkForFlush();
}
//-----------------------------------------------------------------------------
//
// minfo, mwarn & merr are ostreams which can be used to send stuff to
// the Maya history window
//
//-----------------------------------------------------------------------------
extern CMayaStream minfo;
extern CMayaWarnStream mwarn;
extern CMayaErrStream merr;
#endif // VALVEMAYA_H