Export when SketchUp not in focus

I need export some sequence of files. I made plugin for this and it works fine until SketchUp is in focus. If I open any window over SketchUp, it stops export (or even crash sometimes). Is there any way to make export when SketchUp not in focus?

When your SU window doesn’t have the system focus the script normally is still running. Without any other information we can’t really help you.

Can you explain what your plugin is doing ? Maybe some code ?

1 Like

The idea is: I create object, export it to the formats, erase everything, then create next object, export, erase, etc…
The export code:

Sketchup.active_model.save(path  + ".skp", Sketchup::Model::VERSION_8)

options_dae = { :show_summary => false,
                  :triangulated_faces => true,
          :doublesided_faces => false,
          :edges => false,
          :author_attribution => false,
          :hidden_geometry => false,
          :preserve_instancing => true,
          :texture_maps => true,
          :selectionset_only => false,
          :camera_lookat => false, 
              }
  options_autocad = { :show_summary => false,
                    :acad_version => "acad_2000",
                    :faces_flag => true,
                    :construction_geometry => true,
                    :dimensions => true,
                    :text => true,
                    :edges => true
                    }
    options_fbx = { :show_summary => false,
                  :units => "model", 
          :triangulated_faces => false,
          :doublesided_faces => false,
          :texture_maps => true,
          :seperate_disconnected_faces => false,
          :swap_yz => true,
          :selectionset_only => false
                 }
  options_3ds = { :show_summary => false,
          :units => "model", 
          :geometry => "full_hierarchy",
          :doublesided_faces => false,
          :faces => "not_two_sided",
          :edges => false,
          :texture_maps => true,
          :preserve_texture_coords => false,
          :cameras => false,
          :selectionset_only => false
                }
    options_obj = { :show_summary => false,
          :units => "model", 
          :triangulated_faces => false,
          :doublesided_faces => true,
          :edges => true,
          :texture_maps => true,
          :swap_yz => true,
          :selectionset_only => false
                }
    Sketchup.active_model.export(path+".dae", options_dae)                
    Sketchup.active_model.export(path+".fbx", options_fbx)  
    Sketchup.active_model.export(path+".obj", options_obj) 
    FormatsAutoCad.each do |format| 
      Sketchup.active_model.export(path+format, options_autocad)
    end
    Sketchup.active_model.export(path+".3ds", options_3ds) 

I suppose the problem is with export only

1 Like

Try this …

# encoding: UTF-8

module Example
  module ExportMultiple

    extend self

    unless defined?(@@loaded)
      @@loaded = false
      WIN = Sketchup.platform == :platform_win rescue RUBY_PLATFORM !~ /darwin/
    end

    FORMATS ||= [:dae,:fbx,:obj,:dwg,:dxf,'3ds']

    OPTIONS ||= Hash[
      :dae, {
        :show_summary => false,
        :triangulated_faces => true,
        :doublesided_faces => false,
        :edges => false,
        :author_attribution => false,
        :hidden_geometry => false,
        :preserve_instancing => true,
        :texture_maps => true,
        :selectionset_only => false,
        :camera_lookat => false, 
      },
      :autocad, {
        :show_summary => false,
        :acad_version => "acad_2000",
        :faces_flag => true,
        :construction_geometry => true,
        :dimensions => true,
        :text => true,
        :edges => true
      },
      :fbx, {
        :show_summary => false,
        :units => "model", 
        :triangulated_faces => false,
        :doublesided_faces => false,
        :texture_maps => true,
        :seperate_disconnected_faces => false,
        :swap_yz => true,
        :selectionset_only => false
      },
      '3ds', {
        :show_summary => false,
        :units => "model", 
        :geometry => "full_hierarchy",
        :doublesided_faces => false,
        :faces => "not_two_sided",
        :edges => false,
        :texture_maps => true,
        :preserve_texture_coords => false,
        :cameras => false,
        :selectionset_only => false
      },
      :obj, {
        :show_summary => false,
        :units => "model", 
        :triangulated_faces => false,
        :doublesided_faces => true,
        :edges => true,
        :texture_maps => true,
        :swap_yz => true,
        :selectionset_only => false
      }
    ]

    def choose_path()
      wildcard = WIN ? 'SketchUp Model Files|*.skp||' : '*.skp'
      UI.savepanel('Choose save path ...',ENV['HOME'],wildcard)
    end

    def start_export( model = Sketchup.active_model )
      pathname = choose_path()
      return false unless pathname # user can cancel export
      @block_called = false
      path = File.dirname(pathname)
      filename = File.basename(pathname,'.skp')
      #
      @tid = UI.start_timer(0.5,false) {
        UI.stop_timer(@tid)
        next if @block_called
        @block_called = true
        #
        model.save("#{path}/#{filename}.skp", Sketchup::Model::VERSION_8)
        #
        FORMATS.each do |sym|               
          model.export(
            "#{path}/#{filename}.#{sym.to_s}",
            OPTIONS[sym]
          )
        end
        #
      }
      #
    end

    unless @@loaded
      UI.menu('Plugins').add_item('Export in Multiple Formats') {
        start_export()
      }
      @@loaded = true
    end

  end # inner plugin module
end # outer namespace module
1 Like

Make a reference to model just once.
Every time you call Sketchup.active_model it does what is says… grab the active model. But, when you switch focus, there is no active model.

So, on top of your code do model = Sketchup.active_model and replace all other occurences with that reference.

2 Likes

Which is what I made sure to do in the example above …

    def start_export( model = Sketchup.active_model )

Correct Dan, Although your example might be a little overwhelming and not really point out what the real issue is.

1 Like

