Team Fortress 2 Source Code as on 22/4/2020
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.

4068 lines
120 KiB

  1. //=============================================================================
  2. //
  3. //========= Copyright Valve Corporation, All rights reserved. ============//
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Converts from any one DMX file format to another
  12. // Can also output SMD or a QCI header from DMX input
  13. //
  14. //=============================================================================
  15. // Standard includes
  16. #include <conio.h>
  17. #include <stdarg.h>
  18. #include <math.h>
  19. #include <time.h>
  20. // Valve includes
  21. #include "vstdlib/cvar.h"
  22. #include "tier1/tier1.h"
  23. #include "tier2/tier2.h"
  24. #include "tier2/tier2dm.h"
  25. #include "tier3/tier3.h"
  26. #include "filesystem.h"
  27. #include "vstdlib/iprocessutils.h"
  28. #include "tier0/icommandline.h"
  29. #include "istudiorender.h"
  30. #include "vphysics_interface.h"
  31. #include "datacache/idatacache.h"
  32. #include "datacache/imdlcache.h"
  33. #include "appframework/AppFramework.h"
  34. #include "dmserializers/idmserializers.h"
  35. #include "datamodel/idatamodel.h"
  36. #include "datamodel/dmelement.h"
  37. #include "movieobjects/dmeanimationset.h"
  38. #include "movieobjects/dmedag.h"
  39. #include "movieobjects/dmemesh.h"
  40. #include "movieobjects/dmevertexdata.h"
  41. #include "movieobjects/dmeselection.h"
  42. #include "movieobjects/dmecombinationoperator.h"
  43. #include "movieobjects/dmobjserializer.h"
  44. #include "movieobjects/dmsmdserializer.h"
  45. #include "movieobjects/dmmeshcomp.h"
  46. #include "movieobjects/dmemodel.h"
  47. #include "movieobjects/dmedccmakefile.h"
  48. #include "movieobjects/dmmeshutils.h"
  49. #include "tier1/utlstring.h"
  50. #include "tier1/utlbuffer.h"
  51. #include "tier2/p4helpers.h"
  52. // Lua includes
  53. #include <lua.h>
  54. #include <lauxlib.h>
  55. #include <lualib.h>
  56. // Local includes
  57. #include "dmxedit.h"
  58. //-----------------------------------------------------------------------------
  59. // Statics
  60. //-----------------------------------------------------------------------------
  61. LuaFunc_s *LuaFunc_s::s_pFirstFunc = NULL;
  62. CDmxEdit LuaFunc_s::m_dmxEdit;
  63. //-----------------------------------------------------------------------------
  64. //
  65. //-----------------------------------------------------------------------------
  66. const CDmxEdit::CFalloffType CDmxEdit::STRAIGHT( CDmeMesh::STRAIGHT );
  67. const CDmxEdit::CFalloffType CDmxEdit::LINEAR( CDmeMesh::STRAIGHT );
  68. const CDmxEdit::CFalloffType CDmxEdit::BELL( CDmeMesh::BELL );
  69. const CDmxEdit::CFalloffType CDmxEdit::SMOOTH( CDmeMesh::BELL );
  70. const CDmxEdit::CFalloffType CDmxEdit::SPIKE( CDmeMesh::SPIKE );
  71. const CDmxEdit::CFalloffType CDmxEdit::DOME( CDmeMesh::DOME );
  72. const CDmxEdit::CSelectOp CDmxEdit::ADD( CDmxEdit::CSelectOp::kAdd );
  73. const CDmxEdit::CSelectOp CDmxEdit::SUBTRACT( CDmxEdit::CSelectOp::kSubtract );
  74. const CDmxEdit::CSelectOp CDmxEdit::TOGGLE( CDmxEdit::CSelectOp::kToggle );
  75. const CDmxEdit::CSelectOp CDmxEdit::INTERSECT( CDmxEdit::CSelectOp::kIntersect );
  76. const CDmxEdit::CSelectOp CDmxEdit::REPLACE( CDmxEdit::CSelectOp::kReplace );
  77. const CDmxEdit::CSelectType CDmxEdit::ALL( CDmxEdit::CSelectType::kAll );
  78. const CDmxEdit::CSelectType CDmxEdit::NONE( CDmxEdit::CSelectType::kNone );
  79. const CDmxEdit::CObjType CDmxEdit::ABSOLUTE( CDmxEdit::CObjType::kAbsolute );
  80. const CDmxEdit::CObjType CDmxEdit::RELATIVE( CDmxEdit::CObjType::kRelative );
  81. const CDmxEdit::CDistanceType CDmxEdit::DIST_ABSOLUTE( CDmeMesh::DIST_ABSOLUTE );
  82. const CDmxEdit::CDistanceType CDmxEdit::DIST_RELATIVE( CDmeMesh::DIST_RELATIVE );
  83. const CDmxEdit::CDistanceType CDmxEdit::DIST_DEFAULT( CDmeMesh::DIST_DEFAULT );
  84. const CDmxEdit::CAxisType CDmxEdit::XAXIS( CDmxEdit::CAxisType::kXAxis );
  85. const CDmxEdit::CAxisType CDmxEdit::YAXIS( CDmxEdit::CAxisType::kYAxis );
  86. const CDmxEdit::CAxisType CDmxEdit::ZAXIS( CDmxEdit::CAxisType::kXAxis );
  87. const CDmxEdit::CHalfType CDmxEdit::LEFT( CDmxEdit::CHalfType::kLeft );
  88. const CDmxEdit::CHalfType CDmxEdit::RIGHT( CDmxEdit::CHalfType::kRight );
  89. //=============================================================================
  90. // CDmxEdit definition
  91. //=============================================================================
  92. //-----------------------------------------------------------------------------
  93. //
  94. //-----------------------------------------------------------------------------
  95. CDmxEdit::CDmxEdit()
  96. : m_pRoot( NULL )
  97. , m_pMesh( NULL )
  98. , m_pCurrentSelection( NULL )
  99. , m_distanceType( CDmeMesh::DIST_ABSOLUTE )
  100. {}
  101. //-----------------------------------------------------------------------------
  102. //
  103. //-----------------------------------------------------------------------------
  104. void CDmxEdit::SetScriptFilename( const char *pFilename )
  105. {
  106. m_scriptFilename = pFilename;
  107. }
  108. //-----------------------------------------------------------------------------
  109. //
  110. //-----------------------------------------------------------------------------
  111. bool CDmxEdit::Load( const char *pFilename, const CObjType &loadType /* = DIST_ABSOLUTE */ )
  112. {
  113. Unload();
  114. const int sLen = Q_strlen( pFilename );
  115. if ( sLen > 4 && !Q_stricmp( pFilename + sLen - 4, ".dmx" ) )
  116. {
  117. g_pDataModel->RestoreFromFile( pFilename, NULL, NULL, &m_pRoot );
  118. if ( !m_pRoot )
  119. return SetErrorString( "DMX Load Failed" );
  120. g_p4factory->AccessFile( pFilename )->Add();
  121. // Find the first mesh with any deltas
  122. // Look for model
  123. CDmeDag *pDag = m_pRoot->GetValueElement< CDmeDag >( "model" );
  124. if ( pDag )
  125. {
  126. CDmeMesh *pFirstMesh = NULL;
  127. CUtlStack< CDmeDag * > traverseStack;
  128. traverseStack.Push( pDag );
  129. while ( traverseStack.Count() )
  130. {
  131. traverseStack.Pop( pDag );
  132. if ( !pDag )
  133. continue;
  134. for ( int i = pDag->GetChildCount() - 1; i >= 0; --i )
  135. {
  136. traverseStack.Push( pDag->GetChild( i ) );
  137. }
  138. CDmeMesh *pMesh = CastElement< CDmeMesh >( pDag->GetShape() );
  139. if ( !pMesh )
  140. continue;
  141. if ( pFirstMesh == NULL )
  142. {
  143. pFirstMesh = pMesh;
  144. }
  145. if ( pMesh->DeltaStateCount() )
  146. {
  147. m_pMesh = pMesh;
  148. break;
  149. }
  150. }
  151. if ( !m_pMesh )
  152. {
  153. if ( pFirstMesh )
  154. {
  155. m_pMesh = pFirstMesh;
  156. LuaWarning( "Cannot Find A DmeMesh With Any Delta States In File, Using First Found Mesh %s", m_pMesh->GetName() );
  157. }
  158. else
  159. {
  160. LuaWarning( "Cannot Find A DmeMesh In File" );
  161. }
  162. }
  163. }
  164. else
  165. {
  166. LuaWarning( "Cannot Find A DmeModel As Element Dme Root Object: %s in %s", m_pRoot->GetName(), pFilename );
  167. }
  168. }
  169. else if ( sLen > 4 && !V_stricmp( pFilename + sLen - 4, ".smd" ) )
  170. {
  171. m_pRoot = CDmSmdSerializer().ReadSMD( pFilename, &m_pMesh );
  172. if ( !m_pRoot )
  173. return SetErrorString( "OBJ Load Failed" );
  174. }
  175. else
  176. {
  177. m_pRoot = CDmObjSerializer().ReadOBJ( pFilename, &m_pMesh, true, loadType() == CObjType::kAbsolute );
  178. if ( !m_pRoot )
  179. return SetErrorString( "OBJ Load Failed" );
  180. }
  181. m_filename = pFilename;
  182. if ( m_pMesh )
  183. {
  184. CDmeVertexData *pBind( m_pMesh->FindBaseState( "bind" ) );
  185. if ( pBind )
  186. {
  187. // Ensure "work" isn't saved!
  188. CDmeVertexData *pWork = m_pMesh->FindOrCreateBaseState( "__dmxEdit_work" );
  189. if ( pWork )
  190. {
  191. pBind->CopyTo( pWork );
  192. m_pMesh->SetCurrentBaseState( "__dmxEdit_work" );
  193. }
  194. }
  195. m_pCurrentSelection = CreateElement< CDmeSingleIndexedComponent >( "selection", m_pRoot->GetFileId() );
  196. m_pMesh->SetValue( "selection", m_pCurrentSelection );
  197. }
  198. return true;
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Lua Interface to CDmxEdit::Load
  202. //-----------------------------------------------------------------------------
  203. LUA_COMMAND(
  204. Load,
  205. "< $file.dmx | $file.obj >, [ $loadType ]",
  206. "Replaces the current scene with the specified scene. The current mesh will be set to the first mesh with a combination operator found in the new scene. $loadType is one of \"absolute\" or \"relative\". If not specified, \"absolute\" is assumed." )
  207. {
  208. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  209. const char *pFilename = luaL_checkstring( pLuaState, 1 );
  210. const char *pLoadType = NULL;
  211. if ( lua_isboolean( pLuaState, 2 ) )
  212. {
  213. if ( lua_toboolean( pLuaState, 2 ) )
  214. {
  215. pLoadType = "Absolute";
  216. }
  217. else
  218. {
  219. pLoadType = "Relative";
  220. }
  221. }
  222. else if ( lua_isstring( pLuaState, 2 ) )
  223. {
  224. pLoadType = lua_tostring( pLuaState, 2 );
  225. }
  226. if ( pLoadType )
  227. {
  228. if ( LuaFunc_s::m_dmxEdit.Load( pFilename, pLoadType ) )
  229. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  230. }
  231. else
  232. {
  233. if ( LuaFunc_s::m_dmxEdit.Load( pFilename ) )
  234. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  235. }
  236. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Imports the Dmx Model file into the current file
  240. //-----------------------------------------------------------------------------
  241. bool CDmxEdit::Import( const char *pFilename, const char *pParentName )
  242. {
  243. if ( !m_pRoot )
  244. {
  245. return Load( pFilename );
  246. }
  247. CDmeModel *pDstModel = m_pRoot->GetValueElement< CDmeModel >( "model" );
  248. if ( !pDstModel )
  249. return SetErrorString( "Can't Find Existing Model Node" );
  250. CDisableUndoScopeGuard sgDisableUndo;
  251. CDmElement *pRoot = NULL;
  252. g_pDataModel->RestoreFromFile( pFilename, NULL, NULL, &pRoot, CR_FORCE_COPY );
  253. if ( !pRoot )
  254. return SetErrorString( "Can't Load DMX File" );
  255. g_p4factory->AccessFile( pFilename )->Add();
  256. CDmeDag *pSrcModel = pRoot->GetValueElement< CDmeDag >( "model" );
  257. if ( !pSrcModel )
  258. {
  259. g_pDataModel->UnloadFile( pRoot->GetFileId() );
  260. return SetErrorString( "Can't Find \"model\" Element On Root Node In DMX File" );
  261. }
  262. int nSkinningJointIndex = -1;
  263. if ( pParentName )
  264. {
  265. CDmeTransform *pJoint = NULL;
  266. const int nJointTransformCount = pDstModel->GetJointTransformCount();
  267. for ( int i = 0; i < nJointTransformCount; ++i )
  268. {
  269. pJoint = pDstModel->GetJointTransform( i );
  270. if ( !Q_stricmp( pJoint->GetName(), pParentName ) )
  271. {
  272. nSkinningJointIndex = i;
  273. break;
  274. }
  275. }
  276. if ( nSkinningJointIndex < 0 )
  277. {
  278. LuaWarning( "Couldn't Find Parent Bone \"%s\"", pParentName );
  279. }
  280. }
  281. CleanupWork();
  282. // CDmeCombinationOperator *pCombo = m_pRoot->GetValueElement< CDmeCombinationOperator >( "combinationOperator" );
  283. // Initialize traversal stack to just model's children (don't want to touch model)
  284. CUtlStack< CDmeDag * > traverseStack;
  285. for ( int i = pSrcModel->GetChildCount() - 1; i >= 0; --i )
  286. {
  287. traverseStack.Push( pSrcModel->GetChild( i ) );
  288. }
  289. CDmeDag *pDag;
  290. while ( traverseStack.Count() )
  291. {
  292. traverseStack.Pop( pDag );
  293. if ( !pDag )
  294. continue;
  295. for ( int i = pDag->GetChildCount() - 1; i >= 0; --i )
  296. {
  297. traverseStack.Push( pDag->GetChild( i ) );
  298. }
  299. CDmeMesh *pMesh = CastElement< CDmeMesh >( pDag->GetShape() );
  300. if ( pMesh )
  301. {
  302. CDmMeshUtils::Merge( pMesh, m_pMesh, nSkinningJointIndex );
  303. }
  304. }
  305. g_pDataModel->UnloadFile( pRoot->GetFileId() );
  306. CreateWork();
  307. return true;
  308. }
  309. //-----------------------------------------------------------------------------
  310. // Lua Interface for CDmxEdit::Import
  311. //-----------------------------------------------------------------------------
  312. LUA_COMMAND(
  313. Import,
  314. "< $file.dmx >, [ $parentBone ]",
  315. "Imports the specified DMX model into the scene. The imported model can optionally be parented (which implictly skins it) to a specified bone in the existing scene via $parentBone." )
  316. {
  317. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  318. const char *pFilename = luaL_checkstring( pLuaState, 1 );
  319. const char *pParentBone = lua_isstring( pLuaState, 2 ) ? lua_tostring( pLuaState, 2 ) : NULL;
  320. if ( LuaFunc_s::m_dmxEdit.Import( pFilename, pParentBone ) )
  321. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  322. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  323. }
  324. //-----------------------------------------------------------------------------
  325. //
  326. //-----------------------------------------------------------------------------
  327. bool CDmxEdit::ListDeltas()
  328. {
  329. if ( !m_pMesh )
  330. return SetErrorString( "No Mesh" );
  331. const int nDeltas( m_pMesh->DeltaStateCount() );
  332. if ( nDeltas <= 0 )
  333. return SetErrorString( "No Deltas Defined On Mesh: %s", m_pMesh->GetName() );
  334. for ( int i( 0 ); i < nDeltas; ++i )
  335. {
  336. Msg( "// Delta %d: %s\n", i, m_pMesh->GetDeltaState( i )->GetName() );
  337. }
  338. return true;
  339. }
  340. //-----------------------------------------------------------------------------
  341. //
  342. //-----------------------------------------------------------------------------
  343. LUA_COMMAND(
  344. ListDeltas,
  345. "",
  346. "Prints a list of all of the deltas present in the current mesh" )
  347. {
  348. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  349. if ( LuaFunc_s::m_dmxEdit.ListDeltas() )
  350. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  351. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  352. }
  353. //-----------------------------------------------------------------------------
  354. //
  355. //-----------------------------------------------------------------------------
  356. int CDmxEdit::DeltaCount()
  357. {
  358. if ( !m_pMesh )
  359. return SetErrorString( "No Mesh" );
  360. return m_pMesh->DeltaStateCount();
  361. }
  362. //-----------------------------------------------------------------------------
  363. //
  364. //-----------------------------------------------------------------------------
  365. LUA_COMMAND(
  366. DeltaCount,
  367. "",
  368. "Returns the number of deltas in the current mesh" )
  369. {
  370. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  371. lua_pushnumber( pLuaState, LuaFunc_s::m_dmxEdit.DeltaCount() );
  372. return 1;
  373. }
  374. //-----------------------------------------------------------------------------
  375. //
  376. //-----------------------------------------------------------------------------
  377. const char *CDmxEdit::DeltaName( int nDeltaStateIndex )
  378. {
  379. if ( !m_pMesh )
  380. {
  381. SetErrorString( "No Mesh" );
  382. return NULL;
  383. }
  384. if ( nDeltaStateIndex >= m_pMesh->DeltaStateCount() )
  385. {
  386. SetErrorString( "Delta Index Too High" );
  387. return NULL;
  388. }
  389. return m_pMesh->GetDeltaState( nDeltaStateIndex )->GetName();
  390. }
  391. //-----------------------------------------------------------------------------
  392. //
  393. //-----------------------------------------------------------------------------
  394. LUA_COMMAND(
  395. DeltaName,
  396. "< #deltaIndex >",
  397. "Returns the name of the delta at the specified index. Use DeltaCount() to get a count of how many deltas are present" )
  398. {
  399. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  400. const int nDeltaIndex = luaL_checknumber( pLuaState, 1 );
  401. const char *pDeltaName = LuaFunc_s::m_dmxEdit.DeltaName( nDeltaIndex );
  402. if ( pDeltaName )
  403. {
  404. lua_pushstring( pLuaState, pDeltaName );
  405. return 1;
  406. }
  407. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  408. }
  409. //-----------------------------------------------------------------------------
  410. //
  411. //-----------------------------------------------------------------------------
  412. void CDmxEdit::Unload()
  413. {
  414. CDisableUndoScopeGuard sgDisableUndo;
  415. if ( m_pCurrentSelection )
  416. {
  417. g_pDataModel->DestroyElement( m_pCurrentSelection->GetHandle() );
  418. }
  419. if ( m_pRoot )
  420. {
  421. g_pDataModel->UnloadFile( m_pRoot->GetFileId() );
  422. }
  423. m_filename = "";
  424. m_pRoot = NULL;
  425. m_pMesh = NULL;
  426. m_pCurrentSelection = NULL;
  427. }
  428. //-----------------------------------------------------------------------------
  429. //
  430. //-----------------------------------------------------------------------------
  431. bool CDmxEdit::ImportComboRules( const char *pFilename, bool bOverwrite /* = true */, bool bPurgeDeltas /* = false */ )
  432. {
  433. if ( !m_pMesh )
  434. return SetErrorString( "No Mesh" );
  435. CDmeCombinationOperator *pDestCombo( FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" ) );
  436. if ( !pDestCombo )
  437. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  438. CDisableUndoScopeGuard sg;
  439. CDmElement *pRoot = NULL;
  440. g_pDataModel->RestoreFromFile( pFilename, NULL, NULL, &pRoot, CR_FORCE_COPY );
  441. if ( !pRoot )
  442. return SetErrorString( "File Cannot Be Read" );
  443. g_p4factory->AccessFile( pFilename )->Add();
  444. // Try to find a combination system in the file
  445. CDmeCombinationOperator *pCombo = CastElement< CDmeCombinationOperator >( pRoot );
  446. if ( !pCombo )
  447. {
  448. pCombo = pRoot->GetValueElement< CDmeCombinationOperator >( "combinationOperator" );
  449. }
  450. if ( !pCombo )
  451. return SetErrorString( "No DmeCombinationOperator Found In File" );
  452. ImportCombinationControls( pCombo, pDestCombo, bOverwrite );
  453. ImportDominationRules( pDestCombo, pCombo, bOverwrite );
  454. pDestCombo->SetToDefault();
  455. g_pDataModel->UnloadFile( pRoot->GetFileId() );
  456. if ( bPurgeDeltas )
  457. {
  458. CDmMeshUtils::PurgeUnusedDeltas( m_pMesh );
  459. }
  460. return true;
  461. }
  462. //-----------------------------------------------------------------------------
  463. //
  464. //-----------------------------------------------------------------------------
  465. LUA_COMMAND(
  466. ImportComboRules,
  467. "< $rules.dmx >, [ #bOverwrite ], [ #bPurgeDeltas ]",
  468. "Imports the combintion rules from the specified dmx file and replaces the current rules with those from the file is #bOverwrite specified or is true. If #bOverwrite is false then any existing controls that are not in the imported rules file are preserved. If #bPurgeDeltas is specified and is true, then any delta states which are no longer referred to by any combination rule or control will be purged. ")
  469. {
  470. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  471. const char *pFilename = luaL_checkstring( pLuaState, 1 );
  472. if ( lua_isboolean( pLuaState, 2 ) )
  473. {
  474. if ( lua_isboolean( pLuaState, 3 ) )
  475. {
  476. if ( LuaFunc_s::m_dmxEdit.ImportComboRules( pFilename, lua_toboolean( pLuaState, 2 ) != 0, lua_toboolean( pLuaState, 3 ) != 0 ) )
  477. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  478. }
  479. else
  480. {
  481. if ( LuaFunc_s::m_dmxEdit.ImportComboRules( pFilename, lua_toboolean( pLuaState, 2 ) != 0 ) )
  482. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  483. }
  484. }
  485. else
  486. {
  487. if ( LuaFunc_s::m_dmxEdit.ImportComboRules( pFilename ) )
  488. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  489. }
  490. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Import dominance rules from this operator
  494. //-----------------------------------------------------------------------------
  495. void CDmxEdit::ImportDominationRules( CDmeCombinationOperator *pDestComboOp, CDmeCombinationOperator *pSrcComboOp, bool bOverwrite )
  496. {
  497. if ( bOverwrite )
  498. {
  499. pDestComboOp->RemoveAllDominationRules();
  500. }
  501. // TODO: Detect conflicts if not overwriting
  502. // Now deal with dominance rules
  503. int nRuleCount = pSrcComboOp->DominationRuleCount();
  504. for ( int i = 0; i < nRuleCount; ++i )
  505. {
  506. bool bMismatch = false;
  507. // Only add dominance rule if *all* raw controls are present and the rule doesn't already exist
  508. CDmeCombinationDominationRule *pSrcRule = pSrcComboOp->GetDominationRule( i );
  509. int nDominatorCount = pSrcRule->DominatorCount();
  510. for ( int j = 0; j < nDominatorCount; ++j )
  511. {
  512. const char *pDominatorName = pSrcRule->GetDominator( j );
  513. if ( !pDestComboOp->HasRawControl( pDominatorName ) )
  514. {
  515. bMismatch = true;
  516. break;
  517. }
  518. }
  519. int nSuppressedCount = pSrcRule->SuppressedCount();
  520. for ( int j = 0; j < nSuppressedCount; ++j )
  521. {
  522. const char *pSuppressedName = pSrcRule->GetSuppressed( j );
  523. if ( !pDestComboOp->HasRawControl( pSuppressedName ) )
  524. {
  525. bMismatch = true;
  526. break;
  527. }
  528. }
  529. if ( bMismatch )
  530. continue;
  531. pDestComboOp->AddDominationRule( pSrcRule );
  532. }
  533. }
  534. //-----------------------------------------------------------------------------
  535. // Returns true if the the control has only 1 raw control and it's the same
  536. // name as the control
  537. //-----------------------------------------------------------------------------
  538. bool IsDefaultControl( CDmeCombinationOperator *pCombo, int nIndex )
  539. {
  540. if ( pCombo->GetRawControlCount( nIndex ) == 1 && !Q_strcmp( pCombo->GetControlName( nIndex ), pCombo->GetRawControlName( nIndex, 0 ) ) )
  541. return true;
  542. return false;
  543. }
  544. //-----------------------------------------------------------------------------
  545. // Returns true if the all raw controls of A are found in B
  546. //-----------------------------------------------------------------------------
  547. bool IsASubsetOfB( CDmeCombinationOperator *pComboA, int nIndexA, CDmeCombinationOperator *pComboB, int nIndexB )
  548. {
  549. const int nCountA = pComboA->GetRawControlCount( nIndexA );
  550. const int nCountB = pComboB->GetRawControlCount( nIndexB );
  551. for ( int i = 0; i < nCountA; ++i )
  552. {
  553. bool bFound = false;
  554. const char *pNameA = pComboA->GetRawControlName( nIndexA, i );
  555. for ( int j = 0; j < nCountB; ++j )
  556. {
  557. if ( !Q_stricmp( pNameA, pComboB->GetRawControlName( nIndexB, j ) ) )
  558. {
  559. bFound = true;
  560. break;
  561. }
  562. }
  563. if ( !bFound )
  564. return false;
  565. }
  566. return true;
  567. }
  568. //-----------------------------------------------------------------------------
  569. // Returns true if the two combination controls have the same raw controls
  570. //-----------------------------------------------------------------------------
  571. bool AreControlsTheSame( CDmeCombinationOperator *pComboA, int nIndexA, CDmeCombinationOperator *pComboB, int nIndexB )
  572. {
  573. const int nCountA = pComboA->GetRawControlCount( nIndexA );
  574. const int nCountB = pComboB->GetRawControlCount( nIndexB );
  575. if ( nCountA != nCountB )
  576. return false;
  577. // This is a test for equality now since we've verified that A & B has same number of elements
  578. return IsASubsetOfB( pComboA, nIndexA, pComboB, nIndexB );
  579. }
  580. //-----------------------------------------------------------------------------
  581. // -1 for failure
  582. //-----------------------------------------------------------------------------
  583. int FindControlIndexFromRawControlName( CDmeCombinationOperator *pCombo, const char *pRawControlName )
  584. {
  585. const int nControlCount = pCombo->GetControlCount();
  586. for ( int i = 0; i < nControlCount; ++i )
  587. {
  588. const int nRawCount = pCombo->GetRawControlCount( i );
  589. for ( int j = 0; j < nRawCount; ++j )
  590. {
  591. if ( !Q_stricmp( pRawControlName, pCombo->GetRawControlName( i, j ) ) )
  592. {
  593. return i;
  594. }
  595. }
  596. }
  597. return -1;
  598. }
  599. //-----------------------------------------------------------------------------
  600. // Import combination rules from this operator
  601. //-----------------------------------------------------------------------------
  602. void CDmxEdit::ImportCombinationControls( CDmeCombinationOperator *pSrcComboOp, CDmeCombinationOperator *pDstComboOp, bool bOverwrite )
  603. {
  604. if ( bOverwrite )
  605. {
  606. // Just remove all existing controls
  607. pDstComboOp->RemoveAllControls();
  608. }
  609. else
  610. {
  611. // Remove conflicting controls, i.e. controls in src which are controlling something that's already
  612. // controlled in dst
  613. bool bConflict = true;
  614. while ( bConflict )
  615. {
  616. bConflict = false;
  617. // Remove all controls from dst that conflict with src
  618. for ( int i = 0; !bConflict && i < pSrcComboOp->GetControlCount(); ++i )
  619. {
  620. const int nSrcRawCount = pSrcComboOp->GetRawControlCount( i );
  621. for ( int j = 0; !bConflict && j < nSrcRawCount; ++j )
  622. {
  623. const char *pSrcRawControlName = pSrcComboOp->GetRawControlName( i, j );
  624. const int nDstControlIndex = FindControlIndexFromRawControlName( pDstComboOp, pSrcRawControlName );
  625. if ( nDstControlIndex < 0 )
  626. continue;
  627. pDstComboOp->RemoveRawControl( nDstControlIndex, pSrcRawControlName );
  628. bConflict = true;
  629. break;
  630. }
  631. }
  632. }
  633. // Purge all empty ones
  634. bConflict = true;
  635. while ( bConflict )
  636. {
  637. bConflict = false;
  638. for ( int i = 0; !bConflict && i < pDstComboOp->GetControlCount(); ++i )
  639. {
  640. if ( pDstComboOp->GetRawControlCount( i ) == 0 )
  641. {
  642. pDstComboOp->RemoveControl( pDstComboOp->GetControlName( i ) );
  643. bConflict = true;
  644. break;
  645. }
  646. }
  647. }
  648. }
  649. // Iterate through all controls in the imported operator.
  650. // For each imported control that contains at least 1 raw control
  651. // for which a delta state exists, create that control
  652. // All conflicts were resolved previously
  653. CUtlVectorFixedGrowable< bool, 256 > foundMatch;
  654. for ( int i = 0; i < pSrcComboOp->GetControlCount(); ++i )
  655. {
  656. const char *pControlName = pSrcComboOp->GetControlName( i );
  657. int nRawControls = pSrcComboOp->GetRawControlCount( i );
  658. int nMatchCount = 0;
  659. foundMatch.EnsureCount( nRawControls );
  660. for ( int j = 0; j < nRawControls; ++j )
  661. {
  662. const char *pRawControl = pSrcComboOp->GetRawControlName( i, j );
  663. foundMatch[j] = pDstComboOp->DoesTargetContainDeltaState( pRawControl );
  664. nMatchCount += foundMatch[j];
  665. }
  666. // No match? Don't import
  667. if ( nMatchCount == 0 )
  668. {
  669. continue;
  670. }
  671. // bool bPartialMatch = ( nMatchCount != nRawControls );
  672. // Found a match! Let's create the control and potentially raw control
  673. pDstComboOp->RemoveControl( pControlName );
  674. bool bIsStereo = pSrcComboOp->IsStereoControl( i );
  675. ControlIndex_t index = pDstComboOp->FindOrCreateControl( pControlName, bIsStereo );
  676. pDstComboOp->SetEyelidControl( index, pSrcComboOp->IsEyelidControl( i ) );
  677. for ( int j = 0; j < nRawControls; ++j )
  678. {
  679. if ( foundMatch[j] )
  680. {
  681. const char *pRawControl = pSrcComboOp->GetRawControlName( i, j );
  682. float flWrinkleScale = pSrcComboOp->GetRawControlWrinkleScale( i, j );
  683. pDstComboOp->AddRawControl( index, pRawControl );
  684. pDstComboOp->SetWrinkleScale( index, pRawControl, flWrinkleScale );
  685. }
  686. }
  687. }
  688. }
  689. //-----------------------------------------------------------------------------
  690. //
  691. //-----------------------------------------------------------------------------
  692. bool CDmxEdit::SetState( const char *pDeltaName )
  693. {
  694. if ( !m_pMesh )
  695. return SetErrorString( "No Mesh" );
  696. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  697. if ( !pDelta )
  698. return SetErrorString( "Invalid Delta \"%s\"", pDeltaName );
  699. return m_pMesh->SetBaseStateToDelta( pDelta );
  700. }
  701. //-----------------------------------------------------------------------------
  702. //
  703. //-----------------------------------------------------------------------------
  704. LUA_COMMAND(
  705. SetState,
  706. "< $delta >",
  707. "Sets the current mesh to the specified $delta")
  708. {
  709. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  710. const char *pDeltaName = luaL_checkstring( pLuaState, 1 );
  711. if ( LuaFunc_s::m_dmxEdit.SetState( pDeltaName ) )
  712. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  713. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  714. }
  715. //-----------------------------------------------------------------------------
  716. //
  717. //-----------------------------------------------------------------------------
  718. bool CDmxEdit::ResetState()
  719. {
  720. if ( !m_pMesh )
  721. return SetErrorString( "No Mesh" );
  722. return m_pMesh->SetBaseStateToDelta( NULL );
  723. }
  724. //-----------------------------------------------------------------------------
  725. //
  726. //-----------------------------------------------------------------------------
  727. LUA_COMMAND(
  728. ResetState,
  729. "",
  730. "Resets the current mesh back to the default base state, i.e. no deltas active")
  731. {
  732. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  733. if ( LuaFunc_s::m_dmxEdit.ResetState() )
  734. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  735. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  736. }
  737. //-----------------------------------------------------------------------------
  738. // Disambiguation when select is called with a selection type of a string
  739. //-----------------------------------------------------------------------------
  740. bool CDmxEdit::Select( const char *pSelectTypeString, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  741. {
  742. const CSelectType selectType( pSelectTypeString );
  743. if ( selectType() == CSelectType::kDelta )
  744. {
  745. CDmeVertexDeltaData *pDelta = FindDeltaState( pSelectTypeString, pPassedMesh );
  746. if ( pDelta)
  747. return Select( pDelta, pPassedSelection, pPassedMesh );
  748. return SetErrorString( "Invalid Delta \"%s\"", pSelectTypeString );
  749. }
  750. return Select( selectType, pPassedSelection, pPassedMesh );
  751. }
  752. //-----------------------------------------------------------------------------
  753. //
  754. //-----------------------------------------------------------------------------
  755. LUA_COMMAND(
  756. Select,
  757. "< $delta >, [ $type ]",
  758. "Changes the selection based on the vertices present in the specified $delta. $type is one of \"add\", \"subtract\", \"toggle\", \"intersect\", \"replace\". If $type is not specified, \"add\" is assumed.")
  759. {
  760. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  761. const char *pSelect1 = luaL_checkstring( pLuaState, 1 );
  762. const char *pSelect2 = lua_tostring( pLuaState, 2 );
  763. if ( pSelect2 )
  764. {
  765. if ( LuaFunc_s::m_dmxEdit.Select( pSelect1, pSelect2 ) )
  766. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  767. }
  768. else
  769. {
  770. if ( LuaFunc_s::m_dmxEdit.Select( pSelect1 ) )
  771. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  772. }
  773. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  774. }
  775. //-----------------------------------------------------------------------------
  776. // Selects vertices of the working mesh that change position in the give delta
  777. //-----------------------------------------------------------------------------
  778. bool CDmxEdit::Select( CDmeVertexDeltaData *pDelta, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  779. {
  780. if ( !pDelta )
  781. return SetErrorString( "Invalid Delta" );
  782. CDmeMesh *pMesh = pPassedMesh ? pPassedMesh : m_pMesh;
  783. if ( !pMesh )
  784. return SetErrorString( "No Mesh" );
  785. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  786. if ( !pSelection )
  787. return SetErrorString( "No Selection To Manipulate" );
  788. pMesh->SelectVerticesFromDelta( pDelta, pSelection );
  789. return true;
  790. }
  791. //-----------------------------------------------------------------------------
  792. // Selects vertices of the working mesh that change position in the give delta
  793. //-----------------------------------------------------------------------------
  794. bool CDmxEdit::Select( const CSelectType &selectType, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  795. {
  796. if ( selectType() == CSelectType::kDelta )
  797. return SetErrorString( "Called Via CSelectType... Wacky!" );
  798. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  799. if ( !pSelection )
  800. return SetErrorString( "No Selection To Manipulate" );
  801. if ( selectType() == CSelectType::kNone )
  802. {
  803. m_pCurrentSelection->Clear();
  804. return true;
  805. }
  806. if ( selectType() != CSelectType::kAll )
  807. return false;
  808. CDmeMesh *pMesh = pPassedMesh ? pPassedMesh : m_pMesh;
  809. if ( !pMesh )
  810. return SetErrorString( "No Mesh" );
  811. pMesh->SelectAllVertices( pSelection );
  812. return true;
  813. }
  814. //-----------------------------------------------------------------------------
  815. // Disambiguation when select is called with a selection type of a string
  816. //-----------------------------------------------------------------------------
  817. bool CDmxEdit::Select( const CSelectOp &selectOp, const char *pSelectTypeString,
  818. CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  819. {
  820. const CSelectType selectType( pSelectTypeString );
  821. if ( selectType() == CSelectType::kDelta )
  822. {
  823. CDmeVertexDeltaData *pDelta = FindDeltaState( pSelectTypeString, pPassedMesh );
  824. if ( pDelta)
  825. return Select( selectOp, pDelta, pPassedSelection, pPassedMesh );
  826. return SetErrorString( "Invalid Delta" );
  827. }
  828. return Select( selectOp, selectType, pPassedSelection, pPassedMesh );
  829. }
  830. //-----------------------------------------------------------------------------
  831. // Selects vertices of the working mesh that change position in the give delta
  832. //-----------------------------------------------------------------------------
  833. bool CDmxEdit::Select( const CSelectOp &selectOp, CDmeVertexDeltaData *pDelta, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  834. {
  835. if ( !pDelta )
  836. return SetErrorString( "Invalid Delta" );
  837. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  838. if ( !pSelection )
  839. return SetErrorString( "No Selection To Manipulate" );
  840. CDmeSingleIndexedComponent *pTempSelection = CreateElement< CDmeSingleIndexedComponent >( "tempSelection", pSelection->GetFileId() );
  841. if ( !pTempSelection )
  842. return false;
  843. const bool retVal = Select( pDelta, pTempSelection, pPassedMesh );
  844. if ( retVal )
  845. {
  846. Select( selectOp, pSelection, pTempSelection );
  847. }
  848. g_pDataModel->DestroyElement( pTempSelection->GetHandle() );
  849. return retVal;
  850. }
  851. //-----------------------------------------------------------------------------
  852. // Selects vertices of the working mesh that change position in the give delta
  853. //-----------------------------------------------------------------------------
  854. bool CDmxEdit::Select( const CSelectOp &selectOp, const CSelectType &selectType, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  855. {
  856. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  857. if ( !pSelection )
  858. return SetErrorString( "No Selection To Manipulate" );
  859. CDmeSingleIndexedComponent *pTempSelection = CreateElement< CDmeSingleIndexedComponent >( "tempSelection", pSelection->GetFileId() );
  860. if ( !pTempSelection )
  861. return false;
  862. const bool retVal = Select( selectType, pTempSelection, pPassedMesh );
  863. if ( retVal )
  864. {
  865. Select( selectOp, pSelection, pTempSelection );
  866. }
  867. g_pDataModel->DestroyElement( pTempSelection->GetHandle() );
  868. return retVal;
  869. }
  870. //-----------------------------------------------------------------------------
  871. // Combines the two selections via the selectOp and puts result into pOriginal
  872. //-----------------------------------------------------------------------------
  873. bool CDmxEdit::Select( const CSelectOp &selectOp, CDmeSingleIndexedComponent *pOriginal, const CDmeSingleIndexedComponent *pNew )
  874. {
  875. if ( !pOriginal || !pNew )
  876. return false;
  877. switch ( selectOp() )
  878. {
  879. case CSelectOp::kAdd:
  880. pOriginal->Add( pNew );
  881. break;
  882. case CSelectOp::kSubtract:
  883. pOriginal->Subtract( pNew );
  884. break;
  885. case CSelectOp::kToggle:
  886. {
  887. CDmeSingleIndexedComponent *pIntersection = CreateElement< CDmeSingleIndexedComponent >( "intersection", pOriginal->GetFileId() );
  888. if ( !pIntersection )
  889. return false;
  890. CDmeSingleIndexedComponent *pNewCopy = CreateElement< CDmeSingleIndexedComponent >( "newCopy", pOriginal->GetFileId() );
  891. if ( !pNewCopy )
  892. return false;
  893. pOriginal->CopyAttributesTo( pIntersection );
  894. pIntersection->Intersection( pNew );
  895. pOriginal->Subtract( pIntersection );
  896. pNew->CopyAttributesTo( pNewCopy );
  897. pNewCopy->Subtract( pIntersection );
  898. pOriginal->Add( pNewCopy );
  899. }
  900. break;
  901. case CSelectOp::kIntersect:
  902. pOriginal->Intersection( pNew );
  903. break;
  904. case CSelectOp::kReplace:
  905. {
  906. CUtlString originalName = pOriginal->GetName();
  907. pNew->CopyAttributesTo( pOriginal );
  908. pOriginal->SetName( originalName );
  909. }
  910. break;
  911. }
  912. return true;
  913. }
  914. //-----------------------------------------------------------------------------
  915. //
  916. //-----------------------------------------------------------------------------
  917. bool CDmxEdit::SelectHalf( const CSelectOp &selectOp, const CHalfType &halfType, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  918. {
  919. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  920. if ( !pSelection )
  921. return SetErrorString( "No Selection To Manipulate" );
  922. CDmeSingleIndexedComponent *pTempSelection = CreateElement< CDmeSingleIndexedComponent >( "tempSelection", pSelection->GetFileId() );
  923. if ( !pTempSelection )
  924. return false;
  925. const bool retVal = SelectHalf( halfType, pTempSelection, pPassedMesh );
  926. if ( retVal )
  927. {
  928. Select( selectOp, pSelection, pTempSelection );
  929. }
  930. g_pDataModel->DestroyElement( pTempSelection->GetHandle() );
  931. return retVal;
  932. }
  933. //-----------------------------------------------------------------------------
  934. //
  935. //-----------------------------------------------------------------------------
  936. bool CDmxEdit::SelectHalf( const CHalfType &halfType, CDmeSingleIndexedComponent *pPassedSelection /* = NULL */, CDmeMesh *pPassedMesh /* = NULL */ )
  937. {
  938. CDmeMesh *pMesh = pPassedMesh ? pPassedMesh : m_pMesh;
  939. if ( !pMesh )
  940. return SetErrorString( "No Mesh" );
  941. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  942. if ( !pSelection )
  943. return SetErrorString( "No Selection To Manipulate" );
  944. pMesh->SelectHalfVertices( halfType() == CHalfType::kLeft ? CDmeMesh::kLeft : CDmeMesh::kRight, pSelection );
  945. return true;
  946. }
  947. //-----------------------------------------------------------------------------
  948. //
  949. //-----------------------------------------------------------------------------
  950. LUA_COMMAND(
  951. SelectHalf,
  952. "< $halfType >",
  953. "Selects all the vertices in half the mesh. Half is one of left or right. Left means the X value of the position is >= 0, right means that the X value of the position is <= 0. So it's the character's left (assuming they're staring down -z)")
  954. {
  955. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  956. const char *pHalfType = luaL_checkstring( pLuaState, 1 );
  957. if ( pHalfType && LuaFunc_s::m_dmxEdit.SelectHalf( pHalfType ) )
  958. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  959. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  960. }
  961. //-----------------------------------------------------------------------------
  962. //
  963. //-----------------------------------------------------------------------------
  964. bool CDmxEdit::GrowSelection( int nSize /* = 1 */ )
  965. {
  966. if ( !m_pMesh )
  967. return SetErrorString( "No Mesh" );
  968. m_pMesh->GrowSelection( nSize, m_pCurrentSelection, NULL );
  969. return true;
  970. }
  971. //-----------------------------------------------------------------------------
  972. //
  973. //-----------------------------------------------------------------------------
  974. LUA_COMMAND(
  975. GrowSelection,
  976. "< #size >",
  977. "Grows the current selection by the specified $size")
  978. {
  979. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  980. if ( lua_isnumber( pLuaState, 1 ) )
  981. {
  982. const int nSize = lua_tointeger( pLuaState, 1 );
  983. if ( LuaFunc_s::m_dmxEdit.GrowSelection( nSize ) )
  984. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  985. }
  986. else
  987. {
  988. if ( LuaFunc_s::m_dmxEdit.GrowSelection() )
  989. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  990. }
  991. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  992. }
  993. //-----------------------------------------------------------------------------
  994. //
  995. //-----------------------------------------------------------------------------
  996. bool CDmxEdit::ShrinkSelection( int nSize /* = 1 */ )
  997. {
  998. if ( !m_pMesh )
  999. return SetErrorString( "No Mesh" );
  1000. m_pMesh->ShrinkSelection( nSize, m_pCurrentSelection, NULL );
  1001. return true;
  1002. }
  1003. //-----------------------------------------------------------------------------
  1004. //
  1005. //-----------------------------------------------------------------------------
  1006. LUA_COMMAND(
  1007. ShrinkSelection,
  1008. "< #size >",
  1009. "Shrinks the current selection by the specified $size (integer)")
  1010. {
  1011. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1012. if ( lua_isnumber( pLuaState, 1 ) )
  1013. {
  1014. const int nSize = lua_tointeger( pLuaState, 1 );
  1015. if ( LuaFunc_s::m_dmxEdit.ShrinkSelection( nSize ) )
  1016. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1017. }
  1018. else
  1019. {
  1020. if ( LuaFunc_s::m_dmxEdit.ShrinkSelection() )
  1021. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1022. }
  1023. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1024. }
  1025. //-----------------------------------------------------------------------------
  1026. //
  1027. //-----------------------------------------------------------------------------
  1028. bool CDmxEdit::Add(
  1029. AddType addType,
  1030. const CDmxEditProxy &e,
  1031. float weight /* = 1.0f */,
  1032. float featherDistance /* = 0.0f */,
  1033. const CFalloffType &falloffType,
  1034. const CDistanceType &passedDistanceType )
  1035. {
  1036. if ( !m_pMesh )
  1037. return SetErrorString( "No Mesh" );
  1038. if ( m_pCurrentSelection->Count() == 0 )
  1039. {
  1040. LuaWarning( "No Vertices Selected, Selected ALL" );
  1041. Select( ALL );
  1042. }
  1043. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  1044. CDmeSingleIndexedComponent *pNewSelection = featherDistance > 0.0f ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), m_pCurrentSelection, NULL ) : NULL;
  1045. const bool retVal =
  1046. addType == kRaw ?
  1047. m_pMesh->AddMaskedDelta( NULL, NULL, weight, pNewSelection ? pNewSelection : m_pCurrentSelection ) :
  1048. m_pMesh->AddCorrectedMaskedDelta( NULL, NULL, weight, pNewSelection ? pNewSelection : m_pCurrentSelection ) ;
  1049. if ( pNewSelection )
  1050. {
  1051. g_pDataModel->DestroyElement( pNewSelection->GetHandle() );
  1052. }
  1053. return retVal;
  1054. }
  1055. //-----------------------------------------------------------------------------
  1056. //
  1057. //-----------------------------------------------------------------------------
  1058. bool CDmxEdit::Add(
  1059. AddType addType,
  1060. const char *pDeltaName,
  1061. float weight /* = 1.0f */,
  1062. float featherDistance /* = 0.0f */,
  1063. const CFalloffType &falloffType,
  1064. const CDistanceType &passedDistanceType )
  1065. {
  1066. if ( !Q_stricmp( "BASE", pDeltaName ) )
  1067. return Add( addType, CDmxEditProxy( *this ), weight, featherDistance, falloffType );
  1068. if ( !m_pMesh )
  1069. return SetErrorString( "No Mesh" );
  1070. if ( m_pCurrentSelection->Count() == 0 )
  1071. {
  1072. LuaWarning( "No Vertices Selected, Selecting ALL" );
  1073. Select( ALL );
  1074. }
  1075. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  1076. CDmeSingleIndexedComponent *pNewSelection = featherDistance > 0.0f ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), m_pCurrentSelection, NULL ) : NULL;
  1077. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  1078. if ( !pDelta )
  1079. return SetErrorString( "Invalid Delta \"%s\"", pDeltaName );
  1080. const bool retVal =
  1081. addType == kRaw ?
  1082. m_pMesh->AddMaskedDelta( pDelta, NULL, weight, pNewSelection ) :
  1083. m_pMesh->AddCorrectedMaskedDelta( pDelta, NULL, weight, pNewSelection );
  1084. if ( pNewSelection )
  1085. {
  1086. g_pDataModel->DestroyElement( pNewSelection->GetHandle() );
  1087. }
  1088. return retVal;
  1089. }
  1090. //-----------------------------------------------------------------------------
  1091. //
  1092. //-----------------------------------------------------------------------------
  1093. LUA_COMMAND(
  1094. Add,
  1095. "< $delta >, [ #weight ], [ #featherDistance ], < $falloffType >",
  1096. "Adds specified state to the current state of the mesh weighted and feathered by the specified #weight, #featherDistance & $falloffType. "
  1097. "$falloffType is one of \"straight\", \"spike\", \"dome\", \"bell\". Note that only the specified delta is added. "
  1098. "i.e. If Add( \"A_B\" ); is called then just A_B is added, A & B are not. See AddCorrected();" )
  1099. {
  1100. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1101. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1102. float weight = lua_isnumber( pLuaState, 2 ) ? lua_tonumber( pLuaState, 2 ) : 1.0f;
  1103. float featherDistance = lua_isnumber( pLuaState, 3 ) ? lua_tonumber( pLuaState, 3 ) : 0.0f;
  1104. const char *pFalloffType = lua_isstring( pLuaState, 4 ) ? lua_tostring( pLuaState, 4 ) : "straight";
  1105. if ( LuaFunc_s::m_dmxEdit.Add( CDmxEdit::kRaw, pString1, weight, featherDistance, pFalloffType ) )
  1106. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1107. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. //
  1111. //-----------------------------------------------------------------------------
  1112. LUA_COMMAND(
  1113. AddCorrected,
  1114. "< $delta >, [ #weight ], [ #featherDistance ], < $falloffType >",
  1115. "Same as Add() except that the corrected delta is added. i.e. If AddCorrected( \"A_B\" ); is called "
  1116. "then A, B & A_B are all added. This works similarly to SetState() whereas Add() just adds "
  1117. "the named delta." )
  1118. {
  1119. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1120. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1121. float weight = lua_isnumber( pLuaState, 2 ) ? lua_tonumber( pLuaState, 2 ) : 1.0f;
  1122. float featherDistance = lua_isnumber( pLuaState, 3 ) ? lua_tonumber( pLuaState, 3 ) : 0.0f;
  1123. const char *pFalloffType = lua_isstring( pLuaState, 4 ) ? lua_tostring( pLuaState, 4 ) : "straight";
  1124. if ( LuaFunc_s::m_dmxEdit.Add( CDmxEdit::kCorrected, pString1, weight, featherDistance, pFalloffType ) )
  1125. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1126. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1127. }
  1128. //-----------------------------------------------------------------------------
  1129. //
  1130. //-----------------------------------------------------------------------------
  1131. bool CDmxEdit::Interp( const CDmxEditProxy &e, float weight /* = 1.0f */, float featherDistance /* = 0.0f */, const CFalloffType &falloffType, const CDistanceType &passedDistanceType )
  1132. {
  1133. if ( !m_pMesh )
  1134. return SetErrorString( "No Mesh" );
  1135. if ( m_pCurrentSelection->Count() == 0 )
  1136. {
  1137. LuaWarning( "No Vertices Selected, Selecting ALL" );
  1138. Select( ALL );
  1139. }
  1140. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  1141. CDmeSingleIndexedComponent *pNewSelection = featherDistance > 0.0f ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), m_pCurrentSelection, NULL ) : NULL;
  1142. const bool retVal = m_pMesh->InterpMaskedDelta( NULL, NULL, weight, pNewSelection ? pNewSelection : m_pCurrentSelection );
  1143. if ( pNewSelection )
  1144. {
  1145. g_pDataModel->DestroyElement( pNewSelection->GetHandle() );
  1146. }
  1147. return retVal;
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. //
  1151. //-----------------------------------------------------------------------------
  1152. bool CDmxEdit::Interp( const char *pDeltaName, float weight /* = 1.0f */, float featherDistance /* = 0.0f */, const CFalloffType &falloffType, const CDistanceType &passedDistanceType )
  1153. {
  1154. if ( !Q_stricmp( "BASE", pDeltaName ) )
  1155. return Interp( CDmxEditProxy( *this ), weight, featherDistance, falloffType );
  1156. if ( !m_pMesh )
  1157. return SetErrorString( "No Mesh" );
  1158. if ( m_pCurrentSelection->Count() == 0 )
  1159. {
  1160. LuaWarning( "No Vertices Selected, Selecting ALL" );
  1161. Select( ALL );
  1162. }
  1163. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  1164. CDmeSingleIndexedComponent *pNewSelection = featherDistance > 0.0f ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), m_pCurrentSelection, NULL ) : NULL;
  1165. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  1166. if ( !pDelta )
  1167. return SetErrorString( "Invalid Delta \"%s\"", pDeltaName );
  1168. const bool retVal = m_pMesh->InterpMaskedDelta( pDelta, NULL, weight, pNewSelection ? pNewSelection : m_pCurrentSelection );
  1169. if ( pNewSelection )
  1170. {
  1171. g_pDataModel->DestroyElement( pNewSelection->GetHandle() );
  1172. }
  1173. return retVal;
  1174. }
  1175. //-----------------------------------------------------------------------------
  1176. //
  1177. //-----------------------------------------------------------------------------
  1178. LUA_COMMAND(
  1179. Interp,
  1180. "< $delta >, [ #weight ], [ #featherDistance ], < $falloffType >",
  1181. "Interpolates the current state of the mesh towards the specified state, $weight, #featherDistance and $falloffType. $falloffType is one of \"straight\", \"spike\", \"dome\", \"bell\"")
  1182. {
  1183. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1184. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1185. float weight = lua_isnumber( pLuaState, 2 ) ? lua_tonumber( pLuaState, 2 ) : 1.0f;
  1186. float featherDistance = lua_isnumber( pLuaState, 3 ) ? lua_tonumber( pLuaState, 3 ) : 0.0f;
  1187. const char *pFalloffType = lua_isstring( pLuaState, 4 ) ? lua_tostring( pLuaState, 4 ) : "straight";
  1188. if ( LuaFunc_s::m_dmxEdit.Interp( pString1, weight, featherDistance, pFalloffType ) )
  1189. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1190. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. //
  1194. //-----------------------------------------------------------------------------
  1195. bool CDmxEdit::Save( const char *pFilename, const CObjType &saveType /* = ABSOLUTE */, const char *pDeltaName /* = NULL */ )
  1196. {
  1197. if ( !pFilename )
  1198. return SetErrorString( "No Filename Specified" );
  1199. if ( !m_pRoot )
  1200. return SetErrorString( "No Dmx Root Object To Save" );
  1201. RemoveExportTags( m_pRoot, "vsDmxIO_exportTags" );
  1202. AddExportTags( m_pRoot, pFilename );
  1203. UpdateMakefile( m_pRoot );
  1204. CleanupWork();
  1205. bool retVal = false;
  1206. const int sLen = Q_strlen( pFilename );
  1207. if ( sLen > 4 && !Q_stricmp( pFilename + sLen - 4, ".dmx" ) )
  1208. {
  1209. retVal = g_p4factory->AccessFile( pFilename )->Edit();
  1210. if ( !retVal )
  1211. {
  1212. retVal = g_p4factory->AccessFile( pFilename )->Add();
  1213. }
  1214. retVal = g_pDataModel->SaveToFile( pFilename, NULL, "keyvalues2", "model", m_pRoot );
  1215. if ( !retVal )
  1216. {
  1217. SetErrorString( "Cannot Write File" );
  1218. }
  1219. }
  1220. else
  1221. {
  1222. bool absolute = true;
  1223. if ( saveType() == CObjType::kRelative )
  1224. {
  1225. // Relative
  1226. absolute = false;
  1227. }
  1228. if ( pDeltaName )
  1229. {
  1230. if ( !Q_stricmp( "base", pDeltaName ) )
  1231. {
  1232. retVal = CDmObjSerializer().WriteOBJ( pFilename, m_pRoot, false, NULL, absolute );
  1233. }
  1234. else
  1235. {
  1236. retVal = CDmObjSerializer().WriteOBJ( pFilename, m_pRoot, true, pDeltaName, absolute );
  1237. }
  1238. }
  1239. else
  1240. {
  1241. retVal = CDmObjSerializer().WriteOBJ( pFilename, m_pRoot, true, NULL, absolute );
  1242. }
  1243. }
  1244. CreateWork();
  1245. return retVal;
  1246. }
  1247. //-----------------------------------------------------------------------------
  1248. //
  1249. //-----------------------------------------------------------------------------
  1250. void CDmxEdit::UpdateMakefile( CDmElement *pRoot )
  1251. {
  1252. if ( !pRoot )
  1253. return;
  1254. CDmeDCCMakefile *pMakefile = CreateElement< CDmeDCCMakefile >( "dmxedit", pRoot->GetFileId() );
  1255. if ( !pMakefile )
  1256. return;
  1257. pRoot->SetValue( "makefile", pMakefile );
  1258. CDmrElementArray< CDmeSourceDCCFile > sources( pMakefile->GetAttribute( "sources" ) );
  1259. sources.AddToTail( CreateElement< CDmeSourceDCCFile >( m_filename, pRoot->GetFileId() ) );
  1260. }
  1261. //-----------------------------------------------------------------------------
  1262. // In winstuff.cpp
  1263. //-----------------------------------------------------------------------------
  1264. void MyGetUserName( char *pszBuf, unsigned long *pBufSiz );
  1265. void MyGetComputerName( char *pszBuf, unsigned long *pBufSiz );
  1266. //-----------------------------------------------------------------------------
  1267. //
  1268. //-----------------------------------------------------------------------------
  1269. void CDmxEdit::AddExportTags( CDmElement *pRoot, const char *pFilename )
  1270. {
  1271. if ( !pRoot )
  1272. return;
  1273. CDmElement *pExportTags = CreateElement< CDmElement >( "dmxedit_exportTags", pRoot->GetFileId() );
  1274. char szTmpBuf[ BUFSIZ ];
  1275. _strdate( szTmpBuf );
  1276. pExportTags->SetValue( "date", szTmpBuf );
  1277. _strtime( szTmpBuf );
  1278. pExportTags->SetValue( "time", szTmpBuf );
  1279. unsigned long dwSize( sizeof( szTmpBuf ) );
  1280. *szTmpBuf ='\0';
  1281. MyGetUserName( szTmpBuf, &dwSize);
  1282. pExportTags->SetValue( "user", szTmpBuf );
  1283. *szTmpBuf ='\0';
  1284. dwSize = sizeof( szTmpBuf );
  1285. MyGetComputerName( szTmpBuf, &dwSize);
  1286. pExportTags->SetValue( "machine", szTmpBuf );
  1287. pExportTags->SetValue( "app", "dmxedit" );
  1288. static const char *pChangeList = "$Change: 633871 $";
  1289. pExportTags->SetValue( "appVersion", pChangeList );
  1290. CUtlString cmdLine( "dmxedit " );
  1291. cmdLine += m_scriptFilename;
  1292. cmdLine += "";
  1293. pExportTags->SetValue( "cmdLine", cmdLine );
  1294. pExportTags->SetValue( "Load", m_filename );
  1295. pExportTags->SetValue( "Save", pFilename );
  1296. pRoot->SetValue( "dmxedit_exportTags", pExportTags );
  1297. }
  1298. //-----------------------------------------------------------------------------
  1299. //
  1300. //-----------------------------------------------------------------------------
  1301. void CDmxEdit::RemoveExportTags( CDmElement *pRoot, const char *pExportTagsName )
  1302. {
  1303. if ( !pRoot )
  1304. return;
  1305. pRoot->RemoveAttribute( pExportTagsName );
  1306. }
  1307. //-----------------------------------------------------------------------------
  1308. // Lua Wrapper For CDmeEdit::Save
  1309. //-----------------------------------------------------------------------------
  1310. LUA_COMMAND(
  1311. Save,
  1312. "[ $file.dmx | $file.obj ], [ $saveType ], [ $delta ]",
  1313. "Saves the current scene to either a single dmx file or a sequence of obj files. If $file is not specified, the filename used in the last Load() command will be used to save the file. $saveType is one of \"absolute\" or \"relative\". If not specified, \"absolute\" is assumed. If $delta is passed and the save type is OBJ, then only a single OBJ of that delta is saved. \"base\" is the base state." )
  1314. {
  1315. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1316. const char *pSave1 = lua_tostring( pLuaState, 1 );
  1317. const char *pSaveType = NULL;
  1318. if ( lua_isboolean( pLuaState, 2 ) )
  1319. {
  1320. if ( lua_toboolean( pLuaState, 2 ) )
  1321. {
  1322. pSaveType = "Absolute";
  1323. }
  1324. else
  1325. {
  1326. pSaveType = "Relative";
  1327. }
  1328. }
  1329. else if ( lua_isstring( pLuaState, 2 ) )
  1330. {
  1331. pSaveType = lua_tostring( pLuaState, 2 );
  1332. }
  1333. if ( pSave1 )
  1334. {
  1335. if ( pSaveType )
  1336. {
  1337. const char *pDeltaName = lua_isstring( pLuaState, 3 ) ? lua_tostring( pLuaState, 3 ) : NULL;
  1338. if ( LuaFunc_s::m_dmxEdit.Save( pSave1, pSaveType, pDeltaName ) )
  1339. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1340. }
  1341. else
  1342. {
  1343. if ( LuaFunc_s::m_dmxEdit.Save( pSave1 ) )
  1344. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1345. }
  1346. }
  1347. else
  1348. {
  1349. if ( LuaFunc_s::m_dmxEdit.Save() )
  1350. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1351. }
  1352. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1353. }
  1354. //-----------------------------------------------------------------------------
  1355. //
  1356. //-----------------------------------------------------------------------------
  1357. bool CDmxEdit::Merge( const char *pInFilename, const char *pOutFilename )
  1358. {
  1359. CDmElement *pRoot = NULL;
  1360. CDisableUndoScopeGuard guard0;
  1361. g_pDataModel->RestoreFromFile( pInFilename, NULL, NULL, &pRoot );
  1362. guard0.Release();
  1363. if ( !pRoot )
  1364. return SetErrorString( "Can't Load File \"%s\"", pInFilename );
  1365. g_p4factory->AccessFile( pInFilename )->Add();
  1366. bool retVal = CDmMeshUtils::Merge( m_pMesh, pRoot );
  1367. CDisableUndoScopeGuard guard1;
  1368. if ( retVal )
  1369. {
  1370. bool bPerforce = g_p4factory->AccessFile( pOutFilename )->Edit();
  1371. if ( !bPerforce )
  1372. {
  1373. bPerforce = g_p4factory->AccessFile( pOutFilename )->Add();
  1374. }
  1375. if ( !g_pDataModel->SaveToFile( pOutFilename, NULL, "keyvalues2", "model", pRoot ) )
  1376. {
  1377. retVal = false;
  1378. SetErrorString( "Can't Write File \"%s\"", pOutFilename );
  1379. }
  1380. }
  1381. else
  1382. {
  1383. SetErrorString( "Failed! \"%s\" Unchanged", pOutFilename );
  1384. }
  1385. g_pDataModel->UnloadFile( pRoot->GetFileId() );
  1386. guard1.Release();
  1387. return retVal;
  1388. }
  1389. //-----------------------------------------------------------------------------
  1390. //
  1391. //-----------------------------------------------------------------------------
  1392. LUA_COMMAND(
  1393. Merge,
  1394. "< $src.dmx >, < $dst.dmx >",
  1395. "Merges the current mesh onto the specified dmx file and saves to the second specified dmx file")
  1396. {
  1397. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1398. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1399. const char *pString2 = luaL_checkstring( pLuaState, 2 );
  1400. if ( LuaFunc_s::m_dmxEdit.Merge( pString1, pString2 ) )
  1401. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1402. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1403. }
  1404. //-----------------------------------------------------------------------------
  1405. //
  1406. //-----------------------------------------------------------------------------
  1407. bool CDmxEdit::SaveDelta( const char *pDeltaName )
  1408. {
  1409. if ( !m_pMesh )
  1410. return SetErrorString( "No Mesh" );
  1411. if ( !Q_stricmp( pDeltaName, "base" ) )
  1412. {
  1413. CDmeVertexData *pCurr = m_pMesh->GetCurrentBaseState();
  1414. if ( !pCurr )
  1415. return SetErrorString( "Couldn't Get Current Base State" );
  1416. CDmeVertexData *pBind = m_pMesh->GetBindBaseState();
  1417. if ( !pBind )
  1418. return SetErrorString( "Couldn't Get Bind Base State" );
  1419. if ( pCurr == pBind )
  1420. return SetErrorString( "Current Is Same As Bind State" );
  1421. pCurr->CopyTo( pBind );
  1422. return true;
  1423. }
  1424. CDmeVertexDeltaData *pDelta = m_pMesh->ModifyOrCreateDeltaStateFromBaseState( pDeltaName );
  1425. if ( pDelta )
  1426. return true;
  1427. return SetErrorString( "Couldn't Create New Delta State From Base State" );
  1428. }
  1429. //-----------------------------------------------------------------------------
  1430. //
  1431. //-----------------------------------------------------------------------------
  1432. LUA_COMMAND(
  1433. SaveDelta,
  1434. "< $delta >",
  1435. "Saves the current state of the mesh to the named delta state")
  1436. {
  1437. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1438. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1439. if ( LuaFunc_s::m_dmxEdit.SaveDelta( pString1 ) )
  1440. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1441. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1442. }
  1443. //-----------------------------------------------------------------------------
  1444. //
  1445. //-----------------------------------------------------------------------------
  1446. bool CDmxEdit::DeleteDelta( const delta &d )
  1447. {
  1448. if ( !m_pMesh )
  1449. return SetErrorString( "No Mesh" );
  1450. if ( !d )
  1451. return SetErrorString( "Invalid Delta" );
  1452. // Only reason it can fail is if Delta doesn't exist...
  1453. // So be silent about it
  1454. m_pMesh->DeleteDeltaState( static_cast< const char * >( d ) );
  1455. return true;
  1456. }
  1457. //-----------------------------------------------------------------------------
  1458. //
  1459. //-----------------------------------------------------------------------------
  1460. LUA_COMMAND(
  1461. DeleteDelta,
  1462. "< $delta >",
  1463. "Deletes the named delta state from the current mesh")
  1464. {
  1465. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1466. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1467. if ( LuaFunc_s::m_dmxEdit.DeleteDelta( pString1 ) )
  1468. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1469. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1470. }
  1471. //-----------------------------------------------------------------------------
  1472. //
  1473. //-----------------------------------------------------------------------------
  1474. bool CDmxEdit::RemapMaterial( int nMaterialIndex, const char *pNewMaterialName )
  1475. {
  1476. return CDmMeshUtils::RemapMaterial( m_pMesh, nMaterialIndex, pNewMaterialName );
  1477. }
  1478. //-----------------------------------------------------------------------------
  1479. //
  1480. //-----------------------------------------------------------------------------
  1481. bool CDmxEdit::RemapMaterial( const char *pOldMaterialName, const char *pNewMaterialName )
  1482. {
  1483. return CDmMeshUtils::RemapMaterial( m_pMesh, pOldMaterialName, pNewMaterialName );
  1484. }
  1485. //-----------------------------------------------------------------------------
  1486. //
  1487. //-----------------------------------------------------------------------------
  1488. LUA_COMMAND(
  1489. RemapMaterial,
  1490. "< #index | $srcMaterial >, < $dstMaterial >",
  1491. "Remaps the specified material to the new material, material can be specified by index or name")
  1492. {
  1493. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1494. const char *pString2 = luaL_checkstring( pLuaState, 2 );
  1495. // TODO: Bad error checking... but failure to remap isn't really an error
  1496. if ( lua_isnumber( pLuaState, 1 ) )
  1497. {
  1498. const int nMaterialIndex = luaL_checkinteger( pLuaState, 1 );
  1499. if ( !LuaFunc_s::m_dmxEdit.RemapMaterial( nMaterialIndex, pString2 ) )
  1500. {
  1501. LuaFunc_s::m_dmxEdit.LuaWarning( "Invalid Material Index To Remap, Couldn't Find Material %d\n", nMaterialIndex );
  1502. }
  1503. }
  1504. else
  1505. {
  1506. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1507. if ( !LuaFunc_s::m_dmxEdit.RemapMaterial( pString1, pString2 ) )
  1508. {
  1509. LuaFunc_s::m_dmxEdit.LuaWarning( "Invalid Material To Remap, Couldn't Find Material \"%s\"\n", pString1 );
  1510. }
  1511. }
  1512. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1513. }
  1514. //-----------------------------------------------------------------------------
  1515. //
  1516. //-----------------------------------------------------------------------------
  1517. bool CDmxEdit::RemoveFacesWithMaterial( const char *pMaterialName )
  1518. {
  1519. return CDmMeshUtils::RemoveFacesWithMaterial( m_pMesh, pMaterialName );
  1520. }
  1521. //-----------------------------------------------------------------------------
  1522. //
  1523. //-----------------------------------------------------------------------------
  1524. LUA_COMMAND(
  1525. RemoveFacesWithMaterial,
  1526. "< $material >",
  1527. "Removes faces from the current mesh which have the specified material")
  1528. {
  1529. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1530. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1531. if ( LuaFunc_s::m_dmxEdit.RemoveFacesWithMaterial( pString1 ) )
  1532. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1533. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1534. }
  1535. //-----------------------------------------------------------------------------
  1536. //
  1537. //-----------------------------------------------------------------------------
  1538. bool CDmxEdit::RemoveFacesWithMoreThanNVerts( int nVertexCount )
  1539. {
  1540. CDmMeshUtils::RemoveFacesWithMoreThanNVerts( m_pMesh, nVertexCount );
  1541. return true;
  1542. }
  1543. //-----------------------------------------------------------------------------
  1544. //
  1545. //-----------------------------------------------------------------------------
  1546. LUA_COMMAND(
  1547. RemoveFacesWithMoreThanNVerts,
  1548. "< #vertexCount >",
  1549. "Removes faces from the current mesh which have more than the specified number of faces")
  1550. {
  1551. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1552. const int nVertsCount = luaL_checkinteger( pLuaState, 1 );
  1553. if ( LuaFunc_s::m_dmxEdit.RemoveFacesWithMoreThanNVerts( nVertsCount ) )
  1554. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1555. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1556. }
  1557. //-----------------------------------------------------------------------------
  1558. //
  1559. //-----------------------------------------------------------------------------
  1560. bool CDmxEdit::Mirror( CAxisType axisType /* = XAXIS */ )
  1561. {
  1562. return CDmMeshUtils::Mirror( m_pMesh, axisType() );
  1563. }
  1564. //-----------------------------------------------------------------------------
  1565. //
  1566. //-----------------------------------------------------------------------------
  1567. LUA_COMMAND(
  1568. Mirror,
  1569. "[ $axis ]",
  1570. "Mirrors the mesh in the specified axis. $axis is one of \"x\", \"y\", \"z\". If $axis is not specified, \"x\" is assumed (i.e. \"x\" == mirror across YZ plane)")
  1571. {
  1572. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1573. if ( lua_isstring( pLuaState, 1 ) )
  1574. {
  1575. const char *pString1 = luaL_checkstring( pLuaState, 1 );
  1576. if ( LuaFunc_s::m_dmxEdit.Mirror( pString1 ) )
  1577. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1578. }
  1579. else
  1580. {
  1581. if ( LuaFunc_s::m_dmxEdit.Mirror() )
  1582. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1583. }
  1584. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1585. }
  1586. //-----------------------------------------------------------------------------
  1587. //
  1588. //-----------------------------------------------------------------------------
  1589. bool CDmxEdit::ComputeNormals()
  1590. {
  1591. m_pMesh->ComputeDeltaStateNormals();
  1592. return true;
  1593. }
  1594. //-----------------------------------------------------------------------------
  1595. //
  1596. //-----------------------------------------------------------------------------
  1597. LUA_COMMAND(
  1598. ComputeNormals,
  1599. "",
  1600. "Computes new smooth normals for the current mesh and all of its delta states.")
  1601. {
  1602. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1603. if ( LuaFunc_s::m_dmxEdit.ComputeNormals() )
  1604. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1605. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1606. }
  1607. //-----------------------------------------------------------------------------
  1608. //
  1609. //-----------------------------------------------------------------------------
  1610. bool CDmxEdit::ComputeWrinkles( bool bOverwrite )
  1611. {
  1612. if ( !m_pMesh )
  1613. return SetErrorString( "No Mesh" );
  1614. CDmeCombinationOperator *pCombo( FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" ) );
  1615. if ( !pCombo )
  1616. return SetErrorString( "No Combination Operator On Mesh \"%s\"", m_pMesh->GetName() );
  1617. pCombo->GenerateWrinkleDeltas( bOverwrite );
  1618. return true;
  1619. }
  1620. //-----------------------------------------------------------------------------
  1621. //
  1622. //-----------------------------------------------------------------------------
  1623. LUA_COMMAND(
  1624. ComputeWrinkles,
  1625. "[ #bOverwrite = false ]",
  1626. "Uses the current combo rules ( i.e. The wrinkleScales, via SetWrinkleScale()) to compute wrinkle delta values. "
  1627. "If #bOverwrite is false, only wrinkle data that doesn't currently exist on the mesh will be computed. "
  1628. "If #bOverwrite is true then all wrinkle data will be computed from the combo rules wiping out any existing data computed using ComputeWrinkle(). "
  1629. )
  1630. {
  1631. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1632. const bool bOverwrite = lua_isboolean( pLuaState, 1 ) ? ( lua_toboolean( pLuaState, 1 ) ? true : false ) : false;
  1633. if ( LuaFunc_s::m_dmxEdit.ComputeWrinkles( bOverwrite ) )
  1634. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1635. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1636. }
  1637. //-----------------------------------------------------------------------------
  1638. //
  1639. //-----------------------------------------------------------------------------
  1640. bool CDmxEdit::ComputeWrinkle( const char *pDeltaName, float flScale, const char *pOperation )
  1641. {
  1642. if ( !m_pMesh )
  1643. return SetErrorString( "No Mesh" );
  1644. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  1645. if ( !pDelta )
  1646. return SetErrorString( "Cannot Find Delta State \"%s\"", pDeltaName );
  1647. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  1648. if ( !pDmeCombo )
  1649. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  1650. CDmMeshUtils::WrinkleOp wrinkleOp = StringHasPrefix( pOperation, "r" ) ? CDmMeshUtils::kReplace : CDmMeshUtils::kAdd;
  1651. const int nControlCount = pDmeCombo->GetControlCount();
  1652. for ( int nControlIndex = 0; nControlIndex < nControlCount; ++nControlIndex )
  1653. {
  1654. const int nRawControlCount = pDmeCombo->GetRawControlCount( nControlIndex );
  1655. for ( int nRawControlIndex = 0; nRawControlIndex < nRawControlCount; ++nRawControlIndex )
  1656. {
  1657. if ( Q_strcmp( pDeltaName, pDmeCombo->GetRawControlName( nControlIndex, nRawControlIndex ) ) )
  1658. continue;
  1659. pDmeCombo->SetWrinkleScale( nControlIndex, pDeltaName, 1.0f );
  1660. break;
  1661. }
  1662. }
  1663. return CDmMeshUtils::CreateWrinkleDeltaFromBaseState( pDelta, flScale, wrinkleOp, m_pMesh );
  1664. }
  1665. //-----------------------------------------------------------------------------
  1666. //
  1667. //-----------------------------------------------------------------------------
  1668. LUA_COMMAND(
  1669. ComputeWrinkle,
  1670. "< $delta > [ , #scale = 1 [ , $operation ] ] ",
  1671. "Updates the wrinkle stretch/compress data for the specified delta based on the position deltas "
  1672. "for the current state scaled by the scale value. If $operation isn't specified then \"REPLACE\" "
  1673. "is assumed. $operation is one of: \"REPLACE\", \"ADD\". "
  1674. "Note that Negative (-) values enable the compress map and Positive (+) values enable the stretch map. "
  1675. "This also sets the wrinkle scale to 1"
  1676. )
  1677. {
  1678. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1679. const char *pDeltaName = luaL_checkstring( pLuaState, 1 );
  1680. const float flScale = lua_isnumber( pLuaState, 2 ) ? lua_tonumber( pLuaState, 2 ) : 1.0f;
  1681. const char *pOperation = lua_isstring( pLuaState, 3 ) ? lua_tostring( pLuaState, 3 ) : "REPLACE";
  1682. if ( LuaFunc_s::m_dmxEdit.ComputeWrinkle( pDeltaName, flScale, pOperation ) )
  1683. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1684. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1685. }
  1686. //-----------------------------------------------------------------------------
  1687. //
  1688. //-----------------------------------------------------------------------------
  1689. bool CDmxEdit::CreateDeltasFromPresets(
  1690. const char *pPresetFilename,
  1691. bool bPurge /* = true */,
  1692. const CUtlVector< CUtlString > *pPurgeAllButThese /* = NULL */,
  1693. const char *pExpressionFilename /* = NULL */ )
  1694. {
  1695. ClearPresetCache();
  1696. CachePreset( pPresetFilename, pExpressionFilename );
  1697. const bool bRetVal = CDmMeshUtils::CreateDeltasFromPresets( m_pMesh, NULL, m_presetCache, bPurge, pPurgeAllButThese );
  1698. ClearPresetCache();
  1699. return bRetVal;
  1700. }
  1701. //-----------------------------------------------------------------------------
  1702. //
  1703. //-----------------------------------------------------------------------------
  1704. LUA_COMMAND(
  1705. CreateDeltasFromPresets,
  1706. "< $preset.pre >, [ #bPurgeExisting = true ], [ { \"NoPurge1\", ... } ], [ $expression.txt ]",
  1707. "Creates a new delta state for every present defined in the specified preset file. #bPurgeExisting specifies whether existing controls should be purged. If #bPurgeExisting is true, then an optional array of deltas to not purge can be specified.")
  1708. {
  1709. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1710. const char *pPresetFilename = luaL_checkstring( pLuaState, 1 );
  1711. const bool bPurge = lua_isboolean( pLuaState, 2 ) ? lua_toboolean( pLuaState, 2 ) ? true : false : true;
  1712. const char *pExpressionFilename = lua_isstring( pLuaState, 4 ) ? lua_tostring( pLuaState, 4 ) : NULL;
  1713. CUtlVector< CUtlString > purgeAllBut;
  1714. const CUtlVector< CUtlString > *pPurgeAllBut = NULL;
  1715. if ( lua_istable( pLuaState, 3 ) )
  1716. {
  1717. pPurgeAllBut = &purgeAllBut;
  1718. // table is in the stack at index '3'
  1719. lua_pushnil( pLuaState ); //
  1720. while (lua_next( pLuaState, 3 ) != 0) {
  1721. // uses 'key' (at index -2) and 'value' (at index -1)
  1722. purgeAllBut.AddToTail( lua_tostring( pLuaState, -1 ) );
  1723. // removes 'value'; keeps 'key' for next iteration */
  1724. lua_pop( pLuaState, 1 );
  1725. }
  1726. }
  1727. if ( LuaFunc_s::m_dmxEdit.CreateDeltasFromPresets( pPresetFilename, bPurge, pPurgeAllBut, pExpressionFilename ) )
  1728. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1729. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1730. }
  1731. //-----------------------------------------------------------------------------
  1732. //
  1733. //-----------------------------------------------------------------------------
  1734. bool CDmxEdit::CachePreset( const char *pPresetFilename, const char *pExpressionFilename /* = NULL */ )
  1735. {
  1736. m_presetCache[ pPresetFilename ] = pExpressionFilename;
  1737. return true;
  1738. }
  1739. //-----------------------------------------------------------------------------
  1740. //
  1741. //-----------------------------------------------------------------------------
  1742. LUA_COMMAND(
  1743. CachePreset,
  1744. "< $preset.pre >, [ $expression.txt ]",
  1745. "Caches the specified $preset file for later processing by a call to CreateDeltasFromCachedPresets(). If an $expression.txt file pathname is specified, an expression file will be written out for this preset file" )
  1746. {
  1747. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1748. const char *pPresetFilename = luaL_checkstring( pLuaState, 1 );
  1749. const char *pExpressionFilename = lua_isstring( pLuaState, 2 ) ? lua_tostring( pLuaState, 2 ) : NULL;
  1750. if ( LuaFunc_s::m_dmxEdit.CachePreset( pPresetFilename, pExpressionFilename ) )
  1751. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1752. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1753. }
  1754. //-----------------------------------------------------------------------------
  1755. //
  1756. //-----------------------------------------------------------------------------
  1757. bool CDmxEdit::ClearPresetCache()
  1758. {
  1759. m_presetCache.Clear();
  1760. return true;
  1761. }
  1762. //-----------------------------------------------------------------------------
  1763. //
  1764. //-----------------------------------------------------------------------------
  1765. LUA_COMMAND(
  1766. ClearPresetCache,
  1767. "",
  1768. "Removes all preset filenames that have been specified via CachePreset() calls")
  1769. {
  1770. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1771. if ( LuaFunc_s::m_dmxEdit.ClearPresetCache() )
  1772. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1773. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1774. }
  1775. //-----------------------------------------------------------------------------
  1776. //
  1777. //-----------------------------------------------------------------------------
  1778. bool CDmxEdit::CreateDeltasFromCachedPresets(
  1779. bool bPurge /* = true */,
  1780. const CUtlVector< CUtlString > *pPurgeAllButThese /* = NULL */ ) const
  1781. {
  1782. return CDmMeshUtils::CreateDeltasFromPresets( m_pMesh, NULL, m_presetCache, bPurge, pPurgeAllButThese );
  1783. }
  1784. //-----------------------------------------------------------------------------
  1785. //
  1786. //-----------------------------------------------------------------------------
  1787. LUA_COMMAND(
  1788. CreateDeltasFromCachedPresets,
  1789. "[ #bPurgeExisting = true ], [ { \"NoPurge1\", ... } ]",
  1790. "Creates a new delta state for every preset defined in the preset files specified by previous CachePreset() calls. Calling this calls ClearPresetCache(). #bPurgeExisting specifies whether existing controls should be purged. If #bPurgeExisting is true, then an optional array of deltas to not purge can be specified.")
  1791. {
  1792. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1793. const bool bPurge = lua_isboolean( pLuaState, 1 ) ? lua_toboolean( pLuaState, 1 ) ? true : false : true;
  1794. CUtlVector< CUtlString > purgeAllBut;
  1795. const CUtlVector< CUtlString > *pPurgeAllBut = NULL;
  1796. if ( lua_istable( pLuaState, 2 ) )
  1797. {
  1798. pPurgeAllBut = &purgeAllBut;
  1799. // table is in the stack at index '2'
  1800. lua_pushnil( pLuaState ); //
  1801. while (lua_next( pLuaState, 2 ) != 0) {
  1802. // uses 'key' (at index -2) and 'value' (at index -1)
  1803. purgeAllBut.AddToTail( lua_tostring( pLuaState, -1 ) );
  1804. // removes 'value'; keeps 'key' for next iteration */
  1805. lua_pop( pLuaState, 1 );
  1806. }
  1807. }
  1808. bool bRetVal = LuaFunc_s::m_dmxEdit.CreateDeltasFromCachedPresets( bPurge, pPurgeAllBut );
  1809. LuaFunc_s::m_dmxEdit.ClearPresetCache();
  1810. if ( bRetVal )
  1811. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1812. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1813. }
  1814. //-----------------------------------------------------------------------------
  1815. //
  1816. //-----------------------------------------------------------------------------
  1817. bool CDmxEdit::CreateExpressionFileFromPresets( const char *pPresetFilename, const char *pExpressionFilename )
  1818. {
  1819. ClearPresetCache();
  1820. CachePreset( pPresetFilename, pExpressionFilename );
  1821. const bool bRetVal = CreateExpressionFilesFromCachedPresets();
  1822. ClearPresetCache();
  1823. return bRetVal;
  1824. }
  1825. //-----------------------------------------------------------------------------
  1826. //
  1827. //-----------------------------------------------------------------------------
  1828. LUA_COMMAND(
  1829. CreateExpressionFileFromPresets,
  1830. "< $preset.pre >, < $expression.txt >",
  1831. "Creates a faceposer expression.txt and expression.vfe file for the specified preset file")
  1832. {
  1833. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1834. const char *pPresetFilename = luaL_checkstring( pLuaState, 1 );
  1835. const char *pExpressionFilename = luaL_checkstring( pLuaState, 2 );
  1836. if ( LuaFunc_s::m_dmxEdit.CreateExpressionFileFromPresets( pPresetFilename, pExpressionFilename ) )
  1837. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1838. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1839. }
  1840. //-----------------------------------------------------------------------------
  1841. //
  1842. //-----------------------------------------------------------------------------
  1843. bool CDmxEdit::CreateExpressionFilesFromCachedPresets() const
  1844. {
  1845. bool bRetVal = true;
  1846. CDisableUndoScopeGuard sgDisableUndo;
  1847. char buf[ MAX_PATH ];
  1848. char buf1[ MAX_PATH ];
  1849. if ( !m_pMesh )
  1850. return false;
  1851. CDmeCombinationOperator *pComboOp( FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" ) );
  1852. if ( !pComboOp )
  1853. return false;
  1854. for ( int i = 0; i < m_presetCache.GetNumStrings(); ++i )
  1855. {
  1856. const char *pPresetFilename = m_presetCache.String( i );
  1857. CDmElement *pRoot = NULL;
  1858. g_p4factory->AccessFile( pPresetFilename )->Add();
  1859. g_pDataModel->RestoreFromFile( pPresetFilename, NULL, NULL, &pRoot );
  1860. if ( !pRoot )
  1861. {
  1862. bRetVal = false;
  1863. continue;
  1864. }
  1865. CDmePresetGroup *pPresetGroup = CastElement< CDmePresetGroup >( pRoot );
  1866. if ( pPresetGroup )
  1867. {
  1868. const CUtlString &expressionFilename = m_presetCache[ i ];
  1869. if ( expressionFilename.IsEmpty() )
  1870. {
  1871. Warning( "// No expression file specified for preset %s\n", pPresetFilename );
  1872. continue;
  1873. }
  1874. const char *pExpressionFilename = expressionFilename.Get();
  1875. Q_strncpy( buf, pExpressionFilename, sizeof( buf ) );
  1876. Q_SetExtension( buf, ".txt", sizeof( buf ) );
  1877. Q_ExtractFilePath( buf, buf1, sizeof( buf1 ) );
  1878. Q_FixSlashes( buf1 );
  1879. g_pFullFileSystem->CreateDirHierarchy( buf1 );
  1880. if ( !g_p4factory->AccessFile( buf )->Edit() )
  1881. {
  1882. g_p4factory->AccessFile( buf )->Add();
  1883. }
  1884. pPresetGroup->ExportToTXT( buf, NULL, pComboOp );
  1885. Q_SetExtension( buf, ".vfe", sizeof( buf ) );
  1886. Q_ExtractFilePath( buf, buf1, sizeof( buf1 ) );
  1887. Q_FixSlashes( buf1 );
  1888. g_pFullFileSystem->CreateDirHierarchy( buf1 );
  1889. if ( !g_p4factory->AccessFile( buf )->Edit() )
  1890. {
  1891. g_p4factory->AccessFile( buf )->Add();
  1892. }
  1893. pPresetGroup->ExportToVFE( buf, NULL, pComboOp );
  1894. }
  1895. g_pDataModel->UnloadFile( pRoot->GetFileId() );
  1896. }
  1897. return bRetVal;
  1898. }
  1899. //-----------------------------------------------------------------------------
  1900. //
  1901. //-----------------------------------------------------------------------------
  1902. LUA_COMMAND(
  1903. CreateExpressionFilesFromCachedPresets,
  1904. "",
  1905. "Creates a faceposer expression.txt and expression.vfe file for each of the preset/expression file pairs specified by previous CachePreset calls")
  1906. {
  1907. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1908. if ( LuaFunc_s::m_dmxEdit.CreateExpressionFilesFromCachedPresets() )
  1909. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  1910. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  1911. }
  1912. //-----------------------------------------------------------------------------
  1913. //
  1914. //-----------------------------------------------------------------------------
  1915. void ScaleDeltaPositions( const CDmrArrayConst< Vector > &bindPosData, CDmeVertexDeltaData *pDelta, float sx, float sy, float sz )
  1916. {
  1917. const int nPosIndex = pDelta->FindFieldIndex( CDmeVertexData::FIELD_POSITION );
  1918. if ( nPosIndex < 0 )
  1919. return;
  1920. CDmrArray< Vector > posData = pDelta->GetVertexData( nPosIndex );
  1921. const int nPosDataCount = posData.Count();
  1922. if ( nPosDataCount <= 0 )
  1923. return;
  1924. Vector *pPosArray = reinterpret_cast< Vector * >( alloca( nPosDataCount * sizeof( Vector ) ) );
  1925. for ( int j = 0; j < nPosDataCount; ++j )
  1926. {
  1927. const Vector &s = posData.Get( j );
  1928. Vector &d = pPosArray[ j ];
  1929. d.x = s.x * sx;
  1930. d.y = s.y * sy;
  1931. d.z = s.z * sz;
  1932. }
  1933. posData.SetMultiple( 0, nPosDataCount, pPosArray );
  1934. }
  1935. //-----------------------------------------------------------------------------
  1936. //
  1937. //-----------------------------------------------------------------------------
  1938. bool CDmxEdit::Scale( float sx, float sy, float sz )
  1939. {
  1940. int nArraySize = 0;
  1941. Vector *pPosArray = NULL;
  1942. const int nBaseStateCount = m_pMesh->BaseStateCount();
  1943. for ( int i = 0; i < nBaseStateCount; ++i )
  1944. {
  1945. CDmeVertexData *pBase = m_pMesh->GetBaseState( i );
  1946. const int nPosIndex = pBase->FindFieldIndex( CDmeVertexData::FIELD_POSITION );
  1947. if ( nPosIndex < 0 )
  1948. continue;
  1949. CDmrArray< Vector > posData = pBase->GetVertexData( nPosIndex );
  1950. const int nPosDataCount = posData.Count();
  1951. if ( nPosDataCount <= 0 )
  1952. continue;
  1953. if ( nArraySize < nPosDataCount || pPosArray == NULL )
  1954. {
  1955. pPosArray = reinterpret_cast< Vector * >( alloca( nPosDataCount * sizeof( Vector ) ) );
  1956. if ( pPosArray )
  1957. {
  1958. nArraySize = nPosDataCount;
  1959. }
  1960. }
  1961. if ( nArraySize < nPosDataCount )
  1962. continue;
  1963. for ( int j = 0; j < nPosDataCount; ++j )
  1964. {
  1965. const Vector &s = posData.Get( j );
  1966. Vector &d = pPosArray[ j ];
  1967. d.x = s.x * sx;
  1968. d.y = s.y * sy;
  1969. d.z = s.z * sz;
  1970. }
  1971. posData.SetMultiple( 0, nPosDataCount, pPosArray );
  1972. }
  1973. {
  1974. CDmeVertexData *pBind = m_pMesh->GetBindBaseState();
  1975. const int nPosIndex = pBind ? pBind->FindFieldIndex( CDmeVertexData::FIELD_POSITION ) : -1;
  1976. if ( !pBind || nPosIndex < 0 )
  1977. {
  1978. Warning( "// Can't Scale Deltas!\n" );
  1979. return false;
  1980. }
  1981. const CDmrArrayConst< Vector > posData = pBind->GetVertexData( nPosIndex );
  1982. const int nDeltaStateCount = m_pMesh->DeltaStateCount();
  1983. for ( int i = 0; i < nDeltaStateCount; ++i )
  1984. {
  1985. ScaleDeltaPositions( posData, m_pMesh->GetDeltaState( i ), sx, sy, sz );
  1986. }
  1987. }
  1988. return true;
  1989. }
  1990. //-----------------------------------------------------------------------------
  1991. //
  1992. //-----------------------------------------------------------------------------
  1993. LUA_COMMAND(
  1994. Scale,
  1995. "< #xScale >, [ #yScale, #zScale ]",
  1996. "Scales the position values by the specified amount. If only 1 scale value is supplied a uniform scale in all three dimensions is applied")
  1997. {
  1998. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  1999. const float sx = luaL_checknumber( pLuaState, 1 );
  2000. float sy = sx;
  2001. float sz = sx;
  2002. if ( lua_isnumber( pLuaState, 2 ) && lua_isnumber( pLuaState, 3 ) )
  2003. {
  2004. sy = lua_tonumber( pLuaState, 2 );
  2005. sz = lua_tonumber( pLuaState, 3 );
  2006. }
  2007. if ( LuaFunc_s::m_dmxEdit.Scale( sx, sy, sz ) )
  2008. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2009. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2010. }
  2011. //-----------------------------------------------------------------------------
  2012. //
  2013. //-----------------------------------------------------------------------------
  2014. bool CDmxEdit::SetDistanceType( const CDistanceType &distanceType )
  2015. {
  2016. m_distanceType = distanceType;
  2017. return true;
  2018. }
  2019. //-----------------------------------------------------------------------------
  2020. //
  2021. //-----------------------------------------------------------------------------
  2022. LUA_COMMAND(
  2023. SetDistanceType,
  2024. "< $distanceType >",
  2025. "Sets the way distances will be interpreted after the command. $distanceType is one of \"absolute\" or \"relative\". By default distances are \"absolute\". All functions that work with distances (Add, Interp and Translate) work on the currently selected vertices. \"absolute\" means use the distance as that number of units, \"relative\" means use the distance as a scale of the radius of the bounding sphere of the selected vertices.")
  2026. {
  2027. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2028. const char *pDistanceType = luaL_checkstring( pLuaState, 1 );
  2029. if ( LuaFunc_s::m_dmxEdit.SetDistanceType( pDistanceType ) )
  2030. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2031. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2032. }
  2033. //-----------------------------------------------------------------------------
  2034. //
  2035. //-----------------------------------------------------------------------------
  2036. bool CDmxEdit::Translate(
  2037. Vector t,
  2038. float featherDistance,
  2039. const CFalloffType &falloffType,
  2040. const CDistanceType &passedDistanceType,
  2041. CDmeMesh *pPassedMesh,
  2042. CDmeVertexData *pPassedBase,
  2043. CDmeSingleIndexedComponent *pPassedSelection )
  2044. {
  2045. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  2046. CDmeMesh *pMesh = pPassedMesh ? pPassedMesh : m_pMesh;
  2047. if ( !m_pMesh )
  2048. return SetErrorString( "No Mesh" );
  2049. CDmeVertexData *pBase = pPassedBase ? pPassedBase : pMesh->GetCurrentBaseState();
  2050. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  2051. CDmeSingleIndexedComponent *pTmpSelection = NULL;
  2052. if ( !pSelection || pSelection->Count() == 0 )
  2053. {
  2054. pTmpSelection = CreateElement< CDmeSingleIndexedComponent >( "__selectAll", DMFILEID_INVALID );
  2055. pSelection = pTmpSelection;
  2056. pMesh->SelectAllVertices( pSelection );
  2057. }
  2058. CDmeSingleIndexedComponent *pNewSelection = ( pSelection && featherDistance > 0.0f ) ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), pSelection, NULL ) : pSelection;
  2059. int nArraySize = 0;
  2060. Vector *pPosArray = NULL;
  2061. const int nPosIndex = pBase->FindFieldIndex( CDmeVertexData::FIELD_POSITION );
  2062. if ( nPosIndex < 0 )
  2063. return false;
  2064. CDmrArray< Vector > posData = pBase->GetVertexData( nPosIndex );
  2065. const int nPosDataCount = posData.Count();
  2066. if ( nPosDataCount <= 0 )
  2067. return false;
  2068. if ( nArraySize < nPosDataCount || pPosArray == NULL )
  2069. {
  2070. pPosArray = reinterpret_cast< Vector * >( alloca( nPosDataCount * sizeof( Vector ) ) );
  2071. if ( pPosArray )
  2072. {
  2073. nArraySize = nPosDataCount;
  2074. }
  2075. }
  2076. if ( nArraySize < nPosDataCount )
  2077. return false;
  2078. if ( distanceType() == CDmeMesh::DIST_RELATIVE )
  2079. {
  2080. Vector vCenter;
  2081. float fRadius;
  2082. pMesh->GetBoundingSphere( vCenter, fRadius, pBase, pSelection );
  2083. t *= fRadius;
  2084. }
  2085. if ( pNewSelection )
  2086. {
  2087. memcpy( pPosArray, posData.Base(), nPosDataCount * sizeof( Vector ) );
  2088. const int nSelectionCount = pNewSelection->Count();
  2089. int nIndex;
  2090. float fWeight;
  2091. for ( int j = 0; j < nSelectionCount; ++j )
  2092. {
  2093. pNewSelection->GetComponent( j, nIndex, fWeight );
  2094. const Vector &s = posData.Get( nIndex );
  2095. Vector &d = pPosArray[ nIndex ];
  2096. d = s + t * fWeight;
  2097. }
  2098. }
  2099. else
  2100. {
  2101. for ( int j = 0; j < nPosDataCount; ++j )
  2102. {
  2103. const Vector &s = posData.Get( j );
  2104. Vector &d = pPosArray[ j ];
  2105. d = s + t;
  2106. }
  2107. }
  2108. posData.SetMultiple( 0, nPosDataCount, pPosArray );
  2109. return true;
  2110. }
  2111. //-----------------------------------------------------------------------------
  2112. //
  2113. //-----------------------------------------------------------------------------
  2114. LUA_COMMAND(
  2115. Translate,
  2116. "< #xTranslate, #yTranslate, #zTranslate >, [ #falloffDistance, $falloffType ]",
  2117. "Translates the selected vertices of the mesh by the specified amount")
  2118. {
  2119. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2120. const float tx = luaL_checknumber( pLuaState, 1 );
  2121. const float ty = luaL_checknumber( pLuaState, 2 );
  2122. const float tz = luaL_checknumber( pLuaState, 3 );
  2123. if ( lua_gettop( pLuaState ) < 4 )
  2124. {
  2125. if ( LuaFunc_s::m_dmxEdit.Translate( Vector( tx, ty, tz ) ) )
  2126. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2127. }
  2128. else
  2129. {
  2130. float flFeatherDistance = 0.0f;
  2131. const char *pFalloffType = "STRAIGHT";
  2132. const char *pDistanceType = "DEFAULT";
  2133. if ( lua_gettop( pLuaState ) == 4 && lua_isstring( pLuaState, 4 ) )
  2134. {
  2135. pDistanceType = lua_tostring( pLuaState, 4 );
  2136. }
  2137. else
  2138. {
  2139. flFeatherDistance = lua_isnumber( pLuaState, 4 ) ? lua_tonumber( pLuaState, 4 ) : flFeatherDistance;
  2140. pFalloffType = lua_isstring( pLuaState, 5 ) ? lua_tostring( pLuaState, 5 ) : pFalloffType;
  2141. pDistanceType = lua_isstring( pLuaState, 6 ) ? lua_tostring( pLuaState, 6 ) : pDistanceType;
  2142. }
  2143. if ( LuaFunc_s::m_dmxEdit.Translate( Vector( tx, ty, tz ), flFeatherDistance, pFalloffType, pDistanceType ) )
  2144. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2145. }
  2146. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2147. }
  2148. //-----------------------------------------------------------------------------
  2149. //
  2150. //-----------------------------------------------------------------------------
  2151. bool CDmxEdit::Rotate(
  2152. Vector r, // X, Y, Z Euler angles in degrees specified by user
  2153. Vector o, // Offset
  2154. float featherDistance /* = 0.0f */,
  2155. const CFalloffType &falloffType /* = STRAIGHT */,
  2156. const CDistanceType &passedDistanceType /* = DIST_DEFAULT */,
  2157. CDmeMesh *pPassedMesh /* = NULL */,
  2158. CDmeVertexData *pPassedBase /* = NULL */,
  2159. CDmeSingleIndexedComponent *pPassedSelection /* = NULL */ )
  2160. {
  2161. const CDistanceType &distanceType = passedDistanceType() == CDmeMesh::DIST_DEFAULT ? m_distanceType : passedDistanceType;
  2162. CDmeMesh *pMesh = pPassedMesh ? pPassedMesh : m_pMesh;
  2163. if ( !m_pMesh )
  2164. return SetErrorString( "No Mesh" );
  2165. CDmeVertexData *pBase = pPassedBase ? pPassedBase : pMesh->GetCurrentBaseState();
  2166. CDmeSingleIndexedComponent *pSelection = pPassedSelection ? pPassedSelection : m_pCurrentSelection;
  2167. CDmeSingleIndexedComponent *pTmpSelection = NULL;
  2168. if ( !pSelection || pSelection->Count() == 0 )
  2169. {
  2170. pTmpSelection = CreateElement< CDmeSingleIndexedComponent >( "__selectAll", DMFILEID_INVALID );
  2171. pSelection = pTmpSelection;
  2172. pMesh->SelectAllVertices( pSelection );
  2173. }
  2174. CDmeSingleIndexedComponent *pNewSelection = ( pSelection && featherDistance > 0.0f ) ? m_pMesh->FeatherSelection( featherDistance, falloffType(), distanceType(), pSelection, NULL ) : pSelection;
  2175. int nArraySize = 0;
  2176. Vector *pPosArray = NULL;
  2177. const int nPosIndex = pBase->FindFieldIndex( CDmeVertexData::FIELD_POSITION );
  2178. if ( nPosIndex < 0 )
  2179. return false;
  2180. CDmrArray< Vector > posData = pBase->GetVertexData( nPosIndex );
  2181. const int nPosDataCount = posData.Count();
  2182. if ( nPosDataCount <= 0 )
  2183. return false;
  2184. if ( nArraySize < nPosDataCount || pPosArray == NULL )
  2185. {
  2186. pPosArray = reinterpret_cast< Vector * >( alloca( nPosDataCount * sizeof( Vector ) ) );
  2187. if ( pPosArray )
  2188. {
  2189. nArraySize = nPosDataCount;
  2190. }
  2191. }
  2192. if ( nArraySize < nPosDataCount )
  2193. return false;
  2194. Vector vCenter;
  2195. float fRadius;
  2196. pMesh->GetBoundingSphere( vCenter, fRadius, pBase, pSelection );
  2197. if ( distanceType() == CDmeMesh::DIST_RELATIVE )
  2198. {
  2199. r *= fRadius;
  2200. }
  2201. VectorAdd( vCenter, o, vCenter );
  2202. matrix3x4_t rpMat;
  2203. SetIdentityMatrix( rpMat );
  2204. PositionMatrix( vCenter, rpMat );
  2205. matrix3x4_t rpiMat;
  2206. SetIdentityMatrix( rpiMat );
  2207. PositionMatrix( -vCenter, rpiMat );
  2208. matrix3x4_t rMat;
  2209. SetIdentityMatrix( rMat );
  2210. if ( pNewSelection )
  2211. {
  2212. memcpy( pPosArray, posData.Base(), nPosDataCount * sizeof( Vector ) );
  2213. const int nSelectionCount = pNewSelection->Count();
  2214. int nIndex;
  2215. float fWeight;
  2216. for ( int j = 0; j < nSelectionCount; ++j )
  2217. {
  2218. pNewSelection->GetComponent( j, nIndex, fWeight );
  2219. const Vector &s = posData.Get( nIndex );
  2220. Vector &d = pPosArray[ nIndex ];
  2221. AngleMatrix( RadianEuler( DEG2RAD( r.x * fWeight ), DEG2RAD( r.y * fWeight ), DEG2RAD( r.z * fWeight ) ), rMat );
  2222. ConcatTransforms( rpMat, rMat, rMat );
  2223. ConcatTransforms( rMat, rpiMat, rMat );
  2224. VectorTransform( s, rMat, d );
  2225. }
  2226. }
  2227. else
  2228. {
  2229. AngleMatrix( RadianEuler( DEG2RAD( r.x ), DEG2RAD( r.y ), DEG2RAD( r.z ) ), rMat );
  2230. ConcatTransforms( rpMat, rMat, rMat );
  2231. ConcatTransforms( rMat, rpiMat, rMat );
  2232. for ( int j = 0; j < nPosDataCount; ++j )
  2233. {
  2234. const Vector &s = posData.Get( j );
  2235. Vector &d = pPosArray[ j ];
  2236. VectorTransform( s, rMat, d );
  2237. }
  2238. }
  2239. posData.SetMultiple( 0, nPosDataCount, pPosArray );
  2240. return true;
  2241. }
  2242. //-----------------------------------------------------------------------------
  2243. //
  2244. //-----------------------------------------------------------------------------
  2245. LUA_COMMAND(
  2246. Rotate,
  2247. "< #xRotate, #yRotate, #zRotate >, [ #xOffset, #yOffset, #zOffset ], [ #falloffDistance, $falloffType ]",
  2248. "Rotates the current selection around the center of the selection by the specified Euler angles (in degrees). The optional offset is added to the computed pivot point.")
  2249. {
  2250. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2251. const float rx = luaL_checknumber( pLuaState, 1 );
  2252. const float ry = luaL_checknumber( pLuaState, 2 );
  2253. const float rz = luaL_checknumber( pLuaState, 3 );
  2254. if ( lua_gettop( pLuaState ) < 4 )
  2255. {
  2256. if ( LuaFunc_s::m_dmxEdit.Rotate( Vector( rx, ry, rz ), Vector( 0.0f, 0.0f, 0.0f ) ) )
  2257. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2258. }
  2259. else
  2260. {
  2261. const float ox = luaL_checknumber( pLuaState, 4 );
  2262. const float oy = luaL_checknumber( pLuaState, 5 );
  2263. const float oz = luaL_checknumber( pLuaState, 6 );
  2264. if ( lua_gettop( pLuaState ) < 7 )
  2265. {
  2266. if ( LuaFunc_s::m_dmxEdit.Rotate( Vector( rx, ry, rz ), Vector( ox, oy, oz ) ) )
  2267. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2268. }
  2269. else
  2270. {
  2271. float flFeatherDistance = 0.0f;
  2272. const char *pFalloffType = "STRAIGHT";
  2273. const char *pDistanceType = "DEFAULT";
  2274. flFeatherDistance = lua_isnumber( pLuaState, 7 ) ? lua_tonumber( pLuaState, 7 ) : flFeatherDistance;
  2275. pFalloffType = lua_isstring( pLuaState, 8 ) ? lua_tostring( pLuaState, 8 ) : pFalloffType;
  2276. pDistanceType = lua_isstring( pLuaState, 9 ) ? lua_tostring( pLuaState, 9 ) : pDistanceType;
  2277. if ( LuaFunc_s::m_dmxEdit.Rotate( Vector( rx, ry, rz ), Vector( ox, oy, oz ), flFeatherDistance, pFalloffType, pDistanceType ) )
  2278. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2279. }
  2280. }
  2281. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2282. }
  2283. //-----------------------------------------------------------------------------
  2284. //
  2285. //-----------------------------------------------------------------------------
  2286. LUA_COMMAND(
  2287. FileExists,
  2288. "< $filename >",
  2289. "Returns true if the given filename exists on disk, false otherwise")
  2290. {
  2291. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2292. const char *pFilename = luaL_checkstring( pLuaState, 1 );
  2293. if ( g_pFullFileSystem->FileExists( pFilename ) )
  2294. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2295. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState, "File \"%s\" Doesn't Exist", pFilename );
  2296. }
  2297. //-----------------------------------------------------------------------------
  2298. // If it finds a duplicate control name, reports an error message and returns it found one
  2299. //-----------------------------------------------------------------------------
  2300. bool HasDuplicateControlName(
  2301. CDmeCombinationOperator *pDmeCombo,
  2302. const char *pControlName,
  2303. CUtlVector< const char * > &retiredControlNames )
  2304. {
  2305. int i;
  2306. int nRetiredControlNameCount = retiredControlNames.Count();
  2307. for ( i = 0; i < nRetiredControlNameCount; ++i )
  2308. {
  2309. if ( !Q_stricmp( retiredControlNames[i], pControlName ) )
  2310. break;
  2311. }
  2312. if ( i == nRetiredControlNameCount )
  2313. {
  2314. if ( pDmeCombo->FindControlIndex( pControlName ) >= 0 )
  2315. return true;
  2316. }
  2317. return false;
  2318. }
  2319. //-----------------------------------------------------------------------------
  2320. //
  2321. //-----------------------------------------------------------------------------
  2322. bool CDmxEdit::GroupControls( const char *pGroupName, CUtlVector< const char * > &rawControlNames )
  2323. {
  2324. if ( !m_pMesh )
  2325. return SetErrorString( "No Mesh" );
  2326. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2327. if ( !pDmeCombo )
  2328. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2329. // Loop through controls to see if any are already group controls, warn and remove
  2330. CUtlVector< const char * > validControlNames;
  2331. bool bStereo = false;
  2332. bool bEyelid = false;
  2333. for ( int i = 0; i < rawControlNames.Count(); ++i )
  2334. {
  2335. ControlIndex_t nControlIndex = pDmeCombo->FindControlIndex( rawControlNames[ i ] );
  2336. if ( nControlIndex < 0 )
  2337. {
  2338. LuaWarning( "Control \"%s\" Doesn't Exist, Ignoring", pGroupName );
  2339. continue;
  2340. }
  2341. if ( pDmeCombo->GetRawControlCount( nControlIndex ) > 1 )
  2342. {
  2343. LuaWarning( "Control \"%s\" Isn't A Raw Control, Ignoring", pGroupName );
  2344. continue;
  2345. }
  2346. validControlNames.AddToTail( rawControlNames[ i ] );
  2347. if ( pDmeCombo->IsStereoControl( nControlIndex ) )
  2348. {
  2349. bStereo = true;
  2350. }
  2351. if ( pDmeCombo->IsEyelidControl( nControlIndex ) )
  2352. {
  2353. bEyelid = true;
  2354. }
  2355. }
  2356. if ( HasDuplicateControlName( pDmeCombo, pGroupName, validControlNames ) )
  2357. return SetErrorString( "Duplicate Control \"%s\" Found", pGroupName );
  2358. if ( validControlNames.Count() <= 0 )
  2359. return SetErrorString( "No Valid Controls Specified" );
  2360. // Remove the old controls
  2361. for ( int i = 0; i < validControlNames.Count(); ++i )
  2362. {
  2363. pDmeCombo->RemoveControl( validControlNames[i] );
  2364. }
  2365. // Create new control
  2366. ControlIndex_t nNewControl = pDmeCombo->FindOrCreateControl( pGroupName, bStereo );
  2367. pDmeCombo->SetEyelidControl( nNewControl, bEyelid );
  2368. for ( int i = 0; i < validControlNames.Count(); ++i )
  2369. {
  2370. pDmeCombo->AddRawControl( nNewControl, validControlNames[i] );
  2371. }
  2372. return true;
  2373. }
  2374. //-----------------------------------------------------------------------------
  2375. //
  2376. //-----------------------------------------------------------------------------
  2377. LUA_COMMAND(
  2378. GroupControls,
  2379. "< $groupName > [ , $rawControlName ... ]",
  2380. "Groups the specified raw controls under a common group control. If the group control already exists, the raw controls are added to it" )
  2381. {
  2382. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2383. const char *pGroupName = luaL_checkstring( pLuaState, 1 );
  2384. CUtlVector< const char * > rawControlNames;
  2385. for ( int i = 2; i <= lua_gettop( pLuaState ); ++i )
  2386. {
  2387. if ( !lua_isstring( pLuaState, i ) )
  2388. break;
  2389. rawControlNames.AddToTail( lua_tostring( pLuaState, i ) );
  2390. }
  2391. if ( rawControlNames.Count() <= 0 )
  2392. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState, "No Raw Controls Specified" );
  2393. if ( LuaFunc_s::m_dmxEdit.GroupControls( pGroupName, rawControlNames ) )
  2394. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2395. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2396. }
  2397. //-----------------------------------------------------------------------------
  2398. //
  2399. //-----------------------------------------------------------------------------
  2400. bool CDmxEdit::ReorderControls( CUtlVector< CUtlString > &controlNames )
  2401. {
  2402. if ( !m_pMesh )
  2403. return SetErrorString( "No Mesh" );
  2404. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2405. if ( !pDmeCombo )
  2406. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2407. // Loop through controls to see if any are already group controls, warn and remove
  2408. CUtlVector< const char * > validControlNames;
  2409. for ( int i = 0; i < controlNames.Count(); ++i )
  2410. {
  2411. ControlIndex_t nControlIndex = pDmeCombo->FindControlIndex( controlNames[ i ] );
  2412. if ( nControlIndex < 0 )
  2413. {
  2414. LuaWarning( "Control \"%s\" doesn't exist, ignoring", controlNames[ i ].Get() );
  2415. continue;
  2416. }
  2417. validControlNames.AddToTail( controlNames[ i ] );
  2418. }
  2419. if ( validControlNames.Count() <= 0 )
  2420. return SetErrorString( "No Valid Controls Specified" );
  2421. for ( int i = 0; i < validControlNames.Count(); ++i )
  2422. {
  2423. pDmeCombo->MoveControlBefore( validControlNames[ i ], pDmeCombo->GetControlName( i ) );
  2424. }
  2425. return true;
  2426. }
  2427. //-----------------------------------------------------------------------------
  2428. //
  2429. //-----------------------------------------------------------------------------
  2430. LUA_COMMAND(
  2431. ReorderControls,
  2432. "< $controlName [ , ... ] >",
  2433. "Reorders the controls in the specified order. "
  2434. "The specified controls will be moved to the front of the list of controls. "
  2435. "Not all of the controls need to be specified. "
  2436. "Unspecified controls will be left in the order they already are after the specified controls " )
  2437. {
  2438. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2439. CUtlVector< CUtlString > controlNames;
  2440. for ( int i = 1; i <= lua_gettop( pLuaState ); ++i )
  2441. {
  2442. if ( lua_istable( pLuaState, i ) )
  2443. {
  2444. const int nCount = luaL_getn( pLuaState, i );
  2445. for ( int j = 1; j <= nCount; ++j )
  2446. {
  2447. lua_rawgeti( pLuaState, i, j );
  2448. if ( lua_isstring( pLuaState, -1 ) )
  2449. {
  2450. controlNames.AddToTail( lua_tostring( pLuaState, -1 ) );
  2451. }
  2452. lua_pop( pLuaState, 1 );
  2453. }
  2454. continue;
  2455. }
  2456. if ( !lua_isstring( pLuaState, i ) )
  2457. continue;
  2458. controlNames.AddToTail( lua_tostring( pLuaState, i ) );
  2459. }
  2460. if ( controlNames.Count() <= 0 )
  2461. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState, "No Controls Specified" );
  2462. if ( LuaFunc_s::m_dmxEdit.ReorderControls( controlNames ) )
  2463. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2464. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2465. }
  2466. //-----------------------------------------------------------------------------
  2467. //
  2468. //-----------------------------------------------------------------------------
  2469. bool CDmxEdit::AddDominationRule( CUtlVector< CUtlString > &dominators, CUtlVector< CUtlString > &supressed )
  2470. {
  2471. if ( !m_pMesh )
  2472. return SetErrorString( "No Mesh" );
  2473. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2474. if ( !pDmeCombo )
  2475. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2476. CUtlVector< const char * > tmpDominators;
  2477. for ( int i = 0; i < dominators.Count(); ++i )
  2478. {
  2479. tmpDominators.AddToTail( dominators[ i ].Get() );
  2480. }
  2481. CUtlVector< const char * > tmpSupressed;
  2482. for ( int i = 0; i < supressed.Count(); ++i )
  2483. {
  2484. tmpSupressed.AddToTail( supressed[ i ].Get() );
  2485. }
  2486. pDmeCombo->AddDominationRule(
  2487. tmpDominators.Count(), ( const char ** )tmpDominators.Base(),
  2488. tmpSupressed.Count(), ( const char ** )tmpSupressed.Base() );
  2489. return true;
  2490. }
  2491. //-----------------------------------------------------------------------------
  2492. //
  2493. //-----------------------------------------------------------------------------
  2494. LUA_COMMAND(
  2495. AddDominationRule,
  2496. "{ < $dominator > [ , ... ] }, { < $supressed > [ , ... ] }",
  2497. "Create a domination rule. A lua array of strings is passed as the 1st argument which are "
  2498. "the controls which are the dominators and a lua array of strings is passed as the 2nd "
  2499. "argument which are the controls to be supressed by the dominators."
  2500. "e.g. AddDominationRule( { \"foo\" }, { \"bar\", \"bob\" } );" )
  2501. {
  2502. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2503. CUtlVector< CUtlString > dominators;
  2504. if ( lua_istable( pLuaState, 1 ) )
  2505. {
  2506. const int nCount = luaL_getn( pLuaState, 1 );
  2507. for ( int i = 1; i <= nCount; ++i )
  2508. {
  2509. lua_rawgeti( pLuaState, 1, i );
  2510. if ( lua_isstring( pLuaState, -1 ) )
  2511. {
  2512. dominators.AddToTail( lua_tostring( pLuaState, -1 ) );
  2513. }
  2514. lua_pop( pLuaState, 1 );
  2515. }
  2516. }
  2517. if ( dominators.Count() <= 0 )
  2518. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState, "No Dominator Controls Specified" );
  2519. CUtlVector< CUtlString > supressed;
  2520. if ( lua_istable( pLuaState, 2 ) )
  2521. {
  2522. const int nCount = luaL_getn( pLuaState, 2 );
  2523. for ( int i = 1; i <= nCount; ++i )
  2524. {
  2525. lua_rawgeti( pLuaState, 2, i );
  2526. if ( lua_isstring( pLuaState, -1 ) )
  2527. {
  2528. supressed.AddToTail( lua_tostring( pLuaState, -1 ) );
  2529. }
  2530. lua_pop( pLuaState, 1 );
  2531. }
  2532. }
  2533. if ( supressed.Count() <= 0 )
  2534. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState, "No Supressed Controls Specified" );
  2535. if ( LuaFunc_s::m_dmxEdit.AddDominationRule( dominators, supressed ) )
  2536. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2537. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2538. }
  2539. //-----------------------------------------------------------------------------
  2540. //
  2541. //-----------------------------------------------------------------------------
  2542. bool CDmxEdit::SetStereoControl( const char *pControlName, bool bStereo )
  2543. {
  2544. if ( !m_pMesh )
  2545. return SetErrorString( "No Mesh" );
  2546. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2547. if ( !pDmeCombo )
  2548. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2549. const ControlIndex_t nControlIndex = pDmeCombo->FindControlIndex( pControlName );
  2550. if ( nControlIndex < 0 )
  2551. return SetErrorString( "No Control Named \"%s\" On DmeCombinationOperator \"%s\" On Mesh \"%s\"", pControlName, pDmeCombo->GetName(), m_pMesh->GetName() );
  2552. pDmeCombo->SetStereoControl( nControlIndex, bStereo );
  2553. return true;
  2554. }
  2555. //-----------------------------------------------------------------------------
  2556. //
  2557. //-----------------------------------------------------------------------------
  2558. LUA_COMMAND(
  2559. SetStereoControl,
  2560. " < $controlName > [ , < true | false > ]",
  2561. "Sets the specified morph control to be stereo if the 2nd argument is true."
  2562. "If the 2nd argument is omitted, true is assumed" )
  2563. {
  2564. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2565. const char *pControlName = luaL_checkstring( pLuaState, 1 );
  2566. const bool bStereo = lua_isboolean( pLuaState, 2 ) ? lua_toboolean( pLuaState, 2 ) != 0 : true;
  2567. if ( LuaFunc_s::m_dmxEdit.SetStereoControl( pControlName, bStereo ) )
  2568. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2569. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2570. }
  2571. //-----------------------------------------------------------------------------
  2572. //
  2573. //-----------------------------------------------------------------------------
  2574. bool CDmxEdit::SetEyelidControl( const char *pControlName, bool bEyelid )
  2575. {
  2576. if ( !m_pMesh )
  2577. return SetErrorString( "No Mesh" );
  2578. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2579. if ( !pDmeCombo )
  2580. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2581. const ControlIndex_t nControlIndex = pDmeCombo->FindControlIndex( pControlName );
  2582. if ( nControlIndex < 0 )
  2583. return SetErrorString( "No Control Named \"%s\" On DmeCombinationOperator \"%s\" On Mesh \"%s\"", pControlName, pDmeCombo->GetName(), m_pMesh->GetName() );
  2584. pDmeCombo->SetEyelidControl( nControlIndex, bEyelid );
  2585. return true;
  2586. }
  2587. //-----------------------------------------------------------------------------
  2588. //
  2589. //-----------------------------------------------------------------------------
  2590. LUA_COMMAND(
  2591. SetEyelidControl,
  2592. " < $controlName > [ , < true | false > ]",
  2593. "Sets the specified morph control to be an eyelid control if the 2nd argument is true."
  2594. "If the 2nd argument is omitted, true is assumed" )
  2595. {
  2596. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2597. const char *pControlName = luaL_checkstring( pLuaState, 1 );
  2598. const bool bStereo = lua_isboolean( pLuaState, 2 ) ? lua_toboolean( pLuaState, 2 ) != 0 : true;
  2599. if ( LuaFunc_s::m_dmxEdit.SetEyelidControl( pControlName, bStereo ) )
  2600. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2601. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2602. }
  2603. //-----------------------------------------------------------------------------
  2604. //
  2605. //-----------------------------------------------------------------------------
  2606. float CDmxEdit::MaxDeltaDistance( const char *pDeltaName )
  2607. {
  2608. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  2609. if ( !pDelta)
  2610. return 0.0f;
  2611. float fSqMaxDelta = 0.0f;
  2612. float fTmpSqLength;
  2613. const CUtlVector< Vector > &positions = pDelta->GetPositionData();
  2614. for ( int i = 0; i < positions.Count(); ++i )
  2615. {
  2616. fTmpSqLength = positions[ i ].LengthSqr();
  2617. if ( fTmpSqLength < fSqMaxDelta )
  2618. continue;
  2619. fSqMaxDelta = fTmpSqLength;
  2620. }
  2621. return sqrt( fSqMaxDelta );
  2622. }
  2623. //-----------------------------------------------------------------------------
  2624. //
  2625. //-----------------------------------------------------------------------------
  2626. LUA_COMMAND(
  2627. MaxDeltaDistance,
  2628. " < $delta >",
  2629. "Returns the maximum distance any vertex moves in the specified delta. Returns 0 if the delta specified doesn't exist" )
  2630. {
  2631. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2632. const char *pDeltaName = luaL_checkstring( pLuaState, 1 );
  2633. lua_pushnumber( pLuaState, LuaFunc_s::m_dmxEdit.MaxDeltaDistance( pDeltaName ) );
  2634. return 1;
  2635. }
  2636. struct DVec
  2637. {
  2638. double x;
  2639. double y;
  2640. double z;
  2641. DVec()
  2642. : x( 0.0 )
  2643. , y( 0.0 )
  2644. , z( 0.0 )
  2645. {
  2646. }
  2647. DVec &operator=( const Vector &rhs )
  2648. {
  2649. x = rhs.x;
  2650. y = rhs.y;
  2651. z = rhs.z;
  2652. return *this;
  2653. }
  2654. };
  2655. //-----------------------------------------------------------------------------
  2656. //
  2657. // An Efficient Bounding Sphere
  2658. // by Jack Ritter
  2659. // from "Graphics Gems", Academic Press, 1990
  2660. //
  2661. // Routine to calculate tight bounding sphere over
  2662. // a set of points in 3D
  2663. // This contains the routine find_bounding_sphere(),
  2664. // the struct definition, and the globals used for parameters.
  2665. // The abs() of all coordinates must be < BIGNUMBER
  2666. // Code written by Jack Ritter and Lyle Rains.
  2667. //-----------------------------------------------------------------------------
  2668. void FindBoundingSphere( CUtlVector< Vector > &points, Vector &cen, float &fRad )
  2669. {
  2670. if ( points.Count() <= 0 )
  2671. {
  2672. cen.Zero();
  2673. fRad = 0.0f;
  2674. return;
  2675. }
  2676. double dx,dy,dz;
  2677. double rad_sq,xspan,yspan,zspan,maxspan;
  2678. double old_to_p,old_to_p_sq,old_to_new;
  2679. Vector xmin,xmax,ymin,ymax,zmin,zmax,dia1,dia2;
  2680. // DVec cen;
  2681. double rad;
  2682. cen = points[ 0 ];
  2683. fRad = 0.0f;
  2684. xmin.x=ymin.y=zmin.z= FLT_MAX; /* initialize for min/max compare */
  2685. xmax.x=ymax.y=zmax.z= -FLT_MAX;
  2686. for ( int i = 0; i < points.Count(); ++i )
  2687. {
  2688. const Vector &caller_p = points[ i ];
  2689. if (caller_p.x<xmin.x)
  2690. xmin = caller_p; /* New xminimum point */
  2691. if (caller_p.x>xmax.x)
  2692. xmax = caller_p;
  2693. if (caller_p.y<ymin.y)
  2694. ymin = caller_p;
  2695. if (caller_p.y>ymax.y)
  2696. ymax = caller_p;
  2697. if (caller_p.z<zmin.z)
  2698. zmin = caller_p;
  2699. if (caller_p.z>zmax.z)
  2700. zmax = caller_p;
  2701. }
  2702. /* Set xspan = distance between the 2 points xmin & xmax (squared) */
  2703. dx = xmax.x - xmin.x;
  2704. dy = xmax.y - xmin.y;
  2705. dz = xmax.z - xmin.z;
  2706. xspan = dx*dx + dy*dy + dz*dz;
  2707. /* Same for y & z spans */
  2708. dx = ymax.x - ymin.x;
  2709. dy = ymax.y - ymin.y;
  2710. dz = ymax.z - ymin.z;
  2711. yspan = dx*dx + dy*dy + dz*dz;
  2712. dx = zmax.x - zmin.x;
  2713. dy = zmax.y - zmin.y;
  2714. dz = zmax.z - zmin.z;
  2715. zspan = dx*dx + dy*dy + dz*dz;
  2716. /* Set points dia1 & dia2 to the maximally separated pair */
  2717. dia1 = xmin; dia2 = xmax; /* assume xspan biggest */
  2718. maxspan = xspan;
  2719. if (yspan>maxspan)
  2720. {
  2721. maxspan = yspan;
  2722. dia1 = ymin; dia2 = ymax;
  2723. }
  2724. if (zspan>maxspan)
  2725. {
  2726. dia1 = zmin; dia2 = zmax;
  2727. }
  2728. /* dia1,dia2 is a diameter of initial sphere */
  2729. /* calc initial center */
  2730. cen.x = (dia1.x+dia2.x)/2.0;
  2731. cen.y = (dia1.y+dia2.y)/2.0;
  2732. cen.z = (dia1.z+dia2.z)/2.0;
  2733. /* calculate initial radius**2 and radius */
  2734. dx = dia2.x-cen.x; /* x component of radius vector */
  2735. dy = dia2.y-cen.y; /* y component of radius vector */
  2736. dz = dia2.z-cen.z; /* z component of radius vector */
  2737. rad_sq = dx*dx + dy*dy + dz*dz;
  2738. rad = sqrt(rad_sq);
  2739. /* SECOND PASS: increment current sphere */
  2740. for ( int i = 0; i < points.Count(); ++i )
  2741. {
  2742. const Vector &caller_p = points[ i ];
  2743. dx = caller_p.x-cen.x;
  2744. dy = caller_p.y-cen.y;
  2745. dz = caller_p.z-cen.z;
  2746. old_to_p_sq = dx*dx + dy*dy + dz*dz;
  2747. if (old_to_p_sq > rad_sq) /* do r**2 test first */
  2748. { /* this point is outside of current sphere */
  2749. old_to_p = sqrt(old_to_p_sq);
  2750. /* calc radius of new sphere */
  2751. rad = (rad + old_to_p) / 2.0;
  2752. rad_sq = rad*rad; /* for next r**2 compare */
  2753. old_to_new = old_to_p - rad;
  2754. /* calc center of new sphere */
  2755. cen.x = (rad*cen.x + old_to_new*caller_p.x) / old_to_p;
  2756. cen.y = (rad*cen.y + old_to_new*caller_p.y) / old_to_p;
  2757. cen.z = (rad*cen.z + old_to_new*caller_p.z) / old_to_p;
  2758. }
  2759. }
  2760. /*
  2761. fCen.x = cen.x;
  2762. fCen.y = cen.y;
  2763. fCen.z = cen.z;
  2764. */
  2765. fRad = rad;
  2766. }
  2767. //-----------------------------------------------------------------------------
  2768. // Take each delta value, add it to the base state, find the bounding sphere
  2769. // of all of the resulting vertices, return the distance from the center to
  2770. // the
  2771. //-----------------------------------------------------------------------------
  2772. float CDmxEdit::DeltaRadius( const char *pDeltaName )
  2773. {
  2774. CDmeVertexData *pBind = GetBindState();
  2775. if ( !pBind )
  2776. return 0.0f;
  2777. CDmeVertexDeltaData *pDelta = FindDeltaState( pDeltaName );
  2778. if ( !pDelta )
  2779. return 0.0f;
  2780. const CUtlVector< Vector > &bindPos = pBind->GetPositionData();
  2781. const CUtlVector< int > &deltaPosIndices = pDelta->GetVertexIndexData( CDmeVertexData::FIELD_POSITION );
  2782. const CUtlVector< Vector > &deltaPos = pDelta->GetPositionData();
  2783. Assert( deltaPosIndices.Count() == deltaPos.Count() );
  2784. CUtlVector< Vector > newPos;
  2785. newPos.SetSize( deltaPos.Count() );
  2786. for ( int i = 0; i < newPos.Count(); ++i )
  2787. {
  2788. newPos[ i ] = bindPos[ deltaPosIndices[ i ] ] + deltaPos[ i ];
  2789. }
  2790. Vector center;
  2791. float radius = 0.0;
  2792. FindBoundingSphere( newPos, center, radius );
  2793. return radius;
  2794. }
  2795. //-----------------------------------------------------------------------------
  2796. //
  2797. //-----------------------------------------------------------------------------
  2798. LUA_COMMAND(
  2799. DeltaRadius,
  2800. " < $delta >",
  2801. "Returns the radius of the specified delta, if it exists. Radius of a delta is defined as the radius of the tight bounding sphere containing all of the deltas added to their base state values" )
  2802. {
  2803. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2804. const char *pDeltaName = luaL_checkstring( pLuaState, 1 );
  2805. lua_pushnumber( pLuaState, LuaFunc_s::m_dmxEdit.DeltaRadius( pDeltaName ) );
  2806. return 1;
  2807. }
  2808. //-----------------------------------------------------------------------------
  2809. //
  2810. //-----------------------------------------------------------------------------
  2811. float CDmxEdit::SelectionRadius()
  2812. {
  2813. if ( !m_pCurrentSelection || m_pCurrentSelection->Count() == 0 )
  2814. return 0.0f;
  2815. if ( !m_pMesh )
  2816. return 0.0f;
  2817. CDmeVertexData *pBase = m_pMesh->GetCurrentBaseState();
  2818. if ( !pBase )
  2819. return 0.0f;
  2820. CUtlVector< int > selection;
  2821. m_pCurrentSelection->GetComponents( selection );
  2822. const CUtlVector< Vector > &pos = pBase->GetPositionData();
  2823. CUtlVector< Vector > newPos;
  2824. newPos.SetSize( selection.Count() );
  2825. for ( int i = 0; i < newPos.Count(); ++i )
  2826. {
  2827. newPos[ i ] = pos[ selection[ i ] ];
  2828. }
  2829. Vector center;
  2830. float radius = 0.0;
  2831. FindBoundingSphere( newPos, center, radius );
  2832. return radius;
  2833. }
  2834. //-----------------------------------------------------------------------------
  2835. //
  2836. //-----------------------------------------------------------------------------
  2837. LUA_COMMAND(
  2838. SelectionRadius,
  2839. "",
  2840. "Returns the radius of the currently selected vertices, if nothing is selected, returns 0. Radius of a the vertices defined as the radius of the tight bounding sphere containing all of the vertices" )
  2841. {
  2842. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2843. lua_pushnumber( pLuaState, LuaFunc_s::m_dmxEdit.SelectionRadius() );
  2844. return 1;
  2845. }
  2846. //-----------------------------------------------------------------------------
  2847. //
  2848. //-----------------------------------------------------------------------------
  2849. bool CDmxEdit::SetWrinkleScale( const char *pControlName, const char *pRawControlName, float flScale )
  2850. {
  2851. if ( !m_pMesh )
  2852. return SetErrorString( "No Mesh" );
  2853. CDmeCombinationOperator *pDmeCombo = FindReferringElement< CDmeCombinationOperator >( m_pMesh, "targets" );
  2854. if ( !pDmeCombo )
  2855. return SetErrorString( "No DmeCombinationOperator On Mesh \"%s\"", m_pMesh->GetName() );
  2856. const ControlIndex_t nControlIndex = pDmeCombo->FindControlIndex( pControlName );
  2857. if ( nControlIndex < 0 )
  2858. return SetErrorString( "Cannot Find Control \"%s\"", pControlName );
  2859. // Check to see if the raw control exists
  2860. bool bFoundRawControl = false;
  2861. for ( int nRawControlIndex = 0; nRawControlIndex < pDmeCombo->GetRawControlCount( nControlIndex ); ++nRawControlIndex )
  2862. {
  2863. if ( !Q_strcmp( pRawControlName, pDmeCombo->GetRawControlName( nControlIndex, nRawControlIndex ) ) )
  2864. {
  2865. bFoundRawControl = true;
  2866. break;
  2867. }
  2868. }
  2869. if ( !bFoundRawControl )
  2870. {
  2871. CUtlString rawControls;
  2872. for ( int nRawControlIndex = 0; nRawControlIndex < pDmeCombo->GetRawControlCount( nControlIndex ); ++nRawControlIndex )
  2873. {
  2874. if ( rawControls.Length() > 0 )
  2875. {
  2876. rawControls += ", ";
  2877. }
  2878. rawControls += pDmeCombo->GetRawControlName( nControlIndex, nRawControlIndex );
  2879. }
  2880. return SetErrorString( "Control \"%s\" Does Not Have Raw Control \"%s\", Raw Controls: %s ", pControlName, pRawControlName, rawControls.Get() );
  2881. }
  2882. pDmeCombo->SetWrinkleScale( nControlIndex, pRawControlName, flScale );
  2883. return true;
  2884. }
  2885. //-----------------------------------------------------------------------------
  2886. //
  2887. //-----------------------------------------------------------------------------
  2888. LUA_COMMAND(
  2889. SetWrinkleScale,
  2890. "< $controlName >, < $rawControlName >, < #scale >",
  2891. "Sets the wrinkle scale of the raw control of the specified control. "
  2892. "NOTE: SetWrinkleScale merely sets the wrinkle scale value of the specified delta on the combination operator. "
  2893. "This value is only used if ComputeWrinkles() is called after all SetWrinkleScale() calls are made. "
  2894. "A wrinkle scale of 0 means there will be no wrinkle deltas. "
  2895. "The function to compute a single delta's wrinkle values, ComputeWrinkle() does not use the wrinkle scale value at all. "
  2896. "Negative (-) is compress, Positive (+) is stretch."
  2897. )
  2898. {
  2899. LuaFunc_s::m_dmxEdit.SetFuncString( pLuaState );
  2900. const char *pControlName = luaL_checkstring( pLuaState, 1 );
  2901. const char *pRawControlName = luaL_checkstring( pLuaState, 2 );
  2902. const double flScale = luaL_checknumber( pLuaState, 3 );
  2903. const float fScale = flScale;
  2904. if ( LuaFunc_s::m_dmxEdit.SetWrinkleScale( pControlName, pRawControlName, fScale ) )
  2905. return LuaFunc_s::m_dmxEdit.LuaOk( pLuaState );
  2906. return LuaFunc_s::m_dmxEdit.LuaError( pLuaState );
  2907. }
  2908. //-----------------------------------------------------------------------------
  2909. //
  2910. //-----------------------------------------------------------------------------
  2911. void CDmxEdit::Error( const tchar *pMsgFormat, ... )
  2912. {
  2913. static Color outColor( 1, 1, 1, 1 );
  2914. SetErrorState();
  2915. va_list args;
  2916. va_start( args, pMsgFormat );
  2917. tchar pTempBuffer[5020];
  2918. assert( _tcslen( pMsgFormat ) < sizeof( pTempBuffer) ); // check that we won't artificially truncate the string
  2919. _vsntprintf( pTempBuffer, sizeof( pTempBuffer ) - 1, pMsgFormat, args );
  2920. pTempBuffer[ ARRAYSIZE(pTempBuffer) - 1 ] = 0;
  2921. printf( "%s", pTempBuffer );
  2922. fflush( stdout );
  2923. va_end(args);
  2924. if ( CommandLine()->FindParm( "-coe" ) || CommandLine()->FindParm( "-continueOnError" ) )
  2925. return;
  2926. if ( Plat_IsInDebugSession() )
  2927. {
  2928. Msg( "\nPress q To Quit, d To Debug Or Any Key To Continue After Error..." );
  2929. }
  2930. else
  2931. {
  2932. Msg( "\nPress q To Quit Or Any Key To Continue After Error..." );
  2933. }
  2934. int ch = getch();
  2935. if ( ch == 'q' || ch == 'Q' )
  2936. {
  2937. Unload();
  2938. exit( 1 );
  2939. }
  2940. else if ( Plat_IsInDebugSession() && ch == 'd' || ch == 'D' )
  2941. {
  2942. DebuggerBreakIfDebugging();
  2943. }
  2944. Msg( "\n" );
  2945. }
  2946. //-----------------------------------------------------------------------------
  2947. //
  2948. //-----------------------------------------------------------------------------
  2949. class CPauseOnScopeExit
  2950. {
  2951. public:
  2952. CPauseOnScopeExit( CDmxEdit &dmxEdit )
  2953. : m_dmxEdit( dmxEdit )
  2954. {
  2955. }
  2956. ~CPauseOnScopeExit()
  2957. {
  2958. m_dmxEdit.Unload();
  2959. if ( m_dmxEdit.ErrorState() )
  2960. {
  2961. Msg( "\nPress Any Key To Continue...\n" );
  2962. getch();
  2963. }
  2964. }
  2965. protected:
  2966. CDmxEdit &m_dmxEdit;
  2967. };
  2968. //-----------------------------------------------------------------------------
  2969. // The application object
  2970. //-----------------------------------------------------------------------------
  2971. void CDmxEdit::DoIt()
  2972. {
  2973. ResetErrorState();
  2974. // Just for sugary sweet syntax
  2975. CDmxEditProxy base( *this );
  2976. CPauseOnScopeExit scopePause( *this );
  2977. CDisableUndoScopeGuard guard;
  2978. return;
  2979. }
  2980. //-----------------------------------------------------------------------------
  2981. //
  2982. //-----------------------------------------------------------------------------
  2983. void CDmxEdit::CleanupWork()
  2984. {
  2985. if ( m_pMesh )
  2986. {
  2987. // Cleanup
  2988. m_pMesh->RemoveAttribute( "selection" );
  2989. if ( m_pCurrentSelection )
  2990. {
  2991. g_pDataModel->DestroyElement( m_pCurrentSelection->GetHandle() );
  2992. m_pCurrentSelection = NULL;
  2993. }
  2994. CDmeVertexData *pBind( m_pMesh->FindBaseState( "bind" ) );
  2995. if ( pBind )
  2996. {
  2997. m_pMesh->SetCurrentBaseState( "bind" );
  2998. }
  2999. CDmeVertexData *pWork( m_pMesh->FindBaseState( "__dmxEdit_work" ) );
  3000. if ( pWork )
  3001. {
  3002. m_pMesh->DeleteBaseState( "__dmxEdit_work" );
  3003. }
  3004. }
  3005. }
  3006. //-----------------------------------------------------------------------------
  3007. //
  3008. //-----------------------------------------------------------------------------
  3009. void CDmxEdit::CreateWork()
  3010. {
  3011. if ( m_pMesh )
  3012. {
  3013. // Undo the cleanup work performed above
  3014. CDmeVertexData *pBind = m_pMesh->FindBaseState( "bind" );
  3015. if ( pBind )
  3016. {
  3017. // Ensure "work" isn't saved!
  3018. CDmeVertexData *pWork = m_pMesh->FindOrCreateBaseState( "__dmxEdit_work" );
  3019. if ( pWork )
  3020. {
  3021. pBind->CopyTo( pWork );
  3022. m_pMesh->SetCurrentBaseState( "__dmxEdit_work" );
  3023. }
  3024. }
  3025. m_pCurrentSelection = CreateElement< CDmeSingleIndexedComponent >( "selection", m_pRoot->GetFileId() );
  3026. m_pMesh->SetValue( "selection", m_pCurrentSelection );
  3027. }
  3028. }
  3029. //-----------------------------------------------------------------------------
  3030. //
  3031. //-----------------------------------------------------------------------------
  3032. void CDmxEdit::GetFuncArg( lua_State *pLuaState, int nIndex, CUtlString &funcString )
  3033. {
  3034. switch ( lua_type( pLuaState, nIndex ) )
  3035. {
  3036. case LUA_TNUMBER:
  3037. funcString += lua_tonumber( pLuaState, nIndex );
  3038. break;
  3039. case LUA_TBOOLEAN:
  3040. funcString += ( lua_toboolean( pLuaState, nIndex ) ? "true" : "false" );
  3041. break;
  3042. case LUA_TSTRING:
  3043. funcString += "\"";
  3044. funcString += lua_tostring( pLuaState, nIndex );
  3045. funcString += "\"";
  3046. break;
  3047. case LUA_TTABLE:
  3048. {
  3049. funcString += "{ ";
  3050. const int nCount = luaL_getn( pLuaState, nIndex );
  3051. for ( int i = 1; i <= nCount; ++i )
  3052. {
  3053. lua_rawgeti( pLuaState, nIndex, i );
  3054. if ( i > 1 )
  3055. {
  3056. funcString += ", ";
  3057. }
  3058. GetFuncArg( pLuaState, lua_gettop( pLuaState ), funcString );
  3059. lua_pop( pLuaState, 1 );
  3060. }
  3061. funcString += " }";
  3062. }
  3063. break;
  3064. case LUA_TNIL:
  3065. case LUA_TFUNCTION:
  3066. case LUA_TUSERDATA:
  3067. case LUA_TTHREAD:
  3068. case LUA_TLIGHTUSERDATA:
  3069. funcString += "<";
  3070. funcString += lua_typename( pLuaState, nIndex );
  3071. funcString += ">";
  3072. break;
  3073. }
  3074. }
  3075. //-----------------------------------------------------------------------------
  3076. // Sets the current function string and line number
  3077. //-----------------------------------------------------------------------------
  3078. const CUtlString &CDmxEdit::SetFuncString( lua_State *pLuaState )
  3079. {
  3080. m_funcString.SetLength( 0 );
  3081. lua_Debug ar;
  3082. lua_getstack( pLuaState, 0, &ar );
  3083. lua_getinfo( pLuaState, "Sln", &ar );
  3084. const char *pFuncName = ar.name;
  3085. if ( pFuncName )
  3086. {
  3087. m_funcString = pFuncName;
  3088. }
  3089. else
  3090. {
  3091. m_funcString = "Unknown";
  3092. }
  3093. m_funcString += "(";
  3094. if ( lua_gettop( pLuaState ) >= 1 )
  3095. m_funcString += " ";
  3096. for ( int i = 1; i <= lua_gettop( pLuaState ); ++i )
  3097. {
  3098. if ( i > 1 )
  3099. {
  3100. m_funcString += ", ";
  3101. }
  3102. GetFuncArg( pLuaState, i, m_funcString );
  3103. }
  3104. if ( lua_gettop( pLuaState ) >= 1 )
  3105. m_funcString += " ";
  3106. m_funcString += ");";
  3107. lua_getstack( pLuaState, 1, &ar );
  3108. lua_getinfo( pLuaState, "Sln", &ar );
  3109. m_lineNo = ar.currentline;
  3110. m_sourceFile.SetLength( 0 );
  3111. if ( ar.source )
  3112. {
  3113. if ( *( ar.source ) == '@' )
  3114. {
  3115. m_sourceFile = ar.source + 1;
  3116. }
  3117. else
  3118. {
  3119. m_sourceFile = ar.source;
  3120. }
  3121. }
  3122. return m_funcString;
  3123. }
  3124. //=============================================================================
  3125. // Lua Interface
  3126. //=============================================================================
  3127. //=============================================================================
  3128. // CDmxEditLua
  3129. //=============================================================================
  3130. //-----------------------------------------------------------------------------
  3131. //
  3132. //-----------------------------------------------------------------------------
  3133. CDmxEditLua::CDmxEditLua()
  3134. : m_pLuaState( lua_open() )
  3135. {
  3136. luaL_openlibs( m_pLuaState );
  3137. for ( LuaFunc_s *pFunc = LuaFunc_s::s_pFirstFunc; pFunc; pFunc = pFunc->m_pNextFunc )
  3138. {
  3139. lua_pushcfunction( m_pLuaState, pFunc->m_pFunc );
  3140. lua_setglobal( m_pLuaState, pFunc->m_pFuncName );
  3141. }
  3142. const char *pLuaInit =
  3143. "vsLuaDir = os.getenv( \"VPROJECT\" );\n"
  3144. "vsLuaDir = string.gsub( vsLuaDir, \"[\\\\/]\", \"/\" );\n"
  3145. "vsLuaDir = string.gsub( vsLuaDir, \"/*$\", \"\" );\n"
  3146. "vsLuaDir = ( string.gsub( vsLuaDir, \"[^/]+$\", \"sdktools/lua\" ) .. \"/?.lua\" );\n"
  3147. "package.path = ( package.path .. \";\" .. vsLuaDir );\n"
  3148. "require( \"vs\" );\n";
  3149. if ( luaL_loadstring( m_pLuaState, pLuaInit ) || lua_pcall( m_pLuaState, 0, LUA_MULTRET, 0 ) )
  3150. {
  3151. Error( "Error: %s\n", lua_tostring( m_pLuaState, -1 ) );
  3152. }
  3153. }
  3154. //-----------------------------------------------------------------------------
  3155. //
  3156. //-----------------------------------------------------------------------------
  3157. bool CDmxEditLua::DoIt( const char *pFilename )
  3158. {
  3159. CDisableUndoScopeGuard guard;
  3160. bool retVal = true;
  3161. LuaFunc_s::m_dmxEdit.SetScriptFilename( pFilename );
  3162. if ( luaL_dofile( m_pLuaState, pFilename ) )
  3163. {
  3164. Error( "Error: %s\n", lua_tostring( m_pLuaState, -1 ) );
  3165. retVal = false;
  3166. }
  3167. lua_close( m_pLuaState );
  3168. LuaFunc_s::m_dmxEdit.Unload();
  3169. return retVal;
  3170. }
  3171. //-----------------------------------------------------------------------------
  3172. //
  3173. //-----------------------------------------------------------------------------
  3174. void CDmxEditLua::SetVar( const CUtlString &var, const CUtlString &val )
  3175. {
  3176. CUtlString sLuaCmd( var );
  3177. sLuaCmd += " = ";
  3178. sLuaCmd += val;
  3179. sLuaCmd += ";";
  3180. if ( luaL_loadstring( m_pLuaState, sLuaCmd.Get() ) || lua_pcall( m_pLuaState, 0, LUA_MULTRET, 0 ) )
  3181. {
  3182. Error( "Error: %s\n", lua_tostring( m_pLuaState, -1 ) );
  3183. }
  3184. }
  3185. //-----------------------------------------------------------------------------
  3186. //
  3187. //-----------------------------------------------------------------------------
  3188. void CDmxEditLua::SetGame( const CUtlString &game )
  3189. {
  3190. CUtlString sLuaCmd( "vs.SetGame( " );
  3191. sLuaCmd += game;
  3192. sLuaCmd += " );";
  3193. if ( luaL_loadstring( m_pLuaState, sLuaCmd.Get() ) || lua_pcall( m_pLuaState, 0, LUA_MULTRET, 0 ) )
  3194. {
  3195. Error( "Error: %s\n", lua_tostring( m_pLuaState, -1 ) );
  3196. }
  3197. }