First of all we hope that you are wrapping your code in a unique top-level namespace module and your extension in a submodule of this namespace module.
There are reasons to use a @@var
and reasons to use a @var
. The main reason to use a variable is that the value will change and it will be used so many places that passing it around from method to method would make the code more difficult to maintain.
A local constant should only be used if the value will not change. This will most often not be true for a model reference, especially on the Mac where multiple models can be open at the same time.
Lastly a local reference would be preferred if the reference is only to be used within a single method. When the method ends and returns all local references go “out of scope” and are marked for garbage collection.
Okay, so let us look at your use of these references.
MODEL
is only used within the turnTexture
method, and not within a loop, so really it should be a local reference and the first line :
model = SketchUp.active_model
ANGLE
is also only used within the second method, but within loop nested in another loop. I think again a local reference defined in the first method before the outer loop, and then passed into the second method as a parameter. Ex:
def self.turnTexture
angle = Math::PI / 4
… and within the loop:
new_uv_mapping = rotate_UV_Coordinates_By_Degrees(current_mapping, angle)
And then add the angle
parameter to the second method definition:
def self.rotate_UV_Coordinates_By_Degrees(positions, angle)
… and change all ANGLE
to use the angle
reference.
You might even go further and create reusable references outside the old_points
mapping loop …
cos = Math.cos(ANGLE)
sin = Math.sin(ANGLE)
new_uv_points = []
… so you are not repeatedly calling those methods inside the loop.
Other tidbits
In Ruby the convention is to use all lower case letters for method names and separate words with an underscore character. Ie: turn_texture()
rather than turnTexture()
. The latter looks like VB or JavaScript. But it’s not enforced rigidly by the interpreter.
In the first method:
faces = MODEL.selection.select { |entity| entity.typename == 'Face' && entity.material&.texture != nil }
… can also be:
faces = model.selection.grep(Sketchup::Face).select { |face| face.material&.texture }
Basically avoid using Entity#typename
comparisons as they are slow. Grepping by class identifier is fast.
Even using entity.is_a?(Sketchup::Face)
within a block is faster than comparing a typename string.
Also, you need not call BasicObject#!=()
to compare against nil
as in Ruby only nil
and false
evaluate Falsely. Everything else including 0
, empty strings and empty arrays, etc. evaluate Truthy. Therefore, in a Boolean expression, any object reference to a Sketchup::Material
or a Sketchup::Texture
will eval as true
.