EJEMPLO

= EJEMPLO = Este es un ejemplo hecho en blender: http://www.youtube.com/watch?v=2ESA7SnvW9E

Este es un ejemplo de un script el cual esta hecho para poder importar el modulo blender.

=

 * 1) README

=

 * 1) this is a simple L-system that uses pre-made mesh elemental blocks
 * 2) source blocks are in a different layer - press tilde (`) in 3d view
 * 3) to see them all
 * 4) a new mesh is created every time; edge creases are carried over from
 * 5) source meshes to allow subsurf work
 * 6) this system uses the turtle concept with pushable/poppable state,
 * 7) just like most other L-systems
 * 8) however, the "drawing" operation here is to place a pre-made mesh
 * 9) instead the usual plant-oriented stuff
 * 10) as a result, this should be pretty well suitable for Discombobulator
 * 11) style work such as generating lots of small detail
 * 12) turtle instructions and any other tokens are XML-like tags
 * 13) instead of single characters; they can also contain an arbitrary
 * 14) argument string
 * 15) rewriting rules are actually defined as functions to allow
 * 16) procedural flexibility
 * 17) things left to do really depend on what the actual use is
 * 18) some ideas include using lists instead of strings for arbitrary
 * 19) argument data (may help with speed); supplying environment
 * 20) info to the rule function (for queries, like in some advanced
 * 21) L-systems out there) as well as the current model info
 * 22) for true Discombobulator-style detail generation there has
 * 23) to be a mode to populate the axiom (initial state) with
 * 24) existing model faces
 * 25) of course, more code cleanup is in order
 * 1) info to the rule function (for queries, like in some advanced
 * 2) L-systems out there) as well as the current model info
 * 3) for true Discombobulator-style detail generation there has
 * 4) to be a mode to populate the axiom (initial state) with
 * 5) existing model faces
 * 6) of course, more code cleanup is in order
 * 1) of course, more code cleanup is in order

import sys import re from random import random

import Blender from Blender import Mathutils from Blender import NMesh

substrate = ''#''

custom = [ \ ('', ''), \ ('', '') \ ]
 * 1) custom characters and their translations

print "growing..."

if sys.modules.has_key('ruleset'): del sys.modules['ruleset'] import ruleset
 * 1) force recompile of ruleset

rules = [ (re.compile('<' + name + '(?::([^>]+))?>'), ruleset.__dict__[name]) for name in dir(ruleset) ]

for stage in range(20):
 * 1) do the matching

curPos = 0

while True: newPos, newEnd = None, None newStr = None for regex, func in rules: match = regex.search(substrate, curPos) if match != None: if newPos == None or newPos > match.start: newPos = match.start newEnd = match.end newStr = func(match.group(1))

if newPos == None: break

substrate = substrate[:newPos] + newStr + substrate[newEnd:] curPos = newPos + len(newStr)

for char, repl in custom: substrate = re.sub(char, repl, substrate)
 * 1) translate custom chars

print "interpreting..."

class StackFrame:
 * 1) interpret the result

def __init__(self, prev=None): self.prev = prev if prev != None: self.matrix = prev.matrix else: self.matrix = Mathutils.Matrix self.matrix.identity self.matrix.resize4x4
 * 1) prev is the parent frame

def rotate(self, axis, angle): rot = Mathutils.RotationMatrix(angle, 4, axis) self.matrix = rot * self.matrix
 * 1) modifications

def offset(self, xyz): tra = Mathutils.TranslationMatrix(Mathutils.Vector(xyz)) self.matrix = tra * self.matrix

def scale(self, ratio): sca = Mathutils.Matrix( \ [ ratio, 0, 0, 0 ], \ [ 0, ratio, 0, 0 ], \ [ 0, 0, ratio, 0 ], \ [ 0, 0, 0, 1 ]) self.matrix = sca * self.matrix


 * 1) raises exception if no parent

def getPrev(self): if self.prev == None: raise Exception, "no parent frames" return self.prev

def addMesh(self, meshObj, dest): mesh = meshObj.getData
 * 1) appends current mesh to given one
 * 2) does NOT change position

vertBase = len(dest.verts) for v in mesh.verts: vec = Mathutils.Vector(v.co[0], v.co[1], v.co[2], 1) vec = vec*self.matrix cv = NMesh.Vert(vec.x, vec.y, vec.z) dest.verts.append(cv)

for f in mesh.faces: cf = NMesh.Face([ dest.verts[v.index + vertBase] for v in f.v ]) dest.addFace(cf)

if mesh.edges != None: for e in mesh.edges: v1, v2 = e.v1, e.v2 nv1, nv2 = dest.verts[v1.index + vertBase], dest.verts[v2.index + vertBase] ne = dest.addEdge(nv1, nv2) ne.crease = e.crease
 * 1) dest.update

mesh = NMesh.New
 * 1) create result mesh
 * 1) mesh.addEdgesData # for things like creases

frame = StackFrame
 * 1) do the dew

def number(str): choices = str.split(':') choice = choices[int(random * len(choices))] limits = choice.split('_', 2) if len(limits) == 2: return random * (float(limits[1]) - float(limits[0])) + float(limits[0]) return float(limits[0])
 * 1) parses and interprets a string of type
 * X:Y_Z (randomly choose either X or something between Y and Z)

instrMatch = re.compile(r""" < (?P \w+ ) (?: (?P [^>]+ ) )? > (?P [\[\]] ) """, re.VERBOSE) curPos = 0 while True: match = instrMatch.search(substrate, curPos) if match == None: break

curPos = match.end

instr = match.group('instr1') if instr == None: instr = match.group('instr2')
 * 1) collect instruction

arg = match.group('arg')

if instr == "[": frame = StackFrame(frame) elif instr == "]": frame = frame.getPrev elif instr == "offset": frame.offset([ number(a) for a in arg.split(',', 3) ]) elif instr == "turn": frame.rotate("z", number(arg)) elif instr == "pitch": frame.rotate("y", number(arg)) elif instr == "twist": frame.rotate("x", number(arg)) elif instr == "scale": frame.scale(number(arg)) elif instr == "add": frame.addMesh(Blender.Object.Get(arg), mesh)
 * 1) do the dew

NMesh.PutRaw(mesh, "Result", 1, 1)

HOME