Hiding a nested component dynamically "onClick"

Hello. A newbie question regarding the possibility to dynamically “hide onClick” a component nested inside another component (or a group within a component).

Is this feasible? If so I’ll do some serious searching on how this can be done but so fat I have searched for the terms on a couple of search engines but have not found yet if this is possible.

Thanks for confirming if we can do that. :slight_smile:

It’s not hard. First, it won’t work if all you want to hide is a buried group. In order to hide something dynamically, it needs to be a component. When working with nested components, it usually best to stick to components and avoid groups anyway.

The component to be hidden will need you to expose the predefined “Hidden” attribute using the Component Attribute window.

And at some level above the component to be hidden, you’ll need to expose the predefined “OnClick” attribute. Don’t do it in the component to be hidden! Why not? Once hidden, the “Interact” tool won’t see the component, so you won’t have an easy way to unhide it!

In the same component that you exposed the “OnClick” attribute, create a custom attribute of your choice. Call it something like “HideMyComponent”.and give it a value of 0 (which,as you’ll see later, is the same as FALSE).

In the “OnClick” attribute, enter the formula: SET(“HideMyComponent”,1,0). This will cause each click to toggle the value in “HideMyComponent”.

Finally, in the component to be hidden, in the “Hidden” attribute, enter: "=<name>!HideMyComponent" where <name> is the name of the component with the “HideMyComponent” field. If it is the direct parent of the component to be hidden, you can use “=parent!HideMyComponent”.

The “Hidden” component attribute takes either TRUE or FALSE. The formula sets it from the “HideMyComponent” attribute in the higher level component. The “OnClick” toggles the “HideMyComponent” value.

Hope this helps!

Hello sjdorst,
First thing a warm thank you for posting the confirmation this is doable and the solution online. Your instructions are very precious and break down how this can be acheived.

Excellent and clear instructions but I must be a bad student. I tried 3 times and feel I may be close, but it does not seem it is allowing me to enter the last set of instructions: “=portes cabinet!HideMyComponent” (where portes cabinet are the cabinet doors I am trying to hide, (it is their name in french)). Each time I enter this last set of instructions it automatically reverts to “FALSE” for some reason.

Would you mind taking a look at the file that I have attached? (it is very small as I copied only the component in question and the stairs to put it in perspective). If you can’t it is ok, and regardless, thank you again for having taken the time to detail how this is done. :slightly_smiling:

Cheers !

Laundry under the stairs (trying to make dynamic).skp (182.0 KB)

When I first saw your reply above, I thought the problem probably related to having a space in the sub-component name “portes cabinet” - and I spend about 10 minutes chasing the wrong thing. But spaces aren’t a great idea in sub component names, I use the underscore “portes_cabinet” to avoid spaces.

But I finally saw it:

In the “Hidden” attribute of your “portes cabinet” sub-component, enter:
=parent!hidemycomponent

Et le voila!

1 Like

I have tried again a 4th time and this is the version 2 of the file.

Laundry under the stairs (trying to make dynamic) v2.skp (184.2 KB)

Stil no go, but this file I feel is better and closer to it working. :slightly_smiling:

Oh we posted at nearly the same time. I’ll look into your second set of instructions to rectify.

<start teacher mode>
For extra credit, instead of hiding the doors, make them pivot between open and closed.
Hint: You’ll need to change your axes on “porte gauche”
<end teacher mode>

As you say sjdorst: et voila !!
Fantastic I am so happy with this, you have no idea, as this will be re-used for other projects.

Again a warm THANK YOU ! :yum:

I have also noted to avoid spaces between names of components in the future, for those which need to be made dynamic.

Merci monsieur.


I had thought about making those doors pivot, but as I suspect it is complicated, this will be for a later test/trial, but I agree this would be better. Noted to rectify the axes for the left door/porte gauche, when doing so.

@sjdorst has a decent point about triggering the onClick outside of the component, but if you don’t mind toggling Hidden geometry visibility on and off, in some situations it is nice to trigger the onClick on a per-child thing. For instance, maybe you are making an array of solar panels but you quickly want to hide a few in odd corners of the roof.

@Luds Looks like you solved your issue but for what it is worth take a look at my simple attached file for another way to use the onClick to hide/unhide. The trick is to set onClick to equal set(“Hidden”,not(Hidden)) but 4 caveats:

  1. The hidden attribute value needs to manually be set to 1 or 0 at least once, but only once.
  2. The master child will control all of the copies generated, if they are in fact copy-generated.
  3. Scaling should reset the visibility on everything.
  4. Can’t be any onClick functions set on parent components.

    fwiw,
    Matt
    hiddennothidden.skp (25.4 KB)

@chamberz,

