Inserting an External SketchUp Component (of some Pants)
March 21st, 2008 | Published in Google SketchUp
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!
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!