Convert .skp to .obj together


#1

I have 7000 sketchup files and I’ve to convert all of them to .obj
format. It is not smart approach to export each file one by one so is
there any way to convert all of them at once?


#2

Write an RB script which opens every SKP in a selected folder in turn and then runs the exporter on it to make the OBJ version…
Here’s a quick v2015 example:

full_path_to_the_folder = UI.select_directory(title: "Select SKP folder...")
return unless full_path_to_the_folder
Dir.entries(full_path_to_the_folder).select{|e|
    File.extname(e).downcase == ".skp"
}.each{|skp|
    Sketchup.active_model.export(File.join(full_path_to_the_folder, skp.gsub(/[Ss][Kk][Pp]$/, "obj")), false)
}

Typed from memory, so it’s untested so check for typos…


#3

should likely be (if you want the obj files in the same folder:)

Sketchup.active_model.export(
  File.join(
    full_path_to_the_folder,
    "#{File.basename(skp,'.skp')}.obj")
  ),
  false
)

ie: full_path_to_the_folder is a var reference, not a string.

The new filename using string interpolation instead of a gsub call is just my preference as it doesn’t make Ruby create a Regexp object. (Perhaps the regular expression way might be faster, IF the Regexp object was created outside the loop, instead of recreated during each iteration.)

Ruby’s flexibility is so cool.

P.S.: TIG’s example is meant to be wrapped in a method (hence the return statement.)


#4

@Dan
Right again.
As I said it was untested - a silly typo.
I’ve edited the original to avoid confusion too…


#5

Here’s a fuller/better version that actually works !
Save text into a file named TIG-Export.rb
Use Notepad++ [PC] or TextWrangler [MAC] to ensure it is encoded as UTF8-without_BOM
Usage: In Ruby Console,
TIG::Exporter.new("obj")
The passed argument let’s you export to any available file-format…

# encoding: UTF-8
### v2015
module TIG
    module Export
        def self.new(typ="obj")
            dir = UI.select_directory(title: "Select folder of SKPs...")
            return unless dir
            dir.tr!("\\", "/")
            puts "#{dir}\n"
            pwd = Dir.pwd
            Dir.chdir(dir)
            Dir.entries(dir).select{|e| e =~ /[.]skp$/ }.each{|skp|
                    nam = skp.gsub(/skp$/, typ)
                    Sketchup.open_file(skp)
                    puts "#{skp} >>> #{nam}"
                    Sketchup.active_model.export(nam, false)
            }
            Dir.chdir(pwd)
            puts "\nDone."
        end#def
    end#module
 end#module

Batch open, import, save and close
#6

Thank u for your valuable comments. Where do i need to place this “TIG-Export.rb” file? Do I only need to write TIG::Exporter.new(“obj”) in SU Ruby Console?


#7

place it in your Plugins folder Sketchup.find_support_file("Plugins")
or use load "add/your/path/to/the.rb" in 'Ruby Console’
or zip the file, change the .zip to .rbz and ‘Preferences’ > ‘Extensions’ > 'Add Extension’
then use TIG::Exporter.new("obj")
john


#8

I placed the file in plugins folder now I’m getting this error on sketchup startup.

Error Loading File TIG-Export.rb
Error: #<SyntaxError: C:/Users/manish/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/TIG-Export.rb:15: syntax error, unexpected ‘,’, expecting '}'
Sketchup.active_model.export(nam), false)

                                              ^

C:/Users/manish/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/TIG-Export.rb:19: syntax error, unexpected keyword_end, expecting '}'
end#def

       ^>

#9

Sorry, a typo by me.
I have removed the extra ) in the posted code.
Recopy if from above and paste over the RB file’s contents.
It should then work !


#10

Shall I replace Sketchup.active_model.export(nam), false) with Sketchup.active_model.export(nam, false) ?


#11

That is the corrected typo.
Well spotted.


#12

Try 1: TIG::Exporter.new(“obj”)

Error: #NameError: uninitialized constant TIG::Exporter

:in `' -e:1:in `eval' Nil

Try 2 :TIG::Export.new(“obj”)

Error: #NoMethodError: undefined method select_directory' for UI:Module C:/Users/manish/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/TIG-Export.rb:6:innew’

:in `' -e:1:in `eval' Nil

#13

You must restart SketchUp to load the file, or… force it to load via the Ruby Console:

load "TIG-Export.rb"

select_directory() is only SketchUp 2015 or later.


#14

[quote=“DanRathbun, post:13, topic:10539”]

You can replace

            dir = UI.select_directory(title: "Select folder of SKPs...")
            return unless dir

with:

            dir = UI.openpanel("Select SKP file in folder...","*.skp","SketchUp Files (*.SKP)|*.skp;||")
            return unless dir
            dir = File.dirname(dir)

#15

It works great… Thanks to all of u…


#16

OBJ=read_wobj(‘examples\zidle_2529df1273423fa7a524f6bfc21eae12.obj’);

Reading Object file : examples\zidle_2529df1273423fa7a524f6bfc21eae12.obj
% Alias OBJ Model File
% Exported from SketchUp, © 2000-2012 Trimble Navigation Limited
% File units = centimeters
Reading Material file : examples\idle_2529df1273423fa7a524f6bfc21eae12.mtl

I’m getting error reading obj beacuse the name of mtl file associated with obj is truncates first letter [idle_2529df1273423fa7a524f6bfc21eae12.mtl] while exporting. What shall I do?


#17

Hello, i was looking for the same solution to changing multiple files formats from .skp to .obj, what i have tried but couldnt get it work.

I downloaded this code : TIG-Export.rb
I loaded this code via sketchup ruby console, and when i load it it says “TRUE” and nothing happends
I tried installing it as .rbz extension but when i installed it, nothing appeared in extension menu. So please, can someone explain me step by step how to convert these files? and how to use this code?


#18

Where did you get that rb file from - above ?

A lone RB file goes in your Plugins folder and is loaded as SketchUp starts.
If you manually load it from the Ruby Console it’s available then, even if it’s not in the Plugins folder.
The instructions are usually provided with a script on its download page.
There are lots of instructions and comments above…
This one is undoubtedly an old simple script - not and Extension - perhaps it is run from the Ruby Console ? [like it says]

From what you say it’s not going to do what you want anyway ?
You want a ‘batch’ tool that takes all of the SKPs in a selected folder and makes OBJs…
Here’s an example RB - Pro only - TIG-ExportBatchOBJ.rb (3.1 KB)
It adds an OBJ Batch Exporter to the File menu, so it’s easier to use on a folder…


#19

If possible, convert all "\" backslashes in pathstrings to "/" forward slashes. (The backslashes and the following characters are read as escape sequences. So it is seeing "\z" as an escape sequence.)

newpath = oldpath.tr("\\",'/')

#20

TIG-Export.rb (829 Bytes)

Keep the name as it is. It is the same provided by TIG sir.