Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

672 lines
20 KiB

  1. //======= Copyright � 1996-2006, Valve Corporation, All rights reserved. ======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef VALVEMAYA_H
  7. #define VALVEMAYA_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. // Maya Includes
  12. #include <maya/MAngle.h>
  13. #include <maya/MDagModifier.h>
  14. #include <maya/MEulerRotation.h>
  15. #include <maya/MFloatVector.h>
  16. #include <maya/MGlobal.h>
  17. #include <maya/MIOStream.h>
  18. #include <maya/MMatrix.h>
  19. #include <maya/MObject.h>
  20. #include <maya/MQuaternion.h>
  21. #include <maya/MSyntax.h>
  22. #include <maya/MString.h>
  23. #include <maya/MDagPath.h>
  24. #include <maya/MVector.h>
  25. // Valve Includes
  26. #include "tier1/stringpool.h"
  27. #include "tier1/utlstring.h"
  28. #include "tier1/utlstringmap.h"
  29. #include "tier1/utlvector.h"
  30. #include "tier1/interface.h"
  31. #include "mathlib/mathlib.h"
  32. #include "ValveMaya/Undo.h"
  33. //-----------------------------------------------------------------------------
  34. // Forward declarations
  35. //-----------------------------------------------------------------------------
  36. class IMayaVGui;
  37. //-----------------------------------------------------------------------------
  38. // Maya-specific library singletons
  39. //-----------------------------------------------------------------------------
  40. extern IMayaVGui *g_pMayaVGui;
  41. //-----------------------------------------------------------------------------
  42. //
  43. // Purpose: Group a bunch of functions into the Valve Maya Namespace
  44. //
  45. //-----------------------------------------------------------------------------
  46. namespace ValveMaya
  47. {
  48. //-----------------------------------------------------------------------------
  49. // Forward declarations
  50. //-----------------------------------------------------------------------------
  51. class CUndo;
  52. //-----------------------------------------------------------------------------
  53. // Statics
  54. //-----------------------------------------------------------------------------
  55. extern const MQuaternion v2mQuat; // Valve Engine -> Maya Quaternion
  56. extern const MQuaternion m2vQuat; // Maya -> Valve Engine Quaternion
  57. extern const MMatrix v2mMat; // Valve Engine -> Maya Matrix
  58. extern const MMatrix m2vMat; // Maya -> Valve Engine Matrix
  59. extern const MQuaternion vc2mcQuat; // Valve Engine Camera -> Maya Camera Quaternion
  60. extern const MQuaternion mc2vcQuat; // Maya Camera -> Valve Camera Engine Quaternion
  61. extern const MMatrix vc2mcMat; // Valve Engine Camera -> Maya Camera Quaternion
  62. extern const MMatrix mc2vcMat; // Maya Camera -> Valve Camera Engine Quaternion
  63. inline MQuaternion ValveToMaya( const MQuaternion &q ) { return q * v2mQuat; }
  64. inline MQuaternion ValveCameraToMayaCamera( const MQuaternion &q ) { return vc2mcQuat * q * v2mQuat; }
  65. inline MQuaternion MayaToValve( const MQuaternion &q ) { return q * m2vQuat; }
  66. inline MQuaternion MayaCameraToValveCamera( const MQuaternion &q ) { return mc2vcQuat * q * m2vQuat; }
  67. inline MVector ValveToMaya( const MVector &p ) { return p.rotateBy( v2mQuat ); }
  68. inline MVector ValveCameraToMayaCamera( const MVector &p ) { return p.rotateBy( v2mQuat ); }
  69. inline MVector MayaToValve( const MVector &p ) { return p.rotateBy( m2vQuat ); }
  70. inline MVector MayaCameraToValveCamera( const MVector &p ) { return p.rotateBy( m2vQuat ); }
  71. //-----------------------------------------------------------------------------
  72. // Connect, disconnect
  73. //-----------------------------------------------------------------------------
  74. bool ConnectLibraries( CreateInterfaceFn factory );
  75. void DisconnectLibraries();
  76. MStatus CreateDagNode(
  77. const char *const i_nodeType,
  78. const char *const i_transformName = NULL,
  79. const MObject &i_parentObj = MObject::kNullObj,
  80. MObject *o_pTransformObj = NULL,
  81. MObject *o_pShapeObj = NULL,
  82. MDagModifier *i_mDagModifier = NULL);
  83. inline MStatus CreateDagNode(
  84. CUndo &undo,
  85. const char *const i_nodeType,
  86. const char *const i_transformName = NULL,
  87. const MObject &i_parentObj = MObject::kNullObj,
  88. MObject *o_pTransformObj = NULL,
  89. MObject *o_pShapeObj = NULL )
  90. {
  91. return CreateDagNode( i_nodeType, i_transformName, i_parentObj, o_pTransformObj, o_pShapeObj, &undo.DagModifier() );
  92. }
  93. bool IsNodeVisible( const MDagPath &mDagPath, bool bTemplateAsInvisible = true );
  94. bool IsPathVisible( MDagPath mDagPath, bool bTemplateAsInvisible = true );
  95. class CMSyntaxHelp
  96. {
  97. public:
  98. CMSyntaxHelp()
  99. : m_groupedHelp( false ) // Make case sensitive
  100. , m_helpCount( 0 )
  101. , m_shortNameLength( 0 )
  102. {}
  103. void Clear();
  104. MStatus AddFlag(
  105. MSyntax &i_mSyntax,
  106. const char *i_shortName,
  107. const char *i_longName,
  108. const char *i_group,
  109. const char *i_help,
  110. const MSyntax::MArgType i_argType1 = MSyntax::kNoArg,
  111. const MSyntax::MArgType i_argType2 = MSyntax::kNoArg,
  112. const MSyntax::MArgType i_argType3 = MSyntax::kNoArg,
  113. const MSyntax::MArgType i_argType4 = MSyntax::kNoArg,
  114. const MSyntax::MArgType i_argType5 = MSyntax::kNoArg,
  115. const MSyntax::MArgType i_argType6 = MSyntax::kNoArg);
  116. void PrintHelp(
  117. const char *const i_cmdName,
  118. const char *const i_cmdDesc,
  119. int i_lineLength = 0U);
  120. void PrintHelp(
  121. const MString &i_cmdName,
  122. const MString &i_cmdDesc,
  123. int i_lineLength = 0U)
  124. {
  125. PrintHelp( i_cmdName.asChar(), i_cmdDesc.asChar(), i_lineLength );
  126. }
  127. protected:
  128. public:
  129. protected:
  130. CCountedStringPool m_stringPool;
  131. struct HelpData_t
  132. {
  133. const char *m_shortName;
  134. const char *m_longName;
  135. const char *m_help;
  136. CUtlVector<MSyntax::MArgType> m_argTypes;
  137. };
  138. CUtlVector<const char *> m_groupOrder;
  139. CUtlStringMap<CUtlVector<HelpData_t> > m_groupedHelp;
  140. int m_helpCount;
  141. int m_shortNameLength;
  142. };
  143. MString RemoveNameSpace( const MString &mString );
  144. MString &BackslashToSlash( MString &mString );
  145. template < class T_t > bool IsDefault( T_t &, const MPlug & );
  146. bool IsDefault( const MPlug &aPlug );
  147. uint NextAvailable( MPlug &mPlug );
  148. MPlug NextAvailablePlug( MPlug &mPlug );
  149. MObject AddColorSetToMesh(
  150. const MString &colorSetName,
  151. const MDagPath &mDagPath,
  152. MDagModifier &mDagModifier );
  153. MString GetMaterialPath(
  154. const MObject &shadingGroupObj,
  155. MObject *pSurfaceShaderObj,
  156. MObject *pFileObj,
  157. MObject *pPlace2dTextureObj,
  158. MObject *pVmtObj,
  159. bool *pbTransparent,
  160. MString *pDebugWhy );
  161. // Returns the first node that is connected to the specified plug as input or output
  162. MObject FindConnectedNode( const MPlug &mPlug );
  163. // Returns the plug connected to the specified plug as an input, a NULL plug if no plug is connected
  164. MPlug FindInputPlug( const MPlug &dstPlug );
  165. MPlug FindInputPlug( const MObject &dstNodeObj, const MString &dstPlugName );
  166. MObject FindInputNode( const MPlug &dstPlug );
  167. MObject FindInputNode( const MObject &dstNodeObj, const MString &dstPlugName );
  168. MObject FindInputAttr( const MPlug &dstPlug );
  169. MObject FindInputAttr( const MObject &dstNodeObj, const MString &dstPlugName );
  170. // Returns the first found node MObject of the specified type in the history of the specified node
  171. MObject FindInputNodeOfType( const MObject &dstNodeObj, const MString &typeName, const MString &dstPlugName );
  172. MObject FindInputNodeOfType( const MObject &dstNodeObj, const MString &typeName, MSelectionList &doneList );
  173. // Creates a deformer of the specified type and deforms the specified shape with an optional component
  174. MObject DeformShape( ValveMaya::CUndo &undo, const MString &deformerType, const MDagPath &inOrigDagPath, MObject &origCompObj = MObject::kNullObj );
  175. bool Substitute( MString &str, const MString &searchStr, const MString &replaceStr );
  176. bool SubstituteCaseInsensitive( MString &str, const MString &searchStr, const MString &replaceStr );
  177. bool SubstituteAll( MString &str, const MString &searchStr, const MString &replaceStr );
  178. bool SubstituteAllCaseInsensitive( MString &str, const MString &searchStr, const MString &replaceStr );
  179. void FixSlashes( MString &str, char correctPathSeparator = '/' );
  180. //-----------------------------------------------------------------------------
  181. // Converts a Maya MMatrix to a Valve matrix3x4_t (transpose)
  182. //-----------------------------------------------------------------------------
  183. inline void MatrixMayaToValve( matrix3x4_t &mValve, const MMatrix &mMaya )
  184. {
  185. mValve[0][0] = mMaya[0][0]; mValve[0][1] = mMaya[1][0]; mValve[0][2] = mMaya[2][0]; mValve[0][3] = mMaya[3][0];
  186. mValve[1][0] = mMaya[0][1]; mValve[1][1] = mMaya[1][1]; mValve[1][2] = mMaya[2][1]; mValve[1][3] = mMaya[3][1];
  187. mValve[2][0] = mMaya[0][2]; mValve[2][1] = mMaya[1][2]; mValve[2][2] = mMaya[2][2]; mValve[2][3] = mMaya[3][2];
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Converts a Valve matrix3x4_t to a Maya MMatrix (transpose)
  191. //-----------------------------------------------------------------------------
  192. inline void MatrixValveToMaya( MMatrix &mMaya, const matrix3x4_t &mValve )
  193. {
  194. mMaya[0][0] = mValve[0][0]; mMaya[0][1] = mValve[1][0]; mMaya[0][2] = mValve[2][0]; mMaya[3][0] = 0.0;
  195. mMaya[1][0] = mValve[0][1]; mMaya[1][1] = mValve[1][1]; mMaya[1][2] = mValve[2][1]; mMaya[3][1] = 0.0;
  196. mMaya[2][0] = mValve[0][2]; mMaya[2][1] = mValve[1][2]; mMaya[2][2] = mValve[2][2]; mMaya[3][2] = 0.0;
  197. mMaya[3][0] = mValve[0][3]; mMaya[3][1] = mValve[1][3]; mMaya[3][2] = mValve[2][3]; mMaya[3][3] = 1.0;
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Converts a Maya MVector to a Valve Vector
  201. //-----------------------------------------------------------------------------
  202. inline void VectorMayaToValve( Vector &vValve, const MVector &vMaya )
  203. {
  204. vValve.x = vMaya.x;
  205. vValve.y = vMaya.y;
  206. vValve.z = vMaya.z;
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Converts a Valve Vector to a Maya MVector
  210. //-----------------------------------------------------------------------------
  211. inline void VectorValveToMaya( MVector &vMaya, const Vector &vValve )
  212. {
  213. vMaya.x = vValve.x;
  214. vMaya.y = vValve.y;
  215. vMaya.z = vValve.z;
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Converts a Maya MQuaternion to a Valve Quaternion
  219. //-----------------------------------------------------------------------------
  220. inline void QuaternionMayaToValve( Quaternion &qValve, const MQuaternion &qMaya )
  221. {
  222. qValve.x = qMaya.x;
  223. qValve.y = qMaya.y;
  224. qValve.z = qMaya.z;
  225. qValve.w = qMaya.w;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Converts a Maya MQuaternion to a Valve Quaternion
  229. //-----------------------------------------------------------------------------
  230. inline void QuaternionMayaToValve( Quaternion &qValve, const MEulerRotation &eMaya )
  231. {
  232. const MQuaternion qMaya = eMaya.asQuaternion();
  233. qValve.x = qMaya.x;
  234. qValve.y = qMaya.y;
  235. qValve.z = qMaya.z;
  236. qValve.w = qMaya.w;
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Converts a Valve Quaternion to a Maya MQuaternion
  240. //-----------------------------------------------------------------------------
  241. inline void QuaternionValveToMaya( MQuaternion &qMaya, const Quaternion &qValve )
  242. {
  243. qMaya.x = qValve.x;
  244. qMaya.y = qValve.y;
  245. qMaya.z = qValve.z;
  246. qMaya.w = qValve.w;
  247. }
  248. //-----------------------------------------------------------------------------
  249. // Converts a Valve Quaternion to a Maya MQuaternion
  250. //-----------------------------------------------------------------------------
  251. inline void QuaternionValveToMaya( MEulerRotation &eMaya, const Quaternion &qValve )
  252. {
  253. MQuaternion qMaya;
  254. QuaternionValveToMaya( qMaya, qValve );
  255. const MEulerRotation::RotationOrder roTmp = eMaya.order;
  256. eMaya = qMaya;
  257. if ( eMaya.order != roTmp )
  258. {
  259. eMaya.reorder( roTmp );
  260. }
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Converts a Maya MEulerRotation to a Valve RadianEuler
  264. //-----------------------------------------------------------------------------
  265. inline void RadianEulerMayaToValve( RadianEuler &eValve, const MEulerRotation &eMaya )
  266. {
  267. if ( eMaya.order == MEulerRotation::kXYZ )
  268. {
  269. eValve.x = eMaya.x;
  270. eValve.y = eMaya.y;
  271. eValve.z = eMaya.z;
  272. }
  273. else
  274. {
  275. MEulerRotation eTmp = eMaya;
  276. eTmp.reorder( MEulerRotation::kXYZ );
  277. eValve.x = eTmp.x;
  278. eValve.y = eTmp.y;
  279. eValve.z = eTmp.z;
  280. }
  281. }
  282. //-----------------------------------------------------------------------------
  283. // Converts a Valve RadianEuler to a Maya MEulerRotation
  284. //-----------------------------------------------------------------------------
  285. inline void RadianEulerValveToMaya( MEulerRotation &eMaya, const RadianEuler &eValve )
  286. {
  287. const MEulerRotation::RotationOrder roTmp = eMaya.order;
  288. if ( roTmp == MEulerRotation::kXYZ )
  289. {
  290. eMaya.x = eValve.x;
  291. eMaya.y = eValve.y;
  292. eMaya.z = eValve.z;
  293. }
  294. else
  295. {
  296. eMaya.reorder( MEulerRotation::kXYZ );
  297. eMaya.x = eValve.x;
  298. eMaya.y = eValve.y;
  299. eMaya.z = eValve.z;
  300. eMaya.reorder( roTmp );
  301. }
  302. }
  303. } // end namespace ValveMaya
  304. // Make an alias for the ValveMaya namespace
  305. namespace vm = ValveMaya;
  306. //-----------------------------------------------------------------------------
  307. // A simple stream class for printing information to the Maya script editor
  308. //-----------------------------------------------------------------------------
  309. class CMayaStream
  310. {
  311. public:
  312. enum StreamType { kInfo, kWarning, kError };
  313. CMayaStream( const StreamType i_streamType = kInfo )
  314. : m_streamType( i_streamType )
  315. {}
  316. template < class T_t >
  317. CMayaStream &operator<<( const T_t &v );
  318. // This is a hack so CMayaStream << std::endl works as expected
  319. CMayaStream &operator<<( std::ostream &(*StdEndl_t)( std::ostream & ) )
  320. {
  321. return flush();
  322. }
  323. CMayaStream &flush() { return outputString(); }
  324. protected:
  325. CMayaStream &outputString()
  326. {
  327. // Always ensure it's terminated with a newline
  328. if ( *( m_string.asChar() + m_string.length() - 1 ) != '\n' )
  329. {
  330. m_string += "\n";
  331. }
  332. const char *pBegin = m_string.asChar();
  333. const char *const pEnd = pBegin + m_string.length();
  334. const char *pCurr = pBegin;
  335. while ( *pCurr && pCurr < pEnd )
  336. {
  337. if ( *pCurr == '\n' )
  338. {
  339. switch ( m_streamType )
  340. {
  341. case kWarning:
  342. MGlobal::displayWarning( MString( pBegin, pCurr - pBegin ) );
  343. break;
  344. case kError:
  345. MGlobal::displayError( MString( pBegin, pCurr - pBegin ) );
  346. break;
  347. default:
  348. MGlobal::displayInfo( MString( pBegin, pCurr - pBegin ) );
  349. break;
  350. }
  351. ++pCurr;
  352. pBegin = pCurr;
  353. }
  354. else
  355. {
  356. ++pCurr;
  357. }
  358. }
  359. m_string.clear();
  360. return *this;
  361. }
  362. CMayaStream &checkForFlush()
  363. {
  364. const char *pCurr = m_string.asChar();
  365. const char *pEnd = pCurr + m_string.length();
  366. while ( *pCurr && pCurr != pEnd )
  367. {
  368. if ( *pCurr == '\n' )
  369. {
  370. return outputString();
  371. }
  372. ++pCurr;
  373. }
  374. return *this;
  375. }
  376. StreamType m_streamType;
  377. MString m_string;
  378. };
  379. //-----------------------------------------------------------------------------
  380. //
  381. //-----------------------------------------------------------------------------
  382. class CMayaWarnStream : public CMayaStream
  383. {
  384. public:
  385. CMayaWarnStream()
  386. : CMayaStream( CMayaStream::kWarning )
  387. {}
  388. };
  389. //-----------------------------------------------------------------------------
  390. //
  391. //-----------------------------------------------------------------------------
  392. class CMayaErrStream : public CMayaStream
  393. {
  394. public:
  395. CMayaErrStream()
  396. : CMayaStream( CMayaStream::kError )
  397. {}
  398. };
  399. //-----------------------------------------------------------------------------
  400. // Specialization for std::string
  401. //-----------------------------------------------------------------------------
  402. template <>
  403. inline CMayaStream &CMayaStream::operator<<( const std::string &v )
  404. {
  405. *this << v.c_str();
  406. return *this;
  407. }
  408. //-----------------------------------------------------------------------------
  409. // Specialization for char
  410. //-----------------------------------------------------------------------------
  411. template <>
  412. inline CMayaStream &CMayaStream::operator<<( const char &v )
  413. {
  414. m_string += MString( &v, 1 );
  415. return *this;
  416. }
  417. //-----------------------------------------------------------------------------
  418. // Specialization for MVector
  419. //-----------------------------------------------------------------------------
  420. template <>
  421. inline CMayaStream &CMayaStream::operator<<( const MVector &v )
  422. {
  423. m_string += v.x;
  424. m_string += " ";
  425. m_string += v.y;
  426. m_string += " ";
  427. m_string += v.z;
  428. return *this;
  429. }
  430. //-----------------------------------------------------------------------------
  431. // Specialization for MFloatVector
  432. //-----------------------------------------------------------------------------
  433. template <>
  434. inline CMayaStream &CMayaStream::operator<<( const MFloatVector &v )
  435. {
  436. m_string += v.x;
  437. m_string += " ";
  438. m_string += v.y;
  439. m_string += " ";
  440. m_string += v.z;
  441. return *this;
  442. }
  443. //-----------------------------------------------------------------------------
  444. // Specialization for MEulerRotation
  445. //-----------------------------------------------------------------------------
  446. template <>
  447. inline CMayaStream &CMayaStream::operator<<( const MEulerRotation &e )
  448. {
  449. m_string += MAngle( e.x, MAngle::kRadians ).asDegrees();
  450. m_string += " ";
  451. m_string += MAngle( e.y, MAngle::kRadians ).asDegrees();
  452. m_string += " ";
  453. m_string += MAngle( e.z, MAngle::kRadians ).asDegrees();
  454. switch ( e.order )
  455. {
  456. case MEulerRotation::kXYZ:
  457. m_string += " (XYZ)";
  458. break;
  459. case MEulerRotation::kXZY:
  460. m_string += " (XZY)";
  461. break;
  462. case MEulerRotation::kYXZ:
  463. m_string += " (YXZ)";
  464. break;
  465. case MEulerRotation::kYZX:
  466. m_string += " (YZX)";
  467. break;
  468. case MEulerRotation::kZXY:
  469. m_string += " (ZXY)";
  470. break;
  471. case MEulerRotation::kZYX:
  472. m_string += " (ZYX)";
  473. break;
  474. default:
  475. m_string += " (Unknown)";
  476. break;
  477. }
  478. return *this;
  479. }
  480. //-----------------------------------------------------------------------------
  481. // Specialization for MQuaternion
  482. //-----------------------------------------------------------------------------
  483. template <>
  484. inline CMayaStream &CMayaStream::operator<<( const MQuaternion &q )
  485. {
  486. m_string += q.x;
  487. m_string += " ";
  488. m_string += q.y;
  489. m_string += " ";
  490. m_string += q.z;
  491. m_string += " ";
  492. m_string += q.w;
  493. m_string += " (";
  494. operator<<( q.asEulerRotation() );
  495. m_string += ")";
  496. return *this;
  497. }
  498. //-----------------------------------------------------------------------------
  499. // Specialization for Quaternion
  500. //-----------------------------------------------------------------------------
  501. template <>
  502. inline CMayaStream &CMayaStream::operator<<( const Quaternion &q )
  503. {
  504. return operator<<( MQuaternion( q.x, q.y, q.z, q.w ) );
  505. }
  506. //-----------------------------------------------------------------------------
  507. // Specialization for RadianEuler
  508. //-----------------------------------------------------------------------------
  509. template <>
  510. inline CMayaStream &CMayaStream::operator<<( const RadianEuler &e )
  511. {
  512. return operator<<( MEulerRotation( e.x, e.y, e.z ) );
  513. }
  514. //-----------------------------------------------------------------------------
  515. // Specialization for RadianEuler
  516. //-----------------------------------------------------------------------------
  517. template <>
  518. inline CMayaStream &CMayaStream::operator<<( const Vector &v )
  519. {
  520. return operator<<( MVector( v.x, v.y, v.z ) );
  521. }
  522. //-----------------------------------------------------------------------------
  523. //
  524. //-----------------------------------------------------------------------------
  525. template < class T_t >
  526. inline CMayaStream &CMayaStream::operator<<( const T_t &v )
  527. {
  528. m_string += v;
  529. return checkForFlush();
  530. }
  531. //-----------------------------------------------------------------------------
  532. //
  533. // minfo, mwarn & merr are ostreams which can be used to send stuff to
  534. // the Maya history window
  535. //
  536. //-----------------------------------------------------------------------------
  537. extern CMayaStream minfo;
  538. extern CMayaWarnStream mwarn;
  539. extern CMayaErrStream merr;
  540. #endif // VALVEMAYA_H