macroScript UnrealTexelDensity category:"Custom" internalCategory:"Custom" tooltip:"Unreal Texel Density Tool" ButtonText:"UTD" autoUndoEnabled:false Icon:#("UVWUnwrapTools2",5) ( --A function which is used to save defaults to a file fn SaveToFile theTexLine = ( local ScriptsFolder = pathconfig.getdir #userscripts local DefaultsFolder = pathconfig.appendPath ScriptsFolder "\\unrealTexelDensityTool" local DefaultsFilePath = pathconfig.appendPath DefaultsFolder "\\defaults.ini" local WarningString = "--This is a default.ini file for Texel Density Tool\n--DO NOT EDIT THIS FILE MANUALLY\n\n" -- If file doesn't exist if not doesFileExist DefaultsFilePath then ( --Create a directory makeDir DefaultsFolder --Create a file local File = createFile DefaultsFilePath --OutPut to log format "% has been created\n" DefaultsFilePath --OutPut theTexLine to a file format "%%" WarningString theTexLine to: File close File --OutPut to log format "Defaults for Texel Density Tool has been saved sucessfully in %\n" DefaultsFilePath ) else ( --Open an existing file for rewrite (deletes file contents if it exists) local File = openFile DefaultsFilePath mode:"w" --OutPut theTexLine to a file format "%%" WarningString theTexLine to: File close File --OutPut to log format "Defaults for Unreal Texel Density Tool has been saved sucessfully in %\n" DefaultsFilePath ) ) --end of fuction --Function that return value from a Position in sequence of values. e.g. in a line "<5> <7>" and Pos = 2 it will return 7 fn LoadValueFromFile ValuePosition = ( local ScriptsFolder = pathconfig.getdir #userscripts local DefaultsFolder = pathconfig.appendPath ScriptsFolder "\\unrealTexelDensityTool" local DefaultsFilePath = pathconfig.appendPath DefaultsFolder "\\defaults.ini" local FilePosition local ReturnValue = "" -- If file exist if doesFileExist DefaultsFilePath then ( --Local counter for iterations local I = 1 --Open File for read-only local File = openFile DefaultsFilePath mode:"r" --Search for "<" symbol if (skipToString File "<") == undefined do ( seek File 0 --Go to the beginning of the file close File return undefined ) --while not end of file while (not eof File) do ( --Read character to memory local c = readChar File --If Postion is found if ValuePosition == I then ( --While c is not ">" collect to ReturnValue and take a next character and then - exit While (not c == ">") and (not eof File) do ( ReturnValue += c c = readChar File ) exit ) else --skip to next ( --Next I I += 1 --Skip to "<" symbol While (not c == "<") and (not eof File) do c = readChar File ) ) ) else return undefined seek File 0 --Go to the beginning of the file close File return ReturnValue ) --end of fuction --Function to calculate how many "meters" in system units fn GetMetersFromGU = ( local meters = case units.systemType of ( #Inches: 0.0254 #Feet: 0.3048 #Miles: 1609.0 #Millimeters: 0.001 #Centimeters: 0.01 #Meters: 1.0 #Kilometers: 1000.0 ) return meters*units.SystemScale --return value ) --end of fuction --new code --Function to add uvwunrap modifier fn uvwFlattenMap objArr mapChannel:1 modName:Name = if objArr.count != 0 do ( clearSelection() SetCommandPanelTaskMode #modify with redraw off ( for node in objArr where validModifier node (modifier = Unwrap_uvw()) do ( addModifier node modifier ui:on modifier.name = modName modifier.setMapChannel mapChannel select node ; modPanel.setCurrentObject modifier modifier.flattenMap 45.0 #([1,0,0], [-1,0,0], [0,1,0], [0,-1,0], [0,0,1], [0,0,-1]) 0.02 on 0 on on --unwrapMod.mappingReset() allfaces = for i = 1 to (modifier.numberPolygons()) collect i modifier.selectFaces (allfaces as bitarray) --OutPut to log format "Defaults for Unreal Texel Density Tool has been saved sucessfully" ) ) ) --end of fuction --Let's create a function to acknowledge Texel Density fn GetTexelDensity theMapSize = ( --Check the Modifier Class whether it is unwrap_uvw or not if classof (unwrapmod = modpanel.getcurrentobject()) == unwrap_uvw do ( --Make an array of selected Faces if (faces = unwrapmod.getselectedfaces()).count > 0 do ( -- Get some information about the selection unwrapmod.getarea faces &mX &mY &mWidth &mHeight &mAreaUVW &mAreaGeom -- Calculate the total texture area textureArea = theMapSize^2 -- Calculate the used area usedAreaPixels = mAreaUVW * textureArea -- Calculate the texel density texelDensity = sqrt (usedAreaPixels/mAreaGeom) -- break() ) ) texelDensity --return value ) --end of fuction --Try to destroy previous rollout try(destroyDialog rol_unrealUVTexelDensity)catch() --Creating new Rollout rollout rol_unrealUVTexelDensity "Unreal Texel Density Tool" ( --defining local variables local theTexDen local theTexDenCluster local theMapSize = 0.0 local theTextureMapArea,theGeomArea,theUsedAreaPixels,theDefinedRatio local obj_ResolutionsArray = #(" 8192x8192", " 4096x4096", " 2048x2048", " 1024x1024", " 512x512", " 256x256", " 128x128") local theULighCh local UnwrapError = "Please select an object with Unwrap UVW modifier applied to it." local ScalePannelState = false local UmodName fn GetResolution ResolutionString = ( local theResolution = case ResolutionString of ( " 8192x8192": 8192 " 4096x4096": 4096 " 2048x2048": 2048 " 1024x1024": 1024 " 512x512": 512 " 256x256": 256 " 128x128": 128 ) return theResolution ) -------------------------------------------------------------------------------------------------------------------------------------------------------- --Interface --Map Dimension and Texel Density spinner spnTexDens range:[0,8192,512] Pos:[100,17] width:50 height:60 type:#integer scale: 128 tooltip: "Texture Texel density" spinner spnULighCh range:[0,10,2] Pos:[100,70] width:55 height:60 type:#integer scale: 128 tooltip: "Unreal LightMap Channel" dropdownlist drpdnMapSize items:obj_ResolutionsArray Pos:[100,43] width:90 selection: 4 Tooltip: "Target map resolution" --spinner spnMapSize "Map Size:" range:[0,8192,1024] Pos:[75,70] width:150 height:16 type:#integer scale:128 tooltip: "Map Size" --Labels label lblTDText "Texel Density\n(Px/m)" Pos:[15,17] width:70 height:50 label lblMSText "Map Size:" Pos:[15,48] width:50 height:15 Tooltip: "Target map resolution" label lblUCText "Unreal Channel:" Pos:[15,70] width:80 height:15 Tooltip: "Unreal Lighmap Channel" --Buttons button btn_setTexelDensity "Set" Pos:[150,14] width:45 height:20 tooltip: "Sets texel density to channel 1" ---------------------------------------------------------------------------------------------------------------------------------------------------------- --Update variables when values changes on spnTexDens changed val do theTexDen = val * (GetMetersFromGU()) --on spnMapSize changed val do theMapSize = val on spnULighCh changed val do theULighCh = val --function that unpress all the checkbuttons fn UnpressButtons = ( chkbtnTop.checked = false chkbtnTopRight.checked = false chkbtnTopLeft.checked = false chkbtnLeft.checked = false chkbtnCenter.checked = false chkbtnRight.checked = false chkbtnBottomLeft.checked = false chkbtnBottom.checked = false chkbtnBottomRight.checked = false ) --end of function ---------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------- ON ROL_UNREALUVTexelDensity OPEN -------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- --Initialize variables on start on rol_UnrealUVTexelDensity open do ( if (LoadValueFromFile 0) != undefined do ( try ( spnTexDens.value = (LoadValueFromFile 1) as integer drpdnMapSize.selection = (LoadValueFromFile 2) as integer spnULighCh.value = (LoadValueFromFile 5) as float UmodName = "UnUVWLmap_Ch"+spnULighCh.value as string ) catch () ) ) --End initialize --button Set on btn_setTexelDensity pressed do ( --new code --messagebox UmodName uvwFlattenMap (selection as array) mapChannel:(spnULighCh.value) modName:UmodName uvwFlattenMap (selection as array) mapChannel:1 modName:"UnUVWTex_Ch1" --new code theTexDen = spnTexDens.value * (GetMetersFromGU()) theMapSize = (GetResolution (drpdnMapSize.selected)) undo "Set Texel Density" on ( if theTexDen >0 then ( if modpanel.getcurrentobject() != undefined and classof (modpanel.getcurrentobject()) == Unwrap_UVW then ( ----it will rescale the cluster to equalize the texel density actionMan.executeAction 2077580866 "40207" -- In this short script 'c' is defining the cluster texelDensityCluster = GetTexelDensity theMapSize format "Texel Density Cluster: %\n" texelDensityCluster --it will acknowledge the texel density of cluster so we can define the ratio --getting the ratio between single face and cluster theDefinedRatio=(theTexDen/texelDensityCluster) theUnwrapmod = modpanel.getcurrentobject() theUnwrapmod.scaleSelectedCenter theDefinedRatio 0 ) else messagebox UnwrapError ) else messagebox "Please set Texel Density correctly." --end of Normalization ) ) ----end of button Set on rol_unrealUVTexelDensity close do ( local theDialogPos = GetDialogPos rol_unrealUVTexelDensity theTextToSave = "<" + spnTexDens.value as string + "><" + drpdnMapSize.selection as string + "><" + theDialogPos.x as string + "><" + theDialogPos.y as string + "><" + spnULighCh.value as string + ">" SaveToFile theTextToSave ) ) --end of full script if (LoadValueFromFile 0) != undefined then ( createDialog rol_unrealUVTexelDensity width:240 height:100 Pos: [(LoadValueFromFile 3) as integer, (LoadValueFromFile 4) as integer] style:#(#style_toolwindow, #style_sysmenu) ) else createDialog rol_unrealUVTexelDensity width:240 height:100 Pos:[1300,230] style:#(#style_toolwindow, #style_sysmenu) --end of rollout )