Lesson 2

This lesson will show you how to make a box with your beautiful/terrifying face all over it!

ben's face on a cube, he looks like a moron!

In order to talk to a program like SketchUp with a programming language we need to use an application programmers interface (API). An API is a way for a programmer to interface with the core of the program, so if we want to Sketchup to draw a line we need to interface with it, and because Sketchup doesn’t speak our language we need the API to act as a go between for us. (This is a pretty inacuarate description, but it’ll get your thinkign fired up int he right direction, we can refine it later. If not knowing things is like pouring red ants into your underpants then have a look here)

Traditionally this was one of the ways to define the difference between programming and scripting, if you were writing a program from the ground up then you were programming, and if you were interacting with it through an API then you were scripting*. In the last couple of years this distinction has blurred as people have tried to rewrite their programs by using their own API as a way to make sure that it really works!

*Again, this is only half the story, there is also the difference between interrpreted and complied languages, but really the important thing to remeber is that if someone starts a conversation about what the difference between scripting and programming is then you should find the quicked way to get away from this incredibly boring person. Life’s too short to be a pedantic loser!

Here’s all the code that we’ll encounter in this lesson, see if you can read it, and I’ll break it down piece by piece afterwards

#setup stuff

mod = Sketchup.active_model

ent = mod.entities

sel = mod.selection

materials = mod.materials

#create a cube

#some cube variables

edgeLength = 1000.mm #because Sketchup is from a backward country they still use inches.

# To make Sketchup convert mm to inches for its own internal use <number>.mm

#setup the corner points

# x y z
points = [
[0, 0, 0],
[edgeLength, 0, 0],
[edgeLength, edgeLength, 0],
[0, edgeLength, 0]
] #add the bottom face

myCubeBase = ent.add_face points

myCubeBase.reverse!#flip it.

#! means ‘in place’ i.e. myCubeBase = myCubeBase.reverse

# Adds a material to the “in-use” material pallet.

m = materials.add “ben

# pulls a texture out of a folder

myPath =’C:\Users\bdoherty\Desktop\BVN_Photos\’

#remember to change this path for your computer!

myImageFilename =’BenDoherty_0024.jpg

m.texture = myPath + myImageFilename

m.texture.size = edgeLength

myCubeBase.material = m

#do a push pull to make it into a cube

myCube = myCubeBase.pushpull(edgeLength, true)

Set up

Lets step through what is going on here. the first section, marked #setup stuff stuff is common to most sketchup Ruby files. It contains a bit of short-cutting, where we make the names for things a bit shorter and easier to deal with, hence Sketchup.active_model becomes mod, and, Sketchup.active_model.entities becomes sel. This is just for convenience, and if you really wanted to, you could say Sketchup.active_model.entities every time you wanted to talk about the things in the model.

Geometry

Next up we have the beginings of the excitment. We need to define how big we want the cube to be, we could do this by desiginng a user interface, but it’s easier for now just to type it in. As it says in the comments, the edge length needs to be specified in inches for sketchup to be happy, so if we put in 1000 and leave it at that, then we’ll get 1000 inches, or almost 24 and a half metres – the size of the dome on the Taj Mahal. The way around that is by using a method on number called mm. That sounds way more complicated than it really is, so just remember to specify your units whenever you write a number.

edgeLength = 1000.mm
the taj mahal

The next line is a bit of a subterfuge, because really it is lots of lines that ruby treats as one. SketchUp is able to understand points descibed in a few ways, we can enter the points as an array of arrays of numbers. That’s a bit of a mouthful, so lets go over what it means.

A point can either be described as:

myPoint = Geom::Point3d.new(100, 200, 300)

or

myPoint = [100, 200, 300]

They both do exactly the same thing, when it comes to passing points around. (The first method allows you to talk about myPoint.x which is easier to read than myPoint[0], there are a load of other clever things you can do with real points too. In the future we’ll write points out as the first way, but for the moment, the second way is easier to understand given what we’ve covered so far.)

