Tuesday 22 April 2014

Lesson 11: Multiple animations (part 3)

In the previous lesson, we looked at a sequential animation where a pair of doors opened, followed by a drawbar sliding across. But the animation only worked in one direction because the opening doors were linked to the onClick handler so they always began first, even when reversing the animation. Now we shall look at how to make the animation sequence work correctly in both directions in a copy of the last version (now called "DC7").

The principle then is to have one animation, the Timeline, and link each door and the drawbar to this attribute. Let's start by examining the values we are working with.

Door 1 (LeftAngle) begins at -90 when open and ends at 0 when closed
Door 2 (RightAngle) begins at -90 when open and ends at -180 when closed
The Drawbar (BarPosition) begins at -210 when drawn back and ends at -5 when closed
The Timeline will go from 0 to 100 in 1.1 seconds.

As a chart, this is:

Time 0 seconds 0.5 seconds 0.6 seconds 1.1 seconds
Timeline 0 45% 55% 100%
LeftAngle -90 0 0 0
RightAngle -90 -180 -180 -180
BarPosition -210 -210 -210 -5
Process Start doors End doors Starts bar End bar

Notes: the % in the Timeline isn't included when we enter actual values, it's just there to show that the Timeline is like a counter going from 0 to 100% (completed). The Process when reversed, would be "End Doors - Start Doors - End Drawbar - Start Drawbar".

