How can set the material of layer by RUBY API?

I can set the material of the layer by SketchUp, but can’t set the material of the layer by ruby API. and I only set the color of the layer.

You can only use he #color= method to set the color of a layer with the limitation that the alpha channel can not be set. ( Sketchup::Layer alpha channel access ! · Issue #715 · SketchUp/api-issue-tracker · GitHub )

Currently it is not possible to set material. It is requested already many times. E.g.:
Layer.material instead of Layer.color · Issue #7 · SketchUp/api-issue-tracker · GitHub

2 Likes

As said, the API only gives access to colors not materials/textures.
However, you can manually assign materials to tags [aka layers in the API].
This would allow a work round where you can make a model SKP with a set of tags/layers with materials assigned to them and save it.
You them ship it in the extension’s subfolder [or a subfolder within that].
In your code you then import that SKP creating the set of tags/layers it has.
You can then access these tags/layers in your API code, so you have control over the tag/layer material by using these imported ones by name…
It’s not ideal but it’s the only way to do it currently…

PS: You can’t import an empty SKP, so in the subfolder’s SKP make a guide-point at the origin and give that the textured tag/layer…

3 Likes

It’s ok. But I can’t predict the user’s layer.

Your API based interface could import the tag/layer as outlined then ask the user to select the existing tag/layer name [perhaps from a sorted list ?]. Then your code can rename that tag/layer temporarily, then assign the new tag/layer to everything in the model [and definitions] having the renamed tag/layer, then finally you can use the API layers method to remove the renamed tag/layer to tidy up.
Note that any attributes, page-behavior, perhaps color etc associated with original tag/layer will need copying to the new textured one, before it’s removed…

All of this is a faff, but currently without sensible API access to a tag/layer material I can see no alternative…

You will have a hard time to prepare all possible material assigned to different layers in that template (and that template will be big size)… especially if you want to deal with the opacities as well. Unless you will limit to only a “few” choice…
Generally I agree. This is the only way!

Colos can not be copied properly, since alpha channel handling is not working. Even you can not “read” the current value of alpha.

I guess the OP wanted to allow user to assign material as a free choice of a “built in” and perhaps additionally attached…
I believe, that was the reason stating: But I can’t predict the user’s layer.

So, sorry to say again. But color/material handling with Ruby is not possible properly. In my opinion better to tell the users to assig color/material themself…
:peace_symbol: :innocent: