FE 'tkGooie' Utilities

'3Dtools' group

3D Model File
(for polyhedra
and other shapes)

(FE = Freedom Environment)

FE 'tkGooie' interface for a
for various 'standard' shapes
allowing for translating or
stretching a 'base' model
with output to formats
such as Wavefront OBJ
or Cyberware PLY

FE Home Page > FE Downloads Page >

FE 'tkGooies' Description Page >

FE 'tkGooies' '3Dtools' Page >

This 3D Model File Generator (for polyhedra) Page

'3DmodelGenerator_polyhedra' utility

Around April 2012, I found a couple of 3D model viewer Tk scripts on the Tclers' Wiki site at wiki.tcl.tk.

I was inspired by the authors of those 2 scripts --- Mark Stucky and Gerard Sookahet --- to create some 3D model viewing utilities --- complete with fairly robust 3D model data importers --- for a variety of 3D data formats --- such as

  • Wavefront OBJ files
  • Cyberware PLY files
  • Geomview OFF files
  • STL stereolithography files.

In 2012, I was getting started on some 3D viewing projects, but to aid in that endeavor, I found that it would be nice to have a variety of test files at hand --- boxes, tetrahedrons, octahedrons, icosahedrons, spheres, cones, cylinders, tori (toruses) --- even buckyballs --- of various sizes and in various file formats.

And what better way to generate such files (with parameters such as widths, heights, depths, radii, etc.) than with Tcl.

And what better way to make an easy-and-quick-and-consolidated-and-self-documenting way of running the code that generates those files than Tk.



Some of the goals that I had in mind for the GUI included:

  • A selector listbox containing names for the polyhedra models to be created.

  • A frame in which widgets are placed (mostly label and entry widgets), according to prompt for parameters with which to make (or transform) the selected model type.

  • A 'WriteModelFile' button.

  • Some radiobuttons by which to choose the type of model file to write (OBJ, STL, etc.)


Below are a couple of screenshots that sum up in a few inches what the resulting GUI looks like.

The 2 images indicate the current extent of the capabilities --- and some capabilities (model-types indicated by a hash-sign at the beginning of the line in the listbox) that may be implemented in the future.

The first image shows that I am dealing with a 'BOX-6-QUADS' model-type.

And the first image shows the parameter prompts for that model-type --- parameters: width,height,depth.

The image also indicates the (ASCII) 3D model file types that I may use in the future:

  • OBJ format
  • PLY format
  • OFF format, and, perhaps,
  • STL format.

The 2nd image indicates that I have clicked on the 'WriteFile' button and the label at the upper right of the GUI indicates that an OBJ file was written --- and the number vertex (point) and face (polygon) records written.

    (A text editor popped up showing the output file --- which is what this utility does, to allow the user to scan and, optionally, edit the output file.

    I dismissed that editor window and took the screenshot.)

The listbox on the left of these images indicates that, so far, I have implemented at least one 'writer' --- usually OBJ format --- for several model-types:

  • BOX-12-TRIAS
  • CONE-N-TRIAS (an entry field prompts for N)
  • CYL-N-QUADS (an entry field prompts for N)
  • OCTA-8-TRIAS (octahedron)


I provide the code for this 3D model generator GUI below.

I follow my usual 'canonical' structure for Tk code.

So for this Tk script:

  0) Set general window & widget parms (win-name, win-position,
     win-color-scheme, fonts, widget-geometry-parms, win-size-control,

  1a) Define ALL frames (and sub-frames, if any).
  1b) Pack   ALL frames and sub-frames.

  2) Define & pack all widgets in the frames, frame by frame.

  3) Define keyboard and mouse/touchpad/touch-sensitive-screen action
     BINDINGS, if needed.

  4) Define PROCS, if needed.

  5) Additional GUI initialization (typically with one or more of
     the procs), if needed.

This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.

This makes it easy for me to find code sections --- while generating and testing this script, and when looking for code snippets to include in other scripts (code re-use).

One new thing that I have started doing in 2013 is using a text-array for text in labels, buttons, and other widgets in the GUI.

This can make it easier for people to internationalize my scripts.

I will be using a text-array like this in most of my scripts in the future.

Experimenting with the GUI

As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main 'pack' parameters --- '-side', '-anchor', '-fill', '-expand' --- on all of the 'pack' commands for the frames and widgets.

That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.

I think I have found a nice combination of the pack parameters.

The listbox and its scrollbars expand/contract when the window is resized --- while the label and button widgets stay fixed in size and relative location.

You can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want.


Additional experimentation:

You might want to change the fonts used for the various GUI widgets. For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'. Or change font families.

In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.

I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding. And I have included the '-relief' parameter on the definitions of frames and widgets.

Feel free to experiment with those 'appearance' parameters as well.

Some features of the code

That said, here's the code --- with plenty of comments to describe what most of the code-sections are doing.

One of the rather unusual features of this script is the fact that the 'parameters' frame is changed according to the 'model-type' selected.

For example:

  • When 'BOX-6-QUADS' is selected, the 'parameters' frame prompts for the 3 parameters width, height, and depth.

  • When 'CONE-N-TRIAS' is selected, the 'parameters' frame prompts for 3 different parameters --- 'radius' and 'height' and 'N' (the number of triangles to be used to make the 'side' of the cone).

