-
Notifications
You must be signed in to change notification settings - Fork 0
/
04LimbTopology.py
executable file
·169 lines (146 loc) · 5.62 KB
/
04LimbTopology.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env -S blender --factory-startup --python
# blender: 3.0
# ref: http://wiki.polycount.com/wiki/Limb_Topology
import sys, importlib
from math import radians
import bpy, bmesh
from mathutils import Matrix, Quaternion
C = bpy.context
D = bpy.data
if '.' not in sys.path:
sys.path.append('.')
import shared
importlib.reload(shared)
def add_limb_one_loop() -> bpy.types.Object:
# bmesh
bm = bmesh.new()
bm_cylinder_zcuts(bm, segments=16, radius=0.1, zcuts=(0.2, 0.2))
# mesh & object
mesh = D.meshes.new('limb_one_loop')
object = D.objects.new(mesh.name, mesh)
C.scene.collection.objects.link(object)
# vertex groups
# ref: https://docs.blender.org/api/current/bmesh.html#customdata-access
deform_layer = bm.verts.layers.deform.new()
b1_vg = object.vertex_groups.new(name='limb.001')
for vert in bm.verts[0:32]:
vert[deform_layer][b1_vg.index] = 1.0
b2_vg = object.vertex_groups.new(name='limb.002')
for vert in bm.verts[16:48]:
vert[deform_layer][b2_vg.index] = 1.0
bm.to_mesh(mesh)
bm.free()
# armature
return add_limb_armature(object, b1_vg.name, b2_vg.name)
def add_limb_two_loops() -> bpy.types.Object:
# bmesh
bm = bmesh.new()
bm_cylinder_zcuts(bm, segments=16, radius=0.1, zcuts=(0.19, 0.01, 0.2))
# mesh & object
mesh = D.meshes.new('limb_two_loops')
object = D.objects.new(mesh.name, mesh)
C.scene.collection.objects.link(object)
# vertex groups
# ref: https://docs.blender.org/api/current/bmesh.html#customdata-access
deform_layer = bm.verts.layers.deform.new()
b1_vg = object.vertex_groups.new(name='limb.001')
for vert in bm.verts[0:32]:
vert[deform_layer][b1_vg.index] = 1.0
b2_vg = object.vertex_groups.new(name='limb.002')
for vert in bm.verts[32:]:
vert[deform_layer][b2_vg.index] = 1.0
bm.to_mesh(mesh)
bm.free()
# armature
return add_limb_armature(object, b1_vg.name, b2_vg.name)
def add_limb_one_triangle() -> bpy.types.Object:
# bmesh
bm = bmesh.new()
bmesh.ops.create_circle(bm, segments=16, radius=0.1)
c1 = { 'verts': bm.verts[:], 'edges': bm.edges[:], 'faces': bm.faces[:] }
c2 = bmesh.ops.extrude_edge_only(bm, edges=c1['edges'])
c2 = shared.bm_geom_split(c2['geom'])
bmesh.ops.translate(bm, verts=c2['verts'], vec=(0, 0, 0.2))
bmesh.ops.transform(bm,
verts=c2['verts'][9:],
matrix=Matrix.Shear('YZ', 4, (0.0, -1.0))
)
c3 = bmesh.ops.extrude_edge_only(bm, edges=c2['edges'][8:])
c3 = shared.bm_geom_split(c3['geom'])
bmesh.ops.transform(bm,
verts=c3['verts'],
matrix=Matrix.Shear('YZ', 4, (0.0, 2.0))
)
c4 = bmesh.ops.extrude_edge_only(bm, edges=c2['edges'][0:8] + c3['edges'])
c4 = shared.bm_geom_split(c4['geom'])
bmesh.ops.translate(bm, verts=c4['verts'], vec=(0, 0, 0.2))
bmesh.ops.transform(bm,
verts=c4['verts'][9:],
matrix=Matrix.Shear('YZ', 4, (0.0, -1.0))
)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
# mesh & object
mesh = D.meshes.new('limb_one_triangle')
object = D.objects.new(mesh.name, mesh)
C.scene.collection.objects.link(object)
# vertex groups
# ref: https://docs.blender.org/api/current/bmesh.html#customdata-access
deform_layer = bm.verts.layers.deform.new()
b1_vg = object.vertex_groups.new(name='limb.001')
for vert in bm.verts[0:32]:
vert[deform_layer][b1_vg.index] = 1.0
b2_vg = object.vertex_groups.new(name='limb.002')
for vert in bm.verts[16:24] + bm.verts[31:]:
vert[deform_layer][b2_vg.index] = 1.0
bm.to_mesh(mesh)
bm.free()
# armature
return add_limb_armature(object, b1_vg.name, b2_vg.name)
def bm_cylinder_zcuts(bm, zcuts, **kwargs):
bmesh.ops.create_circle(bm, **kwargs)
c_old = { 'verts': bm.verts[:], 'edges': bm.edges[:], 'faces': bm.faces[:] }
for z in zcuts:
c_new = bmesh.ops.extrude_edge_only(bm, edges=c_old['edges'])
c_new = shared.bm_geom_split(c_new['geom'])
bmesh.ops.translate(bm, verts=c_new['verts'], vec=(0, 0, z))
c_old = c_new
# ref: https://www.youtube.com/watch?v=cZ3o5tjO51s
# ref: https://blender.stackexchange.com/a/51697
def add_limb_armature(object, b1_name, b2_name):
# armature
armature = D.armatures.new('armature')
armature = D.objects.new(armature.name, armature)
armature.show_in_front = True
C.scene.collection.objects.link(armature)
C.view_layer.objects.active = armature
bpy.ops.object.mode_set(mode='EDIT')
bones = armature.data.edit_bones
b1 = bones.new(b1_name)
b1.head = (0.0, 0.0, 0.0)
b1.tail = (0.0, 0.0, 0.2)
b2 = bones.new(b2_name)
b2.head = (0.0, 0.0, 0.0)
b2.tail = (0.0, 0.0, 0.4)
b2.parent = b1
b2.use_connect = True
bpy.ops.object.mode_set(mode='OBJECT')
# set armature as parent to object
object.parent = armature
modifier = object.modifiers.new(name='armature', type='ARMATURE')
modifier.object = armature
# set poses & keyframes
pose_bones = armature.pose.bones
pose_bones[b2_name].keyframe_insert(data_path='rotation_quaternion', frame=1)
pose_bones[b2_name].keyframe_insert(data_path='rotation_quaternion', frame=100)
pose_bones[b2_name].rotation_quaternion = Quaternion((0, 0, 1), radians(-90))
pose_bones[b2_name].keyframe_insert(data_path='rotation_quaternion', frame=50)
return armature
if __name__ == '__main__':
shared.delete_data()
C.scene.frame_current = 1
C.scene.frame_end = 100
limb1 = add_limb_one_loop()
limb2 = add_limb_two_loops()
limb2.location.x += 0.4
limb3 = add_limb_one_triangle()
limb3.location.x += 0.8