Basically, you must have a page selected and displayed to change any rendering options or style attributes.
If it is a style attribute you wish to change, and if the page has it’s own style object, then you must also select the page’s style in the Styles collection with styles.selected_style=, and then update the page’s style with styles.update_selected_style.
Keep in mind that many rendering options are really Style attributes and the RenderingOptions interface is just a means to access these style attributes.
We unfortunately have no direct API to change many of the scene properties. For now you typically have to update the current view and update the scene.
As Dan says the rendering options aren’t saved directly to the Scene but to the Style. This means you can activate the style, update it and switch back to the previous style without changing the active scene, but it also means changes to that Style applies to any Scenes using the same style.
Yes, I need to use different styles in different scenarios, but I cannot add a style based on the current style.
The API only allows adding a style through a style file. Sketchup::Styles#add_style-instance_method
I hope to add a style based on the user’s current style that only turns off section display and section cutting.
And I also hope to have this option when updating page.
… rename it (and perhaps change the description), …
Then copy the style attributes (as needed) from the user’s current style to the new style by doing what I said above. Then make your changes to the user’s current style, then …
Set the new style to the selected_style and call styles.update_selected_style.
Then undo your changes to the current style.
Lastly use the new style by setting a scene to use it.
Here’s an example that creates a page highlighting what the user has selected, sets the background to all white, and changes the camera to a ‘2D’ look. My intention is to use this page view in layout. Note that the style name change does not work in this example - don’t know why.
def set_page_rendering_options
model = Sketchup.active_model
styles = model.styles
status = styles.add_style(Sketchup.find_support_file("00Default Style.style", "Styles/Default Styles"), true)
my_style = styles.selected_style
my_style.name = "My Style"
pages = model.pages
my_page = pages.add "My Page"
selection = model.selection
view = model.active_view
camera = Sketchup::Camera.new
camera.perspective = false
up = camera.up
up.set!(0, 0, 1) # level
target = camera.target
target.set!(0, 0, 45) # parallel to y axis
eye = camera.eye
eye.set!(0, -1000, 45)
camera.set(eye, target, up)
view.camera = camera
view.zoom(selection)
model.rendering_options["DrawHorizon"] = false
model.rendering_options["BackgroundColor"] = "white"
styles.update_selected_style
my_page.use_style = my_style
status = my_page.update
pages.selected_page = my_page
end
Hmmm… weird. I had to do some testing to find out why.
It seems that you cannot rename a style (or perhaps a default style) whilst it is the selected style.
If you do it gets reverted back to the default name. (FYI, default names are transparently square bracketed.)
ADD: The “active style” is the current rendering options being shown. Any changes are discarded when the selected style is changed, so the selection of my_style must be made after renaming but before changing rendering options.
This seems to work:
def set_page_rendering_options
model = Sketchup.active_model
styles = model.styles
path = Sketchup.find_support_file("00Default Style.style", "Styles/Default Styles")
status = styles.add_style(path, false)
my_style = styles["[Default Style]"]
puts "my_style:#{my_style.name} #{my_style.inspect}"
my_style.name= "My Style"
puts " renamed:#{my_style.name} #{my_style.inspect}"
styles.selected_style= my_style
pages = model.pages
my_page = pages.add "My Page"
selection = model.selection
view = model.active_view
camera = Sketchup::Camera.new
camera.perspective = false
up = camera.up
up.set!(0, 0, 1) # level
target = camera.target
target.set!(0, 0, 45) # parallel to y axis
eye = camera.eye
eye.set!(0, -1000, 45)
camera.set(eye, target, up)
view.camera = camera
view.zoom(selection)
model.rendering_options["DrawHorizon"] = false
model.rendering_options["BackgroundColor"] = "white"
styles.update_selected_style
my_page.use_style = my_style
status = my_page.update
pages.selected_page = my_page
end