You are on page 1of 3

Spawn Objects - Scripts with Code Comments

Script 1
import bpy

# The following line assigns the tree object to a variable.


Here, we named our variable “tree”.
tree = bpy.data.objects['tree']

# This line allows us to access the active object, which is


the plane.
obj = bpy.context.active_object

# Now we can create the list of all the vertices that we just
selected. To do that we can use a list comprehension. We use
co array of the MeshVertex struct to access the local vertex
coordinate. In order to get global vertex coordinates we must
multiply it by the world transformation matrix. We named our
list “selected”.
selected = [(obj.matrix_world @ v.co) for v in
obj.data.vertices if v.select]

# Now we can loop through all the selected vertices and add a
tree at each of them. Here, we created a loop variable “vertex”
to loop through the list “selected”.
for vertex in selected:

# We need a name for each new tree. Here, we create the names
by concatenating the word 'tree' with the coordinates of the
tree. To do that, we created a variable “name” and used f’
which means formatted string literals to concatenate the
original name tree to the coordinates {vertex}.
name = f'tree {vertex}'

# Now we can create a new tree, give it the name and object
data from the original tree. We'll assign the new tree to a
variable. Here, we created a variable “new_tree” for every new
tree that will be created and the values of the “name”
variable plus the data of the original tree will be
passed/stored to the variable “new_tree”.
new_tree = bpy.data.objects.new(name=name,
object_data=tree.data)

# This line positions the tree at the coordinates of the


selected vertex.
new_tree.location = (vertex[0], vertex[1], vertex[2]) #

Finally, this line adds every new tree to the trees collection.
bpy.data.collections["trees"].objects.link(new_tree)
Script 2

import bpy

# Here, we import the randint and uniform functions from the


random module. The functions are used to randomize rotation
and scale in the code below. It’s good to only import the
functions that you are going to use instead of the entire
module.
from random import randint, uniform

# We will rotate the trees, but the rotation_euler method that


we're going to use accepts angles in radians. So we will use
the radians function from the math module in order to convert
degrees to radians. Here, we import the radians function from
the math module.
from math import radians

# The following line assigns the tree object to a variable.


Here, we named our variable “tree”.
tree = bpy.data.objects['tree']

# This line allows us to access the active object, which is


the plane.
obj = bpy.context.active_object

# Now we can create the list of all the vertices that we just
selected. To do that we can use a list comprehension. We use
co array of the MeshVertex struct to access the local vertex
coordinate. In order to get global vertex coordinates we must
multiply it by the world transformation matrix. We named our
list “selected”.
selected = [(obj.matrix_world @ v.co) for v in
obj.data.vertices if v.select]

# Now we can loop through all the selected vertices and add a
tree at each of them. Here, we created a loop variable “vertex”
to loop through the list “selected”.
for vertex in selected:

# We need a name for each new tree. Here, we create the names
by concatenating the word 'tree' with the coordinates of the
tree. To do that, we created a variable “name” and used f’
which means formatted string literals to concatenate the
original name tree to the coordinates {vertex}.
name = f'tree {vertex}'
# Now we can create a new tree, give it the name and object
data from the original tree. We'll assign the new tree to a
variable. Here, we created a variable “new_tree” for every new
tree that will be created and the values of the “name”
variable plus the data of the original tree will be
passed/stored to the variable “new_tree”.
new_tree = bpy.data.objects.new(name=name,
object_data=tree.data)

# This line positions the tree at the coordinates of the


selected vertex.
new_tree.location = (vertex[0], vertex[1], vertex[2])

# Now we need random angles for all three axes. They should be
between -10 and # 10 degrees for the X and Y axes and between
-45 and 45 for the Z axis. We used the randint function here
to generate random numbers between the specified ranges.
angleX = randint(-10, 10)
angleY = randint(-10, 10)
angleZ = randint(-45, 45)

# Now we can use the angles to rotate the tree. Remember to


use radians instead of degrees because the rotation_euler
method only accepts angles in radians. In order to convert
from degrees to radians, here we used the radians function.
new_tree.rotation_euler = (radians(angleX), radians(angleY),
radians(angleZ))

# Here, we created a variable “scale” to scale every new tree


that’s created randomly. This will give you a more natural
look. Let's pick a random scale between 0.6 and 3 for example.
scale = uniform(0.6, 3)

# In this line, we use the same scale for all three axes. This
means a random scale value between 0.6 and 3 will be assigned
to the axes.
new_tree.scale = (scale, scale, scale)

# Finally, here we add the tree to the trees collection.


bpy.data.collections["trees"].objects.link(new_tree)

You might also like