How to remove attributes from materials?

I’m working on some Mac specific performance issues. In one example model it has had a lot of use of PlusSpecs, and so all components and materials have custom attributes. My suspicion is that it’s the reading of the attributes that is causing the problem.

The two main symptoms are that although the model is only 7 MB, and about 10,000 faces, it takes several minutes to open the file. As soon as the model is visible, it then takes another few minutes before the cursor stops spinning.

My guess is that it’s the attributes on the materials that cause the slow opening, and the attributes on the components that cause the delay after it has opened. If I use Attribute Inspector to remove the custom attributes from the components, that fixes the delay after the model is opened. If I purge unused, most materials go away, and the slow opening problem is fixed.

I’ve tested a model that had just as many materials, but no attributes, and it doesn’t have the slow opening issue.

My goal isn’t to fix the model, but to find out what in the model is causing the problem, so my next test would be to remove the attributes from the materials, without deleting the materials. From what I’ve seen so far it looks like I can only delete the custom attributes if I paint a surface with a material, then that one material can have its attributes removed.

There are over 4600 materials, and it could take a while to remove the attributes one at a time.

So, getting to the question, is there a way to remove attributes from colors in model that are not currently being used? Either a Mac or Windows solution would be fine, I can open the model instantly with SketchUp running under Parallels, and could fix the issue there, to then try on Mac OS again.

I think @john_drivenupthewall may have messed with this on Mac.

You can remove an attribute_dictionary from a material in a model.

Sketchup.active_model.materials.each{|m|
  ds=m.attribute_dictionaries
  ds.delete("Name_of_Dictionary") if ds
}

To get a list of all materials’ attribute_dictionary names use:

names=[]
Sketchup.active_model.materials.each{|m|
  ds=m.attribute_dictionaries
  ds.each{|d| names << d.name unless names.include?(d.name) } if ds
}
puts names

But a material has to be loaded into the model’s materials.
Other materials in collections stored in folders outside of the model are not accessible in this way.

You COULD edit all of the thousands of SKM files you have in your collections.
It’s no simple task…*
You need to iterate through all of the problem SKM files in turn and extract the file ‘document.xml’ from inside it - a SKM is actually a simple ZIP file, so there are tools available to do this ‘extraction’ and subsequent ‘replacement’ within the ZIP file-format.

Put simply - find the line starting …<n0:AttributeDictionaries count=
and later on find the line saying …</n0:AttributeDictionaries>\n
[Where the … are 4 spaces and the \n is the newline]
Delete those two lines and all lines in between them, by splitting the file’s text at these locations and recombining the bits you want to keep.
Save the changes to that changed file and add it back into the ZIP/SKM.
Repeat until you are done…

*You could either do the processing using Ruby within SketchUp, or if you are proficient with other tools - like the MAC’s Terminal - then perhaps it’s also possible that way…
Now all of those SKMs should no longer contain any attributes - typically these are added by rendering apps, and when a SKM is saved out of a model the attributes are remembered in that xml file.

Thanks for reminding me why I haven’t got to grips with Ruby yet! I do understand lines like this:

|d| names << d.name unless names.include?(d.name)

but I’m more used to:

if(!names.indexOf(d.name)) names.push(d.name);

Neither are very English though. I wonder if Ruby is old school, and uses operators that would feel at home in assembler languages.

I ran the test, seems the materials only use “rubysketch_ezispec”. Getting rid of that didn’t solve the slow file opening. But, the same attribute was on drawing elements and scenes, and deleting those, sadly, also didn’t solve the problem.

This model was just worked on with Mac, does that rule out collections and SKM issues?

Thanks for the detailed suggestions.

The folders of Collections of SketchUp materials are all SKM files - on both PC and MAC.
You’ll just have them saved in different locations ?

Your code example produces the same result as mine !
names<<name is just shorthand for names.push(name)
I also think that the names.include? method is more ‘English’ ?
And unless names… is shorthand for if !names…
There are many ways to do the same thing using Ruby’s syntax…

Good to know about the syntax.

On Mac or Windows I only have one .skp file. Where would I look for the SKM files?

When the materials are in the model the model.materials lists them all - as ‘materials’.
The SKM files are material which are stored externally in a ‘collection’ folder.
Presumably when you save materials from your model into a collection you can see the ‘materials’ folder where they get stored ?
It’s probably set in your Preferences too ?

This model has never had collections. The preferences materials folder is empty. As far as I can tell, everything in the model is inside the skp file.

You misunderstand.
A model never has a ‘collection’.
A model has some materials loaded into it.
A ‘collection’ is a set of external material [SKM] files that is shared across all of your models.
You can save a material or all of your model’s materials into a ‘collection’ folder.
If that ‘collection’ folder is included in the list of possible materials that are available to your models, then you can see its contents in the materials-browser…
Some materials are shipped with SketchUp - these are not ‘in’ the model, but are available to it.
Similarly your own collections of materials can be shared across models…

I didn’t make the model, my materials folder is empty, not sure if their folder is empty.

If I save a new material, it does appear in my materials folder.

You can use Aerilus’ Attribute Inspector to easily see and edit any attributes.

When it comes to external material libraries they shouldn’t matter. They are just files on a disk, not objects SketchUp should have to worry about (except when browsing them in the Material browser). The in model material collection is more likely to be relevant here.

Here’s a very simple snippet to list the materials that have attributes so you don’t need to manually go through them all:

puts Sketchup.active_model.materials.select(&:attribute_dictionaries).map(&:name)

