Skip to content

Instantly share code, notes, and snippets.

@Code47X
Last active December 28, 2021 06:47
Show Gist options
  • Save Code47X/ad0bbbf8ff0aefd5536e9431e66d5f0d to your computer and use it in GitHub Desktop.
Save Code47X/ad0bbbf8ff0aefd5536e9431e66d5f0d to your computer and use it in GitHub Desktop.
A pipeline script to expedite my react-three-fiber workflow. Blender -> Draco Compression -> GLTFJSX -> Component
#!/home/tanner/.rvm/rubies/ruby-2.7.2/bin/ruby
# frozen_string_literal: false
require 'pathname'
require 'colorize'
# rubocop:disable Style/Documentation, Style/ClassVars, Style/GuardClause, Metrics/AbcSize
class Pipeline
@@project_dir = Dir.new("#{Dir.home}/Development/tanman.dev")
@@blender_exports_dir = Dir.new("#{Dir.home}/Blender/tanman.dev/exports")
@@draco_output_dir = Dir.new("#{@@project_dir.path}/public/gltf")
@@jsx_output_dir = Dir.new("#{@@project_dir.path}/src/components")
def initialize(model_filename)
validate(model_filename)
@model_path = Pathname.new("#{@@blender_exports_dir.path}/#{model_filename}")
@draco_file_name = File.basename(@model_path, '.glb').concat('-draco.glb')
@draco_path = Pathname.new("#{@@draco_output_dir.path}/#{@draco_file_name}")
@component_file_name = File.basename(@draco_path, '.glb').capitalize.concat('.tsx')
@component_path = Pathname.new("#{@@project_dir.path}/#{@component_file_name}")
@camelcase_file_name = File.basename(@component_path, '-draco.tsx').split('-').map(&:capitalize).join.concat('.tsx')
@camelcase_path = Pathname.new("#{@@jsx_output_dir.path}/#{@camelcase_file_name}")
create_backup_files
end
def draco_compression
`gltf-pipeline -i #{@model_path} -o #{@draco_path} -d`
puts "#{'Success:'.light_white.on_light_green} Draco compression"
end
def gltf_jsx
`npx --no-install gltfjsx #{@draco_path} -t`
puts "#{'Success:'.light_white.on_light_green} GLTF-JSX conversion"
end
def move_and_rename_component
`mv #{@component_path} #{@camelcase_path}`
`sed -i "s,/#{@draco_file_name},gltf/#{@draco_file_name},g" #{@camelcase_path}` # Fix paths inside component
puts "#{'Success:'.light_white.on_light_green} #{@camelcase_file_name} created"
end
private
def validate(filename)
if filename.nil? || !File.file?("#{@@blender_exports_dir.path}/#{filename}")
puts 'File not found:'.red + " #{@@blender_exports_dir.path}/#{filename}"
exit
end
end
def create_backup_files
puts 'Creating backup files'.light_blue.on_black if File.file?(@draco_path) || File.file?(@camelcase_path)
# If an existing .backup file is found, prompt user to overwrite
`cp -S .backup --force --backup -i #{@draco_path} #{@draco_path}` if File.file?(@draco_path)
`cp -S .backup --force --backup -i #{@camelcase_path} #{@camelcase_path}` if File.file?(@camelcase_path)
end
end
# rubocop:enable Style/Documentation, Style/ClassVars, Style/GuardClause, Metrics/AbcSize
pipeline = Pipeline.new(ARGV[0])
pipeline.draco_compression
pipeline.gltf_jsx
pipeline.move_and_rename_component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment