fbxrenamingstrategy.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxrenamingstrategy.h
  9. #ifndef _FBXSDK_UTILS_RENAMINGSTRATEGY_H_
  10. #define _FBXSDK_UTILS_RENAMINGSTRATEGY_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/base/fbxcharptrset.h>
  13. #include <fbxsdk/utils/fbxnamehandler.h>
  14. #include <fbxsdk/fbxsdk_nsbegin.h>
  15. class FbxScene;
  16. class FbxNode;
  17. /** This base class is an abstract implementation of a renaming strategy for avoiding name clashes.
  18. * An implementation of a reader (FbxReader) or writer (FbxWriter) class must call a concrete implementation
  19. * of "FbxRenamingStrategyInterface::Rename()" every time a name is imported or exported to avoid name clashes.
  20. * Any class deriving from FbxRenamingStrategyBase must implement FbxRenamingStrategyInterface::Clear(),
  21. * FbxRenamingStrategyInterface::Rename(), and FbxRenamingStrategyInterface::Clone().
  22. * \nosubgrouping
  23. * \see FbxNameHandler FbxRenamingStrategyNumber
  24. */
  25. class FBXSDK_DLL FbxRenamingStrategyInterface
  26. {
  27. public:
  28. //! Constructor.
  29. FbxRenamingStrategyInterface();
  30. //! Destructor.
  31. virtual ~FbxRenamingStrategyInterface ();
  32. //! Resets internal state regarding assigned names.
  33. virtual void Clear() = 0;
  34. /** Rename a name if necessary to avoid name-clash issues.
  35. * \param pName The name to be renamed.
  36. * \return Return \c true on success, \c false otherwise.
  37. */
  38. virtual bool Rename(FbxNameHandler& pName) = 0;
  39. /** Create a dynamic renaming strategy instance of the same type as the child class.
  40. * \return New instance.
  41. */
  42. virtual FbxRenamingStrategyInterface* Clone() = 0;
  43. };
  44. /** Implements a renaming strategy that resolves name clashes by adding number postfixes.
  45. * For example, when there are three objects with the same names "MyObject",
  46. * and they will be renamed to "MyObject", "MyObject1" and "MyObject2".
  47. * \nosubgrouping
  48. * \see FbxNameHandler FbxRenamingStrategyBase
  49. */
  50. class FBXSDK_DLL FbxRenamingStrategyNumber : public FbxRenamingStrategyInterface
  51. {
  52. public:
  53. //! Constructor.
  54. FbxRenamingStrategyNumber();
  55. //! Destructor.
  56. virtual ~FbxRenamingStrategyNumber ();
  57. //! Resets internal state regarding assigned names.
  58. virtual void Clear();
  59. /** Rename a name if necessary to avoid name-clash issues.
  60. * \param pName The name to be renamed.
  61. * \return Return \c true on success, \c false otherwise.
  62. */
  63. virtual bool Rename(FbxNameHandler& pName);
  64. /** Create a dynamic renaming strategy instance of the same type as the child class.
  65. * \return New instance.
  66. */
  67. virtual FbxRenamingStrategyInterface* Clone();
  68. /*****************************************************************************************************************************
  69. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  70. *****************************************************************************************************************************/
  71. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  72. private:
  73. struct NameCell
  74. {
  75. NameCell(const char* pName) :
  76. mName(pName),
  77. mInstanceCount(0)
  78. {
  79. }
  80. FbxString mName;
  81. int mInstanceCount;
  82. };
  83. FbxArray<NameCell*> mNameArray;
  84. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  85. };
  86. /** The FbxRenamingStrategy object can be set to rename all the objects in a scene.
  87. * It can remove name clashing, remove illegal characters, manage namespaces and manage backward compatibility.
  88. * It is better to choose FbxSceneRenamer instead of this class to simplify the usage.
  89. * \nosubgrouping
  90. * \see FbxSceneRenamer
  91. */
  92. class FBXSDK_DLL FbxRenamingStrategy : public FbxRenamingStrategyInterface
  93. {
  94. public:
  95. /** \enum EDirection The mode describing the convention direction, from FBX format or to FBX format.
  96. * - \e eToFBX Convert to FBX format from another format.
  97. * - \e eFromFBX Convert from FBX format to another format.
  98. */
  99. enum EDirection
  100. {
  101. eToFBX,
  102. eFromFBX
  103. };
  104. /** Constructor.
  105. * \param pMod The mode describing the convention direction, from FBX format or to FBX format.
  106. * \param pOnCreationRun
  107. */
  108. FbxRenamingStrategy(EDirection pMod, bool pOnCreationRun = false);
  109. //! Destructor.
  110. virtual ~FbxRenamingStrategy();
  111. /** Rename a name if necessary.
  112. * \param pName The name to be renamed.
  113. * \return Return \c true on success, \c false otherwise.
  114. */
  115. virtual bool Rename(FbxNameHandler& pName);
  116. //! Resets internal state regarding assigned names.
  117. virtual void Clear();
  118. /** Create a dynamic renaming strategy instance of the same type as the child class.
  119. * \return New instance.
  120. */
  121. virtual FbxRenamingStrategyInterface* Clone();
  122. /** \enum EClashType
  123. * - \e eNameClashAuto
  124. * - \e eNameClashType1
  125. * - \e eNameClashType2
  126. */
  127. enum EClashType
  128. {
  129. eNameClashAuto,
  130. eNameClashType1,
  131. eNameClashType2
  132. };
  133. /** Setup the strategy to perform this algorithm
  134. * \param pType
  135. */
  136. void SetClashSoverType(EClashType pType);
  137. /** Returns a name with its prefix removed.
  138. * \param pName A name containing a prefix.
  139. * \return The part of pName following the "::"
  140. */
  141. static char* NoPrefixName (const char* pName);
  142. /** Returns a name with its prefix removed.
  143. * \param pName A name containing a prefix.
  144. * \return The part of pName following the "::"
  145. */
  146. static char* NoPrefixName (FbxString& pName);
  147. /** Get the namespace of the last renamed object.
  148. * \return Char pointer to the namespace.
  149. */
  150. virtual char* GetNameSpace() { return mNameSpace.Buffer(); }
  151. /** Sets the current scene namespace symbol.
  152. * \param pNameSpaceSymbol namespace symbol.
  153. */
  154. virtual void SetInNameSpaceSymbol(FbxString pNameSpaceSymbol){mInNameSpaceSymbol = pNameSpaceSymbol;}
  155. /** Sets the wanted scene namespace symbol.
  156. * \param pNameSpaceSymbol namespace symbol.
  157. */
  158. virtual void SetOutNameSpaceSymbol(FbxString pNameSpaceSymbol){mOutNameSpaceSymbol = pNameSpaceSymbol;}
  159. /** Sets case sensitivity for name clashing.
  160. * \param pIsCaseSensitive Set to \c true to make the name clashing case sensitive.
  161. */
  162. virtual void SetCaseSensibility(bool pIsCaseSensitive){mCaseSensitive = pIsCaseSensitive ;}
  163. /** Sets the flag for character acceptance during renaming.
  164. * \param pReplaceNonAlphaNum Set to \c true to replace illegal characters with an underscore ("_").
  165. */
  166. virtual void SetReplaceNonAlphaNum(bool pReplaceNonAlphaNum){mReplaceNonAlphaNum = pReplaceNonAlphaNum;}
  167. /** Sets the flag for first character acceptance during renaming.
  168. * \param pFirstNotNum Set to \c true to add an underscore to the name if the first character is a number.
  169. */
  170. virtual void SetFirstNotNum(bool pFirstNotNum){mFirstNotNum = pFirstNotNum;}
  171. /** Recursively renames all the unparented namespaced objects (Prefix mode) starting from this node.
  172. * \param pNode Parent node.
  173. * \param pIsRoot The root node.
  174. * \remarks This function adds "_NSclash" when it encounters an unparented namespaced object.
  175. */
  176. virtual bool RenameUnparentNameSpace(FbxNode* pNode, bool pIsRoot = false);
  177. /** Recursively removes all the unparented namespaced "key" starting from this node.
  178. * \param pNode Parent node.
  179. * \remarks This function removes "_NSclash" when encountered. This is the opposite from RenameUnparentNameSpace.
  180. */
  181. virtual bool RemoveImportNameSpaceClash(FbxNode* pNode);
  182. /** Recursively get all the namespace starting from this node's parent.
  183. * \param pNode Parent node.
  184. * \param pNameSpaceList output the namespace list from pNode's parent to the root node.
  185. */
  186. virtual void GetParentsNameSpaceList(FbxNode* pNode, FbxArray<FbxString*> &pNameSpaceList);
  187. /** Recursively replace the namespace starting from this node to its children.
  188. * \param pNode Current node.
  189. * \param OldNS The old namespace to be replaced with the NewNs.
  190. * \param NewNS The new namespace to replace OldNs.
  191. */
  192. virtual bool PropagateNameSpaceChange(FbxNode* pNode, FbxString OldNS, FbxString NewNS);
  193. /*****************************************************************************************************************************
  194. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  195. *****************************************************************************************************************************/
  196. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  197. protected:
  198. virtual bool RenameToFBX(FbxNameHandler& pName);
  199. virtual bool RenameFromFBX(FbxNameHandler& pName);
  200. virtual FbxString& ReplaceNonAlphaNum(FbxString& pName, const char* pReplace, bool pIgnoreNameSpace);
  201. EDirection mMode;
  202. EClashType mType;
  203. struct NameCell
  204. {
  205. NameCell(const char* pName) :
  206. mName(pName),
  207. mInstanceCount(0)
  208. {
  209. }
  210. FbxString mName;
  211. int mInstanceCount;
  212. };
  213. FbxCharPtrSet mStringNameArray;
  214. FbxArray<NameCell*> mExistingNsList;
  215. bool mOnCreationRun;
  216. bool mCaseSensitive;
  217. bool mReplaceNonAlphaNum;
  218. bool mFirstNotNum;
  219. FbxString mNameSpace;
  220. FbxString mInNameSpaceSymbol; //symbol identifying a name space
  221. FbxString mOutNameSpaceSymbol;
  222. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  223. };
  224. /** The FbxSceneRenamer provides a way to easily rename objects in a scene without using the FbxRenamingStrategy class.
  225. * FbxSceneRenamer can remove name clashing and illegal characters. It also manages namespaces.
  226. *
  227. * Example:
  228. * Maya only accepts names with letters, digits, or underscores, and we want to convert
  229. * all the names of a scene from FBX format to Maya format.
  230. * \code
  231. * FbxSceneRenamer lSceneRenamer(pScene);
  232. * lSceneRenamer.RenameFor(FbxSceneRenamer::eFBX_TO_MAYA);
  233. * \endcode
  234. * \nosubgrouping
  235. * \see FbxRenamingStrategy
  236. */
  237. class FBXSDK_DLL FbxSceneRenamer
  238. {
  239. public:
  240. /** Constructor
  241. * \param pScene A scene which contains objects to be renamed.
  242. */
  243. FbxSceneRenamer(FbxScene* pScene) {mScene = pScene;};
  244. //! Destructor.
  245. virtual ~FbxSceneRenamer(){};
  246. /** \enum ERenamingMode The Mode describing from which format to which format.
  247. * - \e eNone
  248. * - \e eMAYA_TO_FBX5
  249. * - \e eMAYA_TO_FBX_MB75
  250. * - \e eMAYA_TO_FBX_MB70
  251. * - \e eFBXMB75_TO_FBXMB70
  252. * - \e eFBX_TO_FBX
  253. * - \e eMAYA_TO_FBX
  254. * - \e eFBX_TO_MAYA
  255. * - \e eLW_TO_FBX
  256. * - \e eFBX_TO_LW
  257. * - \e eXSI_TO_FBX
  258. * - \e eFBX_TO_XSI
  259. * - \e eMAX_TO_FBX
  260. * - \e eFBX_TO_MAX
  261. * - \e eMB_TO_FBX
  262. * - \e eFBX_TO_MB
  263. * - \e eDAE_TO_FBX
  264. * - \e eFBX_TO_DAE
  265. */
  266. enum ERenamingMode
  267. {
  268. eNone,
  269. eMAYA_TO_FBX5,
  270. eMAYA_TO_FBX_MB75,
  271. eMAYA_TO_FBX_MB70,
  272. eFBXMB75_TO_FBXMB70,
  273. eFBX_TO_FBX,
  274. eMAYA_TO_FBX,
  275. eFBX_TO_MAYA,
  276. eLW_TO_FBX,
  277. eFBX_TO_LW,
  278. eXSI_TO_FBX,
  279. eFBX_TO_XSI,
  280. eMAX_TO_FBX,
  281. eFBX_TO_MAX,
  282. eMB_TO_FBX,
  283. eFBX_TO_MB,
  284. eDAE_TO_FBX,
  285. eFBX_TO_DAE
  286. };
  287. /** Rename the objects of the scene according the specific mode.
  288. * \param pMode A mode describing from which format to which format.
  289. */
  290. void RenameFor(ERenamingMode pMode);
  291. /*****************************************************************************************************************************
  292. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  293. *****************************************************************************************************************************/
  294. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  295. private:
  296. void ResolveNameClashing( bool pFromFbx, bool pIgnoreNS, bool pIsCaseSensitive,
  297. bool pReplaceNonAlphaNum, bool pFirstNotNum,
  298. FbxString pInNameSpaceSymbol, FbxString pOutNameSpaceSymbol,
  299. bool pNoUnparentNS/*for MB < 7.5*/, bool pRemoveNameSpaceClash);
  300. FbxRenamingStrategyInterface* mNodeRenamingStrategy;
  301. FbxScene* mScene;
  302. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  303. };
  304. #include <fbxsdk/fbxsdk_nsend.h>
  305. #endif /* _FBXSDK_UTILS_RENAMINGSTRATEGY_H_ */