Blender 3D:零基础到专业级-单元3:拓展视野
Introduction
::引言
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.
::注意:Python代码被放置在文本编辑器窗口中.将3D视图窗口分成一个单独的部分可能有帮助,这样您可以同时使用按,3D视图和文本编辑器窗口.
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:
:: Python Platformer 教程系列的初始阶段主要是复制逻辑板Platformer 教程中的内容.如果你没有阅读和理解这些教程,你可能不会完全理解大部分代码的功能.本教程详细介绍:
-
An explanation of the "code sections" to be modified, rewritten, or redefined frequently throughout the series
::解释"代码部分"在整个系列中经常修改,重写或重新定义的内容. -
Creating an object linked to a pre-made mesh
::创建一个与预制网格连接的对象 -
Linking an object to a scene
::将一个对象与一个场景联系起来
and will detail by completion:
::详细说明:
-
How to move an object in response to keyboard triggers
::如何根据键盘触发来移动一个对象
Code Sections
::代码部分
We will refer to the sections of the code in this manner throughout the series:
::我们将在本系列中以这种方式引用代码的部分:
Import Section
::进口部分
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:
::代码的开头的导入命令系列,就像HTML文档中头标签的内容. Python有一个基本的命令集,自然保留在语言中,其余的命令是导入的,这样不需要时不会加载大量命令,导致不必要的内存支出. 这使得能够通过写定制命令集来扩展语言,以便将其导入到Python,这是Blender与Python的接口.
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
::基本的脚本
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
::添加播放器对象
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.
::开始创建一个新文档并删除基本立方体.创建一个子/苏珊.在编辑面板中,选择子,将链接和材料下的网格名称 (ME 改为"英雄".单击输入框旁边的 F,即使没有链接,也可以保存数据块.现在删除子对象.
The following code will create an object called "Player" in the library and link it to the Hero mesh:
::下面的代码将在库中创建一个名为"Player"的对象, 并将其链接到英雄网格:
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.
::在第一行,Blender.Object.New显然是指一个新的对象. "Mesh"变量不应该被改变为此代码的目的. 我不知道它的功能,所以我不想散发错误的信息,但我猜测它与对象类型有关. 如果对象是灯或相机类型,例如,你将无法应用一个网格. 第二个变量",Player"是你正在创建的实际对象的名称. 根据你的喜好来改变它.
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.
::在第二行,我们将player (被等同为我们的新对象) 链接到已经存在的网格"英雄",这是我们之前处理过的苏珊网格.使用这种方法,您可以预先建模您的角色的网格,但实际的角色是动态创建的.使用这个代码的复杂化,您可以使player.link (() 链接到一个变量而不是"bpy.data.meshes[]"位.该变量可以引用一个现有的网格或它可以创建一个新的.对于链接的值的含义,bpy.data.meshes是一个包含电影中的所有网格的数组.同样,bpy.data.objects包含电影中的所有对象.通过进入脚本窗口并进入Scripts-System-> Interactive Console,您可以获得这些数组的
Appending the Object to the Scene
::让一个物体成为现场
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.
::如果您在鼠标悬停在文本编辑器窗口上时按下ALT-P来测试此代码,它将在您的场景的源点创建一个名为Player的对象,其中将子"英雄"网格. 请确保您在所有其他代码之前包含本教程的导入部分,并在所有其他代码后包含必要的脚.
version 2.7:
::版本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