Skip to content

Commit bbb4914

Browse files
committed
UNI-42752 code review fixes 2
- move import helper functions into a struct - create a function for switching project
1 parent e5f1d0f commit bbb4914

File tree

1 file changed

+88
-82
lines changed

1 file changed

+88
-82
lines changed

Assets/Integrations/Autodesk/max/scripts/UnityFbxForMaxPlugin.ms

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,6 @@ macroScript UnityImport category:"Unity" tooltip:"Import an FBX file from a Unit
1212

1313
persistent global unityAskSetUnits
1414

15-
fn getOrCreateDummy name = (
16-
unityDummy = getNodeByName (name)
17-
if (unityDummy == undefined) do (
18-
unityDummy = Dummy()
19-
unityDummy.name = name
20-
unityDummy.boxsize = [1,1,1]
21-
)
22-
unityDummy -- return dummy
23-
)
24-
25-
fn getOrCreateRootDummy = (
26-
getOrCreateDummy rootDummyName
27-
)
28-
29-
fn getOrCreateSettingsDummy name = (
30-
unityDummy = getOrCreateDummy name
31-
32-
if not (isProperty unityDummy "modelFilePath") do (
33-
unitySetData = attributes unityData
34-
(
35-
parameters main rollout:params
36-
(
37-
modelFilePath type: #string ui:filep default:""
38-
modelFileName type: #string ui:filen default:""
39-
animFilePath type: #string ui:afilep default:""
40-
animFileName type: #string ui:afilen default:""
41-
)
42-
rollout params "Unity Export Parameters"
43-
(
44-
edittext filep "Model File Path:" text:modelFilePath readOnly:false labelOnTop:true
45-
edittext filen "Model File Name:" text:modelFileName readOnly:false labelOnTop:true
46-
edittext afilep "Animation File Path:" text:animFilePath readOnly:false labelOnTop:true
47-
edittext afilen "Animation File Name:" text:animFileName readOnly:false labelOnTop:true
48-
)
49-
)
50-
custAttributes.add unityDummy unitySetData
51-
)
52-
53-
unityDummy -- return dummy
54-
)
55-
5615
global afterImport
5716
fn afterImport = (
5817
unityFbxExportSet = #()
@@ -69,7 +28,7 @@ macroScript UnityImport category:"Unity" tooltip:"Import an FBX file from a Unit
6928
selectionSets[currSetName] = unityFbxExportSet
7029

7130
-- check if dummy already exists in scene
72-
unityDummy = getOrCreateSettingsDummy currSetName
31+
unityDummy = UnityImportHelpers.getOrCreateSettingsDummy currSetName
7332

7433
if((not isAnimFile) or unityDummy.unityData.modelFilePath == "") do (
7534
unityDummy.unityData.modelFilePath = unityFbxFilePathAttr
@@ -80,46 +39,19 @@ macroScript UnityImport category:"Unity" tooltip:"Import an FBX file from a Unit
8039
unityDummy.unityData.animFilePath = unityAnimFilePathAttr
8140
unityDummy.unityData.animFileName = unityAnimFileNameAttr
8241

83-
unityRootDummy = getOrCreateRootDummy()
42+
unityRootDummy = UnityImportHelpers.getOrCreateRootDummy rootDummyName
8443
unityDummy.parent = unityRootDummy
8544
);
8645

87-
fn loadUnityFbxImportSettings = (
88-
fbxImportSettings = getINISetting (GetMAXIniFile()) "Unity" "UnityFbxImportSettings"
89-
if fbxImportSettings != undefined and doesFileExist fbxImportSettings then(
90-
filein fbxImportSettings
91-
)
92-
);
93-
94-
-- allow multiple files to be selected for import
95-
fn getMultiOpenFilenames caption:"Open" filename:"" types:"All Files (*.*)|*.*" default:1 =
96-
(
97-
local dialog = DotNetObject "System.Windows.Forms.OpenFileDialog"
98-
dialog.multiSelect = true
99-
dialog.title = caption
100-
101-
if doesFileExist filename then
102-
dialog.initialDirectory = filename
103-
104-
dialog.filter = types
105-
dialog.filterIndex = default
106-
107-
local result = dialog.ShowDialog()
108-
if (result.Equals result.OK) then
109-
dialog.filenames
110-
else
111-
undefined
112-
)
113-
11446
-- Make sure the FbxImporter plugin is loaded
11547
pluginManager.loadClass FbxImporter
11648

117-
loadUnityFbxImportSettings()
49+
UnityImportHelpers.loadUnityFbxImportSettings()
11850

11951
unityProjectPath = getINISetting (GetMAXIniFile()) "Unity" "UnityProject"
12052
local unityAssetsPath = (unityProjectPath + "/Assets/")
12153
unityAssetsPath = substituteString unityAssetsPath "/" "\\" -- dot net class requires backspaces
122-
local fbxFileNames = getMultiOpenFilenames caption:"Import FBX from Unity" filename:unityAssetsPath types:"FBX (*.fbx)|*.fbx"
54+
local fbxFileNames = UnityImportHelpers.getMultiOpenFilenames caption:"Import FBX from Unity" filename:unityAssetsPath types:"FBX (*.fbx)|*.fbx"
12355
if fbxFileNames != undefined then
12456
(
12557
-- ask to set units to cm if not already
@@ -168,9 +100,51 @@ macroScript UnityImport category:"Unity" tooltip:"Import an FBX file from a Unit
168100
callbacks.removeScripts #postImport id:#unityPlugin
169101
)
170102

103+
UnityImportHelpers.switchProject unityFbxFilePathAttr
104+
)
105+
)
106+
macroScript UnityExport category:"Unity" tooltip:"Export models and animation to Unity"
107+
(
108+
UnityExportHelpers.exportSelection modelOnly:false
109+
)
110+
111+
macroScript UnityExportModel category:"Unity" tooltip:"Export models to Unity"
112+
(
113+
UnityExportHelpers.exportSelection modelOnly:true
114+
)
115+
116+
struct UnityImportHelpers (
117+
fn loadUnityFbxImportSettings = (
118+
fbxImportSettings = getINISetting (GetMAXIniFile()) "Unity" "UnityFbxImportSettings"
119+
if fbxImportSettings != undefined and doesFileExist fbxImportSettings then(
120+
filein fbxImportSettings
121+
)
122+
),
123+
124+
-- allow multiple files to be selected for import
125+
fn getMultiOpenFilenames caption:"Open" filename:"" types:"All Files (*.*)|*.*" default:1 =
126+
(
127+
local dialog = DotNetObject "System.Windows.Forms.OpenFileDialog"
128+
dialog.multiSelect = true
129+
dialog.title = caption
130+
131+
if doesFileExist filename then
132+
dialog.initialDirectory = filename
133+
134+
dialog.filter = types
135+
dialog.filterIndex = default
136+
137+
local result = dialog.ShowDialog()
138+
if (result.Equals result.OK) then
139+
dialog.filenames
140+
else
141+
undefined
142+
),
143+
144+
fn switchProject filePath = (
171145
-- Change Unity project if fbx is from a different Unity project.
172146
-- Get the project based on the folder structure (i.e. folder above Assets)
173-
local head = unityFbxFilePathAttr
147+
local head = filePath
174148
head = trimRight head "\\/"
175149
-- Check that we are not at the root directory.
176150
while head != "" and not (pathConfig.isRootPath head) do(
@@ -185,17 +159,49 @@ macroScript UnityImport category:"Unity" tooltip:"Import an FBX file from a Unit
185159
)
186160
head = pathConfig.removePathLeaf head
187161
)
162+
),
163+
164+
fn getOrCreateDummy name = (
165+
unityDummy = getNodeByName (name)
166+
if (unityDummy == undefined) do (
167+
unityDummy = Dummy()
168+
unityDummy.name = name
169+
unityDummy.boxsize = [1,1,1]
170+
)
171+
unityDummy -- return dummy
172+
),
173+
174+
fn getOrCreateRootDummy rootDummyName = (
175+
UnityImportHelpers.getOrCreateDummy rootDummyName
176+
),
177+
178+
fn getOrCreateSettingsDummy name = (
179+
unityDummy = UnityImportHelpers.getOrCreateDummy name
180+
181+
if not (isProperty unityDummy "modelFilePath") do (
182+
unitySetData = attributes unityData
183+
(
184+
parameters main rollout:params
185+
(
186+
modelFilePath type: #string ui:filep default:""
187+
modelFileName type: #string ui:filen default:""
188+
animFilePath type: #string ui:afilep default:""
189+
animFileName type: #string ui:afilen default:""
190+
)
191+
rollout params "Unity Export Parameters"
192+
(
193+
edittext filep "Model File Path:" text:modelFilePath readOnly:false labelOnTop:true
194+
edittext filen "Model File Name:" text:modelFileName readOnly:false labelOnTop:true
195+
edittext afilep "Animation File Path:" text:animFilePath readOnly:false labelOnTop:true
196+
edittext afilen "Animation File Name:" text:animFileName readOnly:false labelOnTop:true
197+
)
198+
)
199+
custAttributes.add unityDummy unitySetData
200+
)
201+
202+
unityDummy -- return dummy
188203
)
189204
)
190-
macroScript UnityExport category:"Unity" tooltip:"Export models and animation to Unity"
191-
(
192-
UnityExportHelpers.exportSelection modelOnly:false
193-
)
194-
195-
macroScript UnityExportModel category:"Unity" tooltip:"Export models to Unity"
196-
(
197-
UnityExportHelpers.exportSelection modelOnly:true
198-
)
199205

200206
struct UnityExportHelpers (
201207
fn unitToScaleFactor unit = (

0 commit comments

Comments
 (0)