Python: apply material from materiallibrary to node

Python: apply material from materiallibrary to node

Anonymous
Not applicable
1,155 Views
2 Replies
Message 1 of 3

Python: apply material from materiallibrary to node

Anonymous
Not applicable

Hi all !

 

I'm trying to apply a material loaded from a material library to a node

The steps:

 

1. MaxPlus.MaterialEditor.LoadMaterialLibrary

2. matlib = MaxPlus.MaterialLibrary.GetCurrentLibrary()

3. loop through matlib to get specific material

4. node.SetMaterial(mat)

 

 

I get the following error on step 4 : TypeError: in method 'INode_SetMaterial', argument 2 of type 'Autodesk::Max::Mtl'

 

 

MaterialLibrary seems to return MtBase class (node.GetMaterial returns a Mtl class) so I pressume this is the problem ?

 

Anyone knows how I can fix this ? 

 

Thanks !

 

Johan

 

 

 

 

 

0 Likes
1,156 Views
2 Replies
Replies (2)
Message 2 of 3

danielkenobi3d
Explorer
Explorer

You are missing the cast before assigning. 😄 

 

SubAnim = MaxPlus.MaterialLibrary.GetMaterial(MatLib,matInde x)
refMat = SubAnim.GetRefTarget()
Material = MaxPlus.Mtl._CastFrom(refMat)

 

 

0 Likes
Message 3 of 3

danielkenobi3d
Explorer
Explorer

So here is the thing. I am trying to set up a value of undefined,

Just a simple test with materials.

I run the following code


Code:
import MaxPlus
selection = MaxPlus.SelectionManager.Nodes
for i in selection:
    g = i.Material
    print g




if the object has a material will print

Animatable(VRayMtl)

if the object doesn´t have a material will print None

but if I try to run the following code (when the object has a material).
I will run in the following error

 

import MaxPlus
 
selection = MaxPlus.SelectionManager.Nodes
for i in selection:
    i.Material = None



>> MAXScript MacroScript Error Exception:
-- Runtime error: Line 6487 SetMaterial()
<type 'exceptions.ValueError'> invalid null reference in method 'INode_SetMaterial', argument 2 of type 'Autodesk::Max::Mtl' <<


So the question is how come the value of the material is None when it doesn't have the material assigned, but if i try to assign this value directly I get an error. what am I missing? any help here will be appreciated.

0 Likes