import bpy, math
from random import uniform, random
## 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.5
gmat.fill_color = [0.1,0.7,0.2]
gmat.color = [0,0.3,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=0, y=0, randomness = 0, top_only=False):
## 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 = 6
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
bottom = a.points[1].co.z
## this is the third point, top of tree
a.points[2].co.x = x
a.points[2].co.z = radius +o +uniform(-radius/2,radius/2)
top = a.points[2].co.z
for pnt in a.points:
pnt.co.y = y
if top_only:
return
## 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 = bottom
##this is the forth point
a.points[3].co.x = -radius *0.125 +x
a.points[3].co.z = bottom
for pnt in a.points:
pnt.co.y = y
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)
offset = 1
a.points[0].co.y = -radius /2
a.points[0].co.z = bottom
a.points[0].co.x = x
## this is the second point
a.points[1].co.y = radius/2
a.points[1].co.z = bottom
a.points[1].co.x = x
## this is the third point, top of tree
#a.points[2].co.y = radius +x
a.points[2].co.z = top
a.points[2].co.x = x
for pnt in a.points:
pnt.co.y += y
for i in range (60):
for j in range(10):
x = math.sin(i*0.1) * 100
y = math.cos(i*0.1) * 100
make_tree(
#x=x,#(i*uniform(1,3)) + x,
#y=y,#(-j*uniform(20,25)) + y,
x=(i*uniform(0.1,0.5)) + x,
y=(-j*uniform(0.2,0.25)) + y,
randomness=8
)
## this extra loop makes the tree tops look better when looking from above.
for i in range (60):
for j in range(10):
x = math.sin(i*0.1) * 100
y = math.cos(i*0.1) * 100
make_tree(
#x=x,#(i*uniform(1,3)) + x,
#y=y,#(-j*uniform(20,25)) + y,
x=(i*uniform(0.1,0.5)) + x,
y=(-j*uniform(0.2,0.25)) + y,
randomness=8,
top_only=True
)
Comments
Post a Comment