The Case of the Unwanted VRML 2 Material Definitions

Here's a little info on using your text editor to change the materials in your VRML files that have been exported with flat colors from a CAD modeler.

Background

Strata can now combine duplicate materials that include an image texture, but if the material is simply defined as a color, as is typical from a CAD modeler, Strata will (still) build a new instance of the material for each object that holds the duplicate material. This can create files that are massive, with Image Texture libraries that are unwieldy or unusable. You may literally have hundreds or thousands of unwanted, duplicated textures upon import:

Strata Library Palette

To use the following technique, you must be using Strata 3D CX 5.5 or higher, and must download and apply the VRML Patch for Strata CX 5.5 if you should happen to still be using 5.5. You'll find that in the Support area of the Strata site.

This tutorial applies to VRML 2 exports from ViaCAD. Different apps may write VRML 2 files that are formatted a little differently, so if you are exporting your VRML 2 file from a different app, you will have to check and see if this works for you as–is, or if it needs modification. Also be aware that altering your file like this can easily permanently ruin it, so always work on a copy, with the original safely filed out of harm's way.

VRML 2 and VRML 97 are very similar formats, this technique applies to both. It does not apply to VRML 1. VRML 2 or 97 are very good formats to use with Strata. VRML 1 is a last-resort choice. (A rule of thumb: avoid VRML 1 for use with Strata if at all possible.)

Grep

In your better text editors, you can choose to use grep or regular expressions to search and replace plain color materials with image-based textures. The most basic editors cannot do this (TextEdit, NotePad, WordPad, etc.)

I use TextWrangler. It's free! It's also Mac-only. You can do something similar using TextPad on Windows, but using regular expressions in TextPad is a little different, so this approach would have to be customized there. There are many good text editors, just be sure yours supports regular expressions, or grep searches.

All you have to do to enable grep searches in TextWrangler is check the box in the search dialog:

textWrangler Search box

To replace a material with a texture that will invoke Strata's new texture combining behavior, I enable grep in the TextWrangler search box, and come up with a search string that will find all materials, by using a wild card search.

Materials in VRML look like this. They start with:

material Material

Followed by the parameters for the material specified in {curly braces}.

So a complete material looks something like this:

material Material {ambientIntensity 0.5 diffuseColor 0.796078 0.298039 0.796078 specularColor 0 0 0 emissveColor 0 0 0 shininess 1 transparency 0} 

In TextWrangler, to find a text string that follows that pattern, you can search for this to find any hunk of text that finds a pattern to define a material:

material Material {(?s).+?}

Say What?

You undoubtedly recognize what I am doing when searching for material Material. I am just doing a plain old text search. However, when I search for

material Material {(?s).+?}

I tell the grep search to look for "material Material" plus some magic stuff.

The magic is the stuff I put between the curly braces. That tells the search to find anything in those curly braces following the material introduction (material Material), so I don't have to be specific as to exacty what is contained within.

The rest of the search criteria just sets up the wild card.

material Material starts the string.

material Material {(?s).+?} tells the search to find any entry with anything inside curly braces following the words material Material.

So, we want to find all material entries and replace them with one Image Texture entry:

texture ImageTexture {url ["no-such-image.jpg"] } 

Let's Kill Off All Those Unwanted Materials

So here's what your search looks like. I simply copied and pasted my search and replace terms from the blue boxes in the preceding section on this page. By default, TextWrangler searches from the point where you have set your cursor, so you may want to enable Start At Top.

Replace All replaces every occurrence in the entire file in one operation.

Search Box Ready to Go

Notes

If I actually wanted to use a specific image, I could put that image in the same folder as the VRML file, and put the real image name above where I typed

"no-such-image.jpg"

I can be more specific in my search-and-replace if I want by using a more specific search criteria, but that's beyond the scope of this tutorial. (I might want to be more specific for instance, if I had 7 materials across 500 objects, and I wanted to retain all seven materials while combining duplicates into only those 7 materials.)

All the buttons and options available in the search box are useful, especially if you are doing multiple searches or more refined searches. What I have shown here should get you started for now.

Danger, Will Robinson!

In addition to the risk of permanently altering an important file, you also risk putting something inappropriate into your VRML file that could cause Strata to crash. So! Save your work (!) and close all open files before trying to open your customized concoction in Strata. I warned you!

In particular, watch out for quotation marks. Work in plain text and don't let your text editor insert smart quotes, or you'll be a-crashin' when you open the file in Strata. Other various and sundry dangers not yet identified undoubtedly lurk as well.

Try It

Here are two small test files to play with. Practice with these before trying the technique on a larger file:

File with 11 items, all the same color

File with 4 items, all different colors