--matReplace 026 Jeff Tiensivu 11/03/2014 --things to add, name matching, wireframe color, create new materials, render bitmaps to buttons, display name and type nicely --www.JeffTiensivu.com --macroScript matReplace category:"Juice" --Create Array of all Materials in Scene allObjArray = $* oldMats = sceneMaterials if oldMats.count > 0 then ( oldMatsNew = #() for n in 1 to allObjArray.count do( for m in 1 to oldMats.count do( if allObjArray[n].material == oldMats[m] then ( oldMatsNew[oldMatsNew.count + 1] = allObjArray[n].material --print oldMats[oldMats.count].name ) ) ) )else( for n in 1 to allObjArray.count do( oldMatsNew[n] = allObjArray[n].material --print oldMatsNew[n].name ) print "makeUniqueArray may take a while" ) try oldMats = makeUniqueArray oldMatsNew catch( messageBox "there may be more materials listed then exist in the scene" ) --print oldMats -- FUNCTION DECLARATIONS fn matFromLib oldMatNum newMat = ( if newMat != undefined then( for m in 1 to allObjArray.count do( if allObjArray[m].material == oldMats[oldMatNum] then( allObjArray[m].material = newMat --print "replaced material " + allObjArray[m].material.name + " with " + newMat.name ) ) ) ) fn selObjs oldMatNum = ( selArray = #() for m in 1 to allObjArray.count do( if allObjArray[m].material == oldMats[oldMatNum] then( selArray[selArray.count + 1] = allObjArray[m] ) ) if selArray.count > 0 then( select selArray )else( clearSelection() --print "should have cleared selection" ) ) fn changeWireColor oldMatNum newCol = ( selArray = #() for m in 1 to allObjArray.count do( if allObjArray[m].material == oldMats[oldMatNum] then( allObjArray[m].wirecolor = newCol ) ) ) fn nameMatch = ( --compare oldMats vs. currentMaterialLibrary for m in 1 to oldMats.count do( oldTokens = filterString oldMats[m].name "_ :" print oldTokens ) ) -- USER INTERFACE ITEMS --create a huge string for executing our rollout --exString:string exString = "rollout TestRollout \"Select a Material Library or Max File and you can apply it's materials\" ( button libBtn \"Select a Material Library\" width:129 height:21 toolTip:\"This Material Library will be used to Replace the Materials in your Scene\"\n button nameMatchBtn \"Name Match\" width:80 height:21 toolTip:\"Match the Names of the current materials with the library\"\n" for n in 1 to oldMats.count do( --exString += "materialButton oldMatBtn" + (n as string) + " \"" + (oldMats[n] as string) + "\" align:#left across:3 width:140" + "\n" exString += "button selObjBtn" + (n as string) + " \"sel:" + (oldMats[n].name) + "\" align:#left across:3 width:140" + "\n" exString += "dropDownList ddl" + (n as string) + " \"replace " + oldMats[n].name + " with\" items:#(\"pick a library\")\n" --exString += "materialButton newMatBtn" + (n as string) + "\n" exString += "colorpicker cp" + (n as string) + " \"Wireframe Color\" color:[0,0,0] modal:false" + "\n" ) exString += "button matReplaceBtn \"replace materials now!\" toolTip:\"Push this button to make things Happen\"\n" exString += "\t\t on libBtn pressed do( fileOpenMatLib() local newMatLib = #() for n in 1 to currentMaterialLibrary.count do( newMatLib[n] = (currentMaterialLibrary[n].name) ) newMatLib[currentMaterialLibrary.count + 1] = \"\"\n" for n in 1 to oldMats.count do( exString += "TestRollout.ddl" + n as string + ".items = newMatLib\n" ) exString += ")\n\n" exString += "\t\t on nameMatchBtn pressed do( for n in 1 to oldMats.count do( oldTokens = filterString oldMats[n].name \"_ :\" print oldTokens local matchScore = #() for m in 1 to currentMaterialLibrary.count do( newTokens = filterString currentMaterialLibrary[m].name \"_ :\" matchScore[m] = 0 for p in 1 to oldTokens.count do( for q in 1 to newTokens.count do( if oldTokens[p] == newTokens[q] then( matchScore[m] = matchScore[m] + 1 print matchScore[m] ) ) ) if matchScore[m] > 0 then ( if oldTokens.count == newTokens.count then( matchScore[m] = matchScore[m] + 1 ) ) ) --which score is highest highScore = 0 for r in 1 to matchScore.count do( if highScore < matchScore[r] then( highScore = matchScore[r] execute(\"TestRollout.ddl\" + n as string + \".selection = \" + r as string) ) ) if highScore < 1 then( execute(\"TestRollout.ddl\" + n as string + \".selection = \" + (currentMaterialLibrary.count + 1) as string) ) ) )\n\n" for n in 1 to oldMats.count do( exString += "on selObjBtn" + (n as string) + " pressed do( selObjs " + (n as string) + " )\n" exString += "on cp" + (n as string) + " changed new_col do( changeWireColor " + (n as string) + " " + "new_col" + " )\n\n" ) exString += "on matReplaceBtn pressed do(\n" for n in 1 to oldMats.count do( exString += "matFromLib " + n as string + " currentMaterialLibrary[ddl" + n as string + ".selected]" + "\n" ) exString += "closeRolloutFloater matReplaceDialog" exString += "\n)" exString += "\n)" --) matReplaceDialog = newRolloutFloater "matReplace" 500 600 execute(exString) --print extring addrollout TestRollout matReplaceDialog