[Closed] Loading Specific Material from a MaterialLibrary
Hello everybody,
I am trying to make somes changes to the CommonTools, a script of Tim Jones ( http://www.seraph3d.com/ ). I am trying to improve the Material tools given by Tim adding a new features. One of these features is the capability to change the present material by the user and store them into a materialibrary. My problem was that I was looking for some function to open a material from a materiallibrary without changing the currentMaterialLibrary but i couldn`t find such as basic function. I think that i solve it properly but i am not sure if it is the best way to do it. So I would like that any GuruScripters tell me how i can improve it.
Thank in advances,
ting
/**
* Open a specific MaterialLibrary from a file
* file The path of the Material Library
* return The array of materials
*/
fn getMaterialFromFile file =
(
local curr_mtl = getMatLibFileName()
if (loadMaterialLibrary file) == false then
return undefined
local temp_mtl =#()
for i=1 to currentMaterialLibrary.count do
temp_mtl[i] = currentMaterialLibrary[i]
if (loadMaterialLibrary curr_mtl) == false then
return undefined
return temp_mtl
)
/**
* Save a specifiv Material into a specific MaterialLibrary
*/
fn setMaterialToFile file index amat =
(
local curr_mtl = getMatLibFileName()
if (loadMaterialLibrary file) == false then
return false
if index > currentMaterialLibrary.count then
return false
currentMaterialLibrary[index] = Copy amat
fileSaveMatLib()
if (loadMaterialLibrary curr_mtl) == false then
return false
return true
)
rollout test "MaterialTest" width:100 height:177
(
local mats = #()
local path = "$Matlibs/CommonToolsAdv_Materials.mat"
button mat_btn1 "" pos:[10,5] width:73 height:15 toolTip:"ShellMatte"
button mat_btn2 "" pos:[10,23] width:73 height:15 toolTip:"Matte"
button mat_btn3 "" pos:[10,41] width:73 height:15 toolTip:"Modeling"
button mat_btn4 "" pos:[10,59] width:73 height:15 toolTip:"Plaster"
button mat_btn5 "" pos:[10,77] width:73 height:15 toolTip:"White"
button mat_btn6 "" pos:[10,95] width:73 height:15 toolTip:"Black"
materialButton mat_mbtn1 "" pos:[2,5] width:8 height:15
materialButton mat_mbtn2 "" pos:[2,23] width:8 height:15
materialButton mat_mbtn3 "" pos:[2,41] width:8 height:15
materialButton mat_mbtn4 "" pos:[2,59] width:8 height:15
materialButton mat_mbtn5 "" pos:[2,77] width:8 height:15
materialButton mat_mbtn6 "" pos:[2,95] width:8 height:15
button reset_btn "Reset" pos:[2,113] width:82 height:15
on test open do
(
mats = getMaterialFromFile path
mat_btn1.caption = mats[1].name
mat_btn2.caption = mats[2].name
mat_btn3.caption = mats[3].name
mat_btn4.caption = mats[4].name
mat_btn5.caption = mats[5].name
mat_btn6.caption = mats[6].name
)
on mat_btn1 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[1]
on mat_btn2 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[2]
on mat_btn3 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[3]
on mat_btn4 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[4]
on mat_btn5 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[5]
on mat_btn6 pressed do
meditmaterials[medit.getactivemtlslot()] = mats[6]
on mat_mbtn1 picked amat do
(
mats[1] = copy amat
mat_btn1.caption = mats[1].name
setMaterialToFile path 1 mats[1]
)
on mat_mbtn2 picked amat do
(
mats[2] = copy amat
mat_btn2.caption = mats[2].name
setMaterialToFile path 2 mats[2]
)
on mat_mbtn3 picked amat do
(
mats[3] = copy amat
mat_btn3.caption = mats[3].name
setMaterialToFile path 3 mats[3]
)
on mat_mbtn4 picked amat do
(
mats[4] = copy amat
mat_btn4.caption = mats[4].name
setMaterialToFile path 4 mats[4]
)
on mat_mbtn5 picked amat do
(
mats[5] = copy amat
mat_btn5.caption = mats[5].name
setMaterialToFile path 5 mats[5]
)
on mat_mbtn6 picked amat do
(
mats[6] = copy amat
mat_btn6.caption = mats[6].name
setMaterialToFile path 6 mats[6]
)
on reset_btn pressed do
(
-- Create The New Shell_BlurMatte
mats[1] = Shell_Material()
mats[1].originalMaterial = MatteShadow()
mats[1].originalMaterial.applyatmosphere = on
mats[1].originalMaterial.atmospheredepth = 1
mats[1].originalMaterial.affectAlpha = off
mats[1].originalMaterial.receiveshadows = off
mats[1].originalMaterial.name = "Matte_NO_SHDW"
mats[1].bakedMaterial = MatteShadow()
mats[1].bakedMaterial.applyatmosphere = on
mats[1].bakedMaterial.atmospheredepth = 1
mats[1].bakedMaterial.affectAlpha = on
mats[1].bakedMaterial.receiveshadows = on
mats[1].bakedMaterial.name = "Matte_SHDW"
-- Save The New Sell_BlurMatte Material into The MTL
setMaterialToFile path 1 mats[1]
-- Update the New Button Caption
mat_btn1.caption = mats[1].name
-- Create The NewMatte
mats[2] = matte()
mats[2].name = "Matte"
setMTLMeditObjType mats[2] 1
mats[2].applyatmosphere = true
mats[2].atmospheredepth = 1
-- Save The NewMatte Material into The MTL
setMaterialToFile path 2 mats[2]
-- Update the New Button Caption
mat_btn2.caption = mats[2].name
-- Modeling
mats[3] = standard()
mats[3].name = "Modeling"
mats[3].diffuse = color 128 128 128
mats[3].specular = color 255 255 255
mats[3].specularlevel = 50
mats[3].glossiness = 0
mats[3].selfillumamount = 25
-- Save The New Modeling Material into The MTL
setMaterialToFile path 3 mats[3]
-- Update the New Button Caption
mat_btn3.caption = mats[3].name
-- White Plaster
mats[4] = Standard()
mats[4].name = "White Plaster"
SetMTLMeditObjType mats[4] 1
mats[4].specularlevel = 0
mats[4].glossiness = 0
mats[4].useSelfIllumColor = off
mats[4].selfIllumAmount = 0
mats[4].ambient = color 255 255 255
mats[4].diffuse = color 255 255 255
mats[4].specular = color 255 255 255
-- Save The New White Plaster Material into The MTL
setMaterialToFile path 4 mats[4]
-- Update the New Button Caption
mat_btn4.caption = mats[4].name
-- Pure White
mats[5] = Standard()
mats[5].name = "Pure White"
setMTLMeditObjType mats[5] 1
mats[5].specularlevel = 0
mats[5].Glossiness = 0
mats[5].useSelfIllumColor = off
mats[5].selfIllumAmount = 100
mats[5].Ambient = color 255 255 255
mats[5].diffuse = color 255 255 255
mats[5].specular = color 255 255 255
-- Save The New Pure White Material into The MTL
setMaterialToFile path 5 mats[5]
-- Update the New Button Caption
mat_btn5.caption = mats[5].name
-- Pure Black
mats[6] = Standard()
mats[6].name = "Pure Black"
setMTLMeditObjType mats[6] 1
mats[6].specularlevel = 0
mats[6].Glossiness = 0
mats[6].useSelfIllumColor = off
mats[6].selfIllumAmount = 100
mats[6].Ambient = color 0 0 0
mats[6].diffuse = color 0 0 0
mats[6].specular = color 0 0 0
-- Save The New Pure Black Material into The MTL
setMaterialToFile path 6 mats[6]
-- Update the New Button Caption
mat_btn6.caption = mats[6].name
)
)
createDialog test 88 130
maybe u have to save currentmateriallibrary to a file and load the library file u need ,after work with it , load the saved “currentMaterialLibrary” with the old materiallibrary. max `s currentMaterialLibrary is a system global , so only one materialLibrary in activition .
Hello/Ni Hao hblan,
That is the same of what I am doing. Read the first lines of code of my script. I created to functions, setMaterialFromFile() & setMaterialToFile, however I dont know if this is the best way to do it.
Regards,
ting