Notifications
Clear all

[Closed] Convert materials in scene to VrayOverrideMtl & ignore selected objects materials?

Hi,

I’m trying to simplify a long winded process, and think it may be achievable in MaxScript. Apologies my knowledge of MaxScript (and any programming) is extremely basic.

What I would like to do is convert every material in a scene to a VrayOverrideMtl, keeping the existing material in the base slot. Then apply a vrayMtl to the Reflect mtl slot, and change the diffuse color of this vrayMtl to black. I’d need the script to ignore certain objects, which could be defined as a selection, layer, etc.

I can’t see how to change the material type through scripting. I can see how it works using the listener, and manually assigning the new material type, but that’s not really how I think it should work. Ideally I think I should be using something like this –

for mat in scenematerials do
(
(
if iskindof o.mat VrayOverrideMtl do
(
)
else
(
SCRIPT TO CHANGE MATERIAL TYPE, ETC
)
)

Any help would be really appreciated.

Thanks,
Dean

11 Replies

What about nested materials?

1 Reply
(@deanpunchard)
Joined: 10 months ago

Posts: 0

What do you mean?

for mat in scenematerials do
(
mat.material = VRayOverrideMtl basemtl:( mat.material) reflectMtl:(VRayMtl Diffuse = color 0 0 0)
)

Maybe something like this?!

Anyone? Really stuck here.
Thanks

You could modify denisT code here to do what you need.

-Eric

Hi Eric,
Thanks for the link.

What I’m really struggling with is a way to capture the existing material, then change the material type to VrayOverrideMtl, then place the old material into the basemtl slot. Most ways I’ve seen simply override the material, and discard the existing material. I need to retain the existing materials.

I think once I have this figured out, the looping through the scene materials should be straight forward (famous last words!)

I’ve tried looking for scripts which might have done something similar with multisubmaterial or vray2sidedmtl but drawing a blank.

Thanks,
Dean

You can either replace existing material with vrayOverrideMtl or apply new vrayOverrideMtl to the dependent nodes.


-- 1.
blackMtl = VRayMtl diffuse:(color 0 0 0) name:"Black Reflection Override"
for mtl in scenematerials where not isKindOf mtl VRayOverrideMtl and superclassof mtl == material do (
    
    tmp = mtl
    vrayOverride = VRayOverrideMtl reflectMtl:blackMtl name:(mtl.name + " override")
    replaceInstances mtl vrayOverride
    vrayOverride.baseMtl = tmp

)    


-- 2.
blackMtl = VRayMtl diffuse:(color 0 0 0) name:"Black Reflection Override"
for mtl in scenematerials where not isKindOf mtl VRayOverrideMtl and superclassof mtl == material do (

    nodes = refs.dependentNodes mtl
    
    if nodes.count > 0 do (

        for n in nodes where not isKindOf n.material VRayOverrideMtl do n.material = VRayOverrideMtl baseMtl:n.material reflectMtl:blackMtl name:(n.material.name + " override")
        
    )
    
)

1 Reply
(@deanpunchard)
Joined: 10 months ago

Posts: 0

Thank you so much! I think version 1 will be perfect! If you ever need a hand with 3D modeling, rendering etc just ask, I owe you!!

Thanks,
Dean

Double post

here is how i would do it… in my example you can see how i replace standard materials with a blend material which uses in map1 slot the source standard material and in map2 newly created standard.


fn replaceMaterials nodes: = 
(
   if nodes == unsupplied do nodes = objects
      
   nodes = nodes as array
   
   mats_to_replace = for mat in scenematerials collect
   (
      nn = refs.dependentnodes mat
      nn = for n in nn where finditem nodes n > 0 and n.material == mat collect n
      
      if nn.count > 0 then #(mat, nn) else dontcollect
   )
   
   for mat in mats_to_replace do
   (
      new_mat = Blend map1:mat[1] map:(Standard diffuse:black)
      mat[2].material = new_mat
   )
   ok
)
(
   undo on 
   (
      replaceMaterials()
      -- replaceMaterials nodes:selection
   )
)

why do i do it this way?

there are several reasons:

#1 i can specify a list of nodes to effect the replacement
note: using replaceinstances we can’t control it, and replace ALL instances of the source material everywhere in the scene and in any max object (ReferenceTarget)

#2 for all nodes which originally had the same material i apply same instance of a new material, so all these nodes still have one same material

#3 all nodes which originally had different materials get a new material with unique slot map2 material, what makes sense

another note:


nodes = refs.dependentNodes mat

when we use this method we have to understand that it might any dependency between material and node, and the case, when this material is applied to this node, even if it’s the most common but only one of possible cases.