Setting the watermark with Ruby

I’m using a watermark as a method of display for an external rendering tool. (export collada model, render, import resulting file as a watermark background) I’m trying to automate the process via an extension but I can’t figure out how to set the watermark.

> s = Sketchup.active_model.styles.first
> s.attribute_dictionaries.nil?
true

I don’t see any useful methods on s, just name and description. I figured this might be set via attributes but it appears there are none on the object. What am I doing wrong?

Make the Style you want - including its watermark.
Save it externally as a .style file.
Ship that file with your extension - in its subfolder.
Now use the method to load that style and set it current…
Class: Sketchup::Styles — SketchUp Ruby API Documentation

filename = full_path_to_MyStyle.style
styles = Sketchup.active_model.styles
status = styles.add_style(filename, true)

Setting the second argument to true means it becomes the active style.
It is straightforward to set the path to the style file - _FILE_ returns the loading RB file’s path, so from that you can get the subfolder+style as a string and pass it to the method…
.

… and, Sketchup::active_model.rendering_options["DisplayWatermarks"]
must be set true (which is a style setting.)

Or, … it can be toggled on (or off) via code:

Sketchup::active_model.rendering_options["DisplayWatermarks"]= true

There is no way to import watermark images except by a Style, and styles are not fully exposed to the API (yet.)

The .style filetype is a Zip archive with embedded XML files and subfolder with resources (ie, images, texture, etc.) The XML schemas for .style (and .skm, etc.) are not published.

That doesn’t help with my problem…the watermark image is dynamic and is updated by an external program I’m running from an extension. I then want to load the resulting image as a watermark.

I manually load a watermark then save the style and unzip it. None of the files (an icon png and some xml) seem to reference the watermark anywhere.

I just did the same, and I have no problem seeing that the watermark image is in the "ref" subfolder (within the archive,) and that the "document.xml" file contains a reference to this file (as well as the path from where it was originally loaded from,) and all of the watermark properties for the model.

Did you update the style after applying the watermark, and before saving it ?

I just tried too, and there isn’t a ref subfolder. Not even an invisible one. And no mention of a png in any of the three XML files.

I figured out the problem. Adding the watermark makes it show up in the model but doesn’t actually store it in the style. There’s a button up near the style name/description to refresh the style with the changes (instead of just a preview I guess). After doing that, I save the style, unzip the .style file and document.xml has the expected entry with the image in res.

I then change the zip to remove the res file and put the .style in my extension. I have some code to do the following:

def import_result
    sname = File.dirname(__FILE__)+"/MyStyle.style"
     `cp "#{sname}" "#{Sketchup.temp_dir}"`
     zname = Sketchup.temp_dir+"/MyStyle.style"
     zf = Zip::File.open(zname)
     zf.add("ref/output.png", @result_path.path)
     zf.close
     Sketchup.active_model.styles.add_style(zname, true)
     Sketchup.active_model.styles.purge_unused
end

Attached is the style I used. (modified from the 3D printing default)
MyStyle.style (4.2 KB)

1 Like

Yup, as I asked:

The action in SketchUp is called “updating the style” with the changes. You have the option of abandoning style changes, so that you can play around until you get what you want. Whenever you have made changes, you’ll see the little preview icon has a “needs updating” icon superimposed over it. But you can ignore that if you just tried something and did not care for the results.

Glad you got the workaround working.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.