Assigning a default material.
-
I am trying to add a Brick_Antique material to a face with Ruby. The material is in the model already, but will not apply the material with this code....
mats = Sketchup.active_model.materials brick = mats['Brick_Antique'] ...... oface.material = brick
However, if I make the Brick material current in the model and then use this....
current = Sketchup.active_model.materials.current ..... oface.material = current
It works!
I assume from this that the display name cannot be used to reference the material. So how would I get the internal name of any in-model material if you only know its display name? -
@deanby7 said:
I am trying to add a Brick_Antique material to a face with Ruby. The material is in the model already, but will not apply the material with this code....
mats = Sketchup.active_model.materials > brick = mats['Brick_Antique'] > ...... > oface.material = brick >
However, if I make the Brick material current in the model and then use this....
current = Sketchup.active_model.materials.current > ..... > oface.material = current
It works!
I assume from this that the display name cannot be used to reference the material. So how would I get the internal name of any in-model material if you only know its display name?This should
mats = Sketchup.active_model.materials brick = mats['[Brick_Antique]'] ...... oface.material = brick
-
or use grep
oface.material = Sketchup.active_model.materials.grep(/Brick_Antique/)
john
-
A material has two 'names'.
If you have created it then they'll be the same,
material.name >>> "SomeName" material.display_name >>> "SomeName"
but if you have imported it from an external collection then they are likely to be different - e.g. inside []...
material.name >>> "[SomeName]" material.display_name >>> "SomeName"
Another way to set it by 'display_name
' [i.e. what you can read] would be...
mats = model.materials name = 'Brick_Antique' mat = nil mats.each{|m| if m.display_name == name mat = m break end } oface.material = mat
In you case I assume it's relating to your 'hole making' ?
If you want to match the new reveals' material to the cut face, then you don't even need to know the material's name at all.
Let's assume the face you are making the hole in is referenced as 'face
'.
Then use
omat = **face**.material oface.material = omat
Which takes a direct reference to the material [if any] - sidestepping any need to know its name/display_name at all... -
The last code is exactly what I want. Thanks.
-
@tig said:
Another way to set it by '
display_name
' [i.e. what you can read] would be...
mats = model.materials name = 'Brick_Antique' mat = nil mats.each{|m| if m.display_name == name mat = m break end } oface.material = mat
Still another way:
mats = model.materials name = 'Brick_Antique' mat = mats.find {|m| m.display_name == name } oface.material = mat
Advertisement