Change line color programmatically


Hi, I just joined.

As the title of my topic says, I, like others before me, want to change edge colors from code. I have read all the relevant threads on this forum going back years, and there have been several people inquiring about the same issue, so I’m not alone in this quest, but I have not seen anything that would answer our need. Some advise to do it with PaintBucket but that’s a manual operation. That’s not what we want. So, could anyone tell me please, why this snippet does not work (line color remains black), and how to go about it?

require 'sketchup.rb’

model = Sketchup.active_model
entities = model.entities
view = model.active_view

pts = []
pts[0] = [0, 0, 0]
pts[1] = [30, 0, 0]
pts[2] = [40, 60, 0]

line0 = entities.add_line(pts[0], pts[1])
line1 = entities.add_line(pts[1], pts[2])
line2 = entities.add_line(pts[2], pts[0])
puts line2.material
line2.material = "red"
puts line2
puts line2.material

And the output is:

load “C:/sketchup_line_colors2.rb”


So, the first puts prints a blank line because the material has has not been set yet, then after: line2.material = "red"
the second puts prints it as something. Is that something a black color and that’s why it is drawn black, or is it a red color but disregarded because of some setting, or is it red but the line was drawn first with black color and then not redrawn even though I tried to refresh the view and the problem lies with the refresh, or is it something else?

I’m on a Win7 machine, using Sketchup Make 16.1.1449 64-bit



The system changed the way my question appears!!! What’s with the big red font? That’s not how I pasted it in! Also, stuff got lost. Here we go, second try …

The output is:

load “C:/Users/nyerges/Documents/sketchup_line_colors2.rb”



Arrg. Third try …

The output is:

load “C:/Users/nyerges/Documents/sketchup_line_colors2.rb”



Ok, I give up. I paste the output in this text window and then click “Create Topic” or “Reply” and your website just deletes three lines of text!!! Hm.


The forum software processes your input as if html, so if your input contains things that look like html it will act accordingly. To avoid this, put a triple backquote on blank lines before and after your text:

 <now this shows without html swallowing the brackets>

Edit: as @DanRathbun notes below, the forum’s software actually uses the Markdown language, not html.


Hmm, this is what shows when I run your snippet from the Ruby console. Looks to me that the edge got colored red. I wonder why it didn’t work for you…

By the way, if the missing stuff looks like this:


That’s because you just puts’d the Ruby objects. You need to access a property of the object and likely use its to_s method to get a printable string. For example,

puts line2.material.color.to_s


Also if the current Style doesn’t ‘display edges by material’, then you will not see the ‘red’ color…


When working with materials, default materials are the nil object. To see the output for line 2, use:

puts "line2.material= #{line2.material.inspect}"

The output is:
line2.material= nil
Trying to output its name when it is nil will raise an exception.

As @TIG mentioned, you need to change the edge styles so that materials are displayed:

But, you may want to do that programmatically as well.


That’s what setting rendering_options[“EdgeColorMode”] = 0 does! After this code, one does not need to manually edit the style.

But you are right that trying to access a property of nil will cause an exception.

Off-Topic:(Click to expand and learn about forum post formatting.)

Read this primer on using the forums:

The post feature uses Markdown formatting. So this:

So, this:




renders as:

And to render some Ruby code:


def some_method(arg)
  if arg.is_a?(Numeric) && arg > 0
    puts "The argument is: #{arg.to_s}"
    return arg + 3

` ` `

renders as:

def some_method(arg)
  if arg.is_a?(Numeric) && arg > 0
    puts "The argument is: #{arg.to_s}"
    return arg + 3


Hi Dan

Thanks for the quick response. How do I insert a captured image into the forum reply?



Hi Guys, thanks for all these speedy replies.

@slbaumgartner: So, it works on your machine? This is what I get:

Maybe I need to do something with my Sketchup install? A global setting perhaps?


@BruceYoung: I looked at this setting and I already have it as suggested:

Still, no joy.


Yes, it works for me (per my earlier image attached). I’m on a Mac. Potentially there is a difference between Mac and Windows on this?

Readers, could another Windows user try the code and see what you get?


Its working for me on Win and Mac.

EDIT: I had previously said (and already deleted) that I could see the same problem in SU 2014. I was wrong, 2014 is working fine for me. I had commented out the code that set the color to red in my testing. My fault! This is working fine for me in 2014, 2016, Mac and Win.


Works fine for me !
Win10, v2016 etc


Color By Layer will over-ride material colors - do you have Color By Layer on?

Sketchup.active_model.rendering_options["DisplayColorByLayer"] = false


In your screen shot, there are “style modified” arrows on the Style icon, this means you need to update the “00 Default Colors” style.


@jim_foltz: Whoopee, that last one did it. I have a red line.

But now, the ruby console output window scrolled for about 20 seconds and displayed a bunch of these lines:

Color(255,   0,   0, 255)
Color(255,   0,   0, 255)
Color(255,   0,   0, 255)
Color(255,   0,   0, 255)
Error: #<SystemStackError: stack level too deep>

And then it just stopped. I suppose it reached some limit and it timed out. Restarting Sketchup temporarily fixed this behavior, so the next session did not exhibit it at first run. Then the second and subsequent runs did the same thing (scrolling). At the end, it complains abut the stack level being too deep. Hm, I don’t have any recursion, at least not intentionally. Any ideas what’s going on here, guys? It looks like it’s stuck in an infinite loop fetching the same material until finally a higher level supervisory routine cuts it off.


Oh, my bad, forget my last question (stupid).

Ok, so thank you, thank you. You all helped me a great deal, with fast, professional responses. I like …:slight_smile: