Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, July 9, 2013

Blender: Find the longest edge in a mesh

I had to write this simple script for my thesis on acoustic simulations on a 3D model of a human head (HRTF stuff).

So this script iterates over all the selected edges of the current object and show the longest one's length (you have to look at the console to view the message).
It also writes the maximum frequency that can be calculated using the BEM according to a paper by Brian Katz:
$$f_{max} = \frac{c}{6 \times  edge_{max}}$$


import bpy

sound_speed = 343.0

obj = bpy.context.active_object
edges = obj.data.edges
vertices = obj.data.vertices
matrix = obj.matrix_world.copy()

mm = 0.0
for ee in edges:
    vv = ee.vertices
    v1 = matrix * vertices[ee.vertices[0]].co
    v2 = matrix * vertices[ee.vertices[1]].co
    mm = max((v1-v2).length, mm)

max_freq = sound_speed / (6 * mm)

print("Longest edge's length: %f"%mm)
print("fmax: %f"%max_freq)

Tuesday, February 1, 2011

Merge Google Maps chunks in a single, large, image

Yesterday I was asked by my brother (an art student in Venice) to write some kind of application to merge Minecraft-Overviewer map chunks in a single image for a project he is doing for an exam (I have not a clue of what he is gonna do :D).

Minecraft-Overviewer can be used to render Minecrat's maps and make them navigable in a browser using Google Maps API, so my python script actually works on any Google Maps image chunks.

To use it, just type in a terminal:
./mergeMap.py <blahblah/ZoomN> <output.ext>
Where "ext" can be any format supported by Python's PIL library (so PNG & JPG for sure).

BIG WARNING:
With Zoom > 4 the processing time / disk space required / RAM required could easily become a problem and the OS could freeze ;)

Download: mergeMap.py
Screenshot: Zoom6.jpg Zoom5.jpg
(Sorry, no thumbinals... they are HUGE images :D something like 10K x 10K and 20K x 20K pixels)

Sunday, May 24, 2009

iPod Lyrics

Tired of manually downloading songs lyrics and copy-paste into plain-text files to view them in iPod's "Notes", I searched the web for a site that could do it for me. I only found some applications to download and use on my computer but it was not what I was looking for, so i wrote a simple python script to do the job.
After thinking a little about it I realized that I could simply write a "one page" website that sent request to my little script... so I did it and if you open you browser at http://ipodlyrics.x10hosting.com/ you'll find a beta version of it.

3mpty

Friday, March 13, 2009

Python CGI Debugging howto: Part 2

Ok, in the previous post I wrote that probably there was a better way to display errors for cgi script in python, so, here it is:

import cgitb
cgitb.enable() #This enable it all


Two lines :)

Simply put them at the beginning of a script and they do all the work. It's also possible to output errors in a log file instead of displaying them in the browser of the user (just take a look at python documentation on cgitb.enable()), a far better solution for production environment.

Python CGI Debugging howto

I found this useful when you are trying to debug a python script running on a remote server (that has a previous version of python, in my case :( ).

To show the classic interpreter error messages just do this:

import sys
sys.stderr = sys.stdout

print "Content-type: text/html\n"


In this way, we set the errors to output text on the standard output (in other words, we redirect the errors to the standard output), then we print the standard HTTP header to inform the browser that the following text will be an HTML page (even text/plain works fine) and if we get an error, it will be in some part of the page source :D.

Simple, probably not the best, but it works.