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!

10 comments:

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

Scott Lininger 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

RoboBert said...

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

Πετσαγγουράκης Γιώργος 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

mbe 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?

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

Ross T 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?

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

chenlu 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