Shoebot experiment – perlin noise..

Perlin noise is pretty cool, you can use it to generate realistic looking clouds or mountains.

Here’s a bot for shoebot I made a while back that uses perlin noise to generate some nice circles.

You’ll need shoebot and the lib “noise” installed into your environment for it to work;

# pip install noise

Then to run;

sbot -w perlin-circlescape1.bot

Here’s a video of them in action -

See below the break for the code -
Continue reading

Cairo with python Ctypes

Uploaded some experiments with python and ctypes to here:

https://gitorious.org/pycairo-ctypes/pycairo-ctypes

 

This is a really rough proof of concept that the pycairo API can be implemented with ctypes + metclasses.

 

 

So far only ImageSurface is supported on the SVG backend, along with Contexts.   The nice thing about this is that you can use a pycairo like API on pypy, where things should be faster (in theory).

 

There’s a test that can be run to show the outputs the same for pycairo and cairo ctypes.

Using PyCha with Django

A quick post on using Python Charts to generate nice SVG charts for your django website (I’ve had the code hanging around for ages – so should just post it). The code is based on the examples there, here I integrate it into Django.

To install you’ll need to do

pip install pycha

Heres the code for a simple view to output a chart directly to SVG:

# views.py

from StringIO import StringIO
from django.http import HttpResponse

import cairo

def colors(request):
    in_req = 1

    svg_buffer = StringIO()

    width, height = (500, 400)
    surface = cairo.SVGSurface(svg_buffer, width, height)

    dataSet = (
     ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))),
     ('dataSet 2', ((0, 2), (1, 4), (2, 3))),
     ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))),
    )

    options = {
       'legend': {'hide': True},
       'background': {'color': '#f0f0f0'},
    }

    #import pycha.bar
    #chart = pycha.bar.VerticalBarChart(surface, options)

    import pycha.line
    chart = pycha.line.LineChart(surface, options)
    chart.addDataset(dataSet)
    chart.render()

    del chart
    del surface

    response = ''
    response = HttpResponse(mimetype='image/svg+xml')
    svg_buffer.seek(0)
    response.write( svg_buffer.read() )
    return response

The basic idea is that – instead of the chart outputting to an svg file, the output goes to a buffer, this is triggered by calling the destructors of the chart and it’s cairo surface.  Once the data is in the buffer, it is rewound and played back to the Http Response.

 

Inline SVG

As is, this won’t work as inline svg, because outputting the XML preamble in the middle of the page will cause problems.  Below is an example that let’s you decide if you need the preamble (full SVG output), or not (SVG fragment for inclusion in a page or another SVG):

# Create your views here.

from StringIO import StringIO
from django.http import HttpResponse
from django.shortcuts import render_to_response

import cairo

XML_PREAMBLE = '<?xml version="1.0" encoding="UTF-8"?>'

def colors_chart(inline = False):
    """
    Generate colours chart

    Set inline to True to disable the XML preamble
    """
    in_req = 1

    svg_buffer = StringIO()

    width, height = (500, 400)
    surface = cairo.SVGSurface(svg_buffer, width, height)

    dataSet = (
     ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))),
     ('dataSet 2', ((0, 2), (1, 4), (2, 3))),
     ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))),
    )

    options = {
       'legend': {'hide': True},
       'background': {'color': '#f0f0f0'},
    }

    import pycha.bar
    chart = pycha.bar.VerticalBarChart(surface, options)

    #import pycha.line
    #chart = pycha.line.LineChart(surface, options)
    chart.addDataset(dataSet)
    chart.render()

    del chart
    del surface

    response = ''

    if inline:
        svg_buffer.seek(len(XML_PREAMBLE))
    else:
        svg_buffer.seek(0)
    return svg_buffer.read()

def colors_svg(request):
    """ render a pure SVG chart """
    response = HttpResponse(mimetype='image/svg+xml')
    response.write(colors_chart(inline = False))
    return response

def index(request):
    """ render a chart into the template """
    chart_svg = colors_chart(inline = True)

    return render_to_response(
        'shapes_index.html',
        { "chart" : chart_svg },
        mimetype='application/xhtml+xml')

An example project with the above code is available here:
pycha_django

 

[EDIT 25/2/2011] Fixed PyCha URL

[EDIT: 2010/10/05]

Oops, I’ve been using TCC/LE by JPSoft as my shell… the following doesn’t work in the normal command prompt

[/EDIT]

I just had a look at SET and it lets you set user/system variables:

Display, create, modify, or delete environment variables.

