-
Notifications
You must be signed in to change notification settings - Fork 3
/
operators.py
135 lines (108 loc) · 4.93 KB
/
operators.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
# ##### BEGIN GPL LICENSE BLOCK #####
# KTFaceRig add-on for Blender is an unofficial Face Rig add-on
# working with KeenTools FaceBuilder model.
# Copyright (C) 2020 Alexander Milovsky
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####
import logging
import bpy
from .faceutils import generate_rig_from_mesh, bake_animation_to_rig,\
select_object, set_custom_attribute, has_custom_attribute, \
get_safe_custom_attribute
from .faceutils.topology_data import TopologyVersion, version_by_object
def find_marked_object(ftype='ARMATURE', fattr='fbmesh'):
for obj in bpy.context.scene.objects:
if obj.type == ftype:
if has_custom_attribute(obj, fattr):
return obj
return None
def selected_pair():
selected = bpy.context.selected_objects
if len(selected) != 2:
return None, None
if selected[0] is bpy.context.active_object:
return selected[0], selected[1]
else:
return selected[1], selected[0]
class FBRigActor(bpy.types.Operator):
bl_idname = 'keentools_facerig.actor_operator'
bl_label = 'Main Action Operator'
bl_options = {'REGISTER', 'UNDO'}
action: bpy.props.StringProperty(name="Action string", default="Just text")
num: bpy.props.IntProperty(name="Numeric parameter", default=0)
def execute(self, context):
logger = logging.getLogger(__name__)
scene = context.scene
obj = context.object
self.report({'INFO'}, "Action: {0}".format(self.action))
if self.action == 'generate_rig':
if obj is None or obj.type != 'MESH':
return {'CANCELLED'}
logger.info('Vertices: {}'.format(len(obj.data.vertices)))
ver = version_by_object(obj)
logger.info('Model version: {}'.format(ver))
if ver == TopologyVersion.undefined:
return {'CANCELLED'}
arm_obj = generate_rig_from_mesh(obj, 'FBRig', ver)
select_object(arm_obj)
obj.select_set(state=True)
set_custom_attribute(arm_obj, 'fbmesh', obj.name)
set_custom_attribute(obj, 'fbrig', arm_obj.name)
elif self.action == 'skinning':
obj1, obj2 = selected_pair()
if obj1 is None:
return {'CANCELLED'}
if obj1.type != 'ARMATURE' or obj2.type != 'MESH':
return {'CANCELLED'}
bpy.ops.object.parent_set(type='ARMATURE_AUTO')
elif self.action == 'animation_source':
obj1, obj2 = selected_pair()
if obj1 is None:
return {'CANCELLED'}
if obj1.type != 'ARMATURE' or obj2.type != 'MESH':
return {'CANCELLED'}
set_custom_attribute(obj1, 'fbanimation', obj2.name)
select_object(obj1)
elif self.action == 'animation_neutral':
obj1, obj2 = selected_pair()
if obj1 is None:
return {'CANCELLED'}
set_custom_attribute(obj1, 'fbneutral', obj2.name)
elif self.action == 'bake_animation':
obj1, obj2 = selected_pair()
if obj1 is None:
return {'CANCELLED'}
if obj1.type != 'ARMATURE' or obj2.type != 'MESH':
return {'CANCELLED'}
neutral = get_safe_custom_attribute(obj2, 'fbneutral')
neutral_obj = None if neutral is None else bpy.context.scene.objects[neutral]
bake_animation_to_rig(obj2, obj1, neutral_obj)
elif self.action == 'clear_scene':
arm_obj = find_marked_object('ARMATURE', 'fbmesh')
obj = find_marked_object('MESH', 'fbrig')
select_object(arm_obj)
obj.select_set(state=True)
bpy.ops.object.select_all(action='INVERT')
bpy.ops.object.delete(use_global=False, confirm=False)
select_object(arm_obj)
obj.select_set(state=True)
elif self.action == 'export_scene':
bpy.ops.export_scene.fbx('INVOKE_DEFAULT',
use_armature_deform_only=True,
add_leaf_bones=False)
print(self.action)
return {'FINISHED'}
# Operator Panel Draw
def draw(self, context):
layout = self.layout
layout.label(text="Simple text label", icon="INFO")
layout.label(text="Action: {}".format(self.action))