Rhys Taylor's home page
Science, Art and Data Visualisation

Code Snippets

This page contains a selection of short snippets of code which show how to do very specific tasks. I save these whenever I find out how to do something which isn't immediately obvious and I need to do it again. Some of these are very simple ! They are not all fully-self contained but intended for reference instead. If you need scripts which actually run and accomplish something, see the Scripts page. All of these are in Python, some are standalone and some for Blender.

In the past I've found that documentation for accomplishing some of these tasks is surprisingly difficult to find, which is why I archive them here. However, things have changed. For very basic tasks like these, I strongly recommend using an AI like ChatGPT. There are many things that chatbots should never be used for, but short basic tasks like these are generally something they do very well and crucially are easy to test.

Python Basics


Change directory : Change to the directory where the script is.

Blank list :  Declare a empty list (NOT a numpy array, though I imagine .asarray would work on this).

Count lines : Find the number of lines in a file.

Remove NaNs : Find and trim NaNs from a Python list, returning a new list without them.

Find NaNs : Another way to remove NaNs from a Python list, this time by finding their indices in the list.

List files : List all files with the specified extension.

Random number list : Generates a list of random numbers in a single line.

Convert file to array : Read values in a file and convert to a Python list/array (not numpy). Alternative, possibly clearer version here.

Maths time ! : Does some basic mathematical operations and times them. Useful for optimising code.

Split string : Split a string by character.

Basic file operations : Write or append to a file.

Read file : Basic ways of reading parameters from an ASCII file, including how to avoid reading in line breaks.

Parallel Python multiprocessing : Simplest possible use of the multiprocessing module, replacing a standard "for" loop with a parallelised equivalent.

Copy and run files.py : Copies files from a specified location to multiple subdirectories and runs them. Useful for generic batch processing.

Line test : Demonstrates how to use linecache to quickly read a specific line from a file (reads in the whole file though, so uses a lot of memory with large files).

Sort array by column : One-line simple demo of how to sort a 2D array by values in a specific column using argsort. I think this has to be a numpy array, not a Python list.

PIL Crop : Simple demo of cropping an image using the Python Image Library. Designed to be run inside Blender but easy to modify.

Add alpha channel : Takes a series on input images and uses their RGB information to generate and apply a transparency channel map.

Blender Basics (2.49)

Some basic tasks I used to do a lot in Blender 2.49. Obsolete I guess since nobody uses 2.49 any more. I never made an equivalent for later versions, but many of the Blender scripts on the Scripts page are written for 2.7+, accomplish the same things, and are short enough that extracting the basic operation should be straightforward.

Change to blend directory : Change to file directory of the current .blend file.

Delete vertices : Demonstrates different ways to deleting the vertices in a mesh.

GUI example : Sets up a basic graphical user interface to illustrate how buttons work and suchlike.

GUI file browser : In principle Blender can set up a GUI file browser in Python and this shows how. But note this is bugged – it's just not possible to send the resulting name/path of the file to any other variables. Bizarre but true ! Not a problem in Blender 2.79, of course.

Assign mesh materials.py : Get the mesh data from specified object and give it materials.

Get material : Get the materials of an object.

Move vertices : How to alter the coordinates of mesh vertices.

Inside or outside ? : Checks if a point is inside a closed mesh or not.

Draw modes : Set object draw mode to solid or transparent.

Extrude points : Extrudes  vertices and I think also their associated faces.

Select objects : How to, ummm, select objects.

Image texture : Creates a material with an image for the texture.

Bounding box dimensions : Get the scale-independent, bounding box size of an object.

Auto render : schedule the time for when a render starts (only on Linux).