Saving an reloading a material from an attribut
-
You can only save certain types of classesas attributes -
integer
,float
,string
,array
andboolean
.
Other types like 'Sketchup::material
' are not usable.
Also don't think to use its ID. if these are useless across sessions - they change between session as the model loads...
BUT you could simply usematerial.display_name
[which is always unique] and save that string as the attribute... asmat_name=nil; mat_name=e.material.display_name if e.material; e.set_attribute("time", "material", mat_name)
...
then when you want to get it back simply usee.material = e.get_attribute("time", "material", nil)
- this even allows for 'nil' material so no need to make extra tests... You can assign a material by 'name'. -
btw comparing strings to find a entity tyep
e.typename == "Face"
is very slow.
Instead use compare the actuall classes - likee.is_a?(Sketchup::Face)
-
@tig said:
You can only save certain types of classesas attributes -
integer
,float
,string
,array
andboolean
.
Other types like 'Sketchup::material
' are not usable.@tig said:
BUT you could simply use material.display_name [which is always unique] and save that string
he did in fact try that -
material.name
. so it should work. unless.name
returned some bogus data.
The API docs doesn't say for.name
that.display_name
should be used - and it never explains what the difference is. -
But the API does say for
material.display_name
use it instead ofmaterial.name
http://code.google.com/apis/sketchup/docs/ourdoc/material.html#display_name ...
-
ok, i assigned a material to a face with the tool in sketchup that is invoken by the icon with the color bucket. after that i loaded a script with the following code (only the face with the material was selected)
<span class="syntaxdefault">mod </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />ents </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities <br />sel </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br />mats </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials<br />mat</span><span class="syntaxkeyword">=</span><span class="syntaxstring">""<br /><br /></span><span class="syntaxdefault">sel</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display_name<br /> </span><span class="syntaxkeyword">}<br /><br /><br /></span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mats</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">]<br /> } </span><span class="syntaxdefault"></span>
the debugger says: #<NoMethodError: undefined method `display_name' for nil:NilClass>
somehow the material doesn't seem to stored in the materials array...
that can't be true, or? -
Sounds like you've tried to store the material of an entity with the Default material. Entities with default material return
nil
forentity.material
. -
i used some of the materials looking like wood. do they belong to the default materials that return nil?
another opportunity could be to save the whole model in the script, then to do something in the script an after that to load the old version that was saved before.
-
Ok - I'm looking at your snippet again:
<span class="syntaxdefault"><br />mod </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />ents </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities <br />sel </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br />mats </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials<br />mat</span><span class="syntaxkeyword">=</span><span class="syntaxstring">""<br /><br /></span><span class="syntaxdefault">sel</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display_name<br /></span><span class="syntaxkeyword">}<br /><br /><br /></span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mats</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">]<br />} <br /> </span><span class="syntaxdefault"></span>
What are you trying to do with this?
<span class="syntaxdefault"><br />sel</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display_name<br /></span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault"></span>
You're assigning the variable mat a material name - which in effect you'll only have the name of the material of the last entity in the selection you just iterated over.
And I also don't understand what you try to do next:
<span class="syntaxdefault"><br />ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mats</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">]<br />} <br /> </span><span class="syntaxdefault"></span>
@kiesewetter said:
i used some of the materials looking like wood. do they belong to the default materials that return nil?
No. But at least one of the entities you iterate over in the selection has no material (returns nil) - so you need to inspect the data you have at hand. (Maybe you have an edge in the selection without any materials?)
-
You current snippets will only set 'mat' to the last value founs as you aren't doing anything with it in side the {} block ??
Your original example ought to work with set attrib'e.material.display_name
... IF you use the iteration loops to set/set the attributes
e.material=mat_temp
by name [NOT by finding it in the materials list] then if it's '=nil' then the default material is used anyway ? -
I looked at your original code and made some smaller rewrites:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># Store face's material name as attributes<br /></span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">next unless e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?( </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face </span><span class="syntaxkeyword">)<br /> if </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material<br /> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"time"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"material"</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br /></span><span class="syntaxkeyword">}<br /><br /><br /></span><span class="syntaxcomment"># Read face's material name from attributes<br /></span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />materials </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials<br />model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">next unless e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?( </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">material </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"time"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"material"</span><span class="syntaxkeyword">)<br /> if </span><span class="syntaxdefault">material </span><span class="syntaxkeyword">&& </span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">end<br /></span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault"></span>
Note
I did in fact have to usematerial.name
as oppose tomaterial.display_name
.I did some testing on the default metric template in SU8 with the Susan character. When I used
.display_name
I could not restore the material for her hair.SketchUp's material editor lists the material as "Color_B24" and so do
.display_name
.
However,model.materials['Color_B24']
returnsnil
.When I used .name I got
"[Color_B24]"
- and when I usedmodel.materials['[Color_B24]']
I got the correct material.So for some reason SU decides to hide start and end brackets.
Once again the API docs fail to provide accurate data.
It appear that
.display_name
is intended to be used only as presentation to the user - while for referencing materials one should use.name
.sigh
-
first of all i got to thank you fpr the time youre spending.
i wrote the last script to test wheter it is possible to assign a material to an entitiy in that way. i wanted to execute it with only one face selected that has a material (the one looking like wood). the name of the material should be saved in the variable mat.
that should be realised with
sel.each{|e|
mat=e.material.display_name
}after that i wanted to assigne every entity in the model with the same material with that part of the code
ents.each{|e|
e.material = mats[mat]
}i still don't know how to acces a material, to save an reload it. using the diplay_name and saving it as an attribut didn't work so far.
-
Did you try the snipped I just posted?
(p.s. can you please wrap the code you post in code blocks? - Makes it easier to read.)
-
oh, ok, when i wrote my last post yours wasn't uploaded. i'll test it tommorrow because it's meanwhile hard to hold the eyes open. thanks once more for the great help.
good night.
tim -
hallo again!
saving to an attribute and reloading worksbut now another problems appears. i only want the materials name to be stored in the attribute if it is not the name of one of the materials bau_mat, fertig_mat or inplanung.
i tried to realise it with the following code, but it seems to be stored every time, even the assigned material is one of the before mentioned:
if e.material.name != bau_mat.name && e.material.name != fertig_mat.name && e.material.name != inplanung.name mat_vorher = e.material.name e.set_attribute "ablauf", "material", mat_vorher end
-
<span class="syntaxdefault">invalid_materials </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxstring">'bau_mat'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'fertig_mat'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'inplanung'</span><span class="syntaxkeyword">]<br /><br /></span><span class="syntaxdefault">unless invalid_materials</span><span class="syntaxkeyword">.include?(</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> mat_vorher </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name<br /> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"ablauf"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"material"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> mat_vorher</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
-
thank you so much.
-
me again...
i don't know why, but the attribut "ablauf", "material" always receives a new value, even if the face's material is included in the array caled invalid_material. i'll post the code, because i realy don't know how to fix it.
ents.each{|e| if e.typename == "Face" start = e.get_attribute "ablauf", "baubeginn" ende = e.get_attribute "ablauf", "bauende" if (e.material) invalid_materials = ['bau_mat', 'fertig_mat', 'inplanung'] unless invalid_materials.include?( e.material.name ) mat_vorher = e.material.name e.set_attribute("ablauf", "material", mat_vorher) end end if @woche.to_i >= start.to_i && @woche.to_i < ende.to_i e.material = bau_mat end if @woche.to_i >= ende.to_i e.material =fertig_mat end if @woche.to_i < start.to_i e.material = inplanung end end } end #now the code that reloads the material def mat_back mod = Sketchup.active_model ents = mod.entities mats = mod.materials default_mat = mats.add "standardwert" default_mat.color = [50,250,50] ents.each{|e| if e.typename == "Face" if (e.get_attribute "ablauf", "material") mat_temp=e.get_attribute "ablauf", "material" e.material=mats[mat_temp] else e.material=default_mat end end } end
Advertisement