/* ---------------------------------------------------------------------------------------- Script Name: AO Bake Swap Filename: SP_Bakescripts_AOBakeSwap Version: v0.01 ---------------------------------------------------------------------------------------- Purpose: - Moves AO style bakes from composite Category: - Bake Scripts Compatibility: - Max 2012. Should work in most other versions Code by: - Michael Vladimir Nicolayeff - SmallpolyArtist@gmail.com - http://www.scriptspot.com/users/smallpoly ---------------------------------------------------------------------------------------- Change Log - v0.x -- 20YY.MM.DD -- Description - v0.1 -- 2017.05.08 -- Initial Version. Created UI and tooltips. - v0.2 -- 2017.05.09 -- Added main functionality - v0.9 -- 2017.05.09 -- Now works on a selection. Removed print statements. - v1.0 -- 2017.05.11 -- Added support for flat diffuse colors. Added support for multi-materials ---------------------------------------------------------------------------------------- Description: - Designed primarily for use with WorldViz software. - On a baked material outputting to Standard:Blinn, copies the lightmap from the Ambient map slot, and the diffuse map from the Diffuse slot into a new Composite material using the selected blending mode. (e.g. Multiply). Usage: - Select the already baked objects you want to do the conversion on. Select the blending mode. Hit "Convert" to convert the materials on that object. Limitations: - Resetting the materials assumes that the composite maps contain both a diffuse map and a lightmap, with the lightmap over the diffuse. Avoid using on objects that have this kind of material. Known Bugs: - Doesn't seem to work with Undo ToDo: - */ macroScript AOBakeSwap category:"# SP_BakeScripts" buttonText: "AOBakeSwap" toolTip:"AO Bake Swap" ( global rolloutSteamRoller mode = "convert" -- convert or revert fb = false blendMode = 5 AOMapSlot = 1 fn getAOMapSlot = ( if fb == true then print "getting the AO map slot" ddAOMapSlot = rolloutSteamRoller.input_dd.selection AOMapSlot = undefined AOMapSlot = case ddAOMapSlot of -- map numbers listed in docs is off by 1 ( 1: 0+1 -- ambient map 2: 5+1 -- self illum map 3: 7+1 -- filter map ) --print AOMapSlot return AOMapSlot ) fn getBlendMode = ( ddlBlendMode = rolloutSteamRoller.output_dd.selection case ddlBlendMode of ( 1: (blendmode = 5; if fb == true then print "5 is multiply") 2: (blendmode = 14; if fb == true then print "14 is overlay") 3: (blendmode = 15; if fb == true then print "15 is soft light") 4: (blendmode = 16; if fb == true then print "16 is hard light") ) return blendmode ) fn makeComposite baked = ( if fb == true then print "building composite maps" sourceDiffuse = baked.diffusemap sourceAO = baked.maps[getAOMapSlot()] -- create composite, set maps and names targetCT = CompositeTexturemap () targetCT.name = "AO Blend" targetCT.mapEnabled.count = 2 targetCT.layername[1] = "Diffuse" targetCT.maplist[1] = sourceDiffuse targetCT.layername[2] = "AO" targetCT.maplist[2] = sourceAO -- set blend mode targetBlendMode = getBlendMode() targetCT.blendMode[2] = targetBlendMode -- multiply -- targetCT.opacity[2] = 100 -- opacity not implemented -- assign the composite and clear the ambient baked.diffuseMap = targetCT baked.maps[getAOMapSlot()] = undefined ) fn unmakeComposite baked = ( -- get rid of composite maps if fb == true then print "unmaking composite maps" sourceCM = baked.diffusemap sourceDiffuse = sourceCM.maplist[1] sourceAO = sourceCM.maplist[2] baked.maps[getAOMapSlot()] = sourceAO baked.diffuseMap = sourceDiffuse ) fn updateComposite baked = ( if fb == true then print "updating composite map" newAO = baked.maps[getAOMapSlot()] if fb == true then print ("new ao is " + newAO as string) if newAO != undefined then ( baked.diffuseMap.maplist[2] = newAO baked.maps[getAOMapSlot()] = undefined ) -- update blend mode targetBlendMode = getBlendMode() if fb == true then print ("target blend mode # is: " + targetBlendMode as string) baked.diffuseMap.blendMode[2] = targetBlendMode -- targetCT.opacity[2] = 100 -- opacity not implemented ) fn processStandard baked = ( if baked.diffuseMap == undefined then ( -- if no diffuse map exists, add in a checkerboard map using the color if fb == true then print ("baked color is" + baked.diffuse as string) colSwatch = Checker () colSwatch.color1 = baked.diffuse colSwatch.color2 = baked.diffuse baked.diffuseMap = colSwatch colSwatch.name = ("RGB: " + baked.diffuse as string) ) if classof baked.diffuseMap == CompositeTexturemap then ( -- if converting and a composite already exists, just update the lightmap and blending mode -- if reverting, disassemble the composite if fb == true then print "already a composite" if fb == true then print ("mode is: " + mode) if mode == "convert" then (updateComposite baked) else if mode == "revert" then (unmakeComposite baked) ) else if baked.diffuseMap != undefined then ( if fb == true then print "something is here that isn't a composite" -- if converting, assemble a composite -- if reverting, no composite exists, so do nothing if mode == "convert" then (makeComposite baked) else if mode == "revert" then (if fb == true then print "already unmade") ) ) fn navMaterials mat = ( -- takes in variable "mat", which is an object's material -- must be a shell, baked must be a standard if classof mat == Shell_Material then ( if classof mat.bakedMaterial == standardMaterial then (processStandard mat.bakedMaterial) if classof mat.bakedMaterial == multiMaterial then ( if fb == true then print "testing multi material" for m in mat.bakedMaterial do (if classof m == standardMaterial then (processStandard m)) ) ) ) fn convertButton = ( /* Goes through selection Looks for shell materials If found, goes into the baked materal Looks for Standard materials and Standard materials that are in multi-materials Copies the diffuse map and lightmap. Lightmap is based on the selected slot. Makes a composite map in the diffuse slot. And creates two layers. Sets the diffuse to the lower layer, lightmap to the upper, then uses the specified blend mode. (and %?) */ mode = "convert" -- convert or revert for s in selection do (navMaterials s.material) ) fn revertButton = ( mode = "revert" -- convert or revert for s in selection do (navMaterials s.material) ) fn testButton = ( AOMat = (getAOMapSlot $.material.bakedMaterial) getBlendMode () ) rollout rolloutSteamRoller "AO Bake Swap v1.0" ( group "Description" -- provides basic info for the user ( label labelInfo "Composites AO Lightmaps with " label labelInfo2 "Diffuse Maps for Vizard R5 Shaders" Height:20 label labelHelp "[ Hover for Usage Info ]" tooltip: "Usage: \nSelect the already baked objects you want to do the conversion on. Select the blending mode. Hit \"Convert\" to convert the materials on that object. \n\nSome items like dropdown lists also have tooltips." label spacer1 "" height:0 ) group "Input" -- lets the user pick which slot to grab from ( label InputInfo1 "Baked Lightmap Location:" dropdownlist input_dd items:#("Standard:Blinn, Ambient Map", "Standard:Blinn, Self-Illum Map", "Standard:Blinn, Filter Map") tooltip:"Pick the option that matches Render to Texture's output location for your lightmap. /nYou can use the Self-Illumination or Filter map slot if the Ambient map slot is recieving a traditional lightmap." ) group "Output" -- lets the user pick what blending mode to use. ( label outputInfo1 "Composite Map Blending Mode:" dropdownlist output_dd items:#("Multiply", "Overlay", "Soft Light", "Hard Light") selection:1 tooltip:"Pick the blending mode to use for the composite map. The lightmap will be on the upper layer." -- spinner spnBlendPercent (blend percent not implemented in shader at this time) ) label spacer2 "" height:0 button btnRevert "Revert Back" align:#left across:2 tooltip:"Remove composite maps and place diffuse and lightmaps back into their slots based on the dropdown list in Input." button btnConvert "Convert Maps" align:#right tooltip:"Create composite maps, transfer the diffuse and lightmap, and apply the selected blending mode." label spacer3 "" height:0 on btnRevert pressed do revertButton() on btnConvert pressed do convertButton() -- button btnTest "test stuff" -- on btnTest pressed do testButton() label spacer4 "" height:0 label labelWarning "Undo not supported. Save before using." hyperLink checkUpdate "- Check for Updates - " address:"http://www.scriptspot.com/3ds-max/scripts/aobakeswap" align:#center color:(color 255 255 0) visitedColor:(color 255 255 0) label spacer5 "" height:0 ) try (destroyDialog rolloutSteamRoller) catch() -- createDialog rolloutSteamRoller height:210 width:200 createDialog rolloutSteamRoller width:240 )