Merging 2 scripts

I’ve got 2 scrips that ChatGPT helped me write, both work perfectly but now I need to merge their functionality.

Enscape renders its panos to numerically named XML files (panorama_1.xml, panorama_2.xml etc)
The jpg’s are stored as base64 images within the XML structure.
There is no differentiation between source SU models in the XML filenames BUT the filename IS stored in the XML file as ‘ProjectName’.
Enscape batch renders the selected scenes ALPHABETICALLY … not in scene order.
Saving them is a manual process through the Enscape interface which is laborious when you have 20-30 in each project that get re-rendered at least 3 times after amendments to the design.

The 1st script produces a list of the SU scenes that start with ‘P-’ and are sorted alphabetically.

The 2nd works through all the XML files in the Enscape Panorama folder, it only acts on the ones who’s ‘ProjectName’ is the same as the current SU model name and it processes them by date and time order.
It extracts the base64 image as a jpg with the filename of the original XML file.

All good so far

Now, what I want is for the date/time extracted jpg’s from the 2nd script to have the corresponding alphabetically sorted scene names from the 1st script as their file names.
This has me and ChatGPT stumped, I’ve written 1 rb script before and that was many years ago so essentially I’m a noob when it comes to this.

Would someone have a look at the attached scripts and help me weld them together?

list P scenes sorted.rb (524 Bytes)
xml to xmljpg.rb (2.2 KB)

In the case of my current project, my thinking is that I need an array/list of the 28 scenes alphabetically sorted (got that in the 1st script I think) and another array of the 28 xml’s date sorted that have the same ProjectName … and then I need to tie the 2 arrays together to be able to extract the image from the n’th XML and save it as the n’th scene name.

This is what I use the Panos for: Moira One - Type 1A

Please reassign this topic to the Ruby API subcategory.

As noted in several other topic threads, ChatGPT does not know the proper rules to write code for use in a shared execution environment like SketchUp’s embedded Ruby process.

(1) The code is not wrapped within an extension submodule inside your unique namespace module.

(2) Often ChatGPT bots choose inefficient methods or algorithms when a person would choose a better one.

For example, instead of Dir::foreach and afterward checking that it’s a XML file, …
I would use Dir::glob('*.[xX][mM][lL]') { |file_name| # code } so that only XML files were in the set.
This also reduces the nesting of if blocks.

(3) The bots often write brute force example-like code that can run one time, but is usually not set up well to run multiple times. (Ie, does not define UI commands to run it’s functionality on demand via menu or toolbar buttons.)

Also hard coded file paths where using UI::select_directory would make the code more versatile.


(a) So, it is Enscape that is creating these XML files all dumped into a single folder ?
Ie, … can panorama XML files be dumped into a subfolder unique to a model whose folder name matches the model name ?

Ie, it is incredibly inefficient to open and parse many XML files that have nothing to do with the current project.

(b) Why JPG ? It is a lossy format. PNG would be much better for web use. Does Enscape let you choose?

(c) There are some upcoming Ruby changes to libraries that may put a kink in how your extension works, if you continue to run this within SketchUp.

Ie, the REXML library will be demoted to a bundled gem at next SketchUp release and may not be distributed with SketchUp. (It likely still can be installed into SketchUp’s Ruby via a Gem.install('rexml') call.)

Also, Base64 is scheduled to be demoted to a bundled gem for Ruby 3.3. (This may not present an issue for several years as SketchUp usually stays on the Ruby major version for 2 to 4 major yearly releases.)

The “jury is still out” with regard to bundled gems being distributed with SketchUp. They’ve said that they would prefer not to include the bundled gem set. However, a standalone system Ruby install does include the set of bundled gems.

You could circumvent these challenges by running your script within a standalone system Ruby install, instead of within SketchUp. (Ie, this is just a file handling routine and does not really need SketchUp and it’s API except to output the alpha list of scene names to a text file. The image extraction can be done with pure Ruby outside of SketchUp.)

1 Like

(A) yea, this is the way Enscape chose to do it, we can’t even choose an alternative folder.

(B) the panos will be used in 3DVista (pano tour software), it’ll slice and dice them up for web delivery and even prior to that I’ll be batch processing them through Topaz Gigapixel to double their size (far far quicker than rendering at 16k x 8k) so 100% quality jpg’s are fine.

(C) ironically I think I found the REMXL refrence from an older post of yours ;).

