How to express a context free design grammar as an internal DSL in Python? -


[note: rereading before submitting, realized q has become bit of epic. thank indulging long explanation of reasoning behind pursuit. feel that, in position undertaking similar project, more on board if knew motivation behind question.]

i have been getting structure synth mikael hvidtfeldt christensen lately. tool generating 3d geometry (mostly) context free grammar called eisenscript. structure synth inspired context free art. context free grammars can create stunning results surprisingly simple rulesets.

my current structure synth workflow involves exporting obj file structure synth, importing blender, setting lights, materials, etcetera, rendering luxrender. unfortunately, importing these obj files brings blender grinding halt there can thousands of objects complex geometry. 'fairly' because structure synth generates basic shapes, sphere represented triangles still has many faces.

thus, generating structures directly in blender preferable current process (blender's deep support python scripting should make possible). intelligent python library use blender's instancing abilities use 1 mesh generate myriad objects, saving memory. plus blender full-featured 3d suite , ability interpret cfdg provide creative possibilities far beyond structure synth can offer.

and question how best translate eisenscript grammar python dsl. here's simple eisenscript looks like:

set maxdepth 2000 { 0.9 hue 30 } r1   rule r1 {    { x 1  rz 3 ry 5  } r1   { s 1 1 0.1 sat 0.9 } box }  rule r1 {    { x 1  rz -3 ry 5  } r1   { s 1 1 0.1 } box } 

to explain, first call r1 (line 2) randomly invoke 1 of 2 definitions of r1. each definition of r1 recursively calls r1 (randomly invoking 1 of 2 definitions) , creates box. first line kills generation after recursion has gone 2000 levels deep.

jeremy ashkenas (of coffeescript fame) implemented context free dsl in ruby using blocks. internally, works creating hash key each rule 'name', , stores blocks each definition of rule in array, randomly chosen when rule invoked.

the previous eisenscript rule definitions translate ruby dsl so:

rule :r1   r1 :x => 1, :rz => 3, :ry => 5   box :s => [1, 1, 0.1], :sat => 0.9 end  rule :r1   r1 :x => 1, :rz => -3, :ry => 5   box :s => [1, 1, 0.1] end 

i novice python user , have been doing research on python's functional programming capabilities. seems lambda limited create similar jeremy's ruby dsl, and, far can tell, lambda option anonymous functions?

how might experienced pythonista approach design?

writing parser context free grammar hard. you're better off using sort of library make things easier on yourself.

i check out pyparsing module. download comes number of examples, 1 of simple sql parser, might enlightening at, @ least first step.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -