Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
1,728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. ------------------------------------- UV Islands To Smoothing Groups -------------------------------------
  2. -- By Martin Palko
  3. -- www.martinpalko.com
  4. --
  5. --
  6. -- Tested: - Max 2014
  7. --
  8. -- Install: - Drag script into max, then assign it to a hotkey or menu bar in "Customize -> User Interface"
  9. -- - Can be found by setting the category to "MP_Tools" while in the "Main UI" group
  10. --
  11. -- Usage: - Select any mesh object, and activate the script. Choose a UV channel and hit run.
  12. --
  13. -- Notes: - This script will not affect your modifier stack, it will simply put an edit poly with the
  14. -- smoothing groups at the top.
  15. -- - Undo is not currently supported, so save before, just in case. You can however, just delete
  16. -- the edit poly modifier to get your old smoothing groups back.
  17. -- - Selecting any channel other than UV channel 1 will show a dialogue box asking if you want to
  18. -- reset UVs. You must hit yes for it to work based on the channel you've specified. This
  19. -- will NOT affect the model's existing UV set.
  20.  
  21. macroscript UVIslandsToSmoothing
  22. category:"MP_Tools"
  23. buttontext:"UV 2 Smooth"
  24. tooltip:"Convert UV islands to smoothing groups"
  25. autoUndoEnabled:false
  26. (
  27. -- Helper function to get the index of the first true element in a bit array
  28. function getFirstActiveInBitarray aBitArray =
  29. (
  30. for i = 1 to aBitArray.count do
  31. (
  32. if aBitArray[i] == true do return i
  33. )
  34. -- return 0 if none are found active
  35. return 0
  36. )
  37.  
  38. -- Actually performs the operation on the currently selected object
  39. function ConvertUVIslandsToSmoothingGroups aUVChannel =
  40. (
  41. if $ != undefined then
  42. (
  43. modPanel.addModToSelection(Edit_Poly()) ui:on
  44. local editPoly = $.modifiers[#edit_poly]
  45.  
  46. local facesDone = #{} -- empty bit array since no faces are done
  47. local allFaces = #{1.. polyop.getNumFaces $}
  48. local facesNotDone = allFaces
  49.  
  50. -- Stick on a UVW modifier
  51. modPanel.addModToSelection (Unwrap_UVW ()) ui:on
  52. local uv_modifier = $.modifiers[#unwrap_uvw]
  53. uv_modifier.unwrap2.setTVSubObjectMode 3 -- Use face selection
  54.  
  55. if (aUVChannel != 1) then -- Only need to mess with this if it's not default
  56. (
  57. uv_modifier.unwrap.setMapChannel aUVChannel
  58. uv_modifier.unwrap.reset()
  59. forcecompleteredraw dodisabled:true -- Hacky fix for a bug, see http://www.polycount.com/forum/showthread.php?t=97059
  60. )
  61.  
  62. local uv_islands = #() -- Empty array that will store bitarrays of all our UV islands
  63. local abort = false -- Abort boolean for breaking out of the loop and avoid the performance penalty of using break
  64.  
  65. -- Build array of UV islands
  66. while (facesNotDone.isEmpty == false and abort == false) do
  67. (
  68. nextFace = getFirstActiveInBitarray facesNotDone -- Get next face that hasn't been processed yet
  69.  
  70. uv_modifier.unwrap2.selectFaces #{nextFace} -- Select that face
  71. uv_modifier.unwrap2.selectElement() -- Grow selection to element
  72. uv_island = uv_modifier.unwrap2.getSelectedFaces() -- Get a bitaray of all those faces (representing a UV island)
  73.  
  74. -- Update faces done/not done bit masks
  75. facesDone += uv_island
  76. facesNotDone -= uv_island
  77.  
  78. insertItem uv_island uv_islands (uv_islands.count + 1) -- Add that bitarray to our array of UV islands
  79.  
  80. if uv_islands.count > allFaces.count then -- this should never happen, if it does means we are in an infinite loop and will crash max, so bail
  81. (
  82. abort = true
  83. print ("Error, calculated too many islands, something went wrong")
  84. )
  85. )
  86.  
  87. deletemodifier $ uv_modifier -- Don't need the UV modifier anymore
  88.  
  89. editPoly.autoSmoothThreshold = 180.0 -- If we auto smooth, it should always be in the same smoothing group
  90.  
  91. for island = 1 to uv_islands.count do -- Select and auto smooth each UV island
  92. (
  93. editPoly.SetSelection #Face uv_islands[island]
  94. editPoly.ButtonOp #Autosmooth
  95. )
  96. )
  97. )
  98.  
  99. local isOpen = false -- Store if the rollout is open or closed
  100.  
  101. rollout UV2SmoothRollout "UV_2_Smooth"
  102. (
  103. spinner UVChannelSpinner "UV Channel" range:[1,99,1] type:#integer
  104. button GoBtn " Run "
  105.  
  106. on GoBtn pressed do
  107. (
  108. ConvertUVIslandsToSmoothingGroups (UVChannelSpinner.value)
  109. destroyDialog UV2SmoothRollout -- Close rollout after running
  110. )
  111.  
  112. on UV2SmoothRollout close do
  113. (
  114. isOpen = false
  115. updateToolbarButtons() -- Update the toolbar icon when closing
  116. )
  117. )
  118.  
  119. on execute do
  120. (
  121. if isOpen then --if open, close it
  122. (
  123. destroyDialog UV2SmoothRollout
  124. )
  125.  
  126. else --if closed, open it
  127. (
  128. createDialog UV2SmoothRollout
  129. isOpen = true
  130. )
  131. )
  132.  
  133. on isChecked return isOpen --return the flag
  134.  
  135. on isEnabled do
  136. (
  137. -- Need an editable poly selected to work
  138. if $ == undefined then
  139. (
  140. -- Close the window if it's open and it shouldn't be
  141. if (isOpen) then
  142. destroyDialog UV2SmoothRollout
  143.  
  144. return false
  145. )
  146. else
  147. return true
  148. )
  149. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement