Wednesday, 12 March 2014

Lesson 4: Creating an opening and closing window

In the previous lesson we found out how to enter simple formulas so let's put this into operation by creating our first "real" dynamic component. Note that most of the components we create will be simple shapes, not realistic furniture or doors. This is so we can concentrate on how the DC works and not what it looks like. Once you learn the principles, you can then take them into an actual wardrobe or car or window.

Entering spreadsheet functions

The formulas we have entered up to now have been simple ones such as "=LenX/3". But, as anyone who has used a spreadsheet knows, there are dozens of more complicated formulas (functions) which you can enter. SketchUp has many of these and some of them you will use regularly. You don't have to enter these by hand though. They are all available in the Attributes window.

Open the Attributes window and create a component or group. Select it. At the top of the window, you will see two tabs: Info and Functions. You might think (I certainly did at first) that changing these tabs would change the whole Attribute window but it actually only changes the grey area immediately below (as you can see in the images below).



"Info" is selected by default and gives you information on the attribute that you select or are in. But if you click on "Functions", you see a drop-down menu with "Select a spreadsheet function..." and a button ("insert") beside it. To add a function, add any attribute to your component, and, with the attribute field empty of any contents and the cursor within it, select any function from the drop-down menu and click "insert". The function should be entered into the attribute field. (As you progress, you will learn many of these functions and you can type them in by hand.)

Obviously you cannot just use any old function in any position. Many only work with numbers so could not be used in text attributes like "Summary". The above operation was just to show how to select and insert a function.

Creating an opening window

So now let's go onto creating a real dynamic component. We shall create something which is very simple but very compelling to watch: an opening and closing panel.

If you've downloaded some DCs, you might have found this already: a car or a wardrobe with an opening door, or an opening window. All this is is a change in the rotation attribute (usually RotZ) of the door/window.

So start a new document and create the two shapes below, a wall and a window. The sizes are unimportant as long as the window is exactly the same size as the hole in the wall. Notice also that the objects do not have any depth (LenY) – they are just two dimensional objects. For this example, it is actually easier not to have any depth but it doesn't matter too much if it has. Make sure each is a separate group or component name "wall" and "window". (You name a group by choosing "Entity Info" from the Window menu or right-click menu, and entering something into the name field.)


Now move the window so that is fits exactly into the hole in the wall (it should snap into place). Then select both objects and create a component (call it "DC1"). Open the attribute window and select the component.

Up to now we have been dealing with one group/component at a time but this time you will see a nested component. Dynamic components are usually composed of other smaller groups or components (which I call sub-components). You can have individual shapes (non-groups/components) within a dynamic component but you have no control over their attributes so you will normally make every separate object within a DC either a group or component.

