Spawn Objects - Scripts With Code Comments
Spawn Objects - Scripts With Code Comments
Script 1
import bpy
# 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)
Finally, this line adds every new tree to the trees collection.
bpy.data.collections["trees"].objects.link(new_tree)
Script 2
import bpy
# 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)
# 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)
# 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)