Hmmm. I like your formulation for the “onClick” ==> set("Hidden,not(Hidden)), it’s more elegant than mine, and it makes clear that you’re toggling between two logical values. With mine, you can - how shall we say this? - play with the users as follows: set(“Hidden”,1,0,0,1,1,0,1,0,0,0,1,0,1,1,0) - IOW you can create something that intuitively should “toggle”, but doesn’t!

As far as your approach to hiding copied components, it’s interesting as well. However, for your stated purposes of hiding “a few odd corners of the roof”, it has the failing of having one of the copies (actually the original) not functioning the same as the rest of the copies.

<meta speculation>
And your criteria #3 shows something that I haven’t seen documented: That scaling a component causes subcomponent - especially subcomponent copies - to be (figuratively) destroyed, then recreated. At least, that’s the only concept I can come up with that explains the behaviour of scaling resetting the visibility on everything.
</meta speculation>

I see your “another way” as a solution to a slightly different problem to the one initially presented by @luds. Neither better, nor worse, just different.

And yes, in my “meta speculation”, I was very specific in my language choice: “…that I haven’t seen documented:” There’s a lot of stuff out there that documents SU behavior, not all of it in the official documentation. As a relative newbie, I know full well that this particular behaviour may, in fact, be documented somewhere, I just haven’t seen it!

Yeah… Just when you think you’ve solved a clever DC problem, you get rewarded with more. I’ve worked around the master-copy controlling feature/problem with something like “if this is copy zero, make it small and hidden” and “throw in an extra copy” and “if this is the extra copy, put it where the master copy was”. I dug through the archives and found this:

Good times. I love this comment from the series:

Got a link for the above? Sounds like it’s entertaining!

Yeah. That series was made back when YouTube was more strict on video length. But here are the four parts:

part1
part2
part3
part4

Attached is the DC I created in that video. I am not sure if a component made 5 SketchUp versions ago still works… Looks like it does.
BikeRack.skp (133.3 KB)

I was referring to the “Too much complicated …” comments that you extolled in the tag line. I’ve already seen - and pretty much internalized - your 4 part series.

Never mind! I just realized that you were referring to a You Tube comment on the series - I looked and there it was - followed a few comments later by the same person saying - essentially - I grok this now!

1 Like

Hey Guys,

My name is Bjorn and I’m trying to deal with a similar situation as yours. I’d like to use the onClick method to switch between different Components.
I’m creating a kitchen block, and I’d like all the cabinets to be clickable and automatically change from plain cabinets, to drawers, to something else etc.

I figured I need to create States. For example: To show only Cabinet A I need to make a State in which Cabinet B and C are hidden.

I guess I’m in the right direction, but I can’t figure out how to actually make this work.

Does any of you have an idea that could help me figuring this out?

Bjorn,

This can be done as you describe, but it can get messy.

First, since the actions within a component can’t effect the attributes of a completely separate component, you’ll have to put all possible appearance variations for a cabinet (or, likely, an adjacent group of cabinets) together, enclosed by an outer component.

Second, for the elements you want to change, you have to model them so that they occupy the same space. So you’ll be “choosing” which one to display from among two (or more) that - were they not hidden - will interpenetrate each other. Prediction: During development, you’ll be using “Hide” and “Unhide” a lot!

The basic approach is:
I’m going to call the outer, enclosing component Mama, and call the variations kid1, kid2, and kid3. (you can extend this to as many kids as you want).

Create a custom attribute in Mama called “WhoCanISee”. Manually set this attribute to “1” - NOT “=1”.
In Mama, activate the “OnClick” attribute and give it:
SET(“WhoCanISee”,1,2,3) ← again, don’t use the “=”!

In each kidX, activate the “Hidden” attribute and set it to:
=IF(Mama!WhoCanISee=X,0,1)

Replacing “X” with the number of the kid.

Your attribute and component names may, of course, be ones that make sense to you!

This approach can be repeated within other “outer” components. For instance, if this is a kitchen, you might have “outer” components of “BaseCabs”, “UpperCabsLeftofSink”, “UpperCabsRightofSink”, “Countertops”, “IslandCabs”, “IslandCounter” and have separate sets of options for each group.

Disclaimer: I’m NOT at a computer that has SketchUp right now. I’m 95% sure of the syntax I used above. If I’m wrong, it’s most likely that the “Hidden” attribute of each kid should have quotation marks around Mama!WhoCanISee: =IF(“Mama!WhoCanISee”=x,0,1) - but I think the way I originally wrote it is most likely correct.

Sjdorst, thank you so so much for your quick response!
You have no idea how much you helped me :slight_smile:

It works perfectly

It works!

Mama.skp (2.3 MB)

1 Like

Did anyone find a way to keep copies hidden when the model is redrawn? I checked out @chamberz bike rack, which unhides all the bikes when the rack is resized…

For my “bike rack”, I created a button for each bike that would sit on the ground, hiding that bike (I needed the bike itself to do something else when clicked). A “reset” button was added elsewhere. But any other changes unhides the whole set.

I tried using a drop-down box with formulas “=0” and “=1”, didn’t work.

How do I get copies to stay hidden???
Help!