Jump to content

Script: Layers from Materials


Recommended Posts

Lately I have been using a script I found on Script Spot that creates a layer for every material in the scene, and then assigns the corresponding objects to the layer with the same name. Sometimes this script works great, other times is gives me the error message shown in the attached jpeg. I have no idea what in the scene is causing the problems. Are there any scripting experts that can give me a little insite as to why it errors half of the time, and the other half, it works fine?

 

 

 

 

-- put objects onto layers one layer for each material and name the layer by the material name

-- by Olaf Finkbeiner for www.mainworks.de

 

layers = #()

obj = #()

for o in geometry where o.material != undefined do (append obj o)

m = scenematerials

c = m.count

for i = 1 to c do (

layername = m.name

layer = layermanager.newLayerFromName layername

for o in obj where o.material.name == m.name do (layer.addnode o)

)

 

Edited by Crazy Homeless Guy
Link to comment
Share on other sites

Hi Travis, I can't tell exactly why this script would error, but the error means that at some step during the process of collecting the names of materials and creating layers it skipped a step and didn't make a layer, and the next line where it puts the object on the layer it compains that the layer dosen't exist (no addnode).

 

When writing scripts to deal with materials there are some dificult situations to deal with, specifically, materials with nested materials, this script may or may not work with multi-sub object materials, blend or other types of materials. I would look at the complexity of the materials in the scenes that work vs don't work.

 

If I get a chance I will try to do some testing of the parts of the script. The way to debug the script would be to add a few print lines that would print to the listener exactly what it is doing at every step of the way, that way you could look at the values it had when it stopped.

 

Hope any of this helps,

 

-Nils

Link to comment
Share on other sites

Here is the definition of SceneMaterials from the reference:

 

"Contains a virtual array of materials and root level maps corresponding to the materials and root level maps present in the scene. You can get scene materials and root level maps via array indexing and iterate over them in a for loop. The array can be indexed by number, or by name or string to select by material or root level map name. This variable is read-only. See MaterialLibrary Values for more information."

 

The array will contain materials and "root level maps" which means that if you have an environtment map setup, it will be in the sceneMaterials but there is no object with that material which might cause the error.

 

I am not sure what would make your script crash but per Nils' idea here is a quick update of the script which will print to the listener what it does and you will be able to see where it's crashing:

 

-- put objects onto layers one layer for each material and name the layer by the material name
-- by Olaf Finkbeiner for www.mainworks.de

layers = #()
obj = #()

for o in geometry where o.material != undefined do (append obj o)
format "----------------------------------\n"
format "objects with materials: %\n" obj.count

m = scenematerials
c = m.count
format "materials in the scene: %\n" c

for i = 1 to c do
(
   layername = m[i].name
   format "material number:% | material name:%\n" i layername
   layer = layermanager.newLayerFromName layername
   if layer != undefined then format "new layer created: %\n" layer.name
   else format "!! the layer was not created !!\n"
   for o in obj where o.material.name == m[i].name do (layer.addnode o) 
)
format "----------------------------------\n"

Let us know what do you find.

Link to comment
Share on other sites

  • 1 month later...

I still haven't been able to fully figure out what was going wrong with this script. It seems to work through Multi Sub Object materials and A & D materials just fine. I think part of the thing holding it up might be the layer structure that is currently in the model. If I place everything on one layer, then run the script, I seem to have better luck than if I just run the script. It doesn't necessarily work every time, but the frequency with which it works is better if I approach it in this fashion.

 

Thanks for everyone's help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...