Friday, March 21, 2008

Inserting an External SketchUp Component (of some Pants)

Posted by Scott Lininger, SketchUp Software Engineer

You've just SketchUped an amazing Robot Monkey, and you're ready to include him in your company brochure. Excitedly, you email the 3D PDF over to the legal department for approval, and you're faced with the biggest letdown of the whole project. They love it, they say, but due to new sensitivity around artificial animal companion rights, all Robot Monkeys must be modestly clothed before being released to the public.

Fortunately, you anticipated this problem and have a very nice model of some respectable trousers. Now you just need to insert them into your model, preferably by using the Ruby API.

If you know the exact file path to the SKP file that you'd like to insert, your job is simple. Just type these lines into the Ruby console (replace the "C:\pants.skp" with the absolute path to your component.)

pants_def = Sketchup.active_model.definitions.load("C:\pants.skp")

If everything typed out okay, then you should be inserting some snazzy pants using the standard "place component" tool. Hooray!

"But that's not very useful," you say, "It's easier to use the Component Browser to do that! What if I don't know the path to my pants? What if you want to place my pants in a particular spot?"

Okay, okay. Here's a more complete snippet that keeps the power in the control of the Ruby.

# First, load our pants (heh). This time we'll search for
# the file inside our Components directory.

pants_path = Sketchup.find_support_file "pants.skp" ,"Components"
pants_def = model.definitions.load pants_path

# Then define a location, and place our pants there.
pants_location = 100,200,0
transform = pants_location
entities = Sketchup.active_model.active_entities
instance = entities.add_instance pants_def, transform

And at long last, your Monkey has Pants, and you're the new company hero!


Unknown said...

Hi there,
would you be abble to produce a simple calculator for Sketch'Up?

when drawing I sometimes need to use a calculator; it would be nice if I could use the VCB instead of looking for the windows calculator, don't you think?

it doesn't have to be more complicated than "+ - * / =" it would be great!

hope this finds an echo in your office.

please keep me posted.

Unknown said...


Here's a trick you can use today. Under the Window, open the Ruby Console. In the little text box there, type in your calculation and it will tell you the answer.

For example, type in 5*5, and the ruby console will show "25".



Unknown said...

hey thanks a lot...
that will help

Unknown said...

also you can try Launchy (from if you are using windows... I'd have never believed it made this sort of thing so easy....

Anonymous said...

Hello Scott,

Where can i find the scripts you have shown on the Google I/O 2008 - Advanced Ruby Scripting seminar ?

Regards, Joe

Unknown said...

Hi Scott,
I wish we could find some tips on your amazing demo of your sketchup video
Have you got something for us?

Anonymous said...

Great posts. Seems like the SketchUp API holds great promise. Could you do a post sometime on doing the same sort of thing, only you're inserting a dwg file, rather than a native SketchUp component? I'm surprised that I can't find an existing script out there that does this already.

Anonymous said...

I'm trying to load an external component, of which already exists in the model... but has a "loaded from" path that isn't on my own computer. However, a component definition of the same name / physical apperance does exist on my computer just in a local directory. In other words, I'd like to "refresh" the component definition by loading the component from my local directories. The problem I'm running into is that Sketcup.active_model.definitions.load function will return the existing component definition that's already in the model if it doesn't differ physically from the component definition I'm trying to load... Is there anyway to force Sketchup to load the component definition from file as opposed to it returning the existing definition?

Anonymous said...

Treats wow gold for Greatfather wow goldWinter: This buy wow goldquest is buy wow goldavailable at cheap wow goldlevel 10 and cheap wow goldis just a simple wow power levelingcooking quest. wow power levelingThe recipe power levelingrequired for the power leveling tickles your wow goldfancy. It is buy wow goldcertainly importantcheap wow goldthey wanted to.

Anonymous said...

runescape money
runescape gold
runescape money
runescape gold
buy runescape gold buy runescape money runescape items
runescape accounts
runescape gp
runescape money
runescape power leveling
runescape money
runescape gold
dofus kamas
cheap runescape money
cheap runescape gold
Guild Wars Gold
buy Guild Wars Gold
lotro gold
buy lotro gold
lotro gold
buy lotro gold
lotro gold
buy lotro gold

Hellgate Palladium
Hellgate London Palladium
Hellgate money
Tabula Rasa gold tabula rasa money
Tabula Rasa Credit
Tabula Rasa Credits
Hellgate gold
Hellgate London gold
wow power leveling
wow powerleveling
Warcraft PowerLeveling
Warcraft Power Leveling
World of Warcraft PowerLeveling World of Warcraft Power Leveling runescape power leveling
runescape powerleveling
eve isk
eve online isk
eve isk
eve online isk
tibia gold
Fiesta Silver
Fiesta Gold
Age of Conan Gold
buy Age of Conan Gold
aoc gold

china tour
beijing tour
beijing travel
china tour
tibet tour
tibet travel
computer monitoring software
employee monitoring