Yea. on Mac it’s a multi-document interface. Sketchup.active_model returns the model object in the document window that has focus. Apparently there is no saving of the last focused object (ie, a fallback reference.) This could be fixed I think.

I’m not sure where the OP’s problem lies, but I don’t think Mac MDI or active_model is the issue. I created a SketchUp session with two models open. I queried and saved the active_model id for each of them in Ruby variables. I then started a repeating timer loop that queried the active_model and printed both its id and the saved ids for the two models. The results were correct when I switched focus between the two models, and the timer continued to report correct results when I opened a Terminal window atop the SketchUp window or changed to a browser window on another virtual desktop. The only time the reporting stopped was when I caused SketchUp to shift focus to one of its own menus, which isn’t too surprising given that the UI is running the timer.

The OP did not use a timer block. I purposefully put one in my example.

I watch movies on with SU off screen running long process’s,

when it stops, it annoying as it drags you back to it…

I even wrote an app that force SU to the back ground to increase the speed…

it worked, so I bought the switching is the issue…

garbage collect after each export is likely to help or run them from a shell script sequentially when the pid is really stopped…

EDIT: old example of waiting for the pid before restarting a mac…

still works…

mac_restart.rb (3.2 KB)

john

Thank you to all! But it is still doesn’t work.

I made simple example what happened in my plugin. And I find out the the crash is caused only on Mac and only if I try to export to AutoCad formats. When SketchUp loose the focus, of course. The other formats works fine.
Probably there is something wrong with export parameters, I don’t know, I think they are usual.

Do you have any ideas?

module Example
  module ExportMultiple

extend self

unless defined?(@@loaded)
  @@loaded = false
  WIN = Sketchup.platform == :platform_win rescue RUBY_PLATFORM !~ /darwin/
end

FORMATS ||= [:dae,:fbx,:obj,:dwg,:dxf,'3ds']
#FORMATS ||= [:dae,:fbx,:obj,'3ds']

OPTIONS ||= Hash[
  :dae, {
    :show_summary => false,
    :triangulated_faces => true,
    :doublesided_faces => false,
    :edges => false,
    :author_attribution => false,
    :hidden_geometry => false,
    :preserve_instancing => true,
    :texture_maps => true,
    :selectionset_only => false,
    :camera_lookat => false, 
  },
  :dwg, {
    :show_summary => false,
    :acad_version => "acad_2000",
    :faces_flag => true,
    :construction_geometry => true,
    :dimensions => true,
    :text => true,
    :edges => true
  },
  :dxf, {
    :show_summary => false,
    :acad_version => "acad_2000",
    :faces_flag => true,
    :construction_geometry => true,
    :dimensions => true,
    :text => true,
    :edges => true
  },
  :fbx, {
    :show_summary => false,
    :units => "model", 
    :triangulated_faces => false,
    :doublesided_faces => false,
    :texture_maps => true,
    :seperate_disconnected_faces => false,
    :swap_yz => true,
    :selectionset_only => false
  },
  '3ds', {
    :show_summary => false,
    :units => "model", 
    :geometry => "full_hierarchy",
    :doublesided_faces => false,
    :faces => "not_two_sided",
    :edges => false,
    :texture_maps => true,
    :preserve_texture_coords => false,
    :cameras => false,
    :selectionset_only => false
  },
  :obj, {
    :show_summary => false,
    :units => "model", 
    :triangulated_faces => false,
    :doublesided_faces => true,
    :edges => true,
    :texture_maps => true,
    :swap_yz => true,
    :selectionset_only => false
  }
]

def choose_path()

 UI.select_directory(title: "Select Export Directory")
 
end

def start_export( model = Sketchup.active_model )


  path = choose_path()
  
  return false unless path # user can cancel export
  @block_called = false

  0.upto(10) { |i| 

	entities = model.active_entities
  	entities.erase_entities entities.to_a
  	size = 10 + i * 10
  	p1 = Geom::Point3d.new(0, 0 ,0)
	p2 = Geom::Point3d.new(size, 0 ,0)
	p3 = Geom::Point3d.new(size, size ,0)
	p4 = Geom::Point3d.new(0, size ,0)

	face = entities.add_face p1, p2, p3, p4
	face.pushpull -size

  filename = 'Test' + i.to_s + '.skp'
  #
 # @tid = UI.start_timer(0.5,false) {
 #   UI.stop_timer(@tid)
 #   next if @block_called
 #   @block_called = true
    #
    model.save("#{path}/#{filename}.skp", Sketchup::Model::VERSION_8)
    #
    FORMATS.each do |sym|               
      model.export(
        "#{path}/#{filename}.#{sym.to_s}",
        OPTIONS[sym]
      )
    end
    #
  #}
  #
}
end

unless @@loaded
  UI.menu('Plugins').add_item('Export in Multiple Formats') {
    start_export(Sketchup.active_model)
  }
  @@loaded = true
end

  end # inner plugin module
end # outer namespace module

it crashes with or without focus…

john

What versions of SketchUp are we talking about here? Does it also happen with the currently latest SU2019.3?

for me it crashes on v19.3…

on the last cycle after making all the exports…

john

Does it crash if you run a different export between the dwg and dxf exports?

I added in pauses and forced Garbage Collection to get to the end, but the crash happens after that for me…

and ‘BugSplat’ crashes without even writing the log…

john

Is there a crash report in

~/Library/Logs/Diagnostic Reports

If the app crashes due to a self-administered abort, BugSplat won’t be triggered, but there will still be a crash report created.

I get the bugSplat window, but it crashes, and the only log is about BugSplat crashing when it tries to send the log using CEF…

not about the ‘export’ crash itself…

Does adding in a beginrescue help catch any exceptions ?

What if the outer loop is removed ? (Ie, reduce the possibly of a stack error.)