Edit materials from code
-
Hello
I have a question
How I can edit a previously created materials? (from code)
and how I can change the display name? -
Access the material object, via the Materials collection.
Then change it's properties. (See the API dictionary for examples, on both.)
-
I just tried changing the display name for a new file, opened with the Susan component.
Her trousers have material 'jean blue'
At the console:
mats = Sketchup.active_model.materials #>> #<Sketchup;;Materials;0x656d2f8> m = mats['jean blue'] #>> #<Sketchup;;Material;0x6525214> m.name.inspect #>> "jean blue" m.display_name.inspect #>> "jean blue" m.name="bluejeans" #>> "bluejeans" m.display_name.inspect #>> "bluejeans"
And I visually see in the Materials Inspector that the display_name has indeed changed.
-
thank you very much, functioned to change the display name
I looked at the API dictionary do not understand,
could you give me an example?
such as changing the color of the material 'blue jean' to red -
I have understood
mats = Sketchup.active_model.materials m = mats['jean blue'] m.color=[180, 0, 0]
I am new to this,that good there are good people willing to help, thank you very much Dan
(I know very little English)
-
I have another related question,
if I have a cube, each side painted a different color and then create a group or component, how I can access the list of materials the component or group to modify these materials from the code?
Would greatly appreciate a simple example of this, I could not find him anywhere
-
Any group [or a component-instance] can themselves have a 'material' - e.g.
group.material
if it's 'nil
' then it has no material, however if it has a material any faces within the group that 'have no material' will display with that group's material [although they still have 'nil
' material individually, they will display with the group's material].
The faces [and edges for that matter] inside a group.entities can have materials [face.material
] - or 'nil
' if none.
Therefore, to find the materials used inside a group [or component definition found from its instance], make a collection of thegroup.entities
faces and then iterate through those faces and collect eachface.material
, use.uniq!
on that array, so you have only one of each; then iterate that array of materials and make a list of their 'names' etc etc...
-
Note:
Material.name
is the internal ID name - whileMaterial.diplay_name
is what SU display in the material browser (exposes to the UI).
For instance, you cannot useMaterial.display_name
as a key in theModel.materials
collection - it will fail in some circumstances (for instance when the name is wrapped in [])Read more: http://www.thomthom.net/thoughts/2012/03/the-secrets-of-sketchups-materials/
-
Diego... see: Ruby Newbie's Guide to Getting Started
-
wow, is more complicated than I expected, I doubt that achieves do with my current knowledge of the Ruby API, I guess I'll have to wait a while thank you very much Dan, Tig and Thomthom for guiding me
-
this is what I have achieved so far
entidad = %(#FF8000)[Sketchup]%(#0040BF)[.active_model.selection][0] curretmaterial = entidad%(#0040BF)[.material] nameM = curretmaterial%(#0040BF)[.name] curretmaterial%(#0040BF)[.color]=[102, 0, 51] curretmaterial%(#0040BF)[.name]=%(#BF8080)["nuevo tono"]
I shall be able then apply the recommendations
(sorry for bad English)
Advertisement