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")
Sketchup.active_model.place_component(pants_def)

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 = Geom::Point3d.new 100,200,0
transform = Geom::Transformation.new 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!

8 comments:

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

Robobert,

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

Regards,

Scott

Unknown said...

hey thanks a lot...
that will help
merci!

Unknown said...

also you can try Launchy (from www.launchy.net) 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 http://www.sketchucation.com/forums/scf/viewtopic.php?f=180&t=10443
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?