From c4ec6830d2dfddce6faa0771b8d1135adf85d57b Mon Sep 17 00:00:00 2001 From: Samuel Tallet Date: Sun, 24 Nov 2019 04:07:27 +0100 Subject: [PATCH] Conform code to Rubocop. --- source/.rubocop.yml | 19 ++++++++++-- source/pbr.rb | 2 +- source/pbr/app_observer.rb | 6 ++-- source/pbr/chromium.rb | 10 +++++-- source/pbr/gltf.rb | 20 +++++++++---- source/pbr/model_observer.rb | 4 +-- source/pbr/shapes.rb | 6 ++-- source/pbr/textures.rb | 39 ++++++++++++++++--------- source/pbr/updates.rb | 56 ++++++++++++++++++++++++------------ source/pbr/viewport.rb | 4 +-- 10 files changed, 114 insertions(+), 52 deletions(-) diff --git a/source/.rubocop.yml b/source/.rubocop.yml index 3c3f230..4e0bb96 100644 --- a/source/.rubocop.yml +++ b/source/.rubocop.yml @@ -1,10 +1,25 @@ # See: https://github.com/bbatsov/rubocop If you want to know what is RuboCop. AllCops: - TargetRubyVersion: 2.2 + TargetRubyVersion: 2.3 + Exclude: + - 'pbr/shapes.rb' + - 'pbr/lights.rb' Layout: Enabled: false -Style/AsciiComments: +Style: Enabled: false + +Naming/RescuedExceptionsVariableName: + PreferredName: exception + +Metrics/MethodLength: + Max: 15 + +Metrics/AbcSize: + Max: 20 + +Metrics/ClassLength: + Max: 150 \ No newline at end of file diff --git a/source/pbr.rb b/source/pbr.rb index 133bae9..ce7585b 100644 --- a/source/pbr.rb +++ b/source/pbr.rb @@ -69,7 +69,7 @@ def self.required_plugin_exist? Centaur.const_get(:GltfExporter) true - rescue NameError => _error + rescue NameError => _exception false end diff --git a/source/pbr/app_observer.rb b/source/pbr/app_observer.rb index 7392246..61147db 100644 --- a/source/pbr/app_observer.rb +++ b/source/pbr/app_observer.rb @@ -1,5 +1,5 @@ # Physically-Based Rendering extension for SketchUp 2017 or newer. -# Copyright: © 2018 Samuel Tallet-Sabathé +# Copyright: © 2019 Samuel Tallet # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -29,7 +29,7 @@ module PBR # Observes SketchUp events and reacts. class AppObserver < Sketchup::AppObserver - # rubocop: disable MethodName + # rubocop: disable Naming/MethodName # When SketchUp user creates a new, empty model. def onNewModel(_model) @@ -60,7 +60,7 @@ def onQuit end - # rubocop: enable MethodName + # rubocop: enable Naming/MethodName end diff --git a/source/pbr/chromium.rb b/source/pbr/chromium.rb index 1c80a2c..995cd87 100644 --- a/source/pbr/chromium.rb +++ b/source/pbr/chromium.rb @@ -61,9 +61,15 @@ def self.make_exec def self.preferences if Sketchup.platform == :platform_osx - File.join('~', 'Library', 'Application Support', 'Chromium', 'Default', 'Preferences') + File.join( + '~', 'Library', 'Application Support', 'Chromium', + 'Default', 'Preferences' + ) else - File.join(ENV['LOCALAPPDATA'], 'Chromium', 'User Data', 'Default', 'Preferences') + File.join( + ENV['LOCALAPPDATA'], 'Chromium', + 'User Data', 'Default', 'Preferences' + ) end end diff --git a/source/pbr/gltf.rb b/source/pbr/gltf.rb index 3645ae8..1adebbf 100644 --- a/source/pbr/gltf.rb +++ b/source/pbr/gltf.rb @@ -67,7 +67,7 @@ def initialize complete - rescue StandardError => _error + rescue StandardError => _exception @valid = false @@ -95,6 +95,19 @@ def json end + # Applies various fixes. + # + # @return [void] + private def apply_various_fixes + + Textures.fix_all_without_filename_or_not_supported + Lights.fix_without_color + NilMaterialFix.new(TRANSLATE['Propagate Materials to Whole Model']) + + nil + + end + # Generates almost all of glTF asset thanks to exporter made by Centaur. # @see https://extensions.sketchup.com/content/gltf-exporter # @@ -105,10 +118,7 @@ def json File.delete(gltfile) if File.exist?(gltfile) - # Apply various fixes. - Textures.fix_without_filename_or_not_supported - Lights.fix_without_color - NilMaterialFix.new(TRANSLATE['Propagate Materials to Whole Model']) + apply_various_fixes Sketchup.active_model.layers.add(Lights::LAYER_NAME) diff --git a/source/pbr/model_observer.rb b/source/pbr/model_observer.rb index 5220377..452615a 100644 --- a/source/pbr/model_observer.rb +++ b/source/pbr/model_observer.rb @@ -29,7 +29,7 @@ module PBR # Observes SketchUp model events and reacts. class ModelObserver < Sketchup::ModelObserver - # rubocop: disable MethodName + # rubocop: disable Naming/MethodName # Before a SketchUp model is saved to disk. def onPreSaveModel(_model) @@ -45,7 +45,7 @@ def onPostSaveModel(_model) end - # rubocop: enable MethodName + # rubocop: enable Naming/MethodName end diff --git a/source/pbr/shapes.rb b/source/pbr/shapes.rb index ec5784c..4ddd169 100644 --- a/source/pbr/shapes.rb +++ b/source/pbr/shapes.rb @@ -136,10 +136,10 @@ def add_extruded_points(pts, center, dir, angle, numsegments) # @param [String] radius Radius. Example: '1m'. # @param [Integer] n90 Segments per 90 degrees. Default: 10. # @param [Integer] smooth Smooth parameter. Default: 12. - # @param [Integer|String] layer_index_or_name Layer index or name. Default: 0. + # @param [Integer|String] layer_idx_or_name Layer index or name. Default: 0. # # @return [Sketchup::Group] Group that contains sphere. - def self.create_sphere(radius, n90 = 10, smooth = 12, layer_index_or_name = 0) + def self.create_sphere(radius, n90 = 10, smooth = 12, layer_idx_or_name = 0) group = Sketchup.active_model.entities.add_group @@ -167,7 +167,7 @@ def self.create_sphere(radius, n90 = 10, smooth = 12, layer_index_or_name = 0) group.entities.add_faces_from_mesh(mesh, smooth) # Assign group to layer. - group.layer = Sketchup.active_model.layers[layer_index_or_name] + group.layer = Sketchup.active_model.layers[layer_idx_or_name] group diff --git a/source/pbr/textures.rb b/source/pbr/textures.rb index 1c8a954..e282d60 100644 --- a/source/pbr/textures.rb +++ b/source/pbr/textures.rb @@ -32,7 +32,7 @@ module Textures # Fixes textures without filename or not supported by glTF (.bmp, .tif...). # # @return [void] - def self.fix_without_filename_or_not_supported + def self.fix_all_without_filename_or_not_supported # XXX Works only on SketchUp >= 2018. return if Sketchup.version.to_i < 18 @@ -45,29 +45,40 @@ def self.fix_without_filename_or_not_supported if mat.texture.filename.empty?\ or mat.texture.filename !~ /\.(jpg|png)$/ - texture_path = File.join( - Sketchup.temp_dir, 'sketchup-tex-' + mat.object_id.to_s + '.png' - ) + fix_one_without_filename_or_not_supported(mat) - texture_width = mat.texture.width + end - texture_height = mat.texture.height + end - mat.texture.image_rep.save_file(texture_path) + } - mat.texture = texture_path + nil - mat.texture.size = [texture_width, texture_height] + end - File.delete(texture_path) + # Fixes texture without filename or not supported by glTF (.bmp, .tif...). + # + # @param [Sketchup::Material] mat SketchUp material texture to fix. + # + # @return [void] + private def fix_one_without_filename_or_not_supported(mat) - end + texture_path = File.join( + Sketchup.temp_dir, 'sketchup-tex-' + mat.object_id.to_s + '.png' + ) - end + texture_width = mat.texture.width - } + texture_height = mat.texture.height - nil + mat.texture.image_rep.save_file(texture_path) + + mat.texture = texture_path + + mat.texture.size = [texture_width, texture_height] + + File.delete(texture_path) end diff --git a/source/pbr/updates.rb b/source/pbr/updates.rb index 294af9d..80fe88b 100644 --- a/source/pbr/updates.rb +++ b/source/pbr/updates.rb @@ -26,41 +26,61 @@ # PBR plugin namespace. module PBR - # Plugin updates. + # PBR plugin updates. module Updates - # URL of plugin homepage at SketchUcation site. + # URL of PBR plugin homepage at SketchUcation website. SKETCHUCATION_URL = 'https://sketchucation.com/plugin/2101-pbr'.freeze - # Check for plugin updates. + # Returns PBR plugin last version number. + # + # @return [String] Last version number. + def self.last_version + + sketchucation_html = URI.open( + SKETCHUCATION_URL, :read_timeout => 5 + ).read + + last_version_arr = sketchucation_html.match(/v(\d+\.\d+.\d+)/).to_a + + last_version_arr[1] + + end + + # Alerts user if a new PBR plugin version is available. + # + # @param [String] last_version_str Last version number. # # @return [void] - def self.check + def self.alert_user(last_version_str) - begin + if VERSION.gsub('.', '').to_i < last_version_str.gsub('.', '').to_i - sketchucation_html = open(SKETCHUCATION_URL, { read_timeout: 5 }).read + UI.messagebox( + TRANSLATE['A newer version of the PBR plugin is available:']\ + + ' ' + last_version_str + ) - last_version_array = sketchucation_html.match(/v(\d+\.\d+.\d+)/).to_a + UI.openURL(SKETCHUCATION_URL) - last_version_int = last_version_array[1].gsub('.', '').to_i + end - if VERSION.gsub('.', '').to_i < last_version_int + nil - UI.messagebox( - TRANSLATE['A newer version of the PBR plugin is available:']\ - + ' ' + last_version_array[1] - ) + end - puts last_version_array[1] + # Checks for plugin updates. + # + # @return [void] + def self.check - UI.openURL(SKETCHUCATION_URL) + begin - end + alert_user(last_version) - rescue StandardError => error + rescue StandardError => exception puts 'Impossible to check if a newer PBR plugin version exists'\ - + ' because: ' + error.to_s + + ' because: ' + exception.to_s end end diff --git a/source/pbr/viewport.rb b/source/pbr/viewport.rb index 63be133..b761aca 100644 --- a/source/pbr/viewport.rb +++ b/source/pbr/viewport.rb @@ -94,7 +94,7 @@ def self.open ) - rescue StandardError => _error + rescue StandardError => _exception UI.messagebox("Unable to find: \"#{Chromium.executable}\". Get latest.") end @@ -108,7 +108,7 @@ def self.close Chromium.simulate_normal_exit true - rescue StandardError => _error + rescue StandardError => _exception false end