Skip to content

Commit

Permalink
Fix missing textures.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Jan 16, 2020
1 parent 2288786 commit d9ce7c4
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 32 deletions.
2 changes: 1 addition & 1 deletion source/universal_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# Universal Importer plugin namespace.
module UniversalImporter

VERSION = '1.1.2'.freeze
VERSION = '1.1.3'.freeze

# Load translation if it's available for current locale.
TRANSLATE = LanguageHandler.new('uir.strings')
Expand Down
4 changes: 2 additions & 2 deletions source/universal_importer/Resources/es/uir.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

"Select a 3D Model"="Seleccionar un Modelo 3D";
"3D Models"="Modelos 3D";
"Select a Texture Atlas (Optional)"="Seleccionar un Atlas de Texturas (Opcional)";
"Model height (cm)"="Altura del modelo (cm)";
"Select a Texture for Material:"="Seleccionar una Textura para el Material:";
"Images"="Imágenes";
"Model has"="El modelo tiene";
"faces"="caras";
"Reduce polygon count?"="¿Reducir la cantidad de polígonos?";
"Target face number"="Número de caras objetivo";
"Polygon Reduction"="Reducción de Polígonos";
"Model height (cm)"="Altura del modelo (cm)";

//
// File: menu.rb
Expand Down
4 changes: 2 additions & 2 deletions source/universal_importer/Resources/fr/uir.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

"Select a 3D Model"="Sélectionner un modèle 3D";
"3D Models"="Modèles 3D";
"Select a Texture Atlas (Optional)"="Sélectionner un atlas de textures (Optionnel)";
"Model height (cm)"="Hauteur du modèle (cm)";
"Select a Texture for Material:"="Sélectionner une texture pour le matériau :";
"Images"="Images";
"Model has"="Le modèle a";
"faces"="faces";
"Reduce polygon count?"="Réduire le nombre de polygones ?";
"Target face number"="Nombre de faces ciblé";
"Polygon Reduction"="Réduction de polygones";
"Model height (cm)"="Hauteur du modèle (cm)";

//
// File: menu.rb
Expand Down
57 changes: 30 additions & 27 deletions source/universal_importer/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'universal_importer/obj'
require 'universal_importer/utils'
require 'universal_importer/assimp'
require 'universal_importer/mtl'
require 'universal_importer/meshlab'

# Universal Importer plugin namespace.
Expand Down Expand Up @@ -69,8 +70,6 @@ def initialize
# Aborts if user cancelled operation.
return if @import_file_path.nil?

import_texture_atlas

ask_for_model_height

copy_to_prog_data_dir
Expand All @@ -79,7 +78,7 @@ def initialize

fix_e_tex_in_obj_export

fix_atlas_in_obj_export
fix_m_tex_in_obj_export

ask_for_poly_reduction

Expand Down Expand Up @@ -138,20 +137,6 @@ def import_from_any_format

end

# Imports optional texture atlas of 3D model.
#
# @return [nil, String]
def import_texture_atlas

@import_texture_atlas_file_path = UI.openpanel(

TRANSLATE['Select a Texture Atlas (Optional)'], nil,
TRANSLATE['Images'] + '|*.jpg;*.png;*.bmp;||'

)

end

# Asks user for model height.
#
# @return [nil]
Expand Down Expand Up @@ -321,7 +306,7 @@ def export_to_obj_format

end

# Fixes embedded textures in Assimp OBJ export?
# If they exist: fixes embedded textures in Assimp OBJ export.
#
# @return [nil]
def fix_e_tex_in_obj_export
Expand Down Expand Up @@ -383,23 +368,41 @@ def fix_e_tex_in_obj_export

end

# Fixes texture atlas in Assimp OBJ export?
# If they exist: fixes missing textures in Assimp OBJ export.
#
# @return [nil]
def fix_atlas_in_obj_export

return if @import_texture_atlas_file_path.nil?
def fix_m_tex_in_obj_export

