Python is a powerful, high-level, dynamic language. The version of Python used in Blender 2.67 is 3.3. If you are unfamiliar with Python, start with the  Python book . If you are familiar with older (2. x ) versions of Python,  this page  summarizes what’s new in 3. x .
:: Python 是一种强大,高级,动态的语言.在 Blender 2.67 中使用的 Python 版本是 3.3.如果你不熟悉 Python,请从 Python 书中开始.如果你熟悉 Python 的旧 (2.x) 版本,此页面总结了 3.x 中的新内容.

If you are familiar with Python scripting in older versions of Blender, be aware that 2.5 x /2.6 x  is completely different; the old  Blender  module is gone. Much of the functionality is now available through the  bpy  module, but don’t expect an exact 1:1 correspondence.
::如果您熟悉Blender的旧版本的Python脚本,请注意2.5x/2.6x完全不同;旧的Blender模块已经消失了.现在通过bpy模块提供了大部分功能,但不要期望完全 1:1对应.

First Steps In Blender Scripting
::在混音程序编写中的第一步

Open a new, default Blender document. If you haven’t customized your settings, there will be a Timeline   window along the bottom; change this to a Python Console  . Perhaps increase its height to let you see more lines at once.
::打开一个新的,默认的Blender文档. 如果您没有自定义设置,下方将会有一个时间线窗口; 将其更改为Python控制台. 也许可以增加它的高度,让您一次看到更多行.

To start with, let’s find out what objects are present in the document. At the “>>>” prompt, type
::首先,让我们找出文件中存在的对象. 在">>>"提示符中,键入"

bpy.data.objects

You should see the response
::你应该看到他们的回应.

<bpy_collection[3], BlendDataObjects>

which isn’t actually all that informative. In fact what you have here is an  iterator ; to see its contents, just do the usual Python thing and convert it to a list. Try entering this:
::实际上这不是那么有信息. 事实上,你在这里看到的是一个代器; 为了查看它的内容,只需做通常的Python,然后将其转换为一个列表. 试着输入:

list(bpy.data.objects) #or bpy.data.objects[:]

This time the response should be
::这次的回应应该是

[bpy.data.objects["Camera"], bpy.data.objects["Cube"], bpy.data.objects["Lamp"]]

which shows you how to refer to the three default objects you can see in the 3D View window.
::显示如何引用3D视图窗口中的三个默认对象.

Let’s get a reference to the Cube object for more convenient access: type
::让我们来获得一个对立方体的引用,以便更方便地访问:类型

Cube = bpy.data.objects["Cube"]

Now let’s try querying the value of one of its attributes: type
::现在让我们尝试查询它的属性之一的值:类型

Cube.delta_location

You should see the response
::你应该看到他们的回应.

Vector((0.0, 0.0, 0.0))

The  Vector  type comes from the  mathutils  module provided by Blender. But unlike  bpy , this is not automatically imported into the Python Console. So let’s bring it in for subsequent use: type
::矢量类型来自Blender提供的 mathutils模块.但与bpy不同,它不会自动导入到Python控制台.所以让我们将其带入以后使用:类型

import mathutils

OK, now let’s try changing the location of the default Cube: type
::现在让我们尝试改变默认立方体的位置:

Cube.delta_location += mathutils.Vector((1, 1, 1))

(Note the doubled parentheses:  mathutils.Vector  takes a  single  argument, which is a tuple of X, Y and Z coordinate values.)
:sad注意双括号: mathutils.Vector 仅使用一个参数,即 X,Y 和 Z 坐标值的元组.

Were you watching the 3D View when you pressed  ENTER ? You should have seen the cube jump to a different position. To make it move again, press  UPARROW  to bring back the above command, so you can execute it again with  ENTER . As soon as you do this, the cube will jump another step, like before. And that’s it—you’re scripting!
::当你按ENTER时,你是在看3D视图吗?你应该看到立方体跳到不同的位置. 要使它再次移动,按下UPARROW来恢复上述命令,这样你就可以再次使用ENTER执行它. 您一旦这样做,立方体就会跳下另一个步骤,就像以前一样. 这就是您编写的脚本!

The  bpy  Module
::模块 bpy

The contents of the  bpy  module are divided into several submodules, among which are:
::模块的内容分为几个子模块,其中包括:

  • bpy.data  — This is where you find the contents of the current document.
    ::您可以在此找到当前文档的内容.
  • bpy.types  — information about the types of the objects in  bpy.data .
    ::bpy.types 关于bpy.data中的对象类型的信息.
  • bpy.ops  —  operators  perform the actual functions of Blender; these can be attached to hotkeys, menu items and buttons. And of course they can be invoked from a script. When you write an addon script, it will typically define new operators. Every operator must have a unique name.
    ::bpy.ops 操作符执行Blender的实际功能;这些操作符可以附加到热键,菜单项和按.当然它们可以从脚本中调用.当你写一个附加脚本时,它通常会定义新的操作符.每个操作符必须有一个唯一的名称.
  • bpy.context  — contains settings like the current 3D mode, which objects are selected, and so on. Also accessible in the Console window via the global variable  C .
    ::bpy.context 包含诸如当前3D模式,选择哪些对象等设置.也可以通过全球变量C在控制台窗口中访问.
  • bpy.props  — functions for defining  properties . These allow your scripts to attach custom information to a scene, that for example the user can adjust through interface elements to control the behaviour of the scripts.
    ::定义属性. 这些允许脚本附加自定义信息到场景,例如用户可以通过界面元素调整以控制脚本的行为.

The  mathutils  Module
::模块的 mathutils

The module  mathutils  defines several important classes that are used heavily in the rest of the Blender API.
::该模块定义了几个重要的类,这些类在Blender API的其余部分中被大量使用.

  • Vector : the representation of 2D or 3D coordinates.
    ::矢量:是二维或三维坐标的表示.
  • Matrix : the most general way of representing any kind of linear transformation.
    ::矩阵:是表示任何类型的线性转换的最一般的方法.
  • Euler : a straightforward way of representing rotations as a set of  Euler angles , which are rotation angles about the X, Y and Z axes. Prone to well-known pitfalls such as  gimbal lock .
    ::欧拉:以欧拉角为单位表示旋转的简单方法,即围绕X,Y和Z轴的旋转角.易受诸如杆锁等众所周知的陷的影响.
  • Quaternion : on the face of it, a more mathematically abstruse way of representing rotations. But in fact this has many nice properties, like absence of gimbal lock, and smoother interpolation between two arbitrary rotations. The latter is particularly important in character animation.
    ::四边形:表面上,它是一种比较复杂的数学方式来表示旋转.但实际上它具有许多不错的特性,如缺少关锁,以及两个任意旋转之间的更顺的插入.后者在角色动画中尤为重要.
  • Color : a representation of RGB colours and conversion to/from HSV space (no alpha channel).
    ::颜色:RGB颜色的表示和转换到/从HSV空间 (没有α频道).

See Also
::另见

  • Scripting & Extending Blender  Advanced
    ::编写脚本和扩展混合器高级
  • Blender 2.67.0 r56533 API Reference
    ::混合器 2.67.0 r56533 美国食品药品管理局参考
  • Blender site Python scripts  catalog
    ::混合器网站 Python 脚本目录

Last modified: Monday, 17 March 2025, 2:37 PM