macroScript NS_CoronaPbrGlossy category:"Corona Renderer" tooltip:"Corona PBR Glossy converter" buttontext:"Corona PBR Glossy" ( -- VARIABLES global nsConvertGlossyCorona rolMain global mtlsArr=#() global lut = #(0.332, 0.333, 0.333, 0.333, 0.334, 0.334, 0.335, 0.336, 0.337, 0.339, 0.342, 0.345, 0.349, 0.354, 0.359, 0.366, 0.374, 0.382, 0.391, 0.401, 0.412, 0.424, 0.437, 0.449, 0.463, 0.477, 0.491, 0.505, 0.519, 0.534, 0.548, 0.562, 0.576, 0.59, 0.603, 0.617, 0.629, 0.642, 0.654, 0.666, 0.677, 0.688, 0.699, 0.709, 0.719, 0.728, 0.737, 0.746, 0.755, 0.763, 0.771, 0.778, 0.786, 0.793, 0.8, 0.806, 0.813, 0.819, 0.825, 0.83, 0.836, 0.841, 0.846, 0.851, 0.856, 0.861, 0.865, 0.87, 0.874, 0.878, 0.882, 0.886, 0.89, 0.894, 0.898, 0.901, 0.905, 0.908, 0.911, 0.915, 0.918, 0.921, 0.924, 0.928, 0.931, 0.934, 0.937, 0.94, 0.943, 0.946, 0.949, 0.952, 0.955, 0.959, 0.962, 0.965, 0.969, 0.973, 0.978, 0.98, 1.00) -- SEARCH CORONA MATERIALS RECURSIVELY fn GetMtlsRecursive matsSearchArr = ( for mat in matsSearchArr do ( mtlCls = CoronaMtl -- APPEND FOUNDED TARGET MATERIAL if classof mat == mtlCls then append mtlsArr mat -- GET ALL MTLS IN MULTIMATERIAL else if classof mat == MultiMaterial then ( for i in idsArr where (findItem mat.materialIDList i != 0) do GetMtlsRecursive #(mat[i]) ) -- GET ALL MTLS IN NESTED MATERAILS LIKE BLEND else if classof mat != UndefinedClass then ( propNames = getpropnames mat propsArr = for prop in propNames collect getproperty mat prop matProps = for prop in propsArr where superclassof prop == material collect prop for submat in matProps do GetMtlsRecursive #(submat) ) ) ) -- GET CORONA MATERIALS fn GetMtlTargets = ( mtlCls = CoronaMtl mtlsArr=#() case rolMain.radMtlSelType.state of ( 1: ( matsFromSel = #() for obj in selection where isproperty obj "material" and obj.material != undefined do appendIfUnique matsFromSel obj.material GetMtlsRecursive matsFromSel ) 2: mtlsArr = getClassInstances mtlCls ) ) -- EXCLUDE MATERIALS BASED ON PBR MODE fn ExcludeMtls = ( propValid = case rolMain.radConvertType.state of ( 1: false 2: true ) validMtls = for mtl in mtlsArr where IsProperty mtl "pbrMode" and GetProperty mtl "pbrMode" == propValid collect mtl mtlsArr = validMtls ) -- CHANGE MATERIALS PROPERTIES fn ChangeMatProps = ( convertType = rolMain.radConvertType.state for mtl in mtlsArr do ( case rolMain.radConvertType.state of ( 1: ( mtl.legacyMode = false mtl.pbrMode = true glossyArrNum = (mtl.reflectGlossiness * 100) as integer if glossyArrNum == 101 then glossyArrNum = 100 mtl.reflectGlossiness = lut[glossyArrNum+1] ) 2: ( mtl.pbrMode = false glossyVal = mtl.reflectGlossiness -- find closest glossy value in lut table foundLutItem = finditem lut glossyVal if foundLutItem == 0 then ( itemBefore = 1 for i=1 to lut.count while glossyVal > lut[i] do itemBefore = i itemAfter = itemBefore+1 if glossyVal-lut[itemBefore] < lut[itemAfter]-glossyVal then foundLutItem = itemBefore else foundLutItem = itemAfter ) mtl.reflectGlossiness = (foundLutItem-1 as float)/100 ) ) ) ) Rollout rolMain "" ( radiobuttons radMtlSelType "" labels:#("Selected objects","All materials in scene") default:1 columns:1 across:2 align:#left radiobuttons radConvertType "" labels:#("1.4 > 1.5","1.5 > 1.4") default:1 columns:1 align:#left tooltip:"1.4 > 1.5: \nGlossy value convert to 1.5 \nPBR Mode enable \nLegacy disable \n\n1.5 > 1.4 \nGlossy value convert to 1.4 \nPBR Mode disable \nLegacy don't change" checkbox chkPbr "Based on PBR Mode" checked:true align:#left tooltip:"Better to be enabled! \n\n1.4 > 1.5 affects only non-PBR materials\n1.5 > 1.4 affects only PBR materials" button btnGo "GO" width:40 height:30 align:#right offset:[5,-55] on btnGo pressed do ( st = timestamp() GetMtlTargets() if chkPbr.checked then ExcludeMtls() ChangeMatProps() format "BatchMedit Processing Time: %ms\n" (timestamp()-st) ) ) -- CLOSE AND CREATE FLOATER try (CloseRolloutFloater nsConvertGlossyCorona) catch() nsConvertGlossyCorona = NewRolloutFloater "Corona PBR Glossy v1.0" 300 95 1500 120 AddRollout rolMain nsConvertGlossyCorona )