-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathop_add_shapekey.py
154 lines (119 loc) · 4.17 KB
/
op_add_shapekey.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
import csv
import bpy
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
from .define import *
from .op_util import *
# 選択しているキーの下に新しいキーを追加
class MIO3SK_OT_add_key_current(Operator):
bl_idname = "object.mio3_add_key_current"
bl_label = "Add Shape Key"
bl_description = "Add: Shape Key at current position"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
obj = context.active_object
return (
obj is not None
and obj.type in OBJECT_TYPES
and obj.mode == "OBJECT"
and obj.data.shape_keys is not None
)
def execute(self, context):
obj = context.active_object
base_idx = obj.active_shape_key_index
move_idx = len(obj.data.shape_keys.key_blocks)
obj.active_shape_key_index = move_idx
bpy.ops.object.shape_key_add(from_mix=False)
for _ in range(move_idx - base_idx - 1):
bpy.ops.object.shape_key_move(type="UP")
return {"FINISHED"}
# ファイルの読み込み
class MIO3SK_OT_some_file(Operator, ImportHelper):
bl_idname = "mio3sk.add_file"
bl_label = "Import"
bl_description = "Add: Import CSV"
bl_options = {"REGISTER", "UNDO"}
filename_ext = ".csv"
filter_glob: bpy.props.StringProperty(
default="*.csv",
options={"HIDDEN"},
maxlen=255,
)
@classmethod
def poll(cls, context):
obj = context.active_object
return obj is not None and obj.type in OBJECT_TYPES
def execute(self, context):
# context, self.filepath, self.use_setting
initShapeKey(context)
with open(self.filepath) as f:
reader = csv.reader(f)
for row in reader:
addNewKey(row[0], context)
return {"FINISHED"}
# プリセットの読み込み
class MIO3SK_OT_add_preset(Operator):
bl_idname = "mio3sk.add_preset"
bl_label = "Import"
bl_description = "Add: from presets"
bl_options = {"REGISTER", "UNDO"}
type: bpy.props.EnumProperty(
default="vrc_viseme",
items=[
("vrc_viseme", "VRChat Viseme", ""),
("mmd_light", "MMD Light", ""),
("perfect_sync", "Perfect Sync", ""),
],
)
@classmethod
def poll(cls, context):
obj = context.active_object
return obj is not None and obj.type in OBJECT_TYPES
def execute(self, context):
initShapeKey(context)
file = os.path.join(TEMPLATE_DIR, self.type + ".csv")
with open(file) as f:
reader = csv.reader(f)
for row in reader:
addNewKey(row[0], context)
return {"FINISHED"}
# コレクション内で使用されているキーをすべて作成
class MIO3SK_OT_fill_keys(Operator):
bl_idname = "mio3sk.fill_keys"
bl_label = "Fill shapekeys from collection"
bl_description = "Fill shapekeys from collection"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return context.active_object is not None and is_sync_collection(context.active_object)
def execute(self, context):
obj = context.active_object
prop_o = obj.mio3sksync
collection_keys = []
for cobj in [o for o in prop_o.syncs.objects if has_shapekey(o)]:
for name in cobj.data.shape_keys.key_blocks.keys():
if name not in collection_keys:
collection_keys.append(name)
for name in collection_keys:
addNewKey(name, context)
return {"FINISHED"}
def addNewKey(keyname, context):
if keyname in context.active_object.data.shape_keys.key_blocks:
return
context.active_object.shape_key_add(name=keyname, from_mix=False)
def initShapeKey(context):
if context.active_object.data.shape_keys is None:
bpy.ops.object.shape_key_add(from_mix=False)
classes = [
MIO3SK_OT_some_file,
MIO3SK_OT_add_key_current,
MIO3SK_OT_add_preset,
MIO3SK_OT_fill_keys,
]
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)