Hey! I’ve written a script that exports all groups in the file separately, but there’s some kind of problem where it will just randomly crash without giving any information on what’s wrong. Ruby isn’t really my programming language, anyone that could help me out?
require 'sketchup.rb'
def exportAssets()
SKETCHUP_CONSOLE.clear
SKETCHUP_CONSOLE.show
model = Sketchup.active_model
mpath = model.path
if not mpath or mpath == ''
UI.beep
puts 'First save the model before exporting asset files'
return nil
end
mpath = File.dirname(mpath)
ents = model.entities
entsa = ents.to_a
defns = model.definitions
ss = model.selection
ssa = ss.to_a
# Find all select all groups
groups = []
defns.each{|d|d.instances.each{|group|groups << group if d.group? and group.parent == model}}
if not groups[0]
UI.beep
puts 'No assets to export (missing groups?)'
return nil
end
names = []
# Set position of each group to origin
groups.each{|group|
point = Geom::Point3d.new 0,0,0
t = Geom::Transformation.new point
group.transformation = t
}
# Hide/show each group and export
groups.each{|group|
ss.clear
ss.add(entsa)
ss.remove(group)
tohide = ss.to_a
ss.clear
hidden = []
tohide.each{|e|
if not e.hidden?
e.hidden = true
hidden << e
end
}
name = ('' + group.name + '_01').gsub(/[^A-Za-z0-9_]/, '_')
while names.include?(name)
name.next!
end
names << name
obj = name + '.obj'
puts path = File.join(mpath, obj).tr('\\', '/')
model.export(path, false)
hidden.each{|e|e.hidden = false}
}
UI.beep
puts 'Export completed'
I tested your script and it makes the files then splats…
I submitted the report, have you been doing that?
I simplified your script and it now runs…
def exportAssets()
SKETCHUP_CONSOLE.clear
SKETCHUP_CONSOLE.show
model = Sketchup.active_model
mpath = model.path
mtitle = model.title
if not mpath or mpath == ''
#UI.beep
puts 'First save the model before exporting asset files'
return nil
end
mpath = File.dirname(mpath)
ents = model.entities
entsa = ents.to_a
defns = model.definitions
# Find all select all groups
groups = []
defns.each{|d|d.instances.each{|group|groups << group if d.group? and group.parent == model}}
if not groups[0]
# UI.beep
puts 'No assets to export (missing groups?)'
return nil
end
# Hide everything
entsa.each{|e| e.hidden = true}
count = 0
groups.each{|group|
# show group, Set position and export
group.hidden = false
point = Geom::Point3d.new 0,0,0
t = Geom::Transformation.new point
group.transformation = t
count += 1
name = (mtitle + group.name + '_0' + count.to_s).gsub(/[^A-Za-z0-9_]/, '_')
obj = name + '.obj'
puts path = File.join(mpath, obj).tr('\\', '/')
model.export(path, false)
# Hide each group
group.hidden = true
break if group == groups.last
}
#UI.beep
puts 'Export completed'
# Unhide everything
entsa.each{|e| e.hidden = false}
end
exportAssets()
taking a little further to reset all the groups to their original positions…
groups.each{|group|
# show group
group.hidden = false
# find out where the group lives...
orig_t = group.transformation
# use the Constant to simplify matters...
group.transformation = ORIGIN
count += 1
name = (mtitle + group.name + '_0' + count.to_s).gsub(/[^A-Za-z0-9_]/, '_')
obj = name + '.obj'
puts path = File.join(mpath, obj).tr('\\', '/')
model.export(path, false)
# move the group back...
group.transformation = orig_t
# re-Hide each group
group.hidden = true
# a break appears to be needed...
break if group == groups.last
}
That’s awesome John, I haven’t been reporting the bugs before simply because I had no clue whether it was my inferior code causing the trouble or SketchUp itself. Much appreciated, will try the code on a couple of projects and report back!
Script working perfectly apart from an issue where it exports not just 1 group per file, but also includes all previous groups. I guess there’s a problem with hiding/showing the groups?
Ah I think I’ve made a mistake, it’s actually working perfectly on my home computer. Possibly some kind of trouble with the settings of my SketchUp installation at work. Attached is the test file, just in case.
I did have a typo in the first that I fixed earlier… [false instead of true]
but it also depends on your last used obj export setting…
make sure the selected only option is not ticked…
a couple of small tweaks and this worked on your file…
the code should be nicely wrapped in a namespace when it’s functioning…
def exportAssets()
SKETCHUP_CONSOLE.clear
SKETCHUP_CONSOLE.show
model = Sketchup.active_model
mpath = model.path
mtitle = model.title
if not mpath or mpath == ''
#UI.beep
puts 'First save the model before exporting asset files'
return nil
end
mpath = File.dirname(mpath)
ents = model.entities
entsa = ents.to_a
defns = model.definitions
# Find all select all groups
groups = []
defns.each{|d|d.instances.each{|group|groups << group if d.group? and group.parent == model}}
if not groups[0]
# UI.beep
puts 'No assets to export (missing groups?)'
return nil
end
# Hide everything
entsa.each{|e| e.hidden = true}
count = 0
groups.each{|group|
# show group
group.hidden = false
# find out where the group lives...
orig_t = group.transformation
# use the Constant to simplify matters...
group.transformation = ORIGIN
count += 1
name = (mtitle + group.name + '_0' + count.to_s).gsub(/[^A-Za-z0-9_]/, '_')
obj = name + '.obj'
puts path = File.join(mpath, obj).tr('\\', '/')
model.export(path, false)
# move the group back...
group.transformation = orig_t
# re-Hide each group
group.hidden = true
# a break appears to be needed...
UI.messagebox 'Export completed' if group == groups.last
}
# Unhide everything
entsa.each{|e| e.hidden = false}
end
exportAssets()