Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guide to amorilia's NIF Optimizer script
10-10-2007, 11:37 PM,
#1
Guide to amorilia's NIF Optimizer script
My name is Razorwing from the Silgrad Tower team. If you're a 3D Studio Max modeller like me and haven't tried amorilia's script before you'll most likely want to read this information.

First I'll make a run-of-the-mill disclaimer that you're using this method at your own risk. It's not tested 100% and could cause you problems. All I can say is that I've used it quite a lot myself and haven't experienced any problems at all.

The 3D Studio Max exporter created by the Niftools group has a ton of perks. Whom can't love the ability to set up arrangements of box collision straight in the program, or why not the amazing ability to create convex shapes. But the exporter does have one flaw, and unfortunately it's a very serious one. It bloats your model with hundreds, sometimes thousands, of redundant and invisible faces. If you haven't noticed the effect before, select your model in 3D Studio Max's viewport then right-click and choose 'Properties...'. Note how many faces Max says the model has. Now open the nif in Nifskope and check out how many "Num Triangles" are reported on the NiTriStripsData block (use a calculator if you have multiple NiTriStrips). You can also see the number reported in the status bar of the Construction Set by selecting it in the world window.

[Image: th_PythonScriptBeforeOptimize.jpg]
My wooden cup exported from Max - 779 faces

In the case of my wooden cup Max says it has 219 faces while Nifskope says it has 779 faces -- in other words, the export process has bloated the model by 350% (!). You can imagine the problem becomes dire if you place ten wooden cups in a home since you'll lose ~5,500 faces from the facecount budget. If you use a lot of custom models like Silgrad Tower does then you can imagine how the problem affects an interior. For instance development ceased on one of our taverns because the facecount budget had been maxed out, tallying 452,349 faces, and the only remedy would have been to divide it into two cells. But after running amorilia's script on our model library the tavern's facecount shrunk to an astonishing 284,257 faces in one fell swoop!

The problem with bloated faces can't be remedied in either 3D Studio Max nor Nifskope. You can achieve a slight optimization by restripifying the model through Nifskope, but it's not a substantial improvement and half the time the process actually increase the number of faces. It would also seem you can save a lot of faces by not stripifying your model -- unticking the "Generate Strips" option on the 3D Studio Max export panel -- but that is a really bad idea, so I won't go into detail on how those methods work. I just wanted to mention them in case the question came up as to why the methods weren't used.

[Image: th_PythonScriptAfterOptimize.jpg]
My wooden cup after amorilia's script - 219 faces

But as it happens, the Blender exporter does not have the same problem with bloating as the 3D Studio Max exporter does. And thankfully amorilla, head of Blender script development over at Niftools, has created a script that optimizes nif files outside of Blender. As a sweet bonus the script also updates the tangent spaces of your models, which is a real timesaver. The algorhitm for updating tangent spaces seem on par with Nifskopes, while the 3D Studio Max exporter's UTS function is slightly worse (but better than no UTS at all, obviously).

The script is written in a different language than Nifskope is, so it's not likely it will be incorporated into the program in the foreseeable future. But one of the great things about using the script vs. importing/re-exporting through Blender is that it can do batch operations.

To get the script to work you'll need to get Python 2.5 and PyFFI 0.4.7 (or later versions if you're reading this in the future). Python is a freeware programming language and executable, while amorilia's PyFFI is an interface of sorts between Python and nif files. You'll also need to work through DOS, but if you're uncomfortable using it then don't worry - I'll explain all the steps.

1. Install Python first, and then PyFFI.

2. Create a folder called "python" on your root C:\ drive.

3. Make a full backup of all nifs you plan to optimize before proceeding. The script is not yet fully tested and if it runs into a nif it doesn't understand it may corrupt it and thereby destroy it.

4. Copy the nifs you want to optimize to "C:\python". You might be tempted to drop a large number of models there, but I wouldn't advise that since the script stops when it runs into a nif it doesn't understand, so it's best to do it one folder at a time.

5. Open the start menu in Windows and select Run (img). In the popup, write cmd and hit enter (img). Now a DOS window will appear. Write the following. <enter> means hit the enter key without leaving a blankspace at the end.
cd C:\Program Files\Python25 <enter>
python Scripts\niftoaster.py optimize "C:\python" <enter>

[Image: th_PythonScriptInDos.jpg]
This is what the DOS window should look like if using the default commands.
NOTE: The code has changed in recent versions of the script. Don't write nifoptimize.py,
write like in the paragraph above:
niftoaster.py optimize

NOTES.
- In the first line you tell DOS to browse to your Python install folder. If you didn't install it in the same folder as I did then just replace the local url with your own local url. If you're unsure what the local url is, open the start menu then go to "Programs > Python 2.5", right-click any of the shortcuts and choose properties. Open the Shortcut tab, where the local url is displayed in the "Target:" textbox. Write that url in your DOS window in the same format as the example.
- In the second line you're telling DOS to make Python run a script and process everything in the folder C:\python. If you created a different folder, for instance because your harddrive has the device letter D:\, just write that instead.

6. If everything works like it should, you should get a warning and a confirmation prompt. Write the letter Y (in uppercase) and hit return. Now the program will work it's way through all the nifs in the folder.

NOTES.
If the script comes across a nif it doesn't understand it will stop working and display an error. Just to avoid any risk of using a corrupted nif you should delete everything in the C:\python folder and start the process over again - but this time without the nif that caused you problems.

7. Don't forget to doublecheck that all nifs work afterwards. If it opens in Nifskope and look alright then I'd say it's a safe bet it's not corrupted.

Good luck, and happy optimizing!










Addendum.
Since I wrote this article it became possible to optimize nifs straight from Windows by right-clicking it and choosing "Optimize with PyFFI". If you, like me, don't want to optimize material names, here's how to do it. I originally posted this on the Niftools forum on Jan 20th 2008.

I love almost everything about the optimizer, but one thing I'm not so fond of is that it optimizes the NiMaterialProperties. I like to name my NiMaterialProperty blocks the same as the texture they use, and by default the optimizer 'confuses' them. The nif does become more optimized that way - to what extent I'm not sure - but to me, it makes the nif more confusing when I work with it in Nifskope. I'm grateful amorilia added a feature that lets the user skip certain parts of the optimization, which he describes on the CS Wiki.

But how to get that to work with the "Optimize with PyFFI" option you get when you right-click a nif? I love that new feature and use it a lot Big Grin What I did was I opened a folder, then went to "Tools > Folder Options...". On the File Types tab, scroll down to the NIF entry and click on the Advanced button. Select Optimize with PyFFI and click the "Edit..." button.

In the text box under Application used to perform action, replace the text string with this:
"C:\Program Files\Python25\python.exe" "C:\Program Files\Python25\Scripts\niftoaster.py" optimize -x NiMaterialProperty --pause "%1"
In older versions of the script, the command is: "C:\Program Files\Python25\python.exe" "C:\Program Files\Python25\Scripts\nifoptimize.py" -x NiMaterialProperty --pause "%1"

Click OK to close the window, OK to close the window underneath, and Close to return to the folder. Next time you use the command the script won't optimize the NiMaterialProperty blocks.
¤ How to add images or files to your post ¤ Silgrad's UBBCode
My pet peeve: huge images in img code. I reserve the right to make any such image into a clickeable thumbnail whenever I see it.
Angel mired in filth
[Image: SignatureBannerRazorwing.jpg]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)