Material names changed by SketchUp
-
Hi,
I have a small problem with material names. Let's say I apply a material named 'Stonewall' from one of the Material Collections in the paint bucket tool and there already is a material with this name in the model, SketchUp will rename the new material to 'Stonewall1' and add it to the model under this name. My problem is that I want to know the original name of this material, because I need to load additional information from a file named 'Stonewall.mat'. Is there a way to get the original name? I suppose there isn't but it can't hurt to ask.
Thanks
Ralf -
Did you notice any difference between the strings returned by the
name()
anddisplay_name()
methods for theMaterial
class ??Failing that... you can get the full path string for the material's
Texture
, thus:
(... wherematl
is the reference to theSketchup::Material
instance in question.)if matl.texture # nil if material has no texture matl_path = matl.texture.filename.gsub(/\+/,'/') matl_ext = File.extname( matl_path ) matl_diskname = File.basename( matl_path, matl_ext ) end
gsub(/\+/,'/')
just makes sure to change all occurances of one or more\
chars to a single/
char. -
There is no property that stores the original name. But you could trim of the trailing digits and assume the remainder is the original name.
This regex captures everything but the trailing digits:
/(.*?)\d+$/
-
Recent versions of the API allow you to change a material's name in code.
If you have a tool that's using a file of data that is material-name specific then clearly you need to know which of the possible materials with similar names it is to be applied to.
There are several possibilities.- You manually add a material from a library with the name 'Stonewall', there is NO preexisting material with that name, and therefore the new material gets called 'Stonewall'.
- You have a material already in the model named 'Stonewall' and you manually add a material from a library with the same name, and therefore the new material gets called 'Stonewall1'.
- You have a material already in the model named 'Stonewall' and you manually add a material from a library with the same name, and therefore because there's also a material in the model named 'Stonewall1' the new material gets called 'Stonewall2' [this will go on until 'Stonewall999' and beyond!].
- You have a material already in the model named 'Stonewall1' and you manually add one from a library with the name 'Stonewall', and because there's NO material in the model named exactly 'Stonewall' so the new material gets called 'Stonewall'.
So how do you decide which is the real 'Stonewall' material you want?
It's likely that themodel.materials
collection is in the order they are created [at least in a session].
Therefore to find the 'true' 'Stonewall' material try this:
name='Stonewall' ### or any other material name to be found... mat=Sketchup.active_model.materials.to_a.find_all{|m|m.name=~/^#{name}/}[-1]
Thus the last match in the matches list is the material last added, so logically is the one you want?
To get that material's name usemat.name
...
Advertisement