home *** CD-ROM | disk | FTP | other *** search
/ Unreal Tools / UnrealTools.iso / ActorXImporter.zip / export_fbx.ms < prev   
Text File  |  2017-12-06  |  10KB  |  334 lines

  1. /*
  2.  
  3.  ActorX batch converter for 3ds Max
  4.  
  5.  Created:    December 15 2010
  6.  
  7.  Author:    Konstantin Nosov (aka Gildor)
  8.  
  9.  Web page:    http://www.gildor.org/projects/unactorx
  10.  
  11.  Revision History:
  12.  
  13.     06.12.2017 v1.07
  14.     - renamed "recurse" option to "look in subfolders" to be less confising to new users
  15.  
  16.     20.09.2017 v1.06
  17.     - fixed error in fbx animation export when track has 0 frames
  18.  
  19.     17.01.2017 v1.05
  20.     - fixed non-multitake animation export
  21.     - fixed length of exported animation
  22.  
  23.     31.07.2016 v1.04
  24.     - option to save multiple animation is single FBX file (multi-take animation)
  25.  
  26.     25.07.2016 v1.03
  27.     - saving animation name as FBX Take name (thanks Skykila)
  28.  
  29.     21.07.2015 v1.02
  30.     - updated to match ActorX Importer 1.33 changes, improved appearance
  31.  
  32.     29.12.2010 v1.01
  33.     - added output format selection (fbx, ase, max)
  34.  
  35.     15.12.2010 v1.00
  36.     - first public release
  37.  
  38. */
  39.  
  40.  
  41. /* TODO
  42.     - save settings to the ActorX Importer ini (using its API)
  43. */
  44.  
  45. global g_axImporterVersion
  46.  
  47. global g_meshDir
  48. global g_meshRecurse
  49. global g_fbxSmGroups
  50. global g_fbxSmMesh
  51. global g_fbxMultiTake
  52. global g_outFormat
  53. global g_useDefaultFBXSettings
  54.  
  55. if (g_meshDir     == undefined) then g_meshDir     = ""
  56. if (g_meshRecurse == undefined) then g_meshRecurse = false
  57. if (g_fbxSmGroups == undefined) then g_fbxSmGroups = true
  58. if (g_fbxSmMesh   == undefined) then g_fbxSmMesh   = true
  59. if (g_fbxMultiTake == undefined) then g_fbxMultiTake = false
  60. if (g_outFormat   == undefined) then g_outFormat   = 1
  61. if (g_useDefaultFBXSettings == undefined) then g_useDefaultFBXSettings = true
  62.  
  63.  
  64. fn VerifyAXI =
  65. (
  66.     if (g_axImporterVersion == undefined) then
  67.     (
  68.         messageBox "ActorX Importer is not loaded!"
  69.         return false
  70.     )
  71.     if (g_axImporterVersion < 133) then
  72.     (
  73.         messageBox "Your ActorX Importer script is too old, please update!"
  74.         return false
  75.     )
  76.     return true
  77. )
  78.  
  79.  
  80. -- configure FBX exporter
  81. fn SetupFBX =
  82. (
  83.     if g_useDefaultFBXSettings then return undefined
  84.  
  85.     -- http://www.the-area.com/forum/autodesk-fbx/fbx-plug-ins-import-export-discussions/maxscript-export-dialog-properties/
  86.     -- both commands should be used to ensure all commands are functional
  87.     pluginManager.loadClass FBXIMP
  88.     pluginManager.loadClass FBXEXP
  89.  
  90.     -- FbxExporterSetParam "Geometries" true -- <bool>
  91.     -- Controls the state of the "Geometries" checkbox in the FBX Export dialog.
  92.     FbxExporterSetParam "NormalsPerPoly" true -- <bool>
  93.     -- Controls the state of the "Support normals per polygon vertex" checkbox in the FBX Export dialog.
  94.     FbxExporterSetParam "Cameras" false -- <bool>
  95.     -- Controls the state of the "Cameras" checkbox in the FBX Export dialog.
  96.     FbxExporterSetParam "Lights" false -- <bool>
  97.     -- Controls the state of the "Lights" checkbox in the FBX Export dialog.
  98.     FbxExporterSetParam "GeomAsBone" true -- <bool>
  99.     -- Controls the state of the "Geometries used as bones, exported as bones" checkbox in the FBX Export dialog.
  100.     FbxExporterSetParam "Shape" false -- <bool>
  101.     -- Controls the state of the "Shape (Morph modifier)" checkbox in the FBX Export dialog.
  102.     FbxExporterSetParam "Skin" true -- <bool>
  103.     -- Controls the state of the "Skins (Skin Modifier and Physique)" checkbox in the FBX Export dialog.
  104.     FbxExporterSetParam "Animation" true -- <bool>
  105.     -- Controls the state of the "Animation" checkbox in the FBX Export dialog.
  106.     -- FbxExporterSetParam "Resampling" -- <float>
  107.     -- Controls the value of the "Resampling rate (when necessary)" field in the FBX Export dialog.
  108.     FbxExporterSetParam "ShowWarnings" false -- <bool>
  109.     -- Controls the state of the "Show warnings" checkbox in the FBX Export dialog.
  110.     FbxExporterSetParam "EmbedTextures" false -- <bool>
  111.     -- Controls the state of the "Embed textures in export file" checkbox in the FBX Export dialog.
  112.     FbxExporterSetParam "SmoothingGroups" g_fbxSmGroups -- <bool>
  113.     -- True or false. See Smoothing Groups for an explanation of this setting.
  114.     FbxExporterSetParam "SmoothMeshExport" g_fbxSmMesh -- <bool>
  115.     -- True or false. See TurboSmooth for an explanation of this setting.
  116. )
  117.  
  118.  
  119. fn GetExportSubDir =
  120. (
  121.     if (g_outFormat == 1) then
  122.     (
  123.         return "FBX"
  124.     )
  125.     if (g_outFormat == 2) then
  126.     (
  127.         return "ase"
  128.     )
  129.     if (g_outFormat == 3) then
  130.     (
  131.         return "max"
  132.     )
  133.     return "unknown"        -- should not get here
  134. )
  135.  
  136.  
  137. fn SaveAXFile filename =
  138. (
  139.     if (g_outFormat == 1) then
  140.     (
  141.         -- FBX
  142.         exportFile filename #noPrompt using:FBXEXP
  143.         return undefined
  144.     )
  145.     if (g_outFormat == 2) then
  146.     (
  147.         -- ASE
  148.         exportFile (filename + ".ase") #noPrompt
  149.         return undefined
  150.     )
  151.     if (g_outFormat == 3) then
  152.     (
  153.         -- MAX
  154.         saveMaxFile filename
  155.         return undefined
  156.     )
  157. )
  158.  
  159.  
  160. fn ExportFbxAnim =
  161. (
  162.     if (not VerifyAXI()) then return undefined
  163.  
  164.     bones = FindAllBones()
  165.     if (bones.count == 0) then
  166.     (
  167.         messageBox "Mesh is not loaded!"
  168.         return undefined
  169.     )
  170.  
  171.     if (Anims.count == 0) then
  172.     (
  173.         messageBox "AnimSet is not loaded!"
  174.         return undefined
  175.     )
  176.  
  177.     SetupFBX()
  178.  
  179.     -- configure ActorX Importer
  180.     local playAnim = g_playAnim    -- save
  181.     g_playAnim = false
  182.  
  183.     -- export all animations
  184.     if (g_fbxMultiTake == false) then
  185.     (
  186.         -- create target directory
  187.         local dir = getFilenamePath(AnimFileName) + GetExportSubDir() + "\\" + getFilenameFile(AnimFileName) + "\\"
  188.         makeDir dir all:true
  189.  
  190.         for i = 1 to Anims.count do
  191.         (
  192.             local track = Anims[i]
  193.             local trackName = track.Name
  194.             local filename = dir + trackName
  195.             local numFrames = track.NumRawFrames-1
  196.             if (numFrames == 0) then numFrames = 1
  197.             -- clear Take information before the export
  198.             FBXExporterSetParam "SplitAnimationIntoTakes" "-clear"
  199.             FBXExporterSetParam "SplitAnimationIntoTakes" trackName 0 numFrames
  200.             format "Exporting animation % (% frames) -> %\n" trackName numFrames filename
  201.  
  202.             ImportPsaFile AnimFileName i
  203.             SaveAXFile filename
  204.         )
  205.     )
  206.     else
  207.     (
  208.         local dir = getFilenamePath(AnimFileName) + GetExportSubDir()
  209.         makeDir dir all:true
  210.         local filename = dir + "\\" + getFilenameFile(AnimFileName)
  211.         format "Exporting all animations -> %\n" filename
  212.         ImportPsaFile AnimFileName 1 all:true
  213.         for i = 1 to Anims.count do
  214.         (
  215.             local track = Anims[i]
  216.             local trackName = track.Name
  217.             local numFrames = track.NumRawFrames-1
  218.             if (numFrames < 1) then numFrames = 1
  219.             FBXExporterSetParam "SplitAnimationIntoTakes" trackName track.FirstRawFrame (track.FirstRawFrame+numFrames)
  220.         )
  221.         SaveAXFile filename
  222.     )
  223.  
  224.     -- clear Take information after the export
  225.     FBXExporterSetParam "SplitAnimationIntoTakes" "-clear"
  226.  
  227.     g_playAnim = playAnim        -- restore
  228. )
  229.  
  230.  
  231. fn ExportFbxMesh psk_filename =
  232. (
  233.     if (not VerifyAXI()) then return undefined
  234.     SetupFBX()
  235.  
  236. --    format "MESH: %\n" filename
  237.  
  238.     -- create target directory
  239.     local dir = (getFilenamePath psk_filename) + GetExportSubDir()
  240.     makeDir dir all:true
  241.     local filename = dir + "\\" + getFilenameFile(psk_filename)
  242.     format "Exporting mesh % -> %\n" psk_filename filename
  243.  
  244.     ClearMaxScene()
  245.     ImportPskFile psk_filename
  246.     SaveAXFile filename
  247. )
  248.  
  249. fn ExportFbxMeshes path recurse =
  250. (
  251.     if (not VerifyAXI()) then return undefined
  252. --    format "EXPORT DIR % %\n" path recurse
  253.  
  254.     local files = getFiles(path + "/*.psk*")
  255.     for file in files do ExportFbxMesh file
  256.     if recurse then
  257.     (
  258.         local dirs = getDirectories(path + "/*")
  259.         for dir in dirs do ExportFbxMeshes dir recurse
  260.     )
  261.  
  262.     ClearMaxScene()
  263. )
  264.  
  265.  
  266. -- UI
  267. rollout fbxExportRollout "ActorX Batch Export"
  268. (
  269.     -- copyright label
  270.     label     Lbl1 "Version 1.07"
  271.     label     Lbl2 "\xA9 2010-2017 Konstantin Nosov (Gildor)"
  272.     hyperlink Lbl3 "http://www.gildor.org/" \
  273.                     address:"http://www.gildor.org/projects/unactorx" align:#center \
  274.                     color:black hovercolor:blue visitedcolor:black
  275.  
  276.     group "Common"
  277.     (
  278.         label    LblOutFormat      "Output format:" across:2
  279.         radiobuttons RadOutFormat labels:#("fbx", "ase", "max") default:g_outFormat align:#left columns:1
  280.         checkbox ChkDefFbxSettings "Use default FBX settings" checked:g_useDefaultFBXSettings
  281.     )
  282.  
  283.     group "Meshes"
  284.     (
  285.         label    Lbl10           "This tool will convert all PSK meshes from" align:#left
  286.         label    Lbl11           "selected directory to specified output format" align:#left
  287.         label    Lbl12           ""
  288.         edittext EdMeshPath      "Path to PSK" text:g_meshDir width:180 across:2
  289.         button   BtnBrowseMesh   "..."     align:#right height:16
  290.         checkbox ChkMeshRecurse  "Look in subfolders" checked:g_meshRecurse
  291.         checkbox ChkSmGroups     "Smoothing groups" checked:g_fbxSmGroups
  292.         checkbox ChkSmMesh       "Use FBX TurboSmooth" checked:g_fbxSmMesh
  293.         button   BtnExportMeshes "Export meshes"
  294.     )
  295.  
  296.     group "Animations"
  297.     (
  298.         label    Lbl20           "Converts all animations from currently"      align:#left
  299.         label    Lbl21           "loaded PSA to FBX format. A mesh should"     align:#left
  300.         label    Lbl22           "be loaded too. If multi-take FBX is not"     align:#left
  301.         label    Lbl23           "selected, each animation track will produce" align:#left
  302.         label    Lbl24           "a separate FBX file."                        align:#left
  303.         checkbox ChkMultiTakeFbx "Save multi-take FBX file" checked:g_fbxMultiTake
  304.         button   BtnExportAnims  "Export animations"
  305.     )
  306.  
  307.     on RadOutFormat      changed state do g_outFormat             = state
  308.     on ChkDefFbxSettings changed state do g_useDefaultFBXSettings = state
  309.     on ChkMultiTakeFbx   changed state do g_fbxMultiTake          = state
  310.     on BtnExportAnims    pressed do ExportFbxAnim()
  311.     on BtnExportMeshes   pressed do ExportFbxMeshes g_meshDir g_meshRecurse
  312.  
  313.     on EdMeshPath        changed val do g_meshDir = val
  314.     on BtnBrowseMesh     pressed do
  315.     (
  316.         dir = getSavePath caption:"Directory for mesh lookup" initialDir:g_meshDir
  317.         if dir != undefined then
  318.         (
  319.             g_meshDir       = dir
  320.             EdMeshPath.text = dir
  321.         )
  322.     )
  323.     on ChkMeshRecurse changed state do g_meshRecurse = state
  324.  
  325.     on ChkSmGroups changed state do g_fbxSmGroups = state
  326.     on ChkSmMesh   changed state do g_fbxSmMesh   = state
  327. )
  328.  
  329.  
  330. if fbxExportFloater != undefined do closeRolloutFloater fbxExportFloater    -- close old window if visible
  331. fbxExportFloater = newRolloutFloater "FBX Batch Export" 250 560 300 100     -- create new window
  332.  
  333. addRollout fbxExportRollout fbxExportFloater
  334.