try (
ScriptWindow.ScriptList.items.Remove(Ab_CopyPasteMat.Controla)
) catch()
rollout Ab_CopyPasteMat ""
(
local Controla
local DataContext
fn hasMat o =
(
o.mat != undefined
)
fn SaveThumb filepaths Image Width = (
c = bitmap Width Width filename:filepaths
rgbas=#();ib=Image.count;ic=ib
for i = 1 to ib do (
if mod ic 3 == 0 then (
append rgbas #(color Image[ic] Image[ic-1] Image[ic-2])
)
ic=ic-1
)
jsy = 88
jsx = 0
for i = 1 to rgbas.count do (
jsy =jsy- 1
setPixels c [jsy,jsx] rgbas[i]
if jsy == 0 do (
jsy = 88
jsx += 1
)
)
save c gamma:1
close c
)
fn SaveMatPreview mat filepath =
(
local GlobalInterface = dotNetClass "Autodesk.Max.GlobalInterface"
if GlobalInterface != undefined do
(
local mtl = meditMaterials[24]
meditMaterials[24] = mat
local slot = GlobalInterface.Instance.COREInterface14.GetMtlSlot 23
if slot != undefined do
(
local pssize = (dotNetClass "Autodesk.Max.PostageStampSize").Large
local pstamp = slot.CreatePStamp pssize true
SaveThumb filepath pstamp.Image pstamp.Width
pstamp.Dispose()
slot.Dispose()
)
meditMaterials[24] = mtl
)
)
fn OnRequestCopy =
(
if selection.count != 1 or $.mat == undefined then messageBox "请选择单个有材质的物体"
else
(
local mat = $.mat
local matItem = dotNetObject "Aobai.UI.Settings.CopyMatItem"
matItem.MatName = mat.name
matItem.MatClass = classof mat as string
if isKindOf $.modifiers[1] Uvwmap then
(
local uvw = dotNetObject "Aobai.UI.Settings.UVW"
uvw.Unit = Ab_Common.convertUnitToString units.SystemType
local mpp = Ab_Common.getModfProps ($.modifiers[1])
for pp in mpp do
(
local item = dotNetObject "Aobai.UI.Settings.MaxPropItem"
item.Name = pp[1]
item.Type = pp[2]
item.Value = pp[3]
uvw.Props.Add item
)
matItem.UVW = uvw
)
local maps = usedMaps mat
for map in maps do matItem.Maps.Add map
DataContext.SaveCopy matItem &matfile &thumbfile
saveTempMaterialLibrary (MaterialLibrary mat) matfile
SaveMatPreview mat thumbfile
)
)
fn OnRequestCopyElse =
(
if not MatEditor.isOpen() then messageBox "请先打开材质编辑器"
else
(
local mat = meditMaterials[activeMeditSlot]
local matItem = dotNetObject "Aobai.UI.Settings.CopyMatItem"
matItem.MatName = mat.name
matItem.MatClass = classof mat as string
local maps = usedMaps mat
for map in maps do matItem.Maps.Add map
DataContext.SaveCopy matItem &matfile &thumbfile
saveTempMaterialLibrary (MaterialLibrary mat) matfile
SaveMatPreview mat thumbfile
)
)
fn OnRequestPaste = undo on
(
if selection.count == 0 then messageBox "请选择要粘贴材质的物体"
else if DataContext.PasteId == undefined then ("未知提示")
else
(
local matItem = DataContext.FindCopyById DataContext.PasteId &matfile
local matlib = loadTempMaterialLibrary matfile
if matlib == undefined then ("未知提示")
else
(
local maps = matItem.Maps.ToArray()
local mat = matlib[1]
if DataContext.IsMapsChecked then
(
local files = #()
local mapdir = if maxFilePath != "" then maxFilePath + @"maps\" else MagicMax.MapsDir
makeDir mapdir
for map in maps do
(
local targetPath = mapdir + filenameFromPath map
copyFile map targetPath
append files targetPath
)
Ab_Common.ResolveMaps #(mat) files
)
else
(
Ab_Common.ResolveMaps #(mat) maps
)
selection.mat = mat
if DataContext.IsUVWChecked and matItem.UVW != undefined do
(
local mpp = #()
for i = 0 to matItem.UVW.Props.count - 1 do
(
local item = matItem.UVW.Props.Item[i]
append mpp #(item.Name, item.Type, item.Value)
)
local modf = Ab_Common.createModf Uvwmap mpp matItem.UVW.Unit
if modf != undefined do Ab_Common.applyModf modf
)
)
)
)
fn OnRequestPasteElse = undo on
(
if selection.count == 0 then messageBox "请选择要粘贴材质的物体"
else if DataContext.PasteId == undefined then ("未知提示")
else
(
local matItem = DataContext.FindCopyById DataContext.PasteId &matfile
local matlib = loadTempMaterialLibrary matfile
if matlib == undefined then ("未知提示")
else
(
local maps = matItem.Maps.ToArray()
local mat = matlib[1]
if DataContext.IsMapsChecked then
(
local files = #()
local mapdir = if maxFilePath != "" then maxFilePath + @"maps\" else MagicMax.MapsDir
makeDir mapdir
for map in maps do
(
local targetPath = mapdir + filenameFromPath map
copyFile map targetPath
append files targetPath
)
Ab_Common.ResolveMaps #(mat) files
)
else
(
Ab_Common.ResolveMaps #(mat) maps
)
MatEditor.open()
if MatEditor.mode != #basic do MatEditor.mode = #basic
medit.PutMtlToMtlEditor mat activeMeditSlot
)
)
)
fn OnRequestPick = undo on
(
if selection.count == 0 then messageBox "请先选择对象"
else
(
SetSelectFilter 1
local po = pickObject message:(DataContext.FindString "material01Message1") filter:hasMat
if isValidNode po do
(
selection.mat = po.mat
if DataContext.IsUVWChecked do
(
if isKindOf po.modifiers[1] Uvwmap do Ab_Common.applyModf po.modifiers[1]
)
)
)
)
fn OnRequestPickElse = undo on
(
SetSelectFilter 1
local po = pickObject message:(DataContext.FindString "material01Message1") filter:hasMat
if isValidNode po do
(
MatEditor.open()
if MatEditor.mode != #basic do MatEditor.mode = #basic
medit.PutMtlToMtlEditor po.mat activeMeditSlot
)
)
fn Init=
(
Controla=dotNetObject "Aobai.MaxTools.ScriptViews.Material.CopyPasteMatControl"
DataContext=Controla.DataContext
dotNet.removeAllEventHandlers DataContext
dotNet.addEventHandler DataContext "RequestCopy" (Ab_CopyPasteMat.OnRequestCopy)
dotNet.addEventHandler DataContext "RequestCopyElse" (Ab_CopyPasteMat.OnRequestCopyElse)
dotNet.addEventHandler DataContext "RequestPaste" (Ab_CopyPasteMat.OnRequestPaste)
dotNet.addEventHandler DataContext "RequestPasteElse" (Ab_CopyPasteMat.OnRequestPasteElse)
dotNet.addEventHandler DataContext "RequestPick" (Ab_CopyPasteMat.OnRequestPick)
dotNet.addEventHandler DataContext "RequestPickElse" (Ab_CopyPasteMat.OnRequestPickElse)
ScriptWindow.ScriptList.items.Add(Controla)
)
)
Ab_CopyPasteMat.Init()