In the second way of writing the point, the numbers are in an array. _Why describes this as staples holding a wiggly catterpillar down (don’t the [square brackets] look like staples!) and so we can think of the array of arrays as a collecting book with big staples being used to staple a whole collection of wiggly number catterpillars to the page.

We now have the corners of a square, and because of the SketchUp-way, 2d things like squares get pushed about to turn into 3d things like cubes. So the next line actually makes the face that joins up the corners.

myCubeBase = ent.add_face points

So the collection ent gets a face added to it, and that face is called myCubeBase.

If we rush ahead and pushpull this square into a cube we’ll have to work hard to put a texture on each face, but if we put a texture on the square then the faces that are generated by SketchUp inherit the texture. Before we do that there is one more tricky little thing that I don’t fully have a handle on. regardless of the order that the points get created in, the face always extrudes downwards (don’t ask me why just yet), so we need to flip it.

myCubeBase.reverse! #flip it. !

You would have thought that the method that does this is called flip, but actually it is called reverse. The ! is a signal in ruby that something is going to happen to the object itself. Like a reflexive pronoun, I was myself, we see ourselves, you talk to yourself, I play… see, the list is endless, but it is happening ‘in place’, you are getting the object to do something to itself, rather than getting it to make a copy of itself and then do the thing to that copy.

Materials

Now we get to start being decorators.

In the setup section we defined materials as materials = mod.materials, (so ultimatly it is materials = Sketchup.active_model.materials that we are working with, but the aliasing makes our life easier).

m = materials.add "ben"

This line makes a material. There is something a bit confusing going on, because ruby calls the material m, and sketchup calls the material ben – so which is it?!

This is the first time you will come across a very confusing subject called ‘scope’. This cheesily grinning pair are going to help me out while I explain scope.

I’m pretty sure that Mrs O calls Mr O some pretty impressive things in the context of their ‘private time’, but if anyone else tried calling the commander in chief of the most powerful army in the world big boy, or my personal stallion, then it wouldn’t be long before Guantanamo re-opened.

So in the scope of the oval office, Mr O is refered to as ‘Mr President‘, but in his house he’s probably just ‘dad‘.

In the scope of the ruby code, the material is called m, but in the scope of the sketchup environment it is called ben.

That’ll do for scope for the moment, but don’t forget about it, we’ll be coming back to it!

# pulls a texture out of a folder

myPath ='C:\Users\bdoherty\Desktop\BVN_Photos\'

myImageFilename = 'BenDoherty_0024.jpg'

m.texture = myPath + myImageFilename

Now we are peering out of our box; this is where we need to interact with the world a bit. I’m keeping my folder full of people on my desktop, which becasue I’m at work it at the path above. You’ll need to look at your file path and work out where it lives (this is tricky on a mac, but you can just drag the folder into the editor and it will write out the path for you – clever things).

You’ll need a square picture of your face, a bit of clever photoshop work can remove the bags under your eyes, and while you are at it, crop it square. The picture of me is called BenDoherty_0024.jpg so I’ve put that in as a variable; you won’t have a picture of me, so use a picture of yourself.

The next line makes the texture property of m be the path joined (or concatinated with) the file name.

m.texture.size = edgeLength

This just sizes the texture so that it is the same size as the face that it is going to be applied to.

myCubeBase.material = m

…and finally, this line applies that material to the square drawn on the floor. Remember that the extruded box will inherit the material from the square that it is extruded from.

#do a push pull to make it into a cube

myCube = myCubeBase.pushpull(edgeLength, true)

All of that faffing around, just to make a box, but at last this is the command that makes the square into a box.

Many boxes will come in lesson 3.

Advertisements

2 thoughts on “Lesson 2

  1. Pingback: The gist of things | BVN ruby lessons

  2. Pingback: Lesson 3 | BVN ruby lessons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s