-
Notifications
You must be signed in to change notification settings - Fork 19
/
gamerig_generate.py
143 lines (108 loc) · 4.69 KB
/
gamerig_generate.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
import bpy
from rigify.utils.bones import new_bone
from rigify.generate import Generator, get_xy_spread
from rigify.utils.rig import get_rigify_type
from rigify.utils.naming import make_original_name, ROOT_NAME
from rigify.ui import rigify_report_exception
from rigify.utils.errors import MetarigError
class Generator_gamerig(Generator):
# Removing this for now as it creates an error if the metarig is called 'Armature'
# def __init__(self, context, metarig):
# super().__init__(context, metarig)
# if not metarig.data.rigify_rig_basename:
# metarig.data.rigify_rig_basename = "Armature"
# Added this just to make game.raw_copy work, which is identical to raw_copy
def _Generator__rename_org_bones(self, obj):
# Make a list of the original bones, so we can keep track of them.
original_bones = [bone.name for bone in obj.data.bones]
# Add the ORG_PREFIX to the original bones.
for i in range(0, len(original_bones)):
bone = obj.pose.bones[original_bones[i]]
# Preserve the root bone as is if present
if bone.name == ROOT_NAME:
if bone.parent:
raise MetarigError('Root bone must have no parent')
if get_rigify_type(bone) not in ('', 'basic.raw_copy'):
raise MetarigError('Root bone must have no rig, or use basic.raw_copy')
continue
# This rig type is special in that it preserves the name of the bone.
if get_rigify_type(bone) not in ('basic.raw_copy', 'game.basic.raw_copy'):
bone.name = make_original_name(original_bones[i])
original_bones[i] = bone.name
self.original_bones = original_bones
def _Generator__create_root_bone(self):
obj = self.obj
metarig = self.metarig
#----------------------------------
# Create the root bone.
root_bone = new_bone(obj, ROOT_NAME)
spread = get_xy_spread(metarig.data.bones) or metarig.data.bones[0].length
spread = float('%.3g' % spread)
scale = spread/0.589
obj.data.edit_bones[root_bone].head = (0, 0, 0)
obj.data.edit_bones[root_bone].tail = (0, scale, 0)
obj.data.edit_bones[root_bone].roll = 0
self.root_bone = root_bone
self.bone_owners[root_bone] = None
# Only this line changed. The rest is a straight copy from Generator
bpy.ops.object.mode_set(mode='POSE')
obj.pose.bones[root_bone].rotation_mode = 'XYZ'
bpy.ops.object.mode_set(mode='EDIT')
def _Generator__lock_transforms(self):
super()._Generator__lock_transforms()
# Unlock on DEF- bones
for pb in self.obj.pose.bones:
if "DEF-" in pb.name:
pb.lock_location = (False, False, False)
pb.lock_rotation = (False, False, False)
pb.lock_rotation_w = False
pb.lock_scale = (False, False, False)
# Part of the execute function for the class below
def generate_rig(context, metarig):
""" Generates a rig from a metarig.
"""
# Initial configuration
rest_backup = metarig.data.pose_position
metarig.data.pose_position = 'REST'
try:
Generator_gamerig(context, metarig).generate()
metarig.data.pose_position = rest_backup
except Exception as e:
# Cleanup if something goes wrong
print("Rigify: failed to generate rig.")
bpy.ops.object.mode_set(mode='OBJECT')
metarig.data.pose_position = rest_backup
# Continue the exception
raise e
class GAMERIG_OT_generate(bpy.types.Operator):
"""Generates a rig from the active metarig armature"""
bl_idname = "pose.gamerig_generate"
bl_label = "GameRig Generate Rig"
bl_options = {'UNDO'}
bl_description = 'Generates a rig from the active metarig armature'
def execute(self, context):
try:
generate_rig(context, context.object)
except MetarigError as rig_exception:
import traceback
traceback.print_exc()
rigify_report_exception(self, rig_exception)
except Exception as rig_exception:
import traceback
traceback.print_exc()
self.report({'ERROR'}, 'Generation has thrown an exception: ' + str(rig_exception))
finally:
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
classes = [
GAMERIG_OT_generate,
]
def register():
from bpy.utils import register_class
for c in classes:
print("registering", c)
register_class(c)
def unregister():
from bpy.utils import unregister_class
for c in classes:
unregister_class(c)