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
|
# SPDX-FileCopyrightText: 2010-2022 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from ...base_rig import BaseRig
from ...utils.naming import make_derived_name
from ...utils.bones import set_bone_widget_transform
from ...utils.widgets import layout_widget_dropdown, create_registered_widget
from ...utils.widgets_basic import create_pivot_widget
from ...utils.switch_parent import SwitchParentBuilder
class Rig(BaseRig):
""" A rig providing a rotation pivot control that can be moved. """
class CtrlBones(BaseRig.CtrlBones):
master: str
pivot: str
class MchBones(BaseRig.MchBones):
pass
bones: BaseRig.ToplevelBones[
str,
'Rig.CtrlBones',
'Rig.MchBones',
str
]
make_control: bool
make_pivot: bool
make_deform: bool
def find_org_bones(self, pose_bone) -> str:
return pose_bone.name
def initialize(self):
self.make_control = self.params.make_extra_control
self.make_pivot = self.params.make_control or not self.make_control
self.make_deform = self.params.make_extra_deform
def generate_bones(self):
org = self.bones.org
if self.make_control:
self.bones.ctrl.master = name = self.copy_bone(org, make_derived_name(org, 'ctrl'), parent=True)
if self.make_pivot:
self.bones.ctrl.pivot = self.copy_bone(org, make_derived_name(org, 'ctrl', '_pivot'))
if self.params.make_parent_switch:
self.build_parent_switch(name)
if self.params.register_parent:
self.register_parent(name, self.get_parent_tags())
else:
self.bones.ctrl.pivot = self.copy_bone(org, make_derived_name(org, 'ctrl'), parent=True)
if self.make_deform:
self.bones.deform = self.copy_bone(org, make_derived_name(org, 'def'), bbone=True)
def build_parent_switch(self, master_name: str):
pbuilder = SwitchParentBuilder(self.generator)
org_parent = self.get_bone_parent(self.bones.org)
parents = [org_parent] if org_parent else []
pbuilder.build_child(
self, master_name,
context_rig=self.rigify_parent, allow_self=True,
prop_name="Parent ({})".format(master_name),
extra_parents=parents, select_parent=org_parent,
controls=lambda: self.bones.ctrl.flatten()
)
def get_parent_tags(self):
tags = {t.strip() for t in self.params.register_parent_tags.split(',')}
if self.params.make_parent_switch:
tags.add('child')
tags.discard('')
return tags
def register_parent(self, master_name: str, tags: set[str]):
pbuilder = SwitchParentBuilder(self.generator)
inject = self.rigify_parent if 'injected' in tags else None
pbuilder.register_parent(
self, self.bones.org, name=master_name,
inject_into=inject, tags=tags
)
def parent_bones(self):
ctrl = self.bones.ctrl
if self.make_pivot:
if self.make_control:
self.set_bone_parent(ctrl.pivot, ctrl.master, use_connect=False)
self.set_bone_parent(self.bones.org, ctrl.pivot, use_connect=False)
else:
self.set_bone_parent(self.bones.org, ctrl.master, use_connect=False)
if self.make_deform:
self.set_bone_parent(self.bones.deform, self.bones.org, use_connect=False)
def configure_bones(self):
org = self.bones.org
ctrl = self.bones.ctrl
main_ctl = ctrl.master if self.make_control else ctrl.pivot
self.copy_bone_properties(org, main_ctl, ui_controls=True)
def rig_bones(self):
if self.make_pivot:
self.make_constraint(
self.bones.org, 'COPY_LOCATION', self.bones.ctrl.pivot,
space='LOCAL', invert_xyz=(True,) * 3
)
def generate_widgets(self):
if self.make_pivot:
create_pivot_widget(self.obj, self.bones.ctrl.pivot, square=True, axis_size=2.0)
if self.make_control:
set_bone_widget_transform(self.obj, self.bones.ctrl.master, self.bones.org)
create_registered_widget(self.obj, self.bones.ctrl.master,
self.params.pivot_master_widget_type or 'cube')
@classmethod
def add_parameters(cls, params):
params.make_control = bpy.props.BoolProperty(
name="Control",
default=True,
description="Create a control bone for the copy"
)
params.pivot_master_widget_type = bpy.props.StringProperty(
name="Widget Type",
default='cube',
description="Choose the type of the widget to create"
)
params.make_parent_switch = bpy.props.BoolProperty(
name="Switchable Parent",
default=False,
description="Allow switching the parent of the master control"
)
params.register_parent = bpy.props.BoolProperty(
name="Register Parent",
default=False,
description="Register the control as a switchable parent candidate"
)
params.register_parent_tags = bpy.props.StringProperty(
name="Parent Tags",
default="",
description="Comma-separated tags to use for the registered parent"
)
params.make_extra_control = bpy.props.BoolProperty(
name="Extra Control",
default=False,
description="Create an optional control"
)
params.make_extra_deform = bpy.props.BoolProperty(
name="Extra Deform",
default=False,
description="Create an optional deform bone"
)
@classmethod
def parameters_ui(cls, layout, params):
r = layout.row()
r.prop(params, "make_extra_control", text="Master Control")
if params.make_extra_control:
layout_widget_dropdown(layout, params, "pivot_master_widget_type")
layout.prop(params, "make_parent_switch")
layout.prop(params, "register_parent")
r = layout.row()
r.active = params.register_parent
r.prop(params, "register_parent_tags", text="Tags")
layout.prop(params, "make_control", text="Pivot Control")
r = layout.row()
r.prop(params, "make_extra_deform", text="Deform Bone")
def create_sample(obj):
""" Create a sample metarig for this rig type.
"""
# generated by rigify.utils.write_metarig
bpy.ops.object.mode_set(mode='EDIT')
arm = obj.data
bones = {}
bone = arm.edit_bones.new('pivot')
bone.head[:] = 0.0000, 0.0000, 0.0000
bone.tail[:] = 0.0000, 0.5000, 0.0000
bone.roll = 0.0000
bone.use_connect = False
bones['pivot'] = bone.name
bpy.ops.object.mode_set(mode='OBJECT')
pbone = obj.pose.bones[bones['pivot']]
pbone.rigify_type = 'basic.pivot'
pbone.lock_location = (False, False, False)
pbone.lock_rotation = (False, False, False)
pbone.lock_rotation_w = False
pbone.lock_scale = (False, False, False)
pbone.rotation_mode = 'QUATERNION'
bpy.ops.object.mode_set(mode='EDIT')
for bone in arm.edit_bones:
bone.select = False
bone.select_head = False
bone.select_tail = False
for b in bones:
bone = arm.edit_bones[bones[b]]
bone.select = True
bone.select_head = True
bone.select_tail = True
arm.edit_bones.active = bone
if bcoll := arm.collections.active:
bcoll.assign(bone)
return bones
|