Export only enabled pages in animation

It’s possible disable some pages of animation in Scene manager and export only the enabled?
How i can export your names in order?

I am studing and for while my code is this.

model = Sketchup.active_model
model_path =     File.dirname(model.path)
model_filename = File.basename(model.path)
pages = model.pages
page = pages.selected_page

my_cam = File.new(model_path + "\\" + model_name + "_camera.txt, "w")

# for each enabled pages

my_cam.puts("page.name" + cam1)
my_cam.puts("page.name" + cam2)
my_cam.puts("page.name" + cam3)
my_cam.puts("page.name" + cam4)

my_cam.close

I’m sorry, the “include in animation” flag is not exposed in the Ruby API.
We requested it be added several versions ago.

The API was updated with the v2018 release:

You can mark which pages to ignore by inserting a indicator in the description attribute. Assume the first char of pg.description[0,1] is '!' (… you’ll have to manually marked the pages to ignore.)

model = Sketchup.active_model
model_path = File.dirname(model.path)
model_name = model.name
if model_name.empty?
  model_name = "untitled"
  ret = UI.inputbox(
    ["Enter model name: "],
    [model_name],
    "Model Name"
  )
  model_name = ret[0] if ret != false
end

enab = model.pages.find_all {|pg|
  pg.description[0,1] != '!' 
}

if !enab.empty?
  filename = File.join(
    model_path,
    "#{model_name}_camera.txt"
  )
  File.open(
    filename,
    mode: "w",
    encoding: "UTF-8:UTF-8",
    textmode: true,
    cr_newline: true
  ) {|file|
    # for each enabled pages
    enab.each {|pg|
      file.puts "#{pg.name} : #{pg.camera.description}"
    }
  }  # the file is closed automatically
end

EDITED: Changed File.new to File.open, used Ruby 2.0 named arguments.

Thank you for your time Dan

I will study each line from the code, like:
What mean ret[0] if ret != false?

Running the full code, i had a warning and the file created is empty.

warning: File::new() does not take block; use File::open() instead

I saw that separately pg.name works. Show me the page names and then various numbers. :grin:

Sorry, a quirk of Ruby 2.0, I changed the call to use File::open(), (see above.)

It is an example. Change the put statement to whatever you like.

Add in calls to camera attributes, like:
#{page.camera.eye.inspect}
etc. See the API on the [Sketchup::Camera][1] class.

UI::inputbox() will return false if the user cancels the window with the “Cancel” or “X” buttons.

But if they do not and click the “OK” button, the method returns an Array of the values from the inputbox window.

The example above, has only 1 text value control for the inputbox, so it will be ret[0] if the user clicked “OK”, but if the user cancelled, ret will NOT be an array, it will be a false value.

So this statement is testing the result from the UI.inputbox() method:

model_name = ret[0] if ret != false

but the statement evaluates like this:

( model_name = ret[0] ) if ( ret != false )

It is an:

assignment if condition

… otherwise, the assignment is not made, and model_name is still the default value of "untitled".
[1]: http://www.sketchup.com/intl/en/developer/docs/ourdoc/camera

Dan, i changed a little bit the code to get the position number of each page with

enab.each_with_index{|pg,i|
  puts "#{pg.name} : position #{ i+1 }"
}

now i have a sequence but not come linked to real position.

See below:

Scene 6 should be position 6 and not position 4.

@tt_su: Can we rely upon iterators returning the pages from the pages collection, in user interface order ? (The UI has a move up and move down feature.)

Or are they returned in creation order ?

Ok let’s simplify it a bit:

model.pages.each_with_index{|pg,i|
  next if pg.description[0,1] == '!'
  puts "#{pg.name} : position #{ i+1 }"
}

Full example:

model = Sketchup.active_model
if !model.pages.size == 0

  model_path = File.dirname(model.path)
  model_name = model.name
  if model_name.empty?
    model_name = "untitled"
    ret = UI.inputbox(
      ["Enter model name: "],
      [model_name],
      "Model Name"
    )
    model_name = ret[0] if ret != false
  end

  filename = File.join(
    model_path,
    "#{model_name}_camera.txt"
  )
  File.open(
    filename,
    mode: "w",
    encoding: "UTF-8:UTF-8",
    textmode: true,
    cr_newline: true
  ) {|file|
    # for each enabled pages
    model.pages.each_with_index {|pg,i|
      next if pg.description[0,1] == '!'
      file.puts "#{pg.name} : position #{ i+1 }"
    }
  }  # the file is closed automatically

else

  UI.messagebox( "Model has no scene pages!" )

end

Hm… Good question. Looking at the API implementation this is undetermined - it just iterates the collection. From a quick glance it looks like it iterates in the order it’s stored in memory. Question is, does the items get re-arranged as pages are moved…

It look as if it currently do re-arrange the internal list based on the order in the UI.

1 Like

Cool! Thanks. (The Docs should mention this.)

@anme: I realized that there is a workaround to test whether a page is used in animation or not.

See example: [Ruby] RfE: Sketchup::Page animation query / setter methods needed - #2 by DanRathbun

I guess this is not much but i really appreciate your effort Dan. Thousand thanks!

I am take baby steps to upgrade an old plugin. This code will be very useful.

1 Like