I had [perhaps incorrectly] assumed that you had a collection of materials and you wished to remove their attributes.
If you simply want to remove all attributes from all of a model’s materials, then use something like this in the Ruby Console…

puts;puts
mod=Sketchup.active_model
mod.start_operation("del_adicts", true)
c=0
t=0
mod.materials.each{|m|
  c+=1
  if ds=m.attribute_dictionaries
    ns=[]
    ds.each{|d| ns << d.name }
    ns.each{|n| ds.delete(n) }
    puts "#{m.name} - deleted adicts #{ns}" if ns[0]
    t+=1 if ns[0]
  end
}
puts "Processed #{t} / #{c} Materials\n"
mod.commit_operation

It prints any changed materials’ names and the dictionaries it has deleted.
It also reports the number of materials processed out of the total.
It’s one step undo-able.
Note how I’ve collected the dictionaries’ names, and then removed them from the dictionaries ‘by-name’, because without testing this I’m not sure that iterating the dictionaries directly while deleting a dictionary will affect the processing - in the same way that you can’t iterate a selection directly and delete some of those selected items, because the selection then changes as you process it, causing some selected entities to get missed out !
Although the materials attribute_dictionaries are ‘emptied’ by the processing, they still exist afterwards, so that’s why I trap for an empty array on names as it iterates.

that’s an extreme number of ‘materials’ for any SU model…

are these all unique ‘materials’ [colors or textures] applied selectivley to the 10,000 faces or are they only unique due to having different attributes…

when writing ‘color’ import scripts, I found any more than 400 ‘colours in model’ had serious performance effects on a mac…

closing the ‘Colors’ panel helps, but reducing the count was far more beneficial…

if some colors aren’t being used, why not simply purge them completely?

john

Obviously you can use the Model Info > Statistics to slim down your model - including unused materials.
Or use my PurgeAll tool - which allows you to remove selected types of unused things rather than absolutely everything that’s unused…

But remember that unused layers and materials might be found only inside unused components etc, so unless you purge those unused components their unused layers/materials will remain in the model.

If you still have so many materials, then perhaps many of them are duplicates.
You can use a tool like my SketchUp Plugins | PluginStore | SketchUcation
to consolidate the model’s materials down to a minimum set, if they use the same color/alpha/texture etc…

I’m thinking the question may have been about unused attributes, not unused materials.

However you can’t see if an attribute is being used. If an extension writes it in the first place that extension probably expects to be able to use it later, so unless you are okay with making a model incompatible with the extension that created it, I wouldn’t recommend changing that extension’s attributes.

I think the OP got the SketchUp model from someone else, and so the renderers’ attributes attached to its materials are, in this case superfluous.
There also seems to be an inordinate number of materials in the model - hence the suggestions to purge the materials down to the ‘used’ set and then ‘consolidate’ them further.
Then he can remove the remaining materials’ attributes - if that’s still an issue…
Initially the OP did not give us all of the relevant information that we needed, so there have been several abortive forays into barren ground…

Thanks for the latest suggestions!

John, I mentioned that the goal isn’t to fix this model, but to track down exactly what it is in the model that upsets SketchUp on Mac. So that SketchUp can be fixed.

e3, your script is useful, but it still shows over 4500 materials that have attributes after running TIGs script that deletes them.

About the number of materials, I believe that some extensions use materials as a way to interpret objects. For example, Sefaira seems to rely on the material to know whether you’re dealing with a internal wall, external wall, etc. Anything that has transparency is treated as glass.

With PlusSpecs it’s looking like it uses a small PNG, and from the materials it then knows what it is exporting. Or something like that. But, it seems not to reuse materials, every time you make something you get another unique copy of the material, and if you delete that geometry the material is still listed in colors in model.

So, the problem may end up not being that there are 4600 materials with attributes, but because there are 4600 unique PNGs that are each 64x64 pixels. If Mac preloads material bitmaps and Windows doesn’t, that could explain the difference in the time to open the file.

Have you tried the following steps?

  1. Purge unused [everything] in Model Info > Statistics.
  2. Consolidate all model.materials [using my plugin].
  3. Then finally, delete all remaining materials’ attribute_dictionaries - using the copy/paste of my code…

Note that if a material has attribute_dictionaries defined, then even when you have deleted all of them, that material still has ‘attributes_dictionaries’ attached to it - although it is 'empty - whereas materials that have never been assigned any attribute_dictionary will of course report that they have no attribute_dictionaries…

I marked your other answer as being correct, because it exactly answers what I first asked.

Maybe I should start a new topic, just about what it is wrong with the model. I mentioned in my first post that purging materials solves the slow opening problem, but I also said a couple of times that my goal is to track down exactly what it is that is upsetting SketchUp. If I do a purge, this particular model is fixed, and I can no longer track down what it was that caused the problem.

The model in question is someone else’s, and is confidential. But, I have a version where I deleted all geometry (I added a rectangle so you can see when the model has opened). Components in model has a few dozen components. I haven’t figured out how to delete those without deleting the materials. The original model did have quite a few scenes, so I deleted those too.

This file shows the slow opening problem, and also another issue where when you have the model open, and you’re just looking at it, Mac OS goes into a locked up mode with SketchUp, long periods of spinning cursor. Which is the main original issue I’m working on, the slow opening was just an easy to reproduce variation of the problem.

Here is the file if you have a Mac and are intrigued.

noscenesnogeometryslowopen.skp.zip (1.2 MB)

1 Like