「Dev:Py/Scripts/Cookbook/Code snippets/Meshes」の版間の差分
< Dev:Py | Scripts | Cookbook | Code snippets
細 (moved Dev:2.5/Py/Scripts/Cookbook/Code snippets/Meshes to Dev:Py/Scripts/Cookbook/Code snippets/Meshes: remove version namespace) |
細 (1版 をインポートしました) |
(相違点なし)
|
2018年6月29日 (金) 04:43時点における最新版
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
# Link object to scene
bpy.context.scene.objects.link(ob)
# 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):
# Add UV sphere
bpy.ops.mesh.primitive_uv_sphere_add(
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:
left.add([v.index], 1.0, 'REPLACE')
elif v.co[0] < -0.001:
right.add([v.index], 1.0, 'REPLACE')
else:
left.add([v.index], 0.5, 'REPLACE')
right.add([v.index], 0.5, 'REPLACE')
# Add Basis key
bpy.ops.object.shape_key_add(None)
basis = ob.active_shape_key
# Add FrontForward key: front verts move one unit forward
# Slider from -1.0 to +2.0
bpy.ops.object.shape_key_add(None)
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:
bpy.ops.object.shape_key_add(None)
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):
# Add single chain link to the scene
bpy.ops.mesh.primitive_torus_add(
#major_radius=1,
#minor_radius=0.25,
major_segments=12,
minor_segments=8,
use_abso=True,
abso_major_rad=1,
abso_minor_rad=0.6,
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
bpy.ops.object.add(
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
# Add modifier
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
scn.objects.unlink(empty)
del(empty)
return
if __name__ == "__main__":
run((0,3,0))