You can look at the procs




to see how the pack forget command is used to change the widgets in the 'parameters' frame.

Those 'load_parameters_frame_XXX' procs also enable or disable the 'output-type' radiobuttons on the GUI to correspond to the currently available 'write' procs for any selected 'model-type'.

You can use the 'Help' button on the GUI to see information on how to add a 'model-type' to this utility.

It involves adding a 'load_parameters_frame_XXX' proc and at least one 'write_model_XXX_YYY' proc to the script.

The bottom of the 'Help' also describes the formats of OBJ, PLY, OFF, and STL files --- and the help describes which parts of the format specifications are being supported by these 'writers'.

It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this --- and perhaps have some fun with the 'pack forget' command.

Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch Earth's/Jupiter's/Uranus's Funniest Home Videos.

The Code

Here is a link to CODE for the script '3DmodelGenerator.tk'.

Setting the Text Editor

Another not-so-common feature of this Tk script --- besides the 'pack forget' technique that is used to 'dynamically' change the parameters frame in the GUI --- is that, after the model file is written, the file is displayed to the user in a GUI text editor of the user's choice.

You can see in the text of the 'HELPtext' variable, set at the bottom of the script above, that the user can edit the 'set' statement for variable 'EDITOR_text', near the bottom of the script, to set the text-editor to be the user's preferred editor.

The editor is called up from within the Tk script by using the Tcl 'exec' command.


The 'writers' are so fast that IMMEDIATELY after clicking on the 'WriteFile' button, the text-editor pops up displaying the model file.

Some Possible Enhancements

One thing that concerned me as I wrote this utility is that the script was greatly increasing in size as I added each polygon-type --- because the vertex-coordinates and the vertex-connectivity information of each model was stored in arrays in the script.

If I ever revise this script, I will probably store the model geometry for each 'shape' in a file containing 'base' or 'unit' coordinates for the vertices --- and the face connectivity information --- say, in OBJ file format.

Then, when the user selects a 'shape' from the listbox, the 'base' model file will be read in and various transformations applied (translation, magnification, rotation) --- according to parameters entered on the GUI.

So, if you find this utility of use, you may want to check back here in coming years to see if an updated version of the script is available.

Other possible enhancements/additions:

  • I may put some color-assignment capability in the code, in the future --- for the model-file types that support color of points and/or faces.

      I may eventually add widgets to the GUI so that the user can choose to add RGB colors --- either one-color applied to all vertices (or all faces) --- or various colors assigned to each vertex (or face) according to some algorithm (for example, based on z-height of the vertex points).

      Adding colors to these models may help me test out 3D model viewers when/if I add features to support reading of colors from 3D model files.


It takes several days to get model-generating code like this in shape.

But, once implemented, I am in a position to generate model files (of various sizes and shapes and numbers of polygons/ponts) at about 30 microseconds per model file, instead of more than 30 minutes per model file. :-) :-) :-) :-) :-)

Thank you, Tcl-Tk developers, for making it possible for me to make nice quality images of geometry in 3-space.

UPDATE 2013jan27

I changed the GUI rather significantly by changing the model-type selection from using a couple of rows of radiobuttons to a using a listbox on the left of the GUI.

This facilitates adding many, many more 'model-types'.

(I updated the text and images above accordingly.)

I can also add more description of each model-type, by taking advantage of the x-scroll-bar of the listbox.

The y-scroll-bar allows for an unlimited number of added 'model-types'.

I have replaced the two GUI images above with the new images, and I have replaced the code above with the new code.

You can see the 'set' statement for the 'HELPtext' variable, near the bottom of the script, for a description of how to add new 'model-types' to the listbox.

Some 'model-types' that I have not implemented yet can be seen in the listbox of the images above. They are indicated by a hash-sign (a comment-sign) at the beginning of those listbox lines.

Over the coming months/years, I may implement more of those 'model-types'.

To accomodate a large number of types of models --- and to avoid putting a lot of model geometry data in the script, I may change the script to read from a library of model files --- and apply transformations to those 'unit' models --- such as magnification, translation, and rotation of coordinates --- to create a new model file --- say, in OBJ format.

Here is an image of the sphere model that was
written out as an OBJ file,
and then read into my 3D model file viewer utility
--- and shown with random coloring
of the model facets.

Bottom of this page for a
3D Model File Generator (for polyhedra, etc.)
--- a utility in the FE 'tkGooies' system,
in the '3Dtools' group.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page history:

The first releasable version of this code was posted in 2012 Nov at http://wiki.tcl.tk/37286.

This FE web page was created 2014 May 14.
(as a backup and alternative to the wiki.tcl.tk page)

This page was changed 2019 Feb 23.
(Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)

Page was changed 2019 Jun 17.
(Specified image widths in percents to size the images according to width of the browser window. Also revised some wording in the 'Possible Enhancements' section.)

The code here MAY become more 'up-to-date' than the code posted on the Tcler's Wiki --- wiki.tcl-lang.org --- formerly wiki.tcl.tk.