simple grease pencil tree generator

import bpy, math
from random import uniform
## prepare scene to use grease pencil ##

## By default blender does not start with grease pencil ready for using .
## The following is just a hack. So you can mostly ignore it.

# Create grease pencil data if none exists
if not bpy.context.scene.grease_pencil:
    a = [ a for a in bpy.context.screen.areas if a.type == 'VIEW_3D' ][0] 
    override = {
        'scene'         : bpy.context.scene,
        'screen'        : bpy.context.screen,
        'object'        : bpy.context.object,
        'area'          : a,
        'region'        : a.regions[0],
        'window'        : bpy.context.window,
        'active_object' : bpy.context.object
    }
    bpy.ops.gpencil.data_add( override )
## End of grease pencil hack for set up,

Palette = bpy.context.scene.grease_pencil.palettes.new(name='my-palette')
gmat = Palette.colors.new()
gmat.name = 'green'
gmat.alpha = 1
gmat.fill_alpha = 0.1
gmat.fill_color = [0,1,0]
gmat.color = [0,0,0]

gmat = Palette.colors.new()
gmat.name = 'brown'
gmat.alpha = 1
gmat.fill_alpha = 1
gmat.fill_color = [0.3,0.3,0]
gmat.color = [0,0,0]


## Each layer contans one or more of frames Below we only use a single layer and a single frame
layer = bpy.context.scene.grease_pencil.layers.new('mylayer')
frame = layer.frames.new(1)  ## 1 is the first frame
def make_tree(x, randomness = 0):

 ## Each frame has an unlimited number of brush strokes,
 ## calling frame.strokes.new() will create and return a new stroke,
 ## which you can assign to a variable, like below the variable `a`
 a = frame.strokes.new()
 a.colorname = 'green'

 ## a brush stroke defaults to screen space, so here we force it to be in 3D space.
 a.draw_mode = '3DSPACE'
 ## This is required because it draws a line from the last point back to the first point
 a.draw_cyclic = True

 ## set a bigger line width, because it defaults to 1 pixel
 a.line_width = 3

 a.points.add( count = 3)

 ## define new variable `radius` which is the radius of the square.
 radius = 2 +uniform(0,randomness)
 o = radius*2
 ## these are the points of the square...the [] of 0=first, 1=second, 2=third, and  3=forth .
 ## here is the radius of your square -radius on both of the x, y
 a.points[0].co.x = -radius +x
 a.points[0].co.z = -radius +o 
 ## this is the second point 
 a.points[1].co.x = radius +x
 a.points[1].co.z = -radius +o 
 ## this is the third point
 a.points[2].co.x = radius +x
 a.points[2].co.z = radius +o +uniform(-radius/2,radius/2)

 ## which you can assign to a variable, like below the variable `a`
 a = frame.strokes.new()
 a.colorname = 'brown'

 ## a brush stroke defaults to screen space, so here we force it to be in 3D space.
 a.draw_mode = '3DSPACE'
 ## This is required because it draws a line from the last point back to the first point
 a.draw_cyclic = True

 ## set a bigger line width, because it defaults to 1 pixel
 a.line_width = 3

 a.points.add( count = 4 )

 ## define new variable `radius` which is the radius of the square.
 radius = 2+uniform(0,randomness)
 ## these are the points of the square...the [] of 0=first, 1=second, 2=third, and  3=forth .
 ## here is the radius of your square -radius on both of the x, y
 a.points[0].co.x = -radius *0.25+x
 a.points[0].co.z = -radius
 ## this is the second point 
 a.points[1].co.x = radius* 0.25+x
 a.points[1].co.z = -radius
 ## this is the third point
 a.points[2].co.x = radius *0.125+x
 a.points[2].co.z = radius
 ##this is the forth point
 a.points[3].co.x = -radius *0.125 +x
 a.points[3].co.z = radius

for i in range (10):
 make_tree (i*uniform(1,10),randomness=5)

Comments

Popular Posts