Transform array

I am trying to set a transformation object to the identity matrix but I am unable to access the 16 element array.
If I try to inspect the values with a call to trans.to_a the value returned is an empty array.

 trans = Geom::Transformation.scaling(1,1,1)

arr = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
trans =

  trans.to_a => []

I have even attempted to inspect the transform array of a constructed transform which I know is properly constructed since the tranformations are correct in the model but I stll cannot access the array.
Does anyone have any ideas?

No idea @AlanMatthews. Could the to_a method have been redefined somehow?

p IDENTITY # => #<Geom::Transformation:0x0001d653b3eec8>
p IDENTITY.to_a # => [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]

Also not what I get. Something is funky in your setup, though maybe it is calling the methods with the wrong arguments?

> t.to_a
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
> t2=Geom::Transformation.scaling([10,20,30],5.0)
> t2.to_a
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -8.0, -16.0, -24.0, 0.2]
> t3.to_a
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]

I remember a bug having to do with SketchUp’s idea of whether a transform was identity or not.
There was some operation which caused an identity transform to “lose” it’s identity. (pun intended, but yes there was a bug. Don’t know if it got fixed.)

ADD: Okay, I remember now. Using Steve’s 3rd example:[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])

> t3.to_a
  1.0, 0.0, 0.0, 0.0,
  0.0, 1.0, 0.0, 0.0, 
  0.0, 0.0, 1.0, 0.0, 
  0.0, 0.0, 0.0, 1.0

#=> false

The #identity?() method should return true here, but obviously does not.


#=> false

t3.to_a == IDENTITY.to_a
#=> true

… strange. Reason is apparently the Geom::Transformation class does not provide an overridden == method so “sameness” can be properly calculated. (It defaults to Object#==.)

Why would you even want to do it?

Geom::Transformation::new creates a new identity transform. (Ie, no arguments)

t = Geom::Transformation::new
#=> true

This appears to be true. Transformation’s == does not compare the values in the Transformations, just the identity of the two referenced objects, which makes it IMO pretty useless.

You’d be surprised Dan. If you’re dealing with a function that you wish to operate on either a component instance or a group the component will require transforming whereas the group will not.

It is just one way of avoiding lots of if blocks as transforming a group with an identity transform will leave it ucnchanged.



Further to my post I should report that the issue appears to be one of visibility. This is consistant with my earlier observation regarding correctly constructed transforms. If I write a simple test utility (see below) the visibility works fine.

require 'sketchup.rb'

class TTest
  def initialize(*args)
    trans = Geom::Transformation.scaling(1,1,1)
    puts trans.to_a


if( not file_loaded?("test.rb") )"Draw").add_item("Test"){}


I will attempt to chase down the problem and report back when I’ve found the errant coding.
To be continued …

Hooray! I’ve found it. The problem was the use of = in a conditional test instead of ==. This is a nasty syntactical error since, unlike Embarcadero pascal, the code interperator doesn’t give the coder warning and it can be a hard one to detect.
I think this might be one for the wish list unless there is a legitimate use of an if = combination. the following shows the error

class TTest
  def initialize(*args)
    trans =

  def printTransform1 trans
    if trans = nil              # <<-----error
      print("trans = nil\n")
      printArray("TTest.printTransform1", trans.to_a)

  def printTransform trans
    if trans == nil
      print("trans = nil\n")
      printArray("TTest.printTransform", trans.to_a)

  def printArray strg, a
    puts strg
    for i in 0..3 
      puts arrayRowString(a, 4 * i)

  def arrayRowString(a, start)
    strg = ""
    finish = start + 3
    for i in start..finish
      strg += ((a == nil) || (a[i] == nil))? "nil":a[i].round(2).to_s
      strg += "," if i < finish
    return strg

The output from this would be as expected

I am glad you found it. It is an easy and common mistake to make. However, it should have generated a warning in the ruby console which would have pointed you to the location of the error.

@AlanMatthews Would you please edit your prior posts,
and wrap your code in the forum markdown code delimiters, ie:


code here


Those are backticks, the character that shares the key with the tilde (~) to the left of the 1 key. (You’ll likely need to re-indent the code as the forum has stripped out the leading whitespace.)

This is a Ruby Core interpreter issue. And assignment in any expression, is allowed under Ruby’s multi-paradigm philosophy. However, most coding style guides strongly discourage it, (or outlaw it for particular projects,) for the very reason that you found. It promotes errors that are hard to find.

So you’ll see the style guides say to make the assignment on the line preceding the conditional test (if, unless, while, until,) etc.

Saving one line of code is not worth it.

I meant why would you wish to create an identity transform
in the way you were attempting. (Ie, it doesn’t really work.)

I refer you to my post above, where I show that the API currently
has only one good way to create a new identity transform.
:arrow_right: Transform array - #4 by DanRathbun
And that the #identity? method is currently bugged.

Then I refer you to a post above, where Jim shows you that you can just use the global identity transform which is there for this very purpose. (It is a shared object so do not change it.)
:arrow_right: Transform array - #2 by jim_foltz

And for @AlanMatthews, and any others interested in working with transformations,
I suggest you get Thomas Thomasen’s Transformation Inspector plugin:

1 Like


I would if I knew how.


knew how to do what ?

  • wrap code ? (I told you how.)

  • edit a previous post ? (use the edit “pencil” icon)