Export.ms 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. --
  2. -- Command & Conquer Renegade(tm)
  3. -- Copyright 2025 Electronic Arts Inc.
  4. --
  5. -- This program is free software: you can redistribute it and/or modify
  6. -- it under the terms of the GNU General Public License as published by
  7. -- the Free Software Foundation, either version 3 of the License, or
  8. -- (at your option) any later version.
  9. --
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. -- GNU General Public License for more details.
  14. --
  15. -- You should have received a copy of the GNU General Public License
  16. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. --
  18. ------------------------------------------------------------------------
  19. --
  20. -- Export.ms - This file contains two batch exporting scripts. The first
  21. -- "exportDependants" will export the current scene, and any others in
  22. -- the current directory that depend on it for their skeletons. The
  23. -- second, "exportDirectoryTree" will export and and all Max files in
  24. -- a given directory tree. Both of these scripts have equivalent macro
  25. -- definitions, making it simple to add them as buttons to a toolbar.
  26. --
  27. ------------------------------------------------------------------------
  28. -- EXP_replace_extension is a utility function used by the scripts in this
  29. -- file to replace the ".max" or ".w3d" of the given filename with a given
  30. -- extension (including the dot, ie. ".w3d")
  31. function EXP_replace_extension
  32. max_filename -- x:\abc\blah.max or blah.max
  33. extension -- .w3d
  34. = (
  35. -- Find the ".max" extension in the filename.
  36. local dot_index = findString max_filename ".MAX"
  37. if dot_index == undefined then
  38. (
  39. dot_index = findString max_filename ".W3D"
  40. if dot_index == undefined then
  41. return max_filename
  42. )
  43. return (replace max_filename dot_index 4 extension)
  44. )
  45. -- USER-CALLABLE: exportW3D exports the current scene as a W3D file.
  46. -- It takes a flag to determine whether it should display any dialogs
  47. -- during the export. This makes the function suitable for batch exports.
  48. function exportW3D
  49. show_dialogs:true
  50. w3d_name:undefined
  51. = (
  52. if w3d_name == undefined then
  53. (
  54. -- Get the name of the current scene as a W3D file.
  55. w3d_name = EXP_replace_extension (maxFilePath + maxFileName) ".w3d"
  56. if show_dialogs == true then
  57. (
  58. -- Allow the user to possibly choose a different name.
  59. w3d_name = getSaveFileName caption:"Select File to Export" \
  60. filename:w3d_name types:"w3d (*.W3D)|*.w3d|"
  61. -- The dialog above will prompt the user to overwrite an
  62. -- existing file. If the user chooses yes, the exportFile
  63. -- call below will prompt him once again to overwrite the
  64. -- file. The solution to this is to delete the w3d file.
  65. if w3d_name != undefined then
  66. deleteFile w3d_name
  67. )
  68. -- If the user canceled the name selection, or there was some
  69. -- sort of error, return false.
  70. if w3d_name == undefined then
  71. return false
  72. )
  73. -- Print an exporting message.
  74. local short_name = filenameFromPath w3d_name
  75. print("Exporting " + short_name + "...")
  76. -- Export the scene, showing the export options if appropriate.
  77. if show_dialogs == true then
  78. exportFile w3d_name
  79. else
  80. exportFile w3d_name #noPrompt
  81. return true
  82. )
  83. -- The macro script definition for the "Export as W3D" toolbar button.
  84. macroScript Export_As_W3D
  85. category:"Westwood Scripts"
  86. buttonText:"OLD - Export as W3D - OLD"
  87. toolTip:"This button should not be used anymore"
  88. icon:#("SchematicView", 1)
  89. (
  90. messageBox "This button is being phased out. Please use Export_As instead."
  91. )
  92. -- The macro script definition for the "Export" toolbar button.
  93. macroScript Export
  94. category:"Westwood Scripts"
  95. buttonText:"Export"
  96. toolTip:"Export the current scene as a W3D file without prompting"
  97. icon:#("GameTools", 2)
  98. (
  99. exportW3D show_dialogs:false
  100. )
  101. -- The macro script definition for the "Export As..." toolbar button
  102. macroScript Export_As
  103. category:"Westwood Scripts"
  104. buttonText:"Export As..."
  105. toolTip:"Export the current scene as a W3D file"
  106. icon:#("GameTools", 3)
  107. (
  108. exportW3D()
  109. )
  110. -- USER-CALLABLE: exportDependants will export the current scene, and
  111. -- and other scenes in the same directory that depend on the current
  112. -- one for their hierarchy.
  113. function exportDependants
  114. = (
  115. local current_scene = undefined
  116. -- Export the current scene.
  117. if checkForSave() == false then
  118. (
  119. -- User aborted the save.
  120. return ok
  121. )
  122. else
  123. (
  124. -- The user saved changes (or the scene was unchanged).
  125. -- Grab the name of the scene file.
  126. current_scene = maxFilePath + maxFileName
  127. )
  128. if current_scene == "" then
  129. (
  130. -- This is an empty scene, don't export.
  131. return ok
  132. )
  133. -- Export the current scene as a W3D file.
  134. local export_file = EXP_replace_extension current_scene ".w3d"
  135. local short_name = filenameFromPath export_file
  136. exportW3D w3d_name:export_file
  137. -- Get (and sort) the list of all other MAX files
  138. -- in the same directory as the current scene.
  139. local pattern = maxFilePath
  140. pattern = pattern + "*.max"
  141. local files = getFiles pattern
  142. sort files
  143. -- Open and export each file in turn.
  144. local exported_count = 1
  145. for f in files do
  146. (
  147. -- Don't export the original scene again.
  148. if f != current_scene then
  149. (
  150. -- Load the Max scene.
  151. if (loadMaxFile f) != true then
  152. (
  153. print("Unable to load " + f)
  154. )
  155. else
  156. (
  157. -- Get the name of the hierarchy the scene depends on.
  158. -- wwGetHierarchyFile returns a relative pathname if it
  159. -- is available, otherwise it returns an absolute path.
  160. local htree_file = wwGetHierarchyFile()
  161. if (htree_file == undefined) or
  162. ((htree_file as name != short_name as name) and
  163. (htree_file as name != current_scene as name)) then
  164. (
  165. -- The filenames don't match, or the scene doesn't
  166. -- depend on a hierarchy at all.
  167. local filename = filenameFromPath f
  168. print (filename + " does not depend on " + short_name)
  169. )
  170. else
  171. (
  172. -- This scene gets its hierarchy from the original.
  173. -- Re-export it (last export settings automatically used).
  174. local w3d_file = EXP_replace_extension f ".w3d"
  175. if (exportW3D w3d_name:w3d_file show_dialogs:false) then
  176. exported_count += 1
  177. )
  178. )
  179. )
  180. )
  181. -- Load up the original scene again and show how many files were re-exported.
  182. loadMaxFile current_scene
  183. print ("Export process finished! " + exported_count as string + " files exported.")
  184. return ok
  185. )
  186. -- The macro script definition for the "Export Dependants" toolbar button.
  187. macroScript Export_Dependants
  188. category:"Westwood Scripts"
  189. buttonText:"Export Dependants"
  190. toolTip:"Export all dependant scenes"
  191. icon:#("Maxscript", 3)
  192. (
  193. exportDependants()
  194. )
  195. -- USER-CALLABLE: exportDirectoryTree exports all MAX scenes in a directory
  196. -- and all it's subdirectories. All scenes are exported with their last
  197. -- export settings. Scenes that have never been exported are done so with
  198. -- default settings as defined in max2w3d (currently exports: hierarchy,
  199. -- animation, geometry).
  200. global EXP_exported = #() -- array of exported scenes
  201. function exportDirectoryTree
  202. dir:undefined
  203. = (
  204. -- If no directory was given, get it from the user using a directory
  205. -- selection dialog.
  206. local recursive = true
  207. if dir == undefined then
  208. (
  209. local ar = #(maxFilePath, true)
  210. local res = wwExportTreeSettings ar
  211. if res == undefined then
  212. return 0
  213. dir = res[1]
  214. recursive = res[2]
  215. EXP_exported = #()
  216. )
  217. -- Append a trailing backslash if the path doesn't have one.
  218. if dir[dir.count] != "\\" then
  219. dir = dir + "\\"
  220. -- Export all max files in the current directory.
  221. local exported_count = 0
  222. local files = getFiles(dir + "*.max")
  223. sort files
  224. for f in files do
  225. (
  226. -- Load 'er up.
  227. if (loadMaxFile f) != true then
  228. (
  229. print("Unable to load " + f)
  230. continue
  231. )
  232. -- Check if this scene requires another to be exported first.
  233. local htree_file = wwGetHierarchyFile()
  234. if htree_file != undefined then
  235. (
  236. -- Convert the relative path to an absolute one.
  237. local htree_abs = wwGetAbsolutePath htree_file dir
  238. local max_file = EXP_replace_extension htree_abs ".max"
  239. -- Has the scene been exported yet?
  240. if (findItem EXP_exported (max_file as Name)) == 0 then
  241. (
  242. -- It hasn't been exported. Do so now.
  243. -- Load the max file.
  244. if (loadMaxFile max_file) != true then
  245. (
  246. print("WARNING: " + max_file + " could not be opened, but " + \
  247. f + " depends on " + htree_abs + "!")
  248. -- Proceed with the export only if a previous W3D file exists.
  249. if (getFiles htree_abs).count == 0 then
  250. continue
  251. )
  252. else
  253. (
  254. -- Export the w3d file
  255. if (exportW3D w3d_name:w3d_file show_dialogs:false) == true then
  256. (
  257. -- Add the scene to the array of exported scenes.
  258. append EXP_exported (max_file as Name)
  259. )
  260. else
  261. (
  262. -- Show an error message
  263. print("ERROR: " + htree_abs + " not exported, but " + \
  264. f + " depends on it!")
  265. continue
  266. )
  267. -- Load the previous scene up again so we can export it.
  268. loadMaxFile f
  269. )
  270. )
  271. )
  272. -- Export it as a W3D file, if it hasn't been already exported.
  273. if (findItem EXP_exported (f as Name)) == 0 then
  274. (
  275. local w3d_file = EXP_replace_extension f ".w3d"
  276. if (exportW3D w3d_name:w3d_file show_dialogs:false) then
  277. (
  278. -- Add the scene to the array of exported scenes.
  279. append EXP_exported (f as Name)
  280. )
  281. )
  282. )
  283. -- Recurse through all subdirectories.
  284. if recursive == true then
  285. (
  286. local folders = getDirectories(dir + "*")
  287. sort folders
  288. for d in folders do
  289. (
  290. print ("Entering " + d)
  291. exportDirectoryTree dir:d
  292. )
  293. )
  294. return EXP_exported.count
  295. )
  296. -- The macro script definition for the "Export Directory Tree" toolbar button.
  297. macroScript Export_Directory_Tree
  298. category:"Westwood Scripts"
  299. buttonText:"Export Directory Tree"
  300. toolTip:"Export all MAX files in a folder and all its sub-folders"
  301. icon:#("SchematicView", 2)
  302. (
  303. local count = exportDirectoryTree()
  304. print (count as string + " files exported")
  305. if count > 0 then
  306. resetMaxFile #noPrompt
  307. )