利用者:JiriHnidek

提供: wiki
2010年11月17日 (水) 06:15時点における84.161.142.46 (トーク)による版 (updated names of object and mesh creation methods; updated property names in foreach_set() method; changed list names (confusing))
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Jiří Hnídek

Module owner of

Simple Example of Blender2.5 Python scripts

Following example of Python code adds cube to the scene. It has several flaws, but it could be helpful to someone. This works with Blender 2.5 beta (2.55.0).

import bpy
#
#   7      6
#   +------+
# 4/|    5/|
# +------+ |
# | |    | |
# | +----|-+
# |/3    |/2
# +------+
# 0      1
#
# List of vertex coordinates
ve = []
ve.extend([0.0, 0.0, 0.0]) # 0
ve.extend([1.0, 0.0, 0.0]) # 1
ve.extend([1.0, 1.0, 0.0]) # 2
ve.extend([0.0, 1.0, 0.0]) # 3
ve.extend([0.0, 0.0, 1.0]) # 4
ve.extend([1.0, 0.0, 1.0]) # 5
ve.extend([1.0, 1.0, 1.0]) # 6
ve.extend([0.0, 1.0, 1.0]) # 7
# List of vertex indices of edges
ed = []
ed.extend([0, 1])
ed.extend([1, 0])
ed.extend([2, 3])
ed.extend([3, 0])
ed.extend([0, 4])
ed.extend([1, 5])
ed.extend([2, 6])
ed.extend([3, 7])
ed.extend([4, 5])
ed.extend([5, 6])
ed.extend([6, 7])
ed.extend([7, 4])
# List of vertex indices of faces
fa = []
fa.extend([2, 1, 0, 3])
fa.extend([0, 1, 5, 4])
fa.extend([1, 2, 6, 5])
fa.extend([2, 3, 7, 6])
fa.extend([3, 0, 4, 7])
fa.extend([4, 5, 6, 7])
# List of face smoothness values
smoothness = []
smoothness.extend([False, False, False, False, False, False])
# Create new empty mesh data block
mesh = bpy.data.meshes.new("Cube")
# Create empty geometry and topology
mesh.vertices.add(len(verts)/3)
mesh.edges.add(len(edges)/2)
mesh.faces.add(len(faces)/4)
# Add vertices
mesh.vertices.foreach_set("co", ve)
# Add edges
mesh.edges.foreach_set("vertices", ed)
# Add faces
mesh.faces.foreach_set("vertices_raw", fa)
# Make faces solid (not nesscesary, solid is default)
mesh.faces.foreach_set("use_smooth", smoothness)
# Create new object and link mesh data to it
obj = bpy.data.objects.new("Cube", mesh)
# Link object to current scene
bpy.context.scene.objects.link(obj)