Blending into python update

Have updated Blending into Python cookbook, now contains many usefull scripts and examples.
Please contribute, anyone can
The tools available are mostly mine and biased to my needs.
(JMS? Scorpius? - you gotta have some example code up your sleeves :slight_smile: )
http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Cookbook

Current list of python examples.


    * 1 Image Functions
          o 1.1 Edit Image in External Program
          o 1.2 Find Images
          o 1.3 Remove Double Images
    * 2 Text Functions
          o 2.1 Paste Text in Unix
    * 3 Mesh Functions
          o 3.1 Mesh tool template
          o 3.2 Desaturate Meshes VertCol
          o 3.3 Triangulate NMesh
          o 3.4 Remove Verts Without removing entire faces
          o 3.5 GMesh AutoSmooth Mesh
          o 3.6 ScanFill
          o 3.7 Expanded Scanfill function
          o 3.8 Copy NMesh Face
          o 3.9 Returns the Faces centre as a Vector
          o 3.10 Flip Faces Up
          o 3.11 Find a material in all Meshes in all Scenes
    * 4 Math Functions
          o 4.1 Apply Matrix
          o 4.2 2D Line Intersection
    * 5 Blender Object Scripts
          o 5.1 Linked Duplicate Object
          o 5.2 Select Double Objects
    * 6 Blender Windowing and User Interface Scripts
          o 6.1 Auto Buttons
          o 6.2 Popup Menu Wrapper
    * 7 General Python
          o 7.1 Binary Conversion (Without Struct)
          o 7.2 Randomize List
          o 7.3 Remove Doubles in List
          o 7.4 Remove Doubles in List (Hash)
    * 8 Colour Scripts
          o 8.1 RGB to HSV
          o 8.2 HSV to RGB
    * 9 Interactive Tools
          o 9.1 Freehand Polyline Draw Tool

I read through your whole cookbook last week and thought it was very interesting. Plus, I think it was you that emailed me some optimization tips for some of my exporters a while ago, but I didn’t listen because the time saved was negligible. Now that I’m dealing with larger meshes, I’m paying more attention. :slight_smile:

I don’t have a whole lot to add, except maybe for a one liner that I picked up when writing my metropolight exporter.

-----------------------------------
Removing Duplicates From A Sequence
-----------------------------------

Use the new built-in set object instead of a dictionary and a for loop. For example, given a list of vertices, just one line will eliminate duplicates. It is just as fast as using a dictionary, but the code is a little cleaner. Note: this will remove only exact duplicates, not duplicates within a tolerance range.

verts = [
 (1, 1, 1),
 (2, 3, 4),
 (1, 1, 1),
 (4, 3, 7),
 (3, 2, 2),
 (2, 3, 4),
 (5, 9, 4),
 (1, 1, 1),
 (2, 3, 4),
 (1, 1, 1),
 (4, 3, 7),
 (3, 2, 2),
 (2, 3, 4),
 (5, 9, 4),
 (1, 1, 1),
 (2, 3, 4),
 (1, 1, 1),
 (4, 3, 7),
 (3, 2, 2),
 (2, 3, 4),
 (5, 9, 4),
 (1, 1, 1),
 (2, 3, 4),
 (1, 1, 1),
 (4, 3, 7),
 (3, 2, 2),
 (2, 3, 4),
 (5, 9, 4)]

print list(set(verts))
[(4, 3, 7), (1, 1, 1), (3, 2, 2), (2, 3, 4), (5, 9, 4)]

The set construct is a new thing in Python 2.4 and quite nifty, imho.

Please note that for distributed scripts,we are trying to stay Python 2.3 compatible since some weenie platforms are still stuck at 2.3

Dicts are good fpr removing doubles too. though its annoying haviung to use them if you only assign None to each key.