-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
333 lines (267 loc) · 12.7 KB
/
__init__.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import bpy
from bpy.utils import register_class, unregister_class
import re
# info about add on
bl_info = {
"name": "Unreal Rigify To GRT",
"version": (1, 0, 0),
"author": "kurethedead",
"location": "3DView",
"description": "Automating GRT rig generation from a Rigify metarig for use with Unreal",
"category": "Armature",
"blender": (4, 1, 0),
}
class GenerateRig(bpy.types.Operator):
# set bl_ properties
bl_description = 'Generates a GRT rig from the selected Rigify metarig. An optional add-on rig will be joined to the Rigify control rig before generating the GRT deform rig. The selected bones in this add-on rig are parented to the "head" bone.'
bl_idname = "object.generate_grt_rig_from_rigify_metarig"
bl_label = "Rigify Metarig To GRT"
bl_options = {"REGISTER", "UNDO", "PRESET"}
# Called on demand (i.e. button press, menu item)
# Can also be called from operator search menu (Spacebar)
def execute(self, context):
if len(context.selected_objects) == 0:
raise RuntimeError("Armature not selected.")
elif type(context.selected_objects[0].data) is not bpy.types.Armature:
raise RuntimeError("Armature not selected.")
metarigObj = context.selected_objects[0]
if context.mode != "POSE":
bpy.ops.object.mode_set(mode="POSE")
# Keep track of these bones to reparent in GRT rig
faceBoneNames = [
bone.name for bone in metarigObj.data.bones["spine.006"].children_recursive
]
# Make IK rig use single bones for each limb, allowing for 2-bone game IK to work
for limbName in ["upper_arm.L", "upper_arm.R", "thigh.L", "thigh.R"]:
poseBone = metarigObj.pose.bones[limbName]
poseBone.rigify_parameters.segments = 1
for limbName in ["thigh.L", "thigh.R"]:
# Set rotation axis so that knees bend forward
poseBone = metarigObj.pose.bones[limbName]
poseBone.rigify_parameters.rotation_axis = "x"
poseBone.rigify_parameters.auto_align_extremity = True
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.pose.rigify_generate()
ikRigObj = bpy.context.active_object
ikRigObj.show_in_front = True
# Set IK parent for hand/feet/torso to 0, making them move independent of the root bone
# This lets us constrain the root bone to the torso, so we get free root motion
for limbName in [
"upper_arm_parent.L",
"upper_arm_parent.R",
"thigh_parent.L",
"thigh_parent.R",
]:
poseBone = ikRigObj.pose.bones[limbName]
poseBone["IK_parent"] = 0
ikRigObj.pose.bones["torso"]["torso_parent"] = 0
ikRigObj.data.bones["root"].use_deform = True
rootPoseBone = ikRigObj.pose.bones["root"]
constraint = rootPoseBone.constraints.new(type="COPY_LOCATION")
constraint.target = ikRigObj
constraint.subtarget = "torso"
constraint.use_x = False
constraint.use_y = True
constraint.use_z = False
# Add shape key rig if applicable.
shapeKeyRig = bpy.context.scene.rigifyToGRTProperty.shapeKeyRig
shapeKeyRigBoneNames = []
if shapeKeyRig:
shapeKeyRigBoneNames = [
bone.name for bone in shapeKeyRig.data.bones if bone.parent is None
]
bpy.ops.object.select_all(action="DESELECT")
shapeKeyRig.select_set(True)
ikRigObj.select_set(True)
bpy.context.view_layer.objects.active = ikRigObj
bpy.ops.object.join()
bpy.ops.object.mode_set(mode="EDIT")
controlEditBones = ikRigObj.data.edit_bones
for childBoneName in shapeKeyRigBoneNames:
controlEditBones[childBoneName].parent = controlEditBones["head"]
# TODO: Handle bone parenting, not all bones should be parented?
# Set ik rig to source armature - need it for game rig generation, so we just set other settings while we're here
bpy.ops.object.mode_set(mode="OBJECT")
GRTSettings = context.scene.GRT_Action_Bakery_Global_Settings
GRTSettings.Overwrite = True
GRTSettings.Push_to_NLA = False
GRTSettings.Source_Armature = ikRigObj
bpy.ops.gamerigtool.generate_game_rig(Deform_Armature_Name="Armature")
bpy.ops.object.mode_set(mode="EDIT")
# Reparent bones on GRT rig so that hierarchy makes sense in Unreal
# Every bone should be in same hierarchy under the root bone
GRTRigObj = bpy.context.active_object
GRTSettings.Target_Armature = GRTRigObj
editBones = GRTRigObj.data.edit_bones
# Arms to Shoulders
editBones["DEF-upper_arm.L"].parent = editBones["DEF-shoulder.L"]
editBones["DEF-upper_arm.R"].parent = editBones["DEF-shoulder.R"]
# Shoulders/Breasts to Spine
for childBoneName in [
"DEF-breast.R",
"DEF-breast.L",
"DEF-shoulder.R",
"DEF-shoulder.L",
]:
editBones[childBoneName].parent = editBones["DEF-spine.003"]
# Thighs to Pelvis to Spine
editBones["DEF-thigh.L"].parent = editBones["DEF-pelvis.L"]
editBones["DEF-thigh.R"].parent = editBones["DEF-pelvis.R"]
editBones["DEF-pelvis.L"].parent = editBones["DEF-spine"]
editBones["DEF-pelvis.R"].parent = editBones["DEF-spine"]
# Face to Head
for childName in faceBoneNames:
name = f"DEF-{childName}"
if name in editBones:
editBones[name].parent = editBones["DEF-spine.006"]
# Shape Key Bones to Head
for childName in shapeKeyRigBoneNames:
editBones[childName].parent = editBones["DEF-spine.006"]
bpy.ops.object.mode_set(mode="OBJECT")
# Add/Reorder collections
collections = {}
sceneCollection = bpy.context.scene.collection
activeCollection = bpy.context.view_layer.active_layer_collection.collection
layerNames = ["Deform", "Control", "Metarig"]
for name in layerNames:
if name not in bpy.data.collections:
collections[name] = bpy.data.collections.new(name)
sceneCollection.children.link(collections[name])
else:
collections[name] = bpy.data.collections[name]
# children = sceneCollection.children[:]
# try:
# for i in range(len(layerNames)):
# childCollection = sceneCollection.children[layerNames[i]]
# children.remove(childCollection)
# children.insert(i + 1, childCollection)
# if layerNames[i] != "Control":
# childCollection.hide_viewport = True
#
# for child in children:
# sceneCollection.children.unlink(child)
# sceneCollection.children.link(child)
# except ValueError:
# print("Control/Deform collection not found.")
# Add rigs to correct collections
for name, rig in tuple(zip(layerNames, [GRTRigObj, ikRigObj, metarigObj])):
if not rig:
continue
if rig not in collections[name].objects[:]:
collections[name].objects.link(rig)
if rig in activeCollection.objects[:]:
activeCollection.objects.unlink(rig)
metarigObj.hide_set(True)
self.report({"INFO"}, "Finished")
return {"FINISHED"} # must return a set
class TransferShapeKeyDrivers(bpy.types.Operator):
# set bl_ properties
bl_description = "Copies shape key driver settings from the active object to all selected objects for any shared shape key names. This optionally sets a new target object for all driver variables."
bl_idname = "object.transfer_shape_key_drivers"
bl_label = "Transfer Shape Key Drivers By Name"
bl_options = {"REGISTER", "UNDO", "PRESET"}
# Called on demand (i.e. button press, menu item)
# Can also be called from operator search menu (Spacebar)
def execute(self, context):
if len(context.selected_objects) < 2:
self.report({"ERROR"}, "Not enough objects selected.")
return {"FINISHED"}
elif bpy.context.view_layer.objects.active is None:
self.report({"ERROR"}, "No active object selected.")
return {"FINISHED"}
for obj in context.selected_objects:
if not isinstance(obj.data, bpy.types.Mesh):
raise RuntimeError("A selected object is not a mesh.")
source = bpy.context.view_layer.objects.active
targets = [obj for obj in context.selected_objects if obj is not source]
targetRig = bpy.context.scene.rigifyToGRTProperty.rigObj
for target in targets:
if source.data.shape_keys.animation_data is None:
self.report({"ERROR"}, "No shape key data found on source object.")
return {"FINISHED"}
for fcurve in source.data.shape_keys.animation_data.drivers:
sourceDriver = fcurve.driver
shapeKeyName = self.getShapeKeyNameFromDriver(fcurve)
targetShapeKeys = target.data.shape_keys
if shapeKeyName and shapeKeyName in targetShapeKeys.key_blocks:
targetShapeKey = targetShapeKeys.key_blocks[shapeKeyName]
targetDriver = targetShapeKey.driver_add("value").driver
targetDriver.type = sourceDriver.type
targetDriver.expression = sourceDriver.expression
for driverVar in sourceDriver.variables:
targetDriverVar = targetDriver.variables.new()
targetDriverVar.name = driverVar.name
targetDriverVar.type = driverVar.type
for i in range(len(driverVar.targets)):
for value in [
"bone_target",
"data_path",
"id",
"rotation_mode",
"transform_space",
"transform_type",
]:
setattr(
targetDriverVar.targets[i],
value,
getattr(driverVar.targets[i], value),
)
# New shape key drivers should target our deform rig
if targetRig:
targetDriverVar.targets[i].id = targetRig
# Need to do this to force update of driver
targetDriver.expression = targetDriver.expression
driver = fcurve.driver
driver.expression
self.report({"INFO"}, "Finished")
return {"FINISHED"} # must return a set
def getShapeKeyNameFromDriver(self, fcurve):
match = re.match(r"key\_blocks\[\"(.*)\"\]\.value", fcurve.data_path)
if match:
return match.group(1)
else:
return None
class ToolsPanel(bpy.types.Panel):
bl_idname = "RIGIFY_GRT_PT_global_tools"
bl_label = "Unreal Rigify To GRT"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Game Rig Tool"
@classmethod
def poll(cls, context):
return True
# called every frame
def draw(self, context):
col = self.layout.column()
prop = bpy.context.scene.rigifyToGRTProperty
generateRig = col.operator(GenerateRig.bl_idname)
prop_split(col, prop, "shapeKeyRig", "Add-on Rig")
col.label(text="Make sure all add-on bones are deformable.")
transferShapeKeyDrivers = col.operator(TransferShapeKeyDrivers.bl_idname)
prop_split(col, prop, "rigObj", "New Driver Target")
def prop_split(layout, data, field, name, **prop_kwargs):
split = layout.split(factor=0.5)
split.label(text=name)
split.prop(data, field, text="", **prop_kwargs)
def pollShapeKeyRig(self, obj):
return isinstance(obj.data, bpy.types.Armature)
class RigifyToGRTProperty(bpy.types.PropertyGroup):
shapeKeyRig: bpy.props.PointerProperty(
type=bpy.types.Object,
poll=pollShapeKeyRig,
)
rigObj: bpy.props.PointerProperty(
type=bpy.types.Object,
poll=pollShapeKeyRig,
)
classes = [GenerateRig, TransferShapeKeyDrivers, ToolsPanel, RigifyToGRTProperty]
def register():
for cls in classes:
register_class(cls)
bpy.types.Scene.rigifyToGRTProperty = bpy.props.PointerProperty(
type=RigifyToGRTProperty
)
def unregister():
del bpy.types.Scene.rigifyToGRTProperty
for cls in classes:
unregister_class(cls)