Opacity/Transparency operations in Sketchup Scene

Hello everybody,

I’m trying to do some manipulations on colors with Skp 2017 and Ruby.
I need to assign different colors and opacity to different surfaces.

I saw there are two ways to change a face opacity:

  • first : use a 4 channel color, ex : (255,255,255,255)
  • second : use a 3 channel color and set an opacity to the material

I need to use the second solution.

So I have a float number for the alpha value I want to use and a color. I’m trying to assign them to a face by creating a new material. This is important because I don’t want faces with same color to change their opacity value as I change another face value.

So here is a simplification of my code:

mod = Sketchup.active_model
ent = mod.entities.first

alphaToSet = 0.589
colorToSet = Sketchup::Color.new(124,124,124)

materialToSet = Sketchup::Material.new
materialToSet.color = colorToSet
materialToSet.alpha = alphaToSet

ent.material = materialToSet

So the two unexpected error are the following :

  • materialToSet.color = colorToSet retruns “wrong argument type (expected Sketchup::Material)”. But you can find here in the API that the expected argument is a color !
  • ent.material = materialToSet retruns “no implicit conversion to Color”. But you can find here in the API that the expected argument is a material

One thing I cannot do (so please don’t propose it without testing) is :

  • assign the colorToSet in the ent.material
  • then assign an alpha value to the entity with : ent.material.alpha = alphaToSet
  • This would make all the surfaces with the same color having the same opacity.

I would be very glad to learn how to do this basic operation of color assignment as it is the last part of a much more complex program I’d like to finish.

I repeat myself but this is important : I need that every surface has a different material ! Otherwise all faces would synchronize their opacity value.

Warning, this will be a forehead smacker, … so put on a soft hat. :wink:

This is your problem …

The Sketchup::Material class is a Sketchup::Entity subclass. These classes are “thinly wrapped” C++ objects created by the SketchUp model engine, so most of them do not implement normal plain Ruby constructor methods. (There are few exceptions, one is the Sketchup::Camera class.) Meaning that the default Ruby constructor does not create a valid API object even though it creates an invalid Ruby object. (Ie, the invalid Ruby object is not referencing an object that is owned by the model or it’s collections.)

Instead, they are created via the use of “factory methods” called upon the model object’s various collections that hold these entity subclass objects.

In the case of a material entity, you need to call the #add method of the model’s materials collection, in order to create a valid material object.

Ex …

materialToSet = mod.materials.add("MyColor_1")
#<Sketchup::Material:0x00023f96e46d30>
materialToSet.color = colorToSet
#=> Color(124, 124, 124, 255)

… gives no error.

2 Likes

Hello @DanRathbun,

It took me some time to figure all this out and test it. But I now have my functions working properly.
Thank you for your fast and helpful support.

1 Like