InstancePath only gives root and leaf?

Hi all, I’m stuck on something simple I think. When I create an InstancePath using a component and another component that is some degree of nesting inside of that component I get back a path that only contains the root and the leaf.

Say I create a batch of nested components like this:

    def add_components
        mod = Sketchup.active_model
        ents = mod.entities
        names = ["orange", "yellow", "blue", "green", "red"]
        for name in names
            x = mod.definitions.add(name)
            face = x.entities.add_face([0,0,0], [1,0,0], [1,1,0], [0,1,0])
            face.reverse!
            face.material = name
            face.pushpull(1)                
        end

        container4 = mod.definitions.add("container4")
        red = mod.definitions.find{|d| d.name == "red"}
        transformation = Geom::Transformation.new([0,3,0])
        container4.entities.add_instance(red, transformation)
        green = mod.definitions.find{|d| d.name == "green"}
        transformation = Geom::Transformation.new([0,0,0])
        container4.entities.add_instance(green, transformation)

        container3 = mod.definitions.add("container3")
        blue = mod.definitions.find{|d| d.name == "blue"}
        transformation1 = Geom::Transformation.new([0,0,0])
        container3.entities.add_instance(blue, transformation1)
        transformation2 = Geom::Transformation.new([0,3,0])
        container3.entities.add_instance(container4, transformation2)

        container2 = mod.definitions.add("container2")
        yellow = mod.definitions.find{|d| d.name == "yellow"}
        transformation1 = Geom::Transformation.new([0,0,0])
        container2.entities.add_instance(yellow, transformation1)
        transformation2 = Geom::Transformation.new([0,3,0])
        container2.entities.add_instance(container3, transformation2)

        container1 = mod.definitions.add("container1")
        orange = mod.definitions.find{|d| d.name == "orange"}
        transformation1 = Geom::Transformation.new([0,0,0])
        container1.entities.add_instance(orange, transformation1)
        transformation2 = Geom::Transformation.new([0,3,0])
        container1.entities.add_instance(container2, transformation2)

        ents.add_instance(container1, IDENTITY)
    end

    add_components

Then I get the variable names of the components

red = mod.definitions.find{|d| d.name == “red”}.instances[0]
green = mod.definitions.find{|d| d.name == “green”}.instances[0]
blue = mod.definitions.find{|d| d.name == “blue”}.instances[0]
yellow = mod.definitions.find{|d| d.name == “yellow”}.instances[0]
orange = mod.definitions.find{|d| d.name == “orange”}.instances[0]
container4 = mod.definitions.find{|d| d.name == “container4”}.instances[0]
container3 = mod.definitions.find{|d| d.name == “container3”}.instances[0]
container2 = mod.definitions.find{|d| d.name == “container2”}.instances[0]
container1 = mod.definitions.find{|d| d.name == “container1”}.instances[0]

Then:

path = Sketchup::InstancePath.new([container1, red])

… and I get only: path[0] is container1 and path[1] is red. Nothing inbetween.

What am I doing wrong?

I don’t think the instance path initialize figures out the nesting for you if there are nested things between what you passed as the root and leaf.

I agree with Steve. The Sketchup::InstancePath class is a rather dumb class*.

It cannot magically deduce the path between a root and a leaf object.

In fact every instance of the definition named "container1" will be an instance root for an instance path that has the leaf named "red". This is because all instances of "container1" share the same entities collection on down the tree to the leaves. So, they all will have the same instance leaf object.

If you inserted 5 instances of definition "container1" … named "container1a"..."container1e", and imagine instance paths down to "red", then all five instance paths would have the same object references except for the root instances.


* If you look at the Sketchup::InstancePath#valid? you’ll see that it is about worthless in testing an instance path for validity.

Thanks very much for your opinions and insights. That will spare me probably many hours of frustration.

No problem. What this class is really for is usually used in tools where the user is drilling down into the nestings. As the model.active_path changes a tool can get an instance path for the current edit context …

ip = Sketchup::InstancePath.new(model.active_path)
global_transform = ip.transformation

Yes, thanks for pointing that out. My goal has been to acquire the global positions of any vertex in any context. What I found was that in an active edit context the transformations of instances where all coming up as IDENTITY. Not very helpful, but thankfully using InstancePath seems to provide the solution. I used:

`transformation = Sketchup::InstancePath.new([Sketchup.active_model.active_path[-1], instance]).transformation`