Blender 3D: Noob to Pro/Python Platformer: Creation

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Introduction[edit | edit source]

Note: Python code is placed in a Text Editor window. It might be helpful to split your 3D View window into a separate part, so that you can use a buttons, 3D view, and text editor window simultaneously.

The initial stages of the Python Platformer tutorial series will mostly have to do with replicating what was done in the logic board Platformer tutorials. If you have not read and understood those tutorials, you may not understand exactly what the function of most of this code is. This tutorial details:

  1. An explanation of the "code sections" to be modified, rewritten, or redefined frequently throughout the series
  2. Creating an object linked to a pre-made mesh
  3. Linking an object to a scene

and will detail by completion:

  1. How to move an object in response to keyboard triggers

Code Sections[edit | edit source]

We will refer to the sections of the code in this manner throughout the series:

Import Section[edit | edit source]

The series of import commands at the beginning of the piece of code, like the contents of the head tag in HTML documents. Python has a basic set of commands kept naturally in the language, and the rest are imported so that a large amount of commands aren't loaded when not needed, causing unnecessary memory expenditure. This lends the advantage of being able to extend the language by writing custom sets of commands to be imported into Python, which is how Blender interfaces with Python. For this tutorial, our import section is:

import Blender
import bpy

When you import something as something else, it basically creates a variable equal to the imported module's name, so that you don't have to type it out. It is inessential, but it makes the coding go faster. You might want to run a find and replace search on your document afterwards, replacing all instances of the name you imported it as with the real module's name and deleting the part that imports the module by another name, as this can make the code run slightly faster.

Essential Footer[edit | edit source]

This is the collection of commands that you must remember to include at the end of the document, regardless of any changes made to the code, for it to work. It must be noted whether you should change the entire footer (as if a feature was removed from the main code-in-progress) or adding a command to your existing footer (if the text deals with the addition of a single feature). Every article in the main tutorial line should contain the full footer as it should look at that point in the series. For this tutorial, our essential footer is:

Blender.Redraw()

Adding the Player Object[edit | edit source]

To start, create a new document and delete the basic cube. Create a Monkey/Suzanne. In the Editing panel with the monkey selected, change the mesh name (ME:) under Link and Materials to "Hero". Click the F next to the input box to preserve the data block even when nothing links to it. Now delete the monkey object.

The following code will create an object called "Player" in the library and link it to the Hero mesh:

player = Blender.Object.New("Mesh","Player")
player.link(bpy.data.meshes["Hero"])

In the first line, Blender.Object.New obviously references a new object. The "Mesh" variable should not be changed for the purposes of this code. I don't know exactly it's function, so I don't want to give out misinformation, but I speculate that it has to do with the object type. If the object were to be a lamp or camera type, for example, you would not be able to apply a mesh to it. The second variable, "Player", is the name of the actual object you're creating. Change it to your liking.

In the second line, we link player (which was equated to our new object) to the pre-existing mesh "Hero", which is the Suzanne mesh we dealt with before. Using this method, you can model your character's mesh beforehand but have the actual character created dynamically. Using a complication of this code, you can make player.link() link to a variable and not the bit "bpy.data.meshes[]". That variable can reference an existing mesh or it can create a new one. As for the meaning of the link's value, bpy.data.meshes is an array containing all the meshes in the movie. Likewise, bpy.data.objects contains all the objects in the movie. By going to the Scripts window and going to Scripts->System->Interactive Console you can gain such information as the contents of these arrays. By entering "list(bpy.data.objects)" into the console, you will be rewarded with a list in the format of [[Object "Camera"], [Object "Cube"], [Object "Lamp"]] which is the list of objects in the standard new document set-up. So, to reference the item [Object "Cube"] you would use the line "bpy.data.objects['Cube']" and so on. For these commands to work, you must make sure to import bpy in your import section.

Appending the Object to the Scene[edit | edit source]

Like you must append the mesh to the object, you must also append the object to the scene, or it will just be a floating data block and not actually appear anywhere.

scene = Blender.Scene.GetCurrent()
scene.link(player)

If you were to test this code by pressing ALT-P while the mouse is hovering over the Text Editor window, it would create an object named Player with the monkey "Hero" mesh at the origin point of your scene. Make sure you included this tutorial's import section before all of the other code and the essential footer after all of other code.

version 2.7:

import bpy

myMesh = bpy.data.meshes["Hero"]                # reference existing mesh
player = bpy.data.objects.new("Mesh", myMesh)   # create new object

player.name = "Player"                          # give it a name

bpy.context.scene.objects.link(player)          # link to the scene to show