lesson 4 – part 1 – a box with a face

This will show you how to make a stack of boxes with the beautiful/terrifying faces
of all the people you work with

some faces mapped onto boxes

ben's face on a cube, he looks like a moron!First
we’ll make a single box with a picture on it like this:

Here’s all the code, see if you can read it, and I’ll break it down piece by piece

#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],
    [edgeLength, 0, 0],
    [edgeLength, edgeLength, 0],
    [0, edgeLength, 0]
  ] #add the bottom face  

myCubeBase = ent.add_face
#flip it. ! means ‘in place’ i.e. myCubeBase = myCubeBase.reverse

# Adds a material to the “in-use” material pallet.
m = materials.add

# pulls a texture out of a folder
myPath ='C:\Users\bdoherty\Desktop\BVN_Photos\'
myImageFilename = BenDoherty_0024.jpg
m.texture = myPath +

= edgeLength

myCubeBase.material = m

#do a push pull to make it into a cube
myCube = myCubeBase.pushpull(edgeLength, true)

Set up

So, 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 therefore,
. 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.


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 with some UI, 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 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.

The next line is a bit of a subterfuge, because really it is lots of lines that
ruby treats as one. Because 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 recap what it means.

A point can either be described as:

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


myPoint = [100,
, 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 ;pad of other clever things you can do with real points too. In the
future, I'd write points out as the first way, but for the moment, the second
way is easier to understand.)

So 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.

myCubeBase = ent.add_face

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.

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

You would have thought that the method that does this is called
, 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.


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

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
, 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
, but in the scope of the sketchup environment it is called

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 +

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).

The picture of me is called BenDoherty_0024.jpg
so I’ve put that in as a variable. 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 part 2 of this.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s