|
#--------------------------------------------------
# File rigged_mesh.py
#--------------------------------------------------
import bpy, mathutils
def createArmature(origin):
# Create armature and object
amt = bpy.data.armatures.new('MyRigData') #armature의 data block을 만든다.
rig = bpy.data.objects.new('MyRig', amt) #armature를 위한 object를 만든다.
rig.location = origin #오브젝트의 시작 위치
rig.show_x_ray = True #다른 오브젝트보다 앞에 표시된다.
amt.show_names = True #뼈의 이름을 표시한다.
# Link object to scene
scn = bpy.context.scene #class bpy.types.Scene(ID)
scn.objects.link(rig) #scene에 오브젝트를 링크시킨다.
scn.objects.active = rig #rig 오브젝트를 active로 만든다.
scn.update()
# Create bones
#next two lines by PKHG SVN 36504 W32
bpy.ops.object.editmode_toggle() #오브젝트의 editmode를 toggle한다.
# bpy.ops.object.mode_set(mode='EDIT')
#기존의 것이 동작하지 않는다면??!! bpy.ops.object.mode_set(mode='EDIT')
base = amt.edit_bones.new('Base')
base.head = (0,0,0)
base.tail = (0,0,1)
mid = amt.edit_bones.new('Mid')
mid.head = (0,0,1)
mid.tail = (0,0,2)
mid.parent = base
mid.use_connect = True
tip = amt.edit_bones.new('Tip')
tip.head = (0,0,2)
tip.tail = (0,0,3)
# Bone constraints. Armature must be in pose mode.
bpy.ops.object.mode_set(mode='POSE') #pose 모드로 전환시킨다.
# IK constraint Mid -> Tip
pMid = rig.pose.bones['Mid'] #bpy.types.object.pose.bones[‘Mid’] 뼈를 선택
cns1 = pMid.constraints.new('IK') #pMid 뼈에 IK 형식의 새로운 constraint를 추가
cns1.name = 'Ik'
cns1.target = rig
cns1.subtarget = 'Tip'
cns1.chain_count = 1
# StretchTo constraint Mid -> Tip with influence 0.5
cns2 = pMid.constraints.new('STRETCH_TO')
#pMid 뼈에 STRETCH_TO constraint를 추가
cns2.name = 'Stretchy' #constraint 이름을 입력한다.
cns2.target = rig
cns2.subtarget = 'Tip'
cns2.influence = 0.5
cns2.keep_axis = 'PLANE_X' #Keep X Axis
cns2.volume = 'VOLUME_XZX' #Maintain the object’s volume as it stretches
# Copy rotation constraints Base -> Tip
pBase = rig.pose.bones['Base'] #bpy.types.object.pose.bones[‘Base’] 뼈를 선택
cns3 = pBase.constraints.new('COPY_ROTATION') #뼈에 해당 constraint를 추가
cns3.name = 'Copy_Rotation'
cns3.target = rig
cns3.subtarget = 'Tip'
cns3.owner_space = 'WORLD'
cns3.target_space = 'WORLD'
bpy.ops.object.mode_set(mode='OBJECT') #object 모드로 전환한다.
return rig
def createMesh(origin):
# Create mesh and object
me = bpy.data.meshes.new('Mesh') #mesh를 위한 data block을 만든다.
ob = bpy.data.objects.new('MeshObject', me) #object를 만든다.
ob.location = origin #위치를 설정한다.
# Link object to scene
scn = bpy.context.scene #Scene data block
scn.objects.link(ob) #scene에 오브젝트를 연결한다.
scn.objects.active = ob #ob를 active 상태로 만든다.
scn.update()
# List of vertex coordinates
verts = [
(0.5, 0.5,0), (0.5,-0.5,0), (-0.5,-0.5,0), (-0.5,0.5,0),
(0.5,0.5,1), (0.5,-0.5,1), (-0.5,-0.5,1), (-0.5,0.5,1),
(-0.5,0.5,2), (-0.5,-0.5,2), (0.5,-0.5,2), (0.5,0.5,2),
(0.5,0.5,3), (0.5,-0.5,3), (-0.5,-0.5,3), (-0.5, 0.5,3)
]
# List of faces.
faces = [
(0, 1, 2, 3),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7),
(4, 7, 8, 11),
(7, 6, 9, 8),
(6, 5, 10, 9),
(5, 4, 11, 10),
(10, 11, 12, 13),
(9, 10, 13, 14),
(8, 9, 14, 15),
(11, 8, 15, 12),
(12, 15, 14, 13)
]
#주어진 verts, edges, faces로부터 mesh를 만든다. edges 혹은
# faces는 반드시 리스트([])여야 한다. 그렇지 않으면 문제가 발생한다.
me.from_pydata(verts, [], faces)
# 새로운 데이터를 가지는 mesh를 업데이트 한다.
me.update(calc_edges=True)
return ob
def skinMesh(ob, rig):
#(vertex, weight) 형식의 vertex groups의 리스트
vgroups = {} #파이썬 빈 사전을 정의한다.
vgroups['Base'] = [ #key는 ‘Base’, value는 리스트이다.
(0, 1.0), (1, 1.0), (2, 1.0), (3, 1.0),
(4, 0.5), (5, 0.5), (6, 0.5), (7, 0.5)]
vgroups['Mid'] = [ #key는 ‘Mid’, value는 리스트이다.
(4, 0.5), (5, 0.5), (6, 0.5), (7, 0.5),
(8, 1.0), (9, 1.0), (10, 1.0), (11, 1.0)]
vgroups['Tip'] = [(12, 1.0), (13, 1.0), (14, 1.0), (15, 1.0)]
# vertex groups를 만들고, verts와 weights를 출가한다.
# 할당된 첫 번째 인수는 list이고, 한 번에 몇 개의 vertgs를 할당할 수 있다.
for name, vgroup in vgroups.items(): #사전에서 key와 value를 하나씩 가져온다.
grp = ob.vertex_groups.new(name) #key라는 이름의 vertex group을 만든다.
for(v, w)in vgroup: #key에 해당하는 value를 하나씩 가져온다.
grp.add([v], w, 'REPLACE') #vertexgroup을 채운다. 밑에 설명을 참고.
# mesh object에 armature modifier를 추가하고, using vertex groups but
# not envelopes
mod = ob.modifiers.new('MyRigModif', 'ARMATURE')
#‘MyRigModif’라는 이름의 armature modifier를 만든다.
mod.object = rig #앞에서 만든 변형될 rig armature object를 입력한다.
mod.use_bone_envelopes = False #bone envelopes를 사용하지 않는다.
mod.use_vertex_groups = True #modifier에 vertex groups를 사용한다.
def run(origin):
rig = createArmature(origin)
ob = createMesh(origin)
skinMesh(ob, rig) #armature와 mesh를 연결한다.
# Move and rotate the tip bone in pose mode
bpy.context.scene.objects.active = rig #armature를 active로 선정한다.
bpy.ops.object.mode_set(mode='POSE') #armature를 pose 모드로 전환한다.
ptip = rig.pose.bones['Tip'] #Tip 이라는 이름의 뼈를 선택한다.
ptip.location = (0.2,-0.5,0) #위치를 설정한다.
rotMatrix = mathutils.Matrix.Rotation(0.6, 3, 'X')
ptip.rotation_quaternion = rotMatrix.to_quaternion()
if __name__ == "__main__":
run((0,0,0))
---------------------------------------------------
class bpy.types.Object(ID)
show_x_ray -다른 것들의 앞에 해당 오브젝트를 표시하도록 만든다.(unsupported for duplicator drawing)
Type : boolean, default False
bpy.context.scene
class bpy.types.Scene(ID)
objects
Type : SceneObjects, bpy_prop_collection of Object, (readonly)
class bpy.types.SceneObjects(bpy_struct)
link(object) - scene에 오브젝트를 링크시킨다, 그런 후에 scene.update() 실행
Parameters: object (Object, (never None)) – scene에 추가할 오브젝트
Returns: The newly created base
Return type: ObjectBase
active - 이 scene을 위한 active 오브젝트
Type : Object
bpy.types.object
pose -Current pose for armatures
Type : Pose, (readonly)
class bpy.types.Pose(bpy_struct) -A collection of pose channels, including settings for animating bones
bones -해당 armature를 위한 개별적인 pose bones
Type : bpy_prop_collection of PoseBone, (readonly)
class bpy.types.PoseBone(bpy_struct) -pose 모드에서 하나의 뼈를 위한 pose data를 정의하는 Channel
constraints -Constraints that act on this PoseChannel
Type : PoseBoneConstraints, bpy_prop_collection of Constraint, (readonly)
class bpy.types.PoseBoneConstraints(bpy_struct) -pose bone constraints의 모음
new(type) - 이 오브젝트를 위한 constraint을 추가한다.
•IK -Inverse Kinematics.
Returns: New constraint
Return type: Constraint
class bpy.types.KinematicConstraint(Constraint) - Inverse Kinematics
base classes — bpy_struct, Constraint
Inherited Properties
Constraint.name
name -Constraint name
Type : string, default “”, (never None)
target -Target Object
Type : Object
subtarget
Type : string, default “”, (never None)
chain_count - How many bones are included in the IK effect - 0 uses all bones
Type : int in [0, 255], default 0
class bpy.types.StretchToConstraint(Constraint) - Stretch to meet the target object
base classes — bpy_struct, Constraint
Inherited Properties
Constraint.name - Constraint name
Type : string, default “”, (never None)
Constraint.influence -Amount of influence constraint will have on the final solution
Type : float in [0, 1], default 0.0
target -Target Object
Type : Object
subtarget
Type : string, default “”, (never None)
keep_axis -Axis to maintain during stretch
•PLANE_X X, Keep X Axis.
•PLANE_Z Z, Keep Z Axis.
Type : enum in [‘PLANE_X’, ‘PLANE_Z’], default ‘PLANE_X’
volume -Maintain the object’s volume as it stretches
Type : enum in [‘VOLUME_XZX’, ‘VOLUME_X’, ‘VOLUME_Z’, ‘NO_VOLUME’],
default ‘VOLUME_XZX’
class bpy.types.CopyRotationConstraint(Constraint)
base classes — bpy_struct, Constraint
Inherited Properties
Constraint.name - Constraint 이름
Type : string, default “”, (never None)
Constraint.owner_space -owner가 평가되는 Space(공간)
•WORLD World Space, 해당 constraint 가 world 좌표 시스템과 연계해서 적용된다.
•POSE Pose Space, 해당 constraint가 Pose Space에 적용된다. 해당 object transformation은 무시된다.
•LOCAL_WITH_PARENT Local With Parent, parent transformation과 함께 해당 constraint가 local 좌표 시스템과 연계해서 적용된다. added.
•LOCAL Local Space, 해당 constraint가 오브젝트의 local 좌표 시스템과 연계해서 적용된다.
Type : enum in [‘WORLD’, ‘POSE’, ‘LOCAL_WITH_PARENT’, ‘LOCAL’],
default ‘WORLD’
Constraint.target_space -target이 평가되는 Space(공간)
•WORLD World Space, 타겟의 변형이 world 좌표 시스템과 관련해서 평가된다.
•POSE Pose Space, 타겟의 변형이 단지 Pose Space 에서만 평가된다. 해당 target armature object transformation은 무시된다.
•LOCAL_WITH_PARENT Local With Parent, 타겟 뼈의 변형이 local 좌표 시스템과 관련해서 평가된다. parent 변형이 또한 추가된다.
•LOCAL Local Space, 타겟의 변형이 local 좌표시스템과 관련해서 평가된다.
Type : enum in [‘WORLD’, ‘POSE’, ‘LOCAL_WITH_PARENT’, ‘LOCAL’],
default ‘WORLD’
target -Target Object
Type : Object
subtarget
Type : string, default “”, (never None)
lass bpy.types.Mesh(ID) 기하학적인 표면을 정의하는 Mesh datablock
from_pydata(vertices, edges, faces)
vertices/edges/faces의 리스트로부터 a mesh를 만든다. 보다 훌륭한 방법을 찾기까지는 이것을 사용할 것이다.
Parameters:
vertices (iterable object) – 실수 3개 한 벌. 각각은 (X, Y, Z)를 나타낸다. eg: [(0.0, 1.0, 0.5), ...].
edges (iterable object) – int형 한 쌍(pair), 각 pair는, 좌표를 나타내는 위의 vertices 리스트에서 색인 두 개를 포함한다. eg: [(1, 2), ...]
faces (iterable object) – faces에 대한 반복자, 각 faces는 위의 vertices 리스트에서, 3개 혹은 4개의 색인을 포함한다. eg: [(5, 6, 8, 9), (1, 2, 3), ...]
class bpy.types.Object(ID) -Object datablock defining an object in a scene
vertex_groups -object의 vertex groups
Type : VertexGroups, bpy_prop_collection of VertexGroup, (readonly)
class bpy.types.VertexGroups(bpy_struct) -Collection of vertex groups
new(name="Group") -object에 vertex group을 추가한다.
Parameters: name (string, (optional, never None)) – Vertex group 이름
Returns: 새로운 vertex group
Return type: VertexGroup
class bpy.types.VertexGroup(bpy_struct) -vertices의 그룹, armature를 변형시키거나 다른 목적을 위해서 사용된다.
add(index, weight, type) -group에 vertices를 추가한다.
Parameters: •index (int array of 1 items in [-inf, inf]) – Index List
•weight (float in [0, 1]) – Vertex weight
•type (enum in [‘REPLACE’, ‘ADD’, ‘SUBTRACT’]) –
Vertex assign mode
◦REPLACE Replace, Replace.
◦ADD Add, Add.
◦SUBTRACT Subtract, Subtract.
class bpy.types.Object(ID)
modifiers -오브젝트의 geometric data에 영향을 주는 modifiers
Type : ObjectModifiers, bpy_prop_collection of Modifier, (readonly)
class bpy.types.ObjectModifiers(bpy_struct) -object modifiers의 모음
new(name, type) -새로운 modifier를 추가한다.
Parameters: •name (string, (never None)) – New name for the modifier
•type (enum in [‘MESH_CACHE’, ‘UV_PROJECT’, ‘UV_WARP’, ‘VERTEX_WEIGHT_EDIT’, ‘VERTEX_WEIGHT_MIX’, ‘VERTEX_WEIGHT_PROXIMITY’, ‘ARRAY’, ‘BEVEL’, ‘BOOLEAN’, ‘BUILD’, ‘DECIMATE’, ‘EDGE_SPLIT’, ‘MASK’, ‘MIRROR’, ‘MULTIRES’, ‘REMESH’, ‘SCREW’, ‘SKIN’, ‘SOLIDIFY’, ‘SUBSURF’, ‘TRIANGULATE’, ‘ARMATURE’, ‘CAST’, ‘CURVE’, ‘DISPLACE’, ‘HOOK’, ‘LAPLACIANSMOOTH’, ‘LATTICE’, ‘MESH_DEFORM’, ‘SHRINKWRAP’, ‘SIMPLE_DEFORM’, ‘SMOOTH’, ‘WARP’, ‘WAVE’, ‘CLOTH’, ‘COLLISION’, ‘DYNAMIC_PAINT’, ‘EXPLODE’, ‘FLUID_SIMULATION’, ‘OCEAN’, ‘PARTICLE_INSTANCE’, ‘PARTICLE_SYSTEM’, ‘SMOKE’, ‘SOFT_BODY’, ‘SURFACE’]) –추가할 Modifier 형식
Returns: 새롭게 만들어진 modifier
Return type: Modifier
class bpy.types.ArmatureModifier(Modifier) -Armature deformation modifier
base classes — bpy_struct, Modifier
object -함께 변형시킬 Armature object
Type : Object
use_bone_envelopes -armature modifier에 bone envelopes를 묶는다.
Type : boolean, default False
use_vertex_groups - armature modifier에 vertex groups를 묶는다.
Type : boolean, default False
class mathutils.Matrix -이 오브젝트는 blender에서 Matrices에 접근할 수 있도록 해준다.
classmethod Rotation(angle, size, axis) -회전을 나타내는 matrix를 만든다.
Parameters: •angle (float) – 원하는 회전의 각도, in radians.
•size (int) – 회전 매트릭스의 크기. 2~4의 값을 갖는다.
•axis (string or Vector) – a string in [‘X’, ‘Y’, ‘Z’] or a 3D Vector Object (optional when size is 2).
Returns: A new rotation matrix.
Return type: Matrix
to_quaternion() -해당 rotation matrix에 대한 quaternion 표현을 리턴 한다.
Returns: Quaternion representation of the rotation matrix.
Return type: Quaternion
class bpy.types.PoseBone(bpy_struct) -pose 모드에서 뼈를 위한 pose data를 정의하는 채널
rotation_quaternion - Quaternions 안에서의 회전
Type : float array of 4 items in [-inf, inf], default (1.0, 0.0, 0.0, 0.0)