I have an alternative methodology, I’ve got a version of the 1st script that writes the scene list out to a json file. I can easily enough write a powershell script that will read it and the xml files to generate the reqd jpgs, it just means doing it in 2 steps, 1st in SU and then subsequently on the desktop…. id prefer it to be one action.

Okay, Paul. Try this extension. It’s wrapped up properly as a SketchUp extension.

  • It should use the scene names for the image files.
  • It populates an Extensions > Enscape Panorama Writer submenu.

(…See updated version below…)

Make sure you’ve removed any conflicting old rb files from the “Plugins” folder.

Install it from the SketchUp Extension Manager’s blue “Install Extension” button.
Afterward, it will appear in the list of installed extensions and can be switched on | off as desired.

P.S. - I tested that it loads and the menu items seem to work, but not the actual extraction method.

1 Like

Wow! I wasn’t expecting this, I was 'hoping ’ for pointers but you’ve gone above and beyond, thanks.

I feel bad now that I’m replying with a bug.
I’m getting an error on launching SU 2022 (I’m avoiding 2023), here’s what’s popping up on launching SU:

Error Loading File C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter/pano_writer_main.rb
Error: #<SyntaxError: C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter/pano_writer_main.rb:5: syntax error, unexpected `defined?', expecting '('
...defined?(REXML) || not defined?(REXML::Document)
...                       ^~~~~~~~
C:/Program Files/SketchUp/SketchUp 2022/Tools/extensions.rb:197:in `require'
C:/Program Files/SketchUp/SketchUp 2022/Tools/extensions.rb:197:in `load'
C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter.rb:48:in `register_extension'
C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter.rb:48:in `<module:EnscapePanoramaWriter>'
C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter.rb:32:in `<module:PaulRussam>'
C:/Users/paul/AppData/Roaming/SketchUp/SketchUp 2022/SketchUp/Plugins/PaulRussam_EnscapePanoramaWriter.rb:31:in `<top (required)>'

Plus, what would be involved in making the destination folder (currently locked to TEST on the desktop) user selectable? … or have you done that already, I couldn’t run the plugin to find out.

No problem. I made that change but didn’t test it. (my bad)
Seems the not keyword is picky. I usually use the ! negation operator. This works (without error) …

require 'rexml/document' if !defined?(REXML) || !defined?(REXML::Document)
require 'base64' if !defined?(Base64)

I previously included a feature on the extension submenu menu as “Choose Output Path”.
If the path is chosen different from previous, it is saved to preferences.

I just added a similar command to set the default path (when the “Use model path” is unchecked.)

Also, the user is always given the opportunity to change the output folder at the beginning of each extract command.

Here is the latest RBZ:

PaulRussam_EnscapePanoramaWriter_2.0.2.rbz (3.7 KB)

The UI.select_directory method is acting quirky with regard to the active model path. I’m still trying to figure out whether it’s a bug in the extension code or the API method.

1 Like

I’ll test that tomorrow morning, I left my machine rendering 30 or so panos this evening so I’ll have a healthy brood to test it on.

@DanRathbun sorry for not getting back sooner, I got hit with a lump of work.

The plugin works! But….
It’s revealed a lot of flaws in my plan that basically make automating the saving impractical, my process/the plugin relies on too many perfect situation factors for it to function correctly.

If for example I render 10 panos and subsequently re-render 2 then the date/time order of the xml’s no longer matches the scene order, there are other deal breakers but any single one will throw off the saving.

This has been a great exercise and your writing of the plugin was above and beyond what I expected.

I’ll see if I can pester the Enscape devs to do something their end in regard to bulk saving panos.

Thanks again, Paul.

1 Like

I suppose, it would help if Enscape itself used the scene names as it’s pano xml filenames.

Even if the scene name(s) were in the xml’s as values like the source file name is, it’d be massively more doable.
It’d simply be a case of extracting the image from the xmls who’s project name in xxx and saving them with the scene name value as the file name, it wouldn’t even need to be an SU plugin as it could be done with a PowerShell script from the desktop.

I’ve asked but I don’t know it it’ll be acted on …. Their track record on this front it patchy.

1 Like