The first thing to do is to remove all the formulas and functions from the main "DC7" attributes (but leave the formulas in the subcomponents linking the Doors' RotZ to LeftAngle and RightAngle, and the Drawbar X position to BarPosition). Your Components Attributes window should look like this:

Now we add the single animation to the onClick attribute:

ANIMATECUSTOM("Timeline",1.1,0,0,0,100)

This is almost the same as the one we used in the previous lesson except that Timeline is now going from 0 (%) to 100 (%) and the easing is zero-zero since I want no change in acceleration. Clicking on the component now seems to have no effect but, if you make the Timeline visible, you can see it changing from 0 to 100 and back on each click.

(If you have forgotten how to make an attribute visible, click on the Details button on the right of the Timeline which looks like a menu combined with an arrow (see image below). Change "Display Rule" to "Users can see this attribute" and Apply. Now, if you have the Component Options window open when you click, you can see the value of the Timeline changing.)

The Details button, used to make the Timeline attribute visible


The formulas

Now for the formulas. If we start with the left door, we see that when Timeline is zero, LeftAngle must equal -90 and, when Timeline is 45, it must equal 0. If we add 90 first to get rid of the negative numbers (which can be confusing) we find that


0 Timeline = 0 LeftAngle
45 Timeline = 90 LeftAngle

and the formula is therefore:

LeftAngle = (Timeline * 2) - 90

(The brackets aren't necessary because of the rules of algebra but they make things clearer.)

When working out a formula, remember to add some intermediate angles in the calculation to check. In this case. 22.5 would be halfway along the Timeline and the LeftAngle would be -45 which, when I check the calculation, works out correctly. This is where the spreadsheet comes in useful.

The formula that we need to enter in the LeftAngle then is:

=IF(Timeline>45,0,Timeline*2-90)

Meaning: if the Timeline is greater than 45, set it to zero (closed), else animate it.
 Enter this, click on the component and you should see the left door open and close.

Note: the fact that the Timeline value is exactly half the LeftAngle is coincidence. It makes it a bit easier but isn't important.

For the second door, we need:

0 Timeline = -90 RightAngle
45 Timeline = -180 RightAngle

A formula I worked out for this is:

RightAngle = 0 - ( 90 + ( Timeline * 2 ) )

 thus

=IF( Timeline>45, -180, 0-(90+(Timeline*2)) )

So now, clicking the model opens both Doors.

Note that, after the Doors open, the animation runs on for 0.6 seconds with nothing apparently happening so don't click again too soon after the first click. Allow the animation time to finish.

Finally the Drawbar where we need:

55 Timeline = -210 BarPosition
100 Timeline = -5 BarPosition

Thanks to Excel, I find that the formula is:

BarPosition = (4.5556 * Timeline) - 460.5

so the formula for BarPosition is:

=IF(Timeline<55,-210,4.5556*Timeline-460.5)

Now, click on the component and two animations work together, both opening and closing.
The final settings:

Creating timelines and linking multiple animations is tricky, involving caeful maths and logical thinking, but the results can be very pleasing.

The final model is available on 3D Warehouse at https://3dwarehouse.sketchup.com/model.html?id=u9644e7be-7be5-4764-a2e6-785d96c41259

Monday 21 April 2014

Lesson 10: Multiple animations (part 2)

Sequential animations

While simultaneous animations are easily produced by placing them together in an attribute, the sequential animation is more complicated. Basically one animation, often an abstract custom attribute, is begun as normal, usually by an onClick formula, while the others are controlled by accessing the value of the first.

As an example, let's elaborate the opening doors we created in Lesson 9. In castles, the main doors were often secured by sliding a wooden beam across when closed so we shall add this to the twin doors. Our animation will consist of the two doors closing and then, once the doors are closed, the drawbar will slide across.

First duplicate the two door component we created in "DC5" and rename it as "DC6". Within the component, create a rectangle named "Drawbar" and position it in the final, closed position as below. Add the X attribute to the Drawbar and make a note of its value.

 Next we need to move the elements to their 'start' postion, so change the Doors' "RotZ" values so they are both open (remembering that we are changing the custom attributes "LeftAngle" and "RightAngle", not the RotZ directly). Then change the Drawbar's X position so that it is drawn back as shown below.


The first animation is straightforward, keeping the existing "onClick" to rotate the doors. The second animation involves the Drawbar sliding across by changing the object's X position. But we don't want this to happen until the doors are closed so how do we accomplish this? One technique is to create a custom attribute which will have a timeline and the animations are based on this, determining their values from this attribute. In this case, because the doors start at the same time as we click it, we can leave them as they are, but the moving of the Drawbar will need to controlled by this.

Before we create anything though, we need to get out a pen and paper (or spreadsheet) and work out some timings. The doors use the Animate function which occurs over half a second so let's say the Drawbar animation will start at 0.6 seconds (to allow a more realistic slight pause between the actions). The Drawbar animation can also take half a second so the total length of the full animation is 0.5 + 0.1 + 0.5 = 1.1 seconds.

In my model, the X position of the Drawbar is -210 when withdrawn and -5 when closed so, putting these together in a table:

Time 0 secs 0.6 secs 1.1 secs
Drawbar X -210 -210 -5
Timeline -456 -210 -5

Now comes the complicated part of working out a formula. I worked out the Timeline figures so that the Drawbar X position matches the Timeline but, obviously we don't want the Drawbar to move until the Timeline reaches -210 so we need to add an IF formula which says (in English):

If Timeline is less than -210, Drawbar position is -210, else Drawbar position is equal to Timeline.

As a SketchUp formula, it is (notice no quote marks around the attribute names:

=IF(Timeline<-210,-210,Timeline)

Finally we simply link the Drawbar position to the Timeline.

The final formulas are shown below:

You may have noticed the CUSTOMANIMATION that controls the Timeline animation. Because we want an odd number, 1.1 seconds, we can't use the standard ANIMATION types so have to use a CUSTOMANIMATION. The parameters for this formula are, compared to the normal ANIMATION:

ANIMATION(attribute, state1, state2 ...)

CUSTOMANIMATION(attribute, time, easein, easeout, state1, state2 ...)

The only thing that might be puzzling is the easein and easeout. These determine the acceleration of the animation with 0 being the fastest and 100 being slowest. The default easein and easeout for an animation is 0 and 100 which means the animation starts fast and slows down, rather like a ball being thrown in the air. Values of 100 and 0 would mean it starts slowly and speeds up (like a rocket taking off).

The details we enter are:

ANIMATECUSTOM("Timeline",1.1,0,100,0,-456,-5,-456)

which means the Timeline value changes over 1.1 seconds, starting quickly and slowing down, going from -456 to -5 on the first click, then back to -456 on the second click.
If you click on the component now, you should see doors open and then, after a pause, the drawbar slide across.

The reverse  animation doesn't work properly with the doors opening before the Drawbar has slid back but, to have the animation working both ways requires a more complicated arrangement. For this to work, you would have to link the animations of both the Drawbar and the two doors to the position of the Timeline which is much more complicated. Maybe you can think about how to achieve this. I shall show my method in the next lesson.


The completed component is available on 3D Warehouse at https://3dwarehouse.sketchup.com/model.html?id=ud59967fd-643b-4621-aeb5-d1cbbbc9fb19

Monday 14 April 2014

Lesson 9: Multiple animations (part 1)

Up to now, all the animations we have created have been simple, one-step animations: the window swings open or close. But some of the more impressive effects are multiple animations and there are two sorts: simultaneous and sequential.

Simultaneous animations

Animations where two or more changes occur at the same time are easy to create. All that is necessary is to add the different effects in the same onClick (or other) area, separated by semicolons, e.g.

ANIMATE("X",0,50); ANIMATE("Y",0,100)

The order is irrelevant as they start together. (They may end at different times depending on which type of animation you use.)

As an example, we shall create a pair of double doors which open simultaneously. So create a door with a cutaway corner (see image below) and make it into a component. Duplicate it and flip the copy along the red axis so that the axes are on the outside (this is where the cutaway shape comes in useful). Then make the two doors into a component ("DC5"). It should look something like this:

 
To open the doors, we add an onClick to the DC5 component to animate them. How do you know which of the two sub-components is which? Well, if you add the X attribute to both doors, you will see that one has a X position of 0 (the left) while the other has a value twice the width of the door. You can also look at the RotX of both which will show 0 and -180.

The problem with the model at the moment is that we have used two copies of the door component; consequently  they both have the same name. If we rename one, they both get renamed. If we animate the RotX, SketchUp can get confused as to which we are referring to. We could have used groups which would be separate but, because the doors are identical, it is better to use components.

To get around the problem, we can create two custom attributes in the top level "DC5" to hold the angles for both doors: LeftAngle and RightAngle. We enter 0 into the first and -180 into the second, then link the RotZ of each door to its appropriate value.

=DC5!LeftAngle
and

=DC5!RightAngle

If we now enter different values into LeftAngle and RightAngle, we should see the doors adjust their angle.

Finally, we enter the two animations into the onClick box of "DC5", separated by a semicolon. The first changes LeftAngle, the second changes RightAngle. If we click the doors with the Interact tool, the doors should open and close together.

If your component isn't working, check it against these formulas:



The model, DC5, is available on 3D Warehouse at
https://3dwarehouse.sketchup.com/model.html?id=u2ba92957-10fd-48bf-b004-9d9d4219e20e

In the next lesson, we will look at how to create sequential animations where they do not occur at the same time.

Monday 7 April 2014

Lesson 8: Limiting the Scale tool, and other attributes

This week, a break from creating dynamic components with a look at the other attributes in the Component Attributes window. We have already seen many of them and most are self-explanatory but let's look at some of the others.

The group titled "Component Info" are all to do with what appears in the Component Options window. The position of the various attributes are shown below. You can also see a piece of italic text in the description and this is to show that you can use HTML codes in any of these areas. Only certain codes are available but they can make your text look more interesting and you can add links to your webpage, for instance.


Available HTML codes
    <a> anchor
    <b> bold
    <i> italic
    <u> underline
    <strong> bold
    <em> italic
    <p> paragraph
    <br> line break
    <ol> ordered list
    <ul> unordered lis
    <li> list item
    <font> font size, colour, etc

The "Form Design" group have similar uses. "ImageURL" allows you to replace the default thumbnail with your own image. The "DialogWidth" and "DialogHeight" determine the size of the Component Options window. (If you are working on a large screen, don't make the "DialogHeight" too large – remember other people will have smaller windows.)

The "Hidden" attribute simply hides or shows a subcomponent. This could be useful where, for example, you have a vehicle with different designs – a lorry that could be flat bed or enclosed. I also used it in a restaurant dining table/ chair set so that you could have variable options – one chair, two chairs opposite, two chairs side-by-side.

The final attribute "ScaleTool" is often used with dynamic components to disable or limit the use of the Scale tool with your component. As an example, imagine you have created a ladder that adds extra rungs as you lengthen it (increase the LenX). It would be all right for the Scale tool to be used to make the ladder longer but not to change the other dimensions (LenY or LenZ). That would distort the rungs and supports. With the ScaleTool attribute, you can disable some or all of the ways the Scale tool can be used.

To do so, double-click the "ScaleTool" attribute or click the menu-arrow icon to the right. The screen below appears.


As you click on the checkboxes, you can see the handles appear and disappear meaning that the Scale tool cannot be used in that way. The easiest way to understand is to deselect some of the checkboxes, click apply, and then try to scale your component with the Scale tool. The setting below shows how the checkbox combination is mirrored when using the Scale tool.

Tuesday 1 April 2014

Lesson 7: Dynamic copies

Previous lessons have looked at simple dynamics such as changing a single attribute but this lesson will tackle something more complicated and which can sometimes take a while to get your head around – dynamic copies.
As an example, imagine a ladder with 10 rungs spaced 50 cm apart. If you double the length of the ladder, you don't want 10 rungs spaced a metre apart, you want 20 rungs spaced 50 cm apart. Similar needs come with sets of stairs, fences, furniture with multiple drawers.

For this tutorial, we shall again keep things simple so you can understand the principles without getting confused by details. Start by creating a block 100 x 50 x 200 on each side, group it and name it "Wall". Then create a step 50 x 10 x 30, make it a component (because it will be repeated, unlike the wall), and name it "Step". Position the step at the bottom edge of the block (see below), then select both and make it in to a component "DC4".


 Notice that we have only made one rung. All the others will be handled and duplicated automatically by formulas but, before we do that, we shall add a couple of custom attributes. Under the DC4 component, add two custom attributes: "TotalHeight" and "Spacing". The first determines the total height of the component so enter "50" for the moment. Then, under the Wall element, add the attribute LenZ and enter into its box the formula:

=DC4!totalHeight

The height of the Wall is now linked to TotalHeight so we can change the height of the subcomponent Wall from the main DC4 component. The Spacing attribute determines how far apart the steps are so enter 50 at the moment (so we have one step per 50 cm of wall height).

If we now enter 100 into TotalHeight, we should see that the wall gets higher but nothing else happens. We need to handle the duplication of the steps to allow for the extra height and this is determined by the attribute called "Copies" (under OnClick in the popup menu). So add the Copies attribute to the Step element (since we are making copies of the Step and enter the formula:

=DC4!totalHeight/DC4!Spacing

This means that the height of the wall is divided by Spacing to determine the number  of total steps. If you try different values in TotalHeight, you should see the value in Copies change:

TotalHeight Formula Copies
50 50/50 = 1
100 100/50 = 2
150 150/50 = 3

So the number of steps is increasing with the height of the Wall. Wait, you protest, I can only see one Step! Well there really are the same number of Steps to match the Copies value but they are all in the same location as the original Step so you can't see them. What we need to do now is change the Y and Z positions of the copies of the Step so that they are positioned separately. This is handled by a new, read-only attribute which is not normally visible called COPY. This holds the number of each copy so the original Step is numbered 0, the next 1, then 2, and so on. If you view the X, Y and Z positions of the Step, you see that it shows X=100, Y=0 and Z=0. If we start with the vertical position of each Step, we already have the Spacing so we simply set the Z position to:

=DC4!Spacing*COPY

The results would be:

COPY No. Formula Z Position
0 50*0 = 0
1 50*1 = 50
2 50*2 = 100
etc.

After entering this formula, you should see the steps suddenly appear next to the wall.


 You can change the horizontal position as well by adding the same formula to the Y position. Now, as you change the TotalHeight, you will see Steps magically appear or disappear.
  
TIP if you double-click and open the component after these copies have been made and select one of the them, you will see the COPY number in the attributes (below)


If your component isn't working, here are the settings below:
Obviously the positioning isn't quite right and here comes the mathematical bit. You need to adjust the formulas to space things correctly, maybe adding on an offset or allowing for the thickness of the step. One of the best ways to go about this is a sort of reverse engineering – draw the final component on paper, working out what the positions of each step would be and work out a formula which would produce that. A spreadsheet comes in very useful here.
Sketch diagram of the finished dimensions

The final formulas which space the Steps correctly are:


Y position = (200-30)/Copies*Copy
Z position = (DC4!totalHeight-10)/Copies*Copy

As before, the component, DC4, is available on 3D Warehouse at

https://3dwarehouse.sketchup.com/model.html?id=udd41c4eb-9a36-43ea-81fa-c49421426634

Monday 24 March 2014

Lesson 6: Changing materials, and adding user selection menus

If you've been following the previous lessons, you should be comfortable with simple attribute changes (rotating a window), the onClick behaviour and animations. Now we'll look at another common dynamic action and the use of user choices through menus.

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).
TIP There is no point changing SET to ANIMATE in this situation because there is no "inbetween" one material and another so SketchUp cannot gradually change them unlike an angle or a dimension.

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)
Select this third option:

"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


Monday 17 March 2014

Lesson 5: Custom attributes and axes

First of all in this tutorial, an interesting thing you may encounter in your DCs.

If you have followed the last lesson, you should have a simple dynamic component called "DC1". (If not, go back to the lesson to create it or download it from 3D Warehouse.) Now go to the components window and make several duplicates of the wall/window. Click to open one of the copy's window. You should see all of the windows in each copy open in unison. This is not usually a good thing – if you have several doors in a building, you don't want them all to open at the same time.


I'm not sure why this happens, not being a SketchUp developer, but I know how to fix it: create custom attributes.

What are custom attributes?

As their name suggests, these are attributes which you define yourself. You can give them any name (as long as they start with a letter, have no spaces and are not the same as existing attributes) and you can give them any values, formulas and functions. On their own, because they don't refer to anything in your component, they do nothing but when linked to other attributes, they become very powerful.

Why would you create a custom attribute? Well, they might be used to define something which has no existing attribute, such as the spacing of rungs in a ladder, but they might also be used to represent existing attributes. What would be the point of that? Well there are several reasons:
  • To give a more logical name to an attribute: "Height" might be more obvious to you than "LenZ"
  • To shorten the  name of a long attribute for use in formulas, eg. "Battlements!material" to "BMcolour" (If you're puzzled by the exclamation mark, I shall explain this below).
  • To refer to a deeply nested subcomponent. (If you have a subcomponent within a subcomponent within the top component, you can only refer directly to the attributes of the next level down.)
  • To differentiate identical subcomponents. (If you have the same component repeated in a DC, they will all have the same name.)
  • To allow users to choose attributes from a menu or enter values.

The exclamation mark

If you have used spreadsheets, you may have come across the exclamation mark in formulas when referring to different sheets. They work in a similar way in SketchUp.

Since every subcomponent, as well as the main component, has their own set of attributes, you need some way to refer to, for example, the window's RotZ instead of the wall's RotZ. So you enter the component's name first, followed by the attribute name, separated by the !. You can think of it as the equivalent of "apostrophe-s-space" so "Window!RotZ" means "Window's Rotz". If the formula is referring to its own subcomponent's attribute, in the sense that our onClick was in the "window" and referring to the "window", the first part and the ! are not necessary.

TIP If you click on the attribute name when entering attributes in formulas, SketchUp automatically adds the prefix and ! if necessary.

Amending DC1

To introduce you to custom attributes, we will add one to our DC1 model which also gets rid of the "linked" windows problem. Open the DC1 file, click on the dynamic component and open the attributes window. In the top component (named "DC1"), click where it says "Add attribute". The attribute menu pops up but you'll also notice the "Add attribute" wording changes to "Enter name". Ignore the menu of attributes and just type the name of the custom attributes in this box and press return. (I shall name it "rotation".)

This attribute is going to hold the rotation of the window, that is, the window's RotZ value. So click in rotation's field and enter the value zero. Now we have to link the RotZ attribute so that it is using the rotation value. Delete the value from RotZ and enter:

=DC1!rotation

Finally we have to amend the onClick so that it is changing rotation instead of RotZ so change it to this:

SET("DC1!rotation",-45,-90,0)

Now create multiple copies of our new component and click on the windows. They should all open independently. (I'm guessing that custom attributes are stored separately for each copy so they can have have different settings. Maybe a SketchUp expert can confirm this.)


If your component doesn't work, check your values with the screenshot below.

If it still doesn't work, I have uploaded to 3D Warehouse (named "DC2"). The link is:
https://3dwarehouse.sketchup.com/model.html?id=uafd2d6fb-4d03-4f14-9cbc-cc9648981d1c

A look as axes

(That is, the plural of axis, not the chopping tool)

When creating the window in the last lesson, you might have wondered how to have the window rotate differently, such as with a horizontal axis, or from the centre, like a revolving door. It is simple to achieve this, and you just have to understand about the axes.

In any SketchUp model, you will be aware of the document's axis, the red, green and blue lines that appear in the middle of the screen and control the snapping when drawing. But you may not be aware that every group and component has its own axis which is independent of the document's. To see this, create a simple cube and group it. Then rotate the cube. When you double-click to open and edit the group, you should see that it has its own red, green and blue lines which now point in different directions from the document's.

Many of the attributes of a dynamic component are related to this axis: the X, Y and X positions are measured from the crossing of this axis, the rotations are around it and the lengths are measured from it (if you make something smaller via attributes, the axis end stays fixed and the other end gets shorter). So, if you need to rotate from a different point, you just have to open the group/component and redraw the axis. (To find out how to move axes, check the SketchUp documentation.)

You can practise by repositioning the axis of your cube and entering different positions and rotations in the attribute window.

Therefore, to change how the window rotates, just go into the window subcomponent of the DC1 model and move the axis. To get the window to rotate like a revolving door, move it to the centre; to get it to rotate from the top (like a letterbox flap), move the axis to the top and move the formula to the RotX attribute (see below).

Top axis window




Centre axis window


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.