- Creating objects from a menu as with *Add » Mesh » ...* is easy. The same goal is also achievable within a Python script. The API is important to get the necessary informations.
- Create an new Python script.
- Create a function that lists all methods available in Blender who create a mesh objects (primitive) .
- Create one instance from each object in your scene. No object should touch an other.
- Create a figure as a composition from a cone and a sphere (UV_Sphere). There are two possible solutions. Maybe you will find a third version.
Structure of the API
All available functionality is grouped by different modules. Only the first two are required in the beginning.
- Data Access (bpy.data)
- Operators (bpy.ops)
- Types (bpy.types)
- Utilities (bpy.utils)
- Path Utilities (bpy.path)
- Application Data (bpy.app)
- Property Definitions (bpy.props)
- Math Types & Utilities (mathutils)
- Font Drawing (blf)
- Audio System (aud)
Game Engine Modules
- Game Engine bge.types Module
- Game Engine bge.logic Module
- Game Engine bge.render Module
- Game Engine bge.events module
Which »primitives« are available?
If you start creating a complex world in 3D/Blender, often a good starting point is to create simple objects. Lets create the available mesh objects. But which objects are available?
First and easiest solution: have a look at the menu Add » Mesh » ... and look at the tooltip at the mouse pointer.
RTFM (read the fine manuals)
If you are familiar with the structures in Blender, the console is a good starting point to discover the API.
If your are not successful searching the API, ask the community. A linklist is available in the appendix.
|Note:||It is essential to work with the API, nobody is able to remember all possibilities!|
Finding the right method(s)
Add mesh objects with Python instead using the menu is shown in a screenshot where the console is used. This is error prone, using a script is a good alternative.
Create the objects
Now that we know how to create new objects lets create a script for this task. Where to place the new object, is controlled by the parameter location, a tuple (values in rounded braces) with tree Values for the x-, y- und z-aches.
Here is a fragment...
bpy.ops.mesh.primitive_cube_add(location=(2, 2, 0)) bpy.ops.mesh.primitive_cylinder_add(location=(-2, -2, 0))
And so the final solution might look like:
Short video about using the API
Try to recreate the following construct unsing only cubes and the menu Add » Mesh » ...! It's an abstract tree. Create only the tree top. With the next step you should be able to add a cube for the tree trunk and scale the cube as you wish.
It's taken from http://b3d101.org/en/introduction/
As you can see, there are tree cubes involved. Each cube has differs in the location and so the script could look like the following example:
#!bpy """ Name: 'simpletree.py' Blender: 2.7x Group: 'Experiment' Tooltip: 'Create an abstract tree' """ import bpy def new_cube(coord=(0,0,0)): """Places a cube at a given location """ bpy.ops.mesh.primitive_cube_add(location=coord, rotation=(0, 0, 0)) if __name__ == '__main__': # Stop edit mode if bpy.ops.object.mode_set.poll(): bpy.ops.object.mode_set(mode='OBJECT') # delete all mesh objects from a scene bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete() # call the new function new_cube(coord=(0,0,0)) new_cube(coord=(-.6,-0.6,0.5)) new_cube(coord=(0.6,0.6,0.7))