Are all settings in styles represented in rendering options?

The documentation says “The majority of the rendering information returned exists in the Styles dialog”.

Are the rendering options a superset of the settings in the style dialog ? (or are there settings in the styles dialog that is not accessible in rendering options)

I want to do something like;

  1. somehow store the currently active style settings
  2. change to another style and/or change style settings
  3. do some things (render some images)
  4. restore all style settings to the state they had before 2

The styles API is a bit limited, I don’t see a way using only the styles API.

But if all style settings are available as rendering options, that might be a solution.

You can get some info how to store-restore style from this topic:

How can update rendering_options of page? - Developers / Ruby API - SketchUp Community

hm, I’m thinking that

    path = Sketchup.find_support_file("00Default Style.style", "Styles/Default Styles")
    status = styles.add_style(path, false)
    ...
    styles.selected_style= my_style

Will overwrite any “unsaved” (not updated to a style) style settings, and I’ll have no way of restoring them. No ?

Some rendering options are not controlled by Styles. Fog is separately controlled and saved directly to the Scene for instance. I think the section cut line state is not even saved to the model at all and was implemented to be used internally in LayOut.

Best way to find out is to go through the list of keys and compare with the UI

There are still many errors in the API documentation. We are hoping it will eventually be fixed. (Major updates to the docs seem to come in spurts every three years or so.)

That said, the class Overview for the RenderingOptions class leave much to be desired. It does not really even “scratch the surface” of this topic.

Not really and not in the sense of a Ruby superclass or subclass.

What the API documentation does not explain well (if at all) is that the SketchUp model viewport is a distinct object separate from scene pages. It is exposed as a singleton instance of the View class for each model object.

Since the view object (what is currently rendered) is singleton, therefore the RenderingOptions is also singleton for each view object. Weirdly, the rendering options should have been exposed as an instance getter method of the view object, but was instead given a getter method from the model object. (Strange but we can work with it as is.)

Now, imagine that a model has no scenes. But all models (hopefully) at least have a style (that originally came from the template which was used to start the model.)
In this simple scenario, the model’s rendering options are those that are used to render the model view. You can change rendering options, zoom or orbit the view and the settings for the style in use do not change. We can even save the model in this state, with modified rendering options that differ from the options saved in the current style, and when we reopen the model the view is restored to how it was when saved. Ie, the current viewport is rendered with the options as set before the save rather than the options as saved in the model’s style.
I explain this simple scenario to help you understand that the model view’s rendering options are transitory, … like a snapshot of how the model is currently being rendered.
IMPORTANT: This is exposed to the API as the active_style object.

Dealing with a model having multiple scene pages that use their own style, is more complicated, but you can refer to the topic @dezmo linked above.
This should not bother you however, as you are only wanting to make temporary render changes and write out images.

So, with regard to your steps 1…4, the current viewport’s rendering options can be changed at any time without affecting the saved styles or any of the scene page’s styling as long as you do not update any of the scene pages or update any styles specifically used by any scene pages.
IMPORTANT: This includes the currently selected_style object, or any of the style objects, in the Styles inspector listing.

See Julia’s reply.

The selected style (styles.selected_style) object will already be holding all the rendering options settings (minus any un-updated changes that have been made to the active style.)

That said, the RenderingOptions class mixes in the Ruby core Enumerable module, which adds a hash conversion method #to_h:

# Make a Hash copy of the model's active_style RenderingOptions:
ropts = Sketchup.active_model.rendering_options.to_h

Change to another style using styles.selected_style= my_style (or pick one of the loaded styles using a name or index into the model’s styles collection.)

Or just make changes to the viewport’s active_style directly by changing values in the model.rendering_options.

… no problem. Use View#write_image, etc.

To reset the viewport to the selected style:

styles.selected_style= styles.selected_style if styles.active_style_changed

This works because calling the Styles#selected_style= setter method automatically discards any changes made to the active_style shown in the viewport.

WARNING: Do not call:styles.selected_style= styles.active_style
… on any version of SketchUp 24 or earlier. This will crash SketchUp.
(I am guessing internally it sets up a vicious loop scenario where SU is trying to set the active_style to itself.)


Addendum - Restoring from a hash:

viewopts = Sketchup.actvie_model.rendering_options
# Referring to the "ropts" hash created in the snippet above:
ropts.each_pair { |key, value|
  viewopts[key]= value
}
1 Like

Thanks for your thorough answers.

Some rendering options are not controlled by Styles.

That’s not a problem, I’m worried about “the opposite”, settings in the styles UI that is not represented in rendering options.

(minus any un-updated changes that have been made to the active style.)

That’s basically what prompted my question, I want to do my thing and then return the user to the exact state as before, or at least as close as possible.
(Now maybe handling of un-updated changes is overkill, if the normal usage of styles doesn’t involve working with un-updated style changes for any significant period of time, I don’t know, I don’t really use sketchup for modelling, I’m the programmer in my organization)

I’m now testing this code (basically Dan’s suggestions)

oldrenderingoptions = model.rendering_options.to_h
oldselectedstyle = model.styles.selected_style

model.styles.add_style("predefinedstyle.style",true)
# ...
# change the camera settings, make changes to the model, add text, write_image, reset camera/model/texts
# ...

model.styles.selected_style = oldselectedstyle
oldrenderingoptions.each_pair { |key, value|
  model.rendering_options[key]= value
}
model.styles.purge_unused

And it seems to work as expected when I tried it with some random un-updated style changes.

1 Like