SET [/A /D /E /P /R file... /S /U /V /X] [name[=][value ]]
        file:  One or more files containing variable definitions
        /A(rithmetic)           /S(ystem variables)
        /D(efault variables)    /U(ser variables)
        /E(nv vars)             /V(olatile variables)
        /R(ead from file)       /X override VariableExclude
        /P(ause)

So to set save a variables MYAPP_HOME as the current directory, just do

SET /U MYAPP_HOME=%CD

Wish I’d have looked that up a few years ago, it’ll save a few seconds next time I need to setup some dev sdk :)

Access files in a Linux virtual machine from Windows

I recently found a neat way of accessing files in a linux VMWare image.   This is really useful, as theres never really a good time to break your VMWare image, this is also handy if you don’t want to run the whole machine, but just access the files inside.

There is one caveat:

  • It only works if the filesystem is ext2 (ext3 works, and ext4 probably works).

Install VMWare DiskMount Utility

Accept the EULA, download and install the VMWare DiskMount utility.

For convenience add the utilities folder to the path:

C:\Program Files\VMware\VMware DiskMount Utility

Do this through the Windows Gui, or even use my addpath utility.

At this point you can mount Windows VMWare images.

The usage is:  vmware-mount drive-letter vmdk-image.

Heres how I mount my Ubuntu image to the j: drive

[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk
[C:\vmware\Ubuntu]

No output here indicates success.

At this point everything seems fine, but a crucial piece of the puzzle is still missing; try and view the files and you still can’t:

Failing to see files in an ext2 VMWare image

The next step is to make Windows understand the ext2 filesystem, using a special driver.

Install ext2ifs

Grab ext2ifs from www.fs-driver.org and install.

If the following steps don’t work then you may need to reboot.

Thats it!

You should be now able to access files inside your VMWare image (assuming it’s ext2 and not reiserfs), remount the image and have a go:

In my case I did:

[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk
[C:\vmware\Ubuntu] dir j:

Heres the output – hooray, I can copy my work out of the image !

Viewing files inside a VMWare image with ext2fs

This is very useful, especially if you do dist-upgrade in ubuntu and can’t access the network.

Bonus tip:

Newer versions of VMWare player let you install VMWare tools from inside the GUI, this is another way to fix the kind of catestrophic problems you can cause yourself by accidentally upgrading the kernel in an image.

Using Java2Python to port a JavaCairo tutorial

I recently came across Java2Python.  As I’m interested in Cairo I thought it would be interesting to try porting one of the example tutorials from ZetCode.

I’ll run through the steps involved in porting then try and reach some conclusions at the end :) .

1. Get setup

This is easiest in Linux, I’m running Ubuntu (in vmware), and installed

antlr 2.x
python2.5.x
sun java6
pygtk
java-gnome

You can install them like this:

# sudo apt-get install antlr python2.5 sun-java6-bin libjava-gnome-java

Then install Python2Java with easy_install

# sudo easy_install-2.5 java2python

To test if it’s working run j2py -i.  It should complain there is no file:

# j2py -i
Usage: j2py [options]

j2py: error: -i option requires an argument

If you get any other errors your missing some packages.

2. Get the Java Code from the Simple Example.

Save the ‘simple.java’ example as ‘GSimple.java’

If java-gnome is running ok, compiling and running it you should see a window:

# javac GSimple.java
# java GSimple

gsimple

Now we’ll run through the code, it’s important to understand what it does before we port it…

Continue reading

The Bloggage and slacking

Just been checking out what Mr Palmer has been up to… quite a lot it seems…   if I could get further than 2 chapters in 6 months with Getting Things Done maybe I could stop procrastination and start GTD too :)

And I’ve been checking out some online comics…

My 6 monthlyish check on SayUncle comics blog always makes me want to do some drawing.

Also Quite enjoying the Beartato in space storyline in nedroid.

Router upgrade nixed xbmc

It turns out my lack of networked media consumption utopia via xbmc was all down to upgrading my router… the xbox was fixed on 10.0.0.x while everything else moved over to 192.168.1.x  …so I learned that SMB doesn’t like that at all.

Oh well, one task down… many more todo… maybe I can actually setup my kurobox in the next few days and then some really nice things might be possible.

Noodleglue – found!

For the last few days I’ve been looking for a project called NoodleGlue.  I got interested when I wanted to look into generating java wrappings for the Verse library.

If you just want to download Noodleglue skip to the end of the article.

Verse is a network protocol that lets 3d applications talk to each other in realtime, being developed by the Blender foundation.  I was wondering how difficult it would be to link the ease of use of processing with the power of Blender.

Continue reading