[Closed] How to Replace Mtl from scenematerials array
Hi,
Is there anyway i can replace material of scenematerials array- MaterialLibrary
for example if I do –
scenematerials[1] = standard()
this will create new material istead of replacing existing one. all my objects those who have this material applied there material will remain unchanged.
I know scenematerials is read only variable but there must be alternative way of doing it
thanks in advance
bkr
Not really efficient but how about:
for o in objects where (o.material == scenematerials[1]) do o.material = standard()
Hi,
I’m going to resurrect this thread. I think while this is a good answer, it’s not really good enough (excuse me Marco! < : )
This will come apart when taking into account multis, blends etc. Is there a sure fire method for replacing one material with another? I imagine if will start with refs.dependents
Any ideas?
Josh.
this function ex-changes two materials for objects and for any materials with sub materials
fn xchangeMats source target = if source != target and iskindof source material and iskindof target material do
(
for n in refs.dependents source do case of
(
(isvalidnode n and n.mat == source): n.mat = target
(iskindof n material): for i=1 to (getNumSubMtls n) where (getSubMtl n i) == source do setSubMtl n i target
)
target
)
it’s easy to modify the function to take into account Custom Attributes as well.
Hi Dennis,
It’s a good script. I like how you have the error checking outside of the function definition, I don’t think I’ve seen that before.
Thanks for your help.
Josh.
I’ve expanded on this so that the search is now recursive so that blends of blends etc. will be found, and also so that materials in the material editor will be replaced.
-- Recursive functions to find and replace materials, in the scene and in the material editor.
fn replacematerialserachmtlsubs m s t=if getnumsubmtls m>0 then for i=1 to (getNumSubMtls m) do if getNumSubMtls (getSubMtl m i)==0 then (if (getSubMtl m i)==s then setSubMtl m i t) else replacematerialserachmtlsubs (getSubMtl m i) s t
fn replacematerial s t=
(
for n in refs.dependents s do case of
(
(isvalidnode n and n.mat==s) : n.mat = t
(iskindof n material) : replacematerialserachmtlsubs n s t
(iskindof n Material_Editor) :
(
for j=1 to meditmaterials.count do case of
(
(meditmaterials[j]==s) : medit.PutMtlToMtlEditor t j
default : replacematerialserachmtlsubs meditmaterials[j] s t
)
)
)
)
-- replacematerial <source material> <target material>
No fancy error checking like Denis’
Josh.
Hi,
If anyone is interested in this I’ll post another update. Maxwell materials are a little special at the moment so I’ve had to adopt a special case scenario < : I believe it wont be a problem when they release another version of their plugin.
I’ve also expanded the recursive function so it’s a little easier to read.
-- Replace Material.
-- www.joshuanewman.net
-- Recursive functions to find and replace materials, in the scene and in the material editor.
fn replacematerialsearchmtlsubs m s t=
(
if getnumsubmtls m>0 then for i=1 to (getNumSubMtls m) do
(
if (getSubMtl m i)!=undefined then
(
if getNumSubMtls (getSubMtl m i)==0 then
(
if (getSubMtl m i)==s then
(
setSubMtl m i t
)
) else
(
replacematerialsearchmtlsubs (getSubMtl m i) s t
)
)
)
)
fn replacematerial s t=
(
for n in refs.dependents s do case of
(
(isvalidnode n and n.mat==s) : n.mat = t
(iskindof n material) : replacematerialsearchmtlsubs n s t
(iskindof n Material_Editor) :
(
for j=1 to meditmaterials.count do case of
(
(meditmaterials[j]==s) : medit.PutMtlToMtlEditor t j
default : replacematerialsearchmtlsubs meditmaterials[j] s t
)
)
)
)
Josh.
Sweet, I was looking at this a couple of weeks ago and was writing loads of code to get around this, looks like you guys have a more efficient function than I did!
Hi Dave,
I hope it’s useful! Please note that I’ve edited the script above, I had a leftover bit in there < ;
J.