Problem changing materials inside frame_change_observer

Do not skip the step of leaning to understand modules and classes. They are required for Ruby, and for deploying code in a shared environment such as SketchUp’s Ruby ObjectSpace.

Without modules, you code can clash with the code of other author’s. So ALL of your code must evaluate within YOUR unique module namespace. It is also suggested that within that module namespace, that you separate each of your plugins / extensions within a uniquely named submodule.

This will not help much as Lisp is a 100% functional language. Ruby is a dynamic, multi-paradigm, procedural, 100% object oriented language. And in addition, within SketchUp it becomes event-driven by the user. (Even a sequential block of code must be fired by the user in some way such as chosing a menu item or clicking a toolbar button.)

In order to really code well, you must first learn basic Ruby constructs.

SketchUp’s API adds extensions to the Ruby core modules and classes.

First off, that code example is written to ONLY work upon Dynamic Components. Not upon normal components, not upon normal groups, not upon primitive geometry.

The example model you have posted does not use any Dynamic Components, so the code example you used would not apply anyway.

Please be specific about what kind of objects you want this to work upon. IF it is ALL objects and ALL materials, then perhaps you need a different approach. It may be better to manipulate the materials collection rather than all the object’s material assignments.

In some observer callbacks the answer is NO (and the docs mention this.) Basically any “onChange” type of callback could go into a vicious endless loop if your code makes changes within it (resulting usually in a memory stack overflow.)

Yes your code is incorrect:

  • You have not changed the toplevel Author module namespace to your OWN uniquely named module.
  • You have not indicated in the file preamble what you changed.

Can you confirm that you are still working within SketchUp 2014 and what edition (Make or Pro) ?
(Looking at the file, it is saved as a version 14 model. The oldest version I have installed is 2016 so I could not exactly test in your situation.)

Also, you appear to have not yet installed the 14.1 maintenance update. Why?

See:

1 Like