obj_mtl_export_file_path = File.join(SESSION[:temp_dir], 'export.mtl')

obj_mtl_export = File.read(obj_mtl_export_file_path)
mtl = MTL.new(obj_mtl_export_file_path)

mtl_materials_wo_textures = mtl.materials_wo_textures

obj_mtl_export += "\n"
return if mtl_materials_wo_textures.empty?

obj_mtl_export += 'map_Kd '
obj_mtl_export += File.basename(@import_texture_atlas_file_path)
mtl_materials_wo_textures.each do |material_name|

texture_path = UI.openpanel(

TRANSLATE['Select a Texture for Material:'] + ' ' + material_name,
nil, TRANSLATE['Images'] + '|*.jpg;*.png;*.bmp;||'

)

# Skips if user cancelled...
next if texture_path.nil?

FileUtils.cp(
texture_path, # source
SESSION[:temp_dir] # destination
)

mtl.set_material_texture(material_name, File.basename(texture_path))

end

File.write(obj_mtl_export_file_path, obj_mtl_export)
File.write(obj_mtl_export_file_path, mtl.rebuilt_file_contents)

nil

Expand Down
162 changes: 162 additions & 0 deletions source/universal_importer/mtl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Universal Importer extension for SketchUp 2017 or newer.
# Copyright: © 2019 Samuel Tallet <samuel.tallet arobase gmail.com>
#
# 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
# the Free Software Foundation, either version 3.0 of the License, or
# (at your option) any later version.
#
# If you release a modified version of this program TO THE PUBLIC,
# the GPL requires you to MAKE THE MODIFIED SOURCE CODE AVAILABLE
# to the program's users, UNDER THE GPL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Get a copy of the GPL here: https://www.gnu.org/licenses/gpl.html

raise 'The UIR plugin requires at least Ruby 2.2.0 or SketchUp 2017.'\
unless RUBY_VERSION.to_f >= 2.2 # SketchUp 2017 includes Ruby 2.2.4.

require 'fileutils'

# Universal Importer plugin namespace.
module UniversalImporter

# Minimal OBJ MTL parser.
class MTL

# Parses an OBJ MTL file.
def initialize(file_path)

raise ArgumentError, 'File Path parameter must be a String.'\
unless file_path.is_a?(String)

file_contents = File.read(file_path)

@header = ''

@materials = []

file_contents.split('newmtl').each do |material_or_header|

line_count = 0

material_or_header.lines.each do |line|

# XXX Naturally, we ignore comments & empty lines.
next if line.start_with?('#') || line.strip.empty?

line_count += 1

end

if line_count == 0

@header = material_or_header

else

@materials.push('newmtl' + material_or_header)

end

end

end

# If they exist: returns materials without textures.
#
# @return [Array<String>]
def materials_wo_textures

output = []

@materials.each do |material|

material_name = ''

material_has_texture = false

material.lines.each do |line|

if line.start_with?('newmtl')

material_name = line.sub('newmtl', '').strip

elsif line.start_with?('map_Kd')

material_has_texture = true

end

end

if !material_has_texture

output.push(material_name)

end

end

output

end

# Assigns a texture to a material.
#
# @return [nil]
def set_material_texture(material_name, texture_path)

new_materials = []

@materials.each do |material|

material_matches = false

material.lines.each do |line|

if line.start_with?('newmtl')\
&& line.sub('newmtl', '').strip == material_name

material_matches = true

break

end

end

if material_matches

material += 'map_Kd ' + texture_path + "\n\n"

end

new_materials.push(material)

end

@materials = new_materials

nil

end

# Returns rebuilt file contents.
#
# @return [String]
def rebuilt_file_contents

tag = "\n# File modified by Universal Importer plugin for SketchUp. \n\n"

@header + tag + @materials.join

end

end

end

0 comments on commit d9ce7c4

Please sign in to comment.