If you have downloaded the SketchUp example "Sang", you will see how you are able to change the colour (strictly speaking "material") of various features by clicking and that is what this lesson will teach, again by using a simple model so you can concentrate on the technique.
To create the dynamic component, create two cubes. On one of them, draw a circle and fill it with any material). Group it and call it "Sub1". Then create another cube, group it and call it "Sub2". Finally select both and make them into a component called "DC3" as shown below. Note that they should all be in the default material apart from the filled circle.
Next you need to add the various materials that you are going to switch to to the model. We need to create a set of swatches which will be kept within the component and hidden. If you do not do this, your user could remove the alternate materials if they purge spare materials from the model. By creating swatches, the materials cannot be removed by purging.
Go into the component (so that the swatches become part of it) and create four rectangles. Then pick the four materials or colours that you wish to use and paint each rectangle with each one, as shown below. (An alternate would be to create a cube and paint each face in a different material). You do not need to group or componentise (is that a word?) these swatches as they are going to be hidden and not used in formulas.
TIP If you use the material names that come with SketchUp, there is a danger that someone could edit that material and therefore change the appearance of your component. If you wish to make sure that your material doesn't change accidentally, edit the name to something unique like "DCcarBlue".
Whether you use the default material names or rename them, make a note of the exact name of each material as you will have to enter these in your formula.
The next step is to move the swatches into the body of the component and hide them. We need to move them inside because, even when hidden, they affect the outer size of the component. So move the swatches into the body of your component, making them smaller if necessary. Then hide them. (The illustration below shows them inside the body which has one face temporarily hidden.) It can be a tricky procedure getting them inside; you may need to switch views or use the X-ray mode to do so.
Now select the component and open the Component Attributes window where you should have "DC3" with the subcomponents "Sub1" and "Sub2". In Sub1, (the one with the coloured circle) add the attribute "onClick". You may also want to add the attribute "material" although it's not strictly necessary. Select the SET function from the Functions pop-up window menu and add it to the onClick attribute. We now need to add the familiar SET as before but with these settings:
SET("Material","mat1","mat2","mat3","mat4")
where "mat1", "mat2", etc. are replaced with the names of the materials you used in the swatches (they must match exactly apart from case). I had renamed the materials in my model so I entered:
SET("Material","DCred","DCyellow","DCgreen","DCblue")
Note that the materials must be enclosed in quote marks because they are names rather than numbers such as RotZ, LenX, etc.
Now click on the model with the Interact tool and you should see the materials change. A few things to note:
- Only areas set to default material change, not those with materials already set (such as the circle)
- Because we added the onClick to group "Sub1", it doesn't affect the default material of "Sub2". If we had added the onClick to the whole component ("DC3"), it would have changed both groups.
- The Material attribute shows the present material name (but not in quotes).
User selection menus
So far so good. But what if there were a dozen materials in the swatches? You wouldn't want to keep clicking to get to the material you wanted. It would be far easier to pick the choice from a menu, and this is easily added with one qualification. User menus can only be added to the top component, not a subcomponent, so we have to add a custom attribute to "DC3" and the material for "Sub1" will be linked to this.First, create a custom attribute for "DC3" and call it "SubMaterial" (we cannot call it "Material" since that name already exists. User selection menus can be added to any attribute and are accessed by clicking the arrow like symbol that appears on the right of the attribute box (see below).
The window switches to another screen which initially shows "Display rule: Users cannot see this attribute". This menu has three other options:
- Users can see this attribute (they can see what the attribute is but not change it)
- Users can edit as a textbox (they can enter their own numbers or letters)
- Users can select from a list (they have a choice of selection)
"Display label" is the caption for the user menu: in this case I change it to "Colour", while the boxes below represent the menu values, "List option" is what the user sees in the menu, "Value" is what SketchUp enters into the formula. So click where it says "Add option" and enter a description of the first material. Where it says "Enter value", enter the name of the material as we typed into the onClick>SET formula. In my case:
LIST OPTION | VALUE |
---|---|
Red | DCred |
Yellow | DCyellow |
Green | DCgreen |
Blue | DCblue |
and click "Apply" (note there are no quotes around either column).
Now, when you open "Component Options", you should see a menu named "Colour" where we can select a colour (and click Apply). Of course, it doesn't work at the moment because we haven't linked the Sub1 material to the custom attribute. So enter this formula in the Material attribute of Sub1.
=Parent!SubMaterial
What's that? Shouldn't it be "DC1!SubMaterial"? Well it could be but this is a little tip I thought I'd share. If you use "Parent" in any subcomponent, it always refer to the component or group which contains it – useful if you have a set of formula snippets you can use in any model without changing the names. (Note there is no equivalent "Child" term since a parent can have many children but a child can have only one parent (in the SketchUp world, at least).
So now, Sub1 material takes its value from the SubMaterial attribute which we change with the menu. Try it out. The settings are shown below in case you have any problems.
As before, DC3 has been uploaded to the 3D Warehouse with this link:
https://3dwarehouse.sketchup.com/model.html?id=ueae2f868-62b2-4d79-a810-539089877a60
No comments:
Post a Comment