Graphics Reference
In-Depth Information
Licensing Information
Typically, scripts that are intended to be made available for others' use should include licensing information. If
you aim to have your script included in an official release of Blender, the script must be licensed with a GPL-
compatible license. You can copy and paste the GPL text directly from other scripts into your own. If you choose
to use another license, make sure that it accurately reflects how you want people to treat the script. Don't assume
that people will read your mind and understand what they can and can't do with your script in the absence of li-
censing information.
The remainder of the template's content is an outline for the actual code. The rest of this chapter covers in
depth the issues of defining classes and writing your script.
Adding a Simple Mesh Object to the Scene
Before you start working with templates, it's a good idea to see how scripts run from the text editor. You can do
thisbywritingaverysimplescriptandexecutingit.Inthissection,you'llseehowtowritethesimplestpossible
script that creates a mesh object with a single vertex and places it in the scene.
While you were poking around the bpy.ops module in the previous section, you might have noticed that
a built-in mesh operator already exists to create a single vertex mesh, just like the ones to add primitives to the
scene. In actual practice, you'd probably just call this operator in your script to make a single-vertex mesh, a
process analogous to how you added the monkey to the scene in the previous section. But that would be a little
too simple. Instead, you'll create a script that will do exactly what that operator does, but you'll code the whole
thing explicitly in Python. This is much closer to what you would need to do if you wanted to create a script to
add a custom shape to the scene.
1. Afterheaderinformation,the import bpy commandisthefirstlineofcodeforanystandardBlender
Python script. Thus, the first thing you need to do is to import the bpy modules, which is done using the
import command like this:
import bpy
2. Next, you need to set a variable to represent the vertices in the mesh you want to create. Vertices in a
mesh are represented as a list of triples or vectors. In Python, a list data structure is enclosed in square
brackets. Multiple list entries are separated by commas. In this example, the mesh will have only a single
vertex,sotherewillbeonlyonetripleinthelistofvertices.Thevertexwillbeplacedattheobjectorigin,
so its coordinates will be 0, 0, and 0. To do this, create the next line of code, which looks like this:
verts = [(0, 0, 0)]
3. Whenyoucreatethemesh,themethodyouusewillalsowantlistsrepresentingtheedgesandthefaces
of the mesh. With a single vertex, there are no edges or faces, so you can assign these to be empty lists
like this:
edges = []
faces = []
4. Next, you'll create a new Python object representing the mesh datablock. I told you the word object
wasambiguous! Here, you'renotyet creating the Blender 3Dobject; you'reonlycreating anewinstance
of a Python class representing mesh data. This is done by calling the new() method (with the name of
the mesh as an argument) for bpy.data.meshes like this:
mesh = bpy.data.meshes.new(name='Single Vertex Mesh')
Search WWH ::




Custom Search