If you have used the names I suggested, you should see the three objects in the attribute window, with the primary component ("DC1") at the top and "window" and "wall" indented beneath (to show that they are sub-components of DC1.

TIP You can change the name given to the objects by double-clicking on the name in the attributes window – very handy if you have several identical sub-components such as wheels.

First test it by expanding the "window" sub-component and add the attribute "RotZ". It should be zero. Now enter "-45" and press return. The window should open towards you. (Negative numbers rotate clockwise, positive rotate anticlockwise.) Undo or enter zero so the window is closed again.

Now we need to add the ability to open and close the window with the Interact (pointing finger) tool. Basically we want to set the RotZ to -45 on the first click, then -90 on the second click, and back to zero on the third. For this we need to use the OnClick attribute. The OnClick attribute controls what happens when you click with the Interact tool. So, in the window sub-component, add the attribute "onClick".

Note that we added it to the "window" sub-component. This means that you have to click on the window to open it. We could have added it to the "DC1" primary component and then we could have clicked anywhere on the wall or window to open it. But that would cause problems if we wanted another animated object in our component so we normally add the onClick to the thing to be animated.

With the cursor in the onClick attribute field, click on Functions at the top, select "SET(attribute, state1, state2, ...stateN)" near the bottom of the menu and click insert. This function is used to change any attribute, in this case by clicking. Change the default text in the onClick attribute to show exactly this and press return:

SET("RotZ",-45,-90,0)

A VERY IMPORTANT NOTE: The attribute in the SET function (RotZ) must be enclosed in quote marks or it won't work. If you click on an attribute name to enter it without typing, the quotes will not be added. If your animation doesn't work, always check first to see if you have missed out the quotes.

A LESS IMPORTANT NOTE: Notice that SET doesn't have an equals in front of it. It is a behaviour, not a formula.

If you now select the Interact tool and click on the window, it should change with every click from closed, to half open, to open, and back to close. (Note that we entered -45, -90, and 0; when SketchUp gets to the zero and we click again, it automatically goes back to the first value and shows -45.)

If it doesn't work, check that your attributes look like the screen below:



If you still can't get it to work, I have uploaded the component to the 3D Warehouse at https://3dwarehouse.sketchup.com/model.html?id=ubf42b594-eb3a-493b-b384-ce8225658b5c (with a bit of texture) so you can download it and see how it differs from yours.

Other Animations

The problem with this component at the moment is that the window jumps open rather than through a nice smooth animation. To improve it, we can use one of the animation functions (ANIMATE, ANIMATEFAST, ANIMATESLOW AND ANIMATECUSTOM). All, apart from the last, have the same structure. You could delete the SET function we have used and insert one of the animate function, or simply type "ANIMATE" where it presently says "SET". That is:

ANIMATE("RotZ",-45,-90,0)

If you do this (remembering the quote marks around the attribute), you should see the window open and close smoothly when you click on it.

The differences between these functions are:
SET  Attribute jumps between values. Best for attributes with no intermediate values (such as HIDDEN attribute which is either true or false), or where you aren't interested in smooth animations.
ANIMATE Attribute changes evenly over half a second.
ANIMATEFAST Attribute changes evenly over a quarter of a second. 
ANIMATESLOW Attribute changes evenly over one second.
ANIMATECUSTOM Allows you to customise the animation, changing the speed and the easing (acceleration).

Try changing the function to each of these animations (you may want to leave the ANIMATECUSTOM to a later date) to see their effects.

You can see how just a single line in one attribute can create a pleasing effect. We can also use these functions for more elaborate animations such as sliding doors, cars which drive by themselves and complicated movements. But, for now, just play around with different shapes and animations until you are happy with this procedure.

Tuesday, 11 March 2014

Lesson 3: Entering formulas

In the last lesson, we looked at entering values into the Attributes window but what makes a component dynamic is formulas – the ability for some attributes to change depending on others.

Start by creating a cube 10cm on each side (you can use inches if you want – units are not important) and making it into a component. Then open the Component Attributes window, select the cube and add the size attributes (LenX, LenY and LenZ). They should all be showing "10". (If they aren't, just enter 10 into the incorrect attribute and press return).

The first thing we are going to do is fix the height (LenZ) so that it remains fixed at 10 whatever else we do to the cube, such as scaling. This is a common task in dynamic components. All we have to do is type "=10" into the LenZ attribute box (not the quote marks, just equals and ten).

Note: if you simply add the = in front of the existing text, you might see this appear:

This means that SketchUp doesn't recognise what you enter. You have to remove the "cm" so the value you enter is "=10". The "cm" will appear when you press return but that is simply SketchUp showing you the units. You don't enter them yourself.

Now take the scaling tool and try changing the dimensions of the cube. If everything is correct, you should see the height change but snap back to 10cm when you release the mouse. The LenZ attribute is now fixed.

TIP Before we go on, a tip. You can either see the computed values or the formulas in the attributes boxes. To toggle between the two, click the "=ƒx" button at the top right of the  Attribute window.

Now we want to link the width (LenY) of the cube to the length (LenX). So delete the contents of the LenY attribute and enter "=LenX/3" and return (again not including the quote marks). What this means is that LenY is set equal to LenX divided by 3. The width of the cube should automatically change to 3.333 cm as soon as you pressed return.
Now try scaling the cube again and you should see the width automatically change to one-third of the length.

If you haven't done so, try scaling just the width using the scaling point below.


You should notice that the cube* remains the same size but simply moves its position. What happened? Well, we have determined that the width is calculated according to the length. We have changed the width but not the length so SketchUp calculates the new width dependant on the length which hasn't changed. The scale tool cannot override the formulas.

You could try switching the length and width around by deleting the LenY formula so that it just shows the value (in grey) and entering "=LenY*3" in the LenX box. Now you see that the scaling tool can change the length but not the width.

TIP When you are entering formulas, you can enter the attribute directly rather than type it by clicking on the attribute name in the window (see below).


What happens if you want to connect the width to the length and the length to the width? If you have the two formulas in both boxes at the same time, you will get the red # coming up to show that SketchUp cannot accept it. It's a circular reference. The LenX value is based on LenY and the LenY is based on LenX. SketchUp, just like a spreadsheet, cannot complete the calculations and will throw a wobbly.

To finish off, try entering various formulas using the position attributes (X, Y and Z). If the formula in position X is "=Y*2", then moving the cube along the X axis will automatically move it to a new Y position. Try entering different formulas in different boxes until you are quite happy with how they work.

* I know it's probably not strictly a cube now but a box, but we won't confuse things by changing the name.

Monday, 10 March 2014

Lesson 2: Entering values

Before you start creating dynamic components, you need to fully understand the component attributes window. So create a simple 3D object such as a cube or cylinder and turn it into a component, giving it a name when you do so (such as "My DC cube"). Then open the component attributes window and make sure your component is selected.

You’ll see in the window a little bar with the same name that you gave the component. This represents the component itself. If it was made up of groups or sub-components, they would be listed beneath this. Beneath the name, you will see "Add attribute".


The attributes in question are all the things which define your component: the position (X, Y, and Z co-ordinates), the size (LenX, LenY and LenZ), the angle or rotation (RotX, RotY and RotY), its material, whether it is visible or hidden, etc.

You can show an individual attribute by clicking "Add attribute" or the plus sign beside it and choosing one of the pop-up list. You can see that they are in groups and you can show all of a group by clicking on the title. If you hover on "Position", it changes to "Position (add all)" - click the title and all three position attributes are added.

You will see that some attributes, such as position and length, already have values in grey. That is because they already exist (any object must have a position, length and rotation) while others such "Name" are blank and can be filled in by you. (The attributes window is basically a spreadsheet and, like that, can contain values or formulas, but we shall look at that later. For the moment, just concentrate on values.)

You can delete an attribute by clicking on it and then clicking the minus sign beside it. Attributes that have existing grey values retain them if you delete them (an object cannot have no length after all) but those that you have written into will lose them.

For the moment, just add all the position, length and rotation attributes. You should see something like this (the value will obviously be different for your component).


Now double-click in the X field (where it says 0 cm in the picture, not the red "X"). Delete the value inside and enter something else such as "20" and press the return key. You should see your component move. to the new X position. Try entering different values in the other fields and see what they do.

Now try moving, scaling or rotating the component itself with the usual tools. See how the attribute values change as you change the object. Finally show the "Material" attribute which will probably be blank. Then apply a material to the component and see how its name appears in the field.

For the rest of this lesson, familiarise yourself with these attributes and how they relate to the component. A clear understanding of this is vital to creating dynamic components.

Sunday, 9 March 2014

Lesson 1: Introduction to the DC tools and windows

We start then with an overview of the way you interact and create dynamic components (DCs)

(Remember that you must have the Pro version of SketchUp to create DCs although you can interact with existing ones with the free version).

There are four tools/windows used with DCs, three specifically and one, the scale tool, which can work differently with some DCs. These are shown below. (The dynamic component toolbar is opened from the View > Toolbars > Dynamic Components menu).

The menu and toolbar to access DC tools/windows
The only specifically-DC tool is the Interact tool (the pointing hand) which (depending on the component) will do certain things when clicked on the DC (such as open and close doors). The next button in the toolbar opens the "Component Options" windows which, again depending on the component, allows you to choose or enter various things (such as the width of a door). The third button opens the "Component Attributes" window which is what you use to actually turn a plain component into a dynamic one. The fourth button shown is the familiar Scale tool which can operate differently with some DCs.

As seen above, the "Component Options" and "Component Attributes" can also be opened from the Window menu and the right-click context menu.

Using the tools and windows

To see how the tools and windows work, go to the 3D Warehouse and download the simple DC figure called "Sang" (Direct link). Do not open directly (or it won't work) but create a new, empty SketchUp document and import it.

Now click on the Interact tool (the pointing hand) and hover it around the figure. You should see the cursor change over different areas as shown below. If you click on any active area, you should see the figure's colours change.


Now open the Component Options window while the Sang figure is selected. You should see something like this:


As you can see, this component gives you the option of changing the colours by entering the choice in the Component Options window (and clicking Apply). Some DCs can be changed via the Interact tool, some by the Options window, and some by both.

Finally open the Component Attributes window and you should see this:

This is where the formulas that define the "dynamic" part of the dynamic component are entered and we actually create the DC. This is also where it can get complicated so it is an area that needs to be taken slowly. For now, you should download a few more dynamic components and play around with them.

A sample of DCs is installed with SketchUp (in the Dynamic Components library in the Components window). You can see they are DCs by the little green icon beside the thumbnail (see below).

If you go to the 3D Warehouse, click on the little cogs in the search box at the top of the page. This takes you to the advanced search where there is a checkbox for "Show only dynamic models".

Saturday, 8 March 2014

Welcome to the Dynamic Components tutorial blog

Introduction

I am a great fan of Google SketchUp, (now known as Trimble SketchUp). I have doodled and created with it for many years now and learned pretty much all the basics. But recently I decided to branch out into an area I have rarely touched – dynamic components (which I often refer to as "DCs"). While there are many books and sites available to teach you the basics, I have found relatively little information on dynamic components so am forced somewhat to find them out for myself, "reverse engineering" the supplied DCs to see how they work.

In this blog, I intend to write a series of lessons on creating DCs, thereby helping others who want to learn about them. Because I am not a professional user but someone who has had to learn it painfully from scratch, I can hopefully explain it in ways that newcomers can understand. Of course, that also means that what I say may not be the best way or the "correct" way, just the way I have learned.

What is SketchUp?

For those who don't know, SketchUp is a 3D-creation program, principally for architecture but which can also be used for anything 3D such as products and forms of transport. It is extremely easy to learn and yet capable of producing amazing and detailed models.

There are two versions of SketchUp, a free version (now known as SketchUp Make) which does pretty much everything and a Pro version which requires payment. The version I have and use is SketchUp 8 while the latest version is SketchUp 2014. As far as I'm aware, there is not much difference between the versions as far as usage is concerned so you should be able to follow this blog even with the newer version.

Links

Dynamic Components

If you haven't used SketchUp before, then it's probably best to stop here until you have some experience. But for those who have learned something about it (which is probably why you've come), dynamic components are components that are not static but can change. For example, with a DC door assembly, you might be able to change the height or width without distortion, change the materials from paint to wood, or have the door closed or ajar – all without having to edit the object. A DC ladder can automatically add new rungs as you scale it up and a solar panel can always turn towards the sun in your model. The possibilities are endless, as they say.
Provisos

While DCs can be used without limitation in the free version of SketchUp, to actually create a DC, you need the Pro version. It also helps if you have some knowledge of spreadsheets and understanding of the XYZ co-ordinate system. I also assume in my posts that you understand the basics of SketchUp, the meaning of words like "component", "material", etc.