Repository navigation
Expand file tree
/
Copy pathmaya_export_keyframe_blendshape_weight_values.py
More file actions
executable file
·159 lines (116 loc) · 4.35 KB
/
Copy pathmaya_export_keyframe_blendshape_weight_values.py
File metadata and controls
executable file
·159 lines (116 loc) · 4.35 KB
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
# coding=utf-8
# """
# Export .obj files for target shapes for a blendshape node in Maya.
# Author: Zhao Yafei (zhaoyafei0210@gmail.com)
# """
import os
import os.path as osp
import json
import maya.cmds as cmds
import maya.mel as mel
from pprint import pprint
def get_current_scene_name():
"""
Get current scene name.
Args:
None.
Returns:
str
Scene name.
"""
scene_name = cmds.file(query=True, sceneName=True, shortName=True)
scene_name = osp.splitext(scene_name)[0]
return scene_name
def get_all_blendshape_nodes(show_type=True):
"""
Get name list of all blendshape nodes.
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all blendshape nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
# blendshape_nodes_list = cmds.lsType("blendShape")
blendshape_nodes_list = cmds.ls(type="blendShape", showType=show_type)
if not show_type:
blendshape_nodes_list.sort()
return blendshape_nodes_list
d
def get_blendshape_keys_list(blendshape_node_name, sort_keys=False):
"""
Get name list (blendshape keys) of target-shapes/morphing-targets of blendshape.
Args:
blendshape_node_name: str
Name of blend shape deformer (blendShape Node) in Maya.
sort_keys: bool
Whether to sort the keys by name.
Returns:
list of str
Name list of target shapes of the input blendshape node.
"""
# Maya Mel cmd
# cmd = 'listAttr -k -m -st "weight" ' + blendshape_node_name
# blendshape_keys_list = mel.eval(cmd)
# Maya python cmd
blendshape_keys_list = cmds.listAttr(
blendshape_node_name, st='weight', multi=True, keyable=True)
if sort_keys:
blendshape_keys_list.sort()
return blendshape_keys_list
def get_blendshape_geometry_name(blendshape_node_name):
"""
Get the geometry name binded with the specified blendshape.
Args:
blendshape_node_name: str
Name of blend shape deformer (blendShape Node) in Maya.
Returns:
list of str
Name list of geometry of the input blendshape node.
"""
# Maya python cmd
geometry_list = cmds.blendShape(
blendshape_node_name, query=True, geometry=True)
geometry_list.sort()
return geometry_list
def export_keyframe_blendshape_weight_values(blendshape_node_name, save_dir='./',
start_frame=1, end_frame=10):
"""
Export keyframe meshes into .json files.
Args:
blendshape_node_name: str or list of str
Name of blendshape node;
save_dir: str
Directory to save .json files for weight values of a blendshape node.
Returns:
None.
"""
pprint("===> blendshape_node_name: {}".format(blendshape_node_name))
if not osp.exists(save_dir):
os.makedirs(save_dir)
scene_name = get_current_scene_name()
if isinstance(blendshape_node_name, str):
blendshape_node_name = [blendshape_node_name]
# Export keyframe blendshape weight values.
for curr_time in range(start_frame, end_frame+1):
pprint('===> Export at time: #{}'.format(curr_time))
cmds.currentTime(curr_time)
for node in blendshape_node_name:
pprint('---> blendshape: {}'.format(node))
save_filename = "{}/{}.keyframe.{}.frame_{}.json".format(save_dir, scene_name, node, curr_time)
blendshape_keys_list = get_blendshape_keys_list(node, sort_keys=False)
weight_values = cmds.getAttr(node+'.weight')[0]
# pprint(len(blendshape_keys_list))
# pprint(len(weight_values[0]))
# pprint(type(weight_values))
# pprint(weight_values)
kv_dict = dict(zip(blendshape_keys_list, weight_values))
fp = open(save_filename, 'w')
json.dump(kv_dict, fp, indent=2)
fp.close()
if __name__ == '__main__':
save_dir = r'/Users/zhaoyafei/Downloads/bs_definition_3D_face/maya_exports'
# blendshape_node_name = r'Head01_blendShape'
blendshape_node_name = get_all_blendshape_nodes(False)
export_keyframe_blendshape_weight_values(blendshape_node_name, save_dir, start_frame=1, end_frame=27)