Skip to content

Instantly share code, notes, and snippets.

View alexschreyer's full-sized avatar

Alexander C. Schreyer alexschreyer

View GitHub Profile
@alexschreyer
alexschreyer / 3D model analysis in SketchUp with OpenAI gpt-4o.rb
Created May 16, 2024 12:35
This code sends a screenshot of the current model to OpenAI's API and asks questions about it.
require 'net/http'
require 'uri'
require 'json'
require 'base64'
# Ask something about what you see in SketchUp
prompt = "Is there anything wrong with this building?"
# Set the endpoint and API key for the OpenAI API
endpoint = "https://api.openai.com/v1/chat/completions"
@alexschreyer
alexschreyer / Call OpenAI API from SketchUp.rb
Last active May 15, 2024 20:30
This code queries OpenAI's API for valid SketchUp Ruby code and then executes it.
require 'net/http'
require 'uri'
require 'json'
# Set the endpoint and API key for the OpenAI API
endpoint = "https://api.openai.com/v1/chat/completions"
api_key = "<YOUR API KEY>"
# Set up the system message for the code completion
sys_message = "Generate only valid SketchUp Ruby code."
=begin
Available version constants:
Layout::Document::VERSION_1
Layout::Document::VERSION_2
Layout::Document::VERSION_3
Layout::Document::VERSION_2013
Layout::Document::VERSION_2014
Layout::Document::VERSION_2015
=begin
Available version constants:
Sketchup::Model::VERSION_3
Sketchup::Model::VERSION_4
Sketchup::Model::VERSION_5
Sketchup::Model::VERSION_6
Sketchup::Model::VERSION_7
Sketchup::Model::VERSION_8
@alexschreyer
alexschreyer / sketchup_sample_schema.xml
Created November 4, 2023 19:22
A sample XML schema for classifying objects in SketchUp
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Furniture">
<xs:complexType>
<xs:sequence>
<xs:element name="Manufacturer" type="xs:string" />
<xs:element name="Cost" type="xs:string" />
<xs:element name="Type" minOccurs="0">
@alexschreyer
alexschreyer / load_from_shared.rb
Last active October 29, 2023 19:15
A small script to load extensions from a shared folder
# Load SketchUp methods
require 'sketchup'
# Provide some feedback
p 'Start loading from shared folder'
# Make the folder path compatible and then load all Ruby files from that folder
loc = File.expand_path('C:/Users/<username>/OneDrive/SketchUp/Plugins')
require_all( loc )
@alexschreyer
alexschreyer / random_texture_pos.rb
Created June 13, 2020 19:17
Randomly position textures on faces in SketchUp
# Randomly position textures on faces
# To begin, select ungrouped faces and/or grouped objects, not components
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
# Get all ungrouped faces and those that are inside groups (from selection)
all_faces = []
@alexschreyer
alexschreyer / random_placement.rb
Created June 10, 2020 16:31
Randomize object placement on faces in SketchUp
# Randomize object placement on faces
# Select one component and at least one ungrouped face to start
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
num = 20 # Number of items to place on each face
max_rot = 360 # Rotation max. degrees
scale_var = 0.5 # Size variation
@alexschreyer
alexschreyer / move_ads.js
Created May 22, 2020 14:25
Creating better in-text ads with jQuery
// Create an in-text ad for long texts by MOVING with jQuery
jQuery('.ad').first().insertAfter('.entry-content > p:nth-of-type(4)');
// Create an in-text ad for long texts by COPYING with jQuery
jQuery('.ad').first().clone().insertAfter('.entry-content > p:nth-of-type(4)');
@alexschreyer
alexschreyer / turntable_animation.rb
Last active June 10, 2020 16:29
Sets up four scenes in SketchUp for a turntable-style animation.
# Sets up four scenes in SketchUp for a turntable-style animation.
mod = Sketchup.active_model # Open model
view = mod.active_view # View
dur = 3 # Scene transition duration
eyehgt = 9 * 12 # Eye height, set at 1/2 height of model
my_camera = Sketchup::Camera.new [0,-500,eyehgt], [0,0,eyehgt], [0,0,1]
view.camera = my_camera