PDA

View Full Version : How to Replace Mtl from scenematerials array


bkravi
04-23-2009, 09:31 AM
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

MarcoBrunetta
04-23-2009, 03:00 PM
Not really efficient but how about:
for o in objects where (o.material == scenematerials[1]) do o.material = standard()

j-man
01-18-2010, 05:16 PM
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.

denisT
01-18-2010, 07:21 PM
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.

j-man
01-19-2010, 10:11 AM
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.

j-man
01-28-2010, 03:16 PM
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.

j-man
01-29-2010, 11:33 AM
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.

DaveWortley
01-29-2010, 02:19 PM
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!

j-man
01-29-2010, 02:29 PM
Hi Dave,

I hope it's useful! Please note that I've edited the script above, I had a leftover bit in there < ;

J.

CGTalk Moderation
01-29-2010, 02:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.