# Dev:Py/Scripts/Cookbook/Code snippets/Meshes

< Dev:Py‎ | Scripts‎ | Cookbook‎ | Code snippets
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

# Meshes

## Mesh

This program creates two meshes. The first is a solid pyramid, with both triangular and quad faces. The second is a wire triangle. The names of both meshes are displayed. The triangle is translated away so it can be seen beside the pyramid. This requires that it is selected.

```#----------------------------------------------------------
# File meshes.py
#----------------------------------------------------------
import bpy

def createMesh(name, origin, verts, edges, faces):
# Create mesh and object
me = bpy.data.meshes.new(name+'Mesh')
ob = bpy.data.objects.new(name, me)
ob.location = origin
ob.show_name = True

# Create mesh from given verts, edges, faces. Either edges or
# faces should be [], or you ask for problems
me.from_pydata(verts, edges, faces)

# Update mesh with new data
me.update(calc_edges=True)
return ob

def run(origin):
(x,y,z) = (0.707107, 0.258819, 0.965926)
verts1 = ((x,x,-1), (x,-x,-1), (-x,-x,-1), (-x,x,-1), (0,0,1))
faces1 = ((1,0,4), (4,2,1), (4,3,2), (4,0,3), (0,1,2,3))
ob1 = createMesh('Solid', origin, verts1, [], faces1)
verts2 = ((x,x,0), (y,-z,0), (-z,y,0))
edges2 = ((1,0), (1,2), (2,0))
ob2 = createMesh('Edgy', origin, verts2, edges2, [])

# Move second object out of the way
ob1.select = False
ob2.select = True
bpy.ops.transform.translate(value=(0,2,0))
return

if __name__ == "__main__":
run((0,0,0))
```

## Vertex groups and shapekeys

This program adds an UV sphere with two vertex groups (Left and Right) and four shapekeys.

```#----------------------------------------------------------
# File shapekey.py
#----------------------------------------------------------
import bpy, random

def run(origin):
segments=6, ring_count=5, size=1, location=origin)
ob = bpy.context.object
ob.name = 'ShapeKeyObject'
ob.show_name = True

# Create Left and Right vertex groups
left = ob.vertex_groups.new('Left')
right = ob.vertex_groups.new('Right')
for v in ob.data.vertices:
if v.co[0] > 0.001:
elif v.co[0] < -0.001:
else:

basis = ob.active_shape_key

# Add FrontForward key: front verts move one unit forward
# Slider from -1.0 to +2.0
frontFwd = ob.active_shape_key
frontFwd.name = 'FrontForward'
frontFwd.slider_min = -1.0
frontFwd.slider_max = 2.0
for v in [19, 20, 23, 24]:
pt = frontFwd.data[v].co
pt[1] = pt[1] - 1

# Add TopUp keys: top verts move one unit up.  TopUp_L and
# TopUp_R only affect left and right halves, respectively
keylist = [(None, ''), ('Left', '_L'), ('Right', '_R')]
for (vgrp, suffix) in keylist:
topUp = ob.active_shape_key
topUp.name = 'TopUp' + suffix
if vgrp:
topUp.vertex_group = vgrp
for v in [0, 1, 9, 10, 17, 18, 25]:
pt = topUp.data[v].co
pt[2] = pt[2] + 1

# Pose shape keys
for shape in ob.data.shape_keys.key_blocks:
shape.value = random.random()
return

if __name__ == "__main__":
# Create five object with random shapekeys
for j in range(5):
run((3*j,0,0))
```

## Applying an array modifier

This program creates a chain with ten links. A link is a primitive torus scaled along the x axis. We give the link an array modifier, where the offset is controlled by an empty. Finally the array modifier is applied, making the chain into a single mesh.

```#----------------------------------------------------------
# File chain.py
# Creates an array modifier and applies it
# Update to API rev. 36523
#----------------------------------------------------------
import bpy
import math
from math import pi

def run(origin):
major_segments=12,
minor_segments=8,
use_abso=True,
location=(0,0,0),
rotation=(0,0,0))

# Scale the torus along the x axis
ob = bpy.context.object
ob.scale = (0.7, 1, 1)
bpy.ops.object.transform_apply(scale=True)

# Create an empty
type='EMPTY',
location=(0,1.2,0.2),
rotation=(pi/2, pi/4, pi/2))
empty = bpy.context.object

# Make chain link active again
scn = bpy.context.scene
scn.objects.active = ob

mod = ob.modifiers.new('Chain', 'ARRAY')
mod.fit_type = 'FIXED_COUNT'
mod.count = 10
mod.use_relative_offset = 0
mod.use_object_offset = True
mod.offset_object = empty

# Apply the modifier
bpy.ops.object.visual_transform_apply()
bpy.ops.object.modifier_apply(apply_as='DATA', modifier='Chain')

# Move chain into place
bpy.ops.transform.translate(value=origin)

# Don't need empty anymore