Max to Sunflow Exporter v0.25
by David Baker 07.04.2008

Installation:

The main script (max2sunflow025.mse) goes in the main Scripts folder (usually
C:\Program Files\Autodesk\3ds Max 9\Scripts)
The macroscript (max2sunflow.mcr) goes in the macroscripts folder:
C:\Program Files\Autodesk\3ds Max 9\ui\macroscripts
The 4 icons (*.bmp) go in the icons folder:
C:\Program Files\Autodesk\3ds Max 9\ui\Icons
Now start Max, go to Customize -> Customize User Interface... and add the macroscript
to a toolbar or menu of your choice (I always use the 'Extras' toolbar for my bits and pieces)

If you are going to be exporting a lot of particles, or very heavy scenes, it night be an
idea to increase the memory allocation for MAXScript. You can do this by going into the
'Customize -> Preferences... -> MAXScript' settings, and increasing the 'Initial heap allocation'
setting. I've got mine set to 64MB.

The source code for the script will eventually be available, but at the moment it is a complete mess (which is why it's encrypted)

What's in there at the moment: 

Antialias settings 
Sample settings 
Filter settings 
Bucket settings
GI settings 
Photon settings

Background colours 

All camera types
If you enable DOF, you can use the cameras 'Far Range' to focus.
Also with DOF, if the 'Lens Sides' setting is less than 3, a circular lens will be used.

Point (omni / free point) and directional lights (free / target spotlights / directlights)
IES_Sun, mr_Sun and Daylight systems
IBL (image-based lighting) is automatic if an HDR or EXR image is used as an environment map (and is turned on). To get the same view of your HDR in SunFlow as in Max, you will need to set the U value of the tiling to -1
Meshlights: the object MUST have a name beginning with "meshlight", and a standard material with an output map in the diffuse channel.
						The diffuse color is multiplied by the output amount to get the radiance amount.

Primitives: sphere, torus, teapot, plane (if the planes render scale is greater that 1 it gets converted to an infinite ground-plane)
Standard particle systems and PFlow systems
Hair and Fur modifiers: the width setting is taken from the "Root Thick" setting in the modifier, the amount from the "Hair Count" setting, and the shader just uses the the "Root Color" setting to create a diffuse shader
Renderable splines
Everything else gets turned into a generic mesh (including UVs and Material IDs)
Object instances get exported as SunFlow instances

Shaders:
	Diffuse - all objects with no material assigned get a diffuse shader with the object colour
					- all objects with a mental ray Arch & Design material get a diffuse shader using the diffuse colour and including bump maps (if present)
					- all objects with a material type not covered by the other shaders get a diffuse shader with the object colour
					- standard and raytrace materials with shader type OrenNayerBlinn get exported as a diffuse shader

	Constant - any materials with a Self-Illumination value of more than 0 get exported as a constant shader
					 - any materials with a Self-Illumination / Luminosity colour more than 0,0,0 get exported as a constant shader

	Glass	- any materials with an opacity of less than 100 are exported as a glass shader
					using the Diffuse colour and IOR set in Max

	Mirror - raytrace materials with a reflection colour more than 0,0,0 get exported as a mirror shader
					 using the Reflect colour as the mirror colour
				 - raytrace materials with a reflection amount of more than 0 get exported as a mirror shader
				 	 using the Reflect Amount to set a greyscale for the mirror colour

	Phong	- standard and raytrace materials with shader type Blinn or Phong get exported as a phong shader
					using the Diffuse and Specular colours in Max
				- the blur strength is calculated using the Softness setting in Max x 1000
					(so if you want a blur strength of 2, you need to set Softness to 0.002)

	Ward - standard and raytrace materials with shader type Anisotropic or Multi-Layer get exported as a ward shader 
				 using the Diffuse and Specular colours in Max. The X and Y roughness is calculated using the
				 Glossiness setting (0 = very blurry, 100 = sharp) and the Anisotropy setting (0 = X and Y equal,
				 50 = Y half the width of X, etc.)

	Shiny - standard and raytrace materials with shader type Metal or Strauss get exported as a shiny shader
					using the Diffuse colour
				- shininess is set using the Glossiness amount
				
	Uber - any Shellac material exports the Base Material as an Uber shader
			 - diffuse color and texture are set using the Diffuse color and map slot in Max
			 - specular color and texture are set using the Specular color and map slot in Max
			 - diffuse and specular blend amounts are set using the Map Amount settings for the Diffuse and Specular maps divided by 100
			 - samples are set using the Specular Level amount in Max
			 - glossiness is set using the Soften amount in Max (I couldn't use the Glossiness setting in Max because it only handles integers)
	
	Amb-Occ - any Standard material with a Falloff map in the Diffuse slot exports as an amb-occ shader
					- the Falloff front colour is used for the SunFlow bright colour
					- the Falloff back colour is used for the SunFlow dark colour
					- at the moment, the "samples" and "dist" values are fixed at 32 and 10. If I can think of a good way of setting them in Max, I'll change this

IMPORTANT - shaders always get assigned in the following order:
						1. Any standard material with a falloff map in the diffuse slot gets an amb-occ shader
						2. Any material with self-illumination gets a constant shader
						3. Any material with transparency gets a glass shader
						4. Any raytrace material with reflection gets a mirror shader
						5. After that the different shader types get assigned according to the material types listed above.

Textures: bitmaps in the Diffuse map channel get exported as part of the shader
					bitmaps in the Bump map channel get exported as bump modifiers, using the 'Bump Amount' setting in the Output section of the map for the strength

Support for single frames and animations
- This uses the Render Scene Dialog to get the image size and whether to render a still, the whole animation or a range of frames

Animations get written to multiple files (test0000.sca, test0001.sca, etc.)
Particle systems get written to an extra file which has the same filename as the .sca file, but the file ending *.part.sca.
If the 'Separate geometry file' checkbox is checked, then all geometry gets exported to an extra file with the ending *.geo.sca

There is also a 'Skip' checkbox which is only active if the 'Separate geometry file' is checked.
This is useful if you've already exported everything once, and just need to tweak materials and render settings.
When this is on, the exporter will export everything except the geometry.

If the 'Start Sunflow after exporting' checkbox is checked, the exported scene is automatically rendered
(the first time you do this you need to set the paths to Sunflow and Java by clicking on the 'Set Paths' button)

Version info:

v0.1 - first release

v0.11 - added particle export

v0.12 - transforms were going wrong for some objects - fixed
			- tidied up a few functions

v0.13 - fixes another transform problem if objects are created in Edge mode instead of Center mode

v0.14 - adds support for PFlow particle systems
			- adds all of the GI options

v0.15 - fixes a bug with particle systems
			- adds photon settings
			- adds support for IES_Sun, mr_Sun and Daylight systems

v0.16 - adds shader support
			- generally faster (more than 50% faster when exporting lots of particles)
			- tidied up the code

v0.17 - updated the script to work with SunFlow v0.07.2
			- fixed a bug in particle export
			- adds camera settings for lens sides and rotation
			- shaders now use the material name if a material has been assigned
			- changed the file extension of particle files form *.bin to *.part.sc
			- added an option to export geometry to a separate file with the extension *.geo.sc

v0.18 - the script now checks the screen resolution, and makes the rollout shorter for lower resolutions
			- legacy particle systems only export the particles visible in the viewport (doesn't apply to PFlow)
			- adds camera motion blur
			- adds diffuse texture map export
			- adds bump map export
			- adds UV export for meshes
			- fixes a bug when exporting Strauss materials

v0.19 - fixes a problem with German-language Max not finding cameras
			- fixes a problem with mental ray shaders stopping export
			- fixes a bug where the exporter tried to export UVs that didn't exist
			- fixes a bug with negative scale values
			- the script no longer exports multiple duplicated materials
			- groups now get exported properly
			- fixes some shaders being exported twice
			- adds image-based lighting

v0.20 - adds settings for light samples
			- removes the image / animations settings from the rollout. The exporter now uses the settings in the Render Scene Dialog
			- adds meshlights
			- adds object instancing and material instancing
			- fixes a bug in animation export

v0.21 - fixes a bug with incorrect scaling of instances
			- fixes a bug with instanced materials not always being recognized
			- changes the way of getting blur strength for Phong shaders (now uses Max softness setting x 100)
			- adds an INI file so that settings from the previous session get loaded when you start the script
			- changes the way of outputting camera transforms, which means that roll angles now get exported properly
			- adds support for the debug shaders (GI, photons and caustics)
			- the script is now a macroscript, and can be started from a toolbar
			- adds support for the Uber shader
			- adds the option to start rendering the scene after exporting it (the first time you do this you need to set up the paths first)

v0.21a - fixes automatic rendering after export
			 - changes the encryption of the script so that it works with older versions of Max again

v0.22 - adds the "Enable Jitter" option to the Sampling settings
			- adds support for spherical cameras (the old exporter only had support for the fisheye camera)
			- adds an option for importance sampling to the IBL settings
			- adds a setting for SunSky turbidity
			- fixes a bug with filenames for separate geometry files
			- planes are now only infinite if the render scale is greater than 1
			- changes the way of getting blur strength for Phong shaders again (now uses Max softness setting x 1000)
			- fixes a bug with GI settings in the INI file not being loaded properly
			- changed the layout of the Camera section to avoid confusion with the different settings for different camera types
			- adds an option to reverse the bucket order
			- fixes a bug where the exporter would crash if the camera was deleted before exporting the scene
			- fixes a bug that was causing problems in Max if the script aborted during export

v0.23 - bumpmaps now work in Uber shaders
			- fixes bumpmaps on instanced objects being missed
			- adds support for Multi/Sub-Object materials
			- adds a "Skip geometry" checkbox. If you've already exported your geometry, and just want to fiddle with the materials or sunflow settings, this will save you some time.
			- fixes a bug in the way that camera transforms are exported
			
v0.24 - adds support for Hair and Fur modifiers
			- adds support for the Amb-Occ shader
			- fixes a bug where exporting a scene more than once resulted in the bump modifiers being left out
			- fixes a problem with naming bump modifiers
			- finally fixes the "center" value for IBL (to get this to work properly, you will need to set the "U Tiling" value in Max to -1)

v0.25 - adds support for renderable splines
			- adds basic support for the mental ray Arch & Design material, turning them into diffuse shaders using the diffuse color (or texture map if one is used)
			- adds support for bump maps on mental ray Arch & Design materials
			- the script now defines any material that isn't a Standard / Raytrace / Shellac / Arch & Design material as a diffuse shader using the objects wire colour
			- adds support for Free_Point lights (you may need to adjust the power setting, at the moment it uses the candela value divided by 1500, and then multiplied by the multiplier value)
			- fixes a bug where objects with a multi/sub-object material and no UVs were missing the "face_shaders" parameter
			- changes the way of setting bump map strength, it now uses the the 'Bump Amount' value from the maps' Output settings