diff options
Diffstat (limited to 'rigify/legacy/utils.py')
-rw-r--r-- | rigify/legacy/utils.py | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/rigify/legacy/utils.py b/rigify/legacy/utils.py index e3927ac3..f74a7abf 100644 --- a/rigify/legacy/utils.py +++ b/rigify/legacy/utils.py @@ -36,8 +36,6 @@ DEF_PREFIX = "DEF-" # Prefix of deformation bones. WGT_PREFIX = "WGT-" # Prefix for widget objects ROOT_NAME = "root" # Name of the root bone. -WGT_LAYERS = [x == 19 for x in range(0, 20)] # Widgets go on the last scene layer. - MODULE_NAME = "rigify" # Windows/Mac blender is weird, so __package__ doesn't work @@ -342,7 +340,7 @@ def obj_to_bone(obj, rig, bone_name): bone = rig.data.bones[bone_name] - mat = rig.matrix_world * bone.matrix_local + mat = rig.matrix_world @ bone.matrix_local obj.location = mat.to_translation() @@ -397,6 +395,7 @@ def create_widget(rig, bone_name, bone_transform_name=None): obj_name = WGT_PREFIX + bone_name scene = bpy.context.scene + collection = bpy.context.collection # Check if it already exists in the scene if obj_name in scene.objects: @@ -416,11 +415,10 @@ def create_widget(rig, bone_name, bone_transform_name=None): # Create mesh object mesh = bpy.data.meshes.new(obj_name) obj = bpy.data.objects.new(obj_name, mesh) - scene.objects.link(obj) + collection.objects.link(obj) # Move object to bone position and set layers obj_to_bone(obj, rig, bone_transform_name) - obj.layers = WGT_LAYERS return obj @@ -606,8 +604,8 @@ def align_bone_roll(obj, bone1, bone2): rot_mat = Matrix.Rotation(angle, 3, axis) # Roll factor - x3 = rot_mat * x1 - dot = x2 * x3 + x3 = rot_mat @ x1 + dot = x2 @ x3 if dot > 1.0: dot = 1.0 elif dot < -1.0: @@ -618,8 +616,8 @@ def align_bone_roll(obj, bone1, bone2): bone1_e.roll = roll # Check if we rolled in the right direction - x3 = rot_mat * bone1_e.x_axis - check = x2 * x3 + x3 = rot_mat @ bone1_e.x_axis + check = x2 @ x3 # If not, reverse if check < 0.9999: @@ -937,3 +935,43 @@ def random_id(length=8): text += random.choice(chars) text += str(hex(int(time.time())))[2:][-tlength:].rjust(tlength, '0')[::-1] return text + + +def find_layer_collection_by_collection(layer_collection, collection): + if collection == layer_collection.collection: + return layer_collection + + # go recursive + for child in layer_collection.children: + layer_collection = find_layer_collection_by_collection(child, collection) + if layer_collection: + return layer_collection + + +def ensure_widget_collection(context): + wgts_collection_name = "Widgets" + + view_layer = context.view_layer + layer_collection = bpy.context.layer_collection + collection = layer_collection.collection + + widget_collection = bpy.data.collections.get(wgts_collection_name) + if not widget_collection: + # ------------------------------------------ + # Create the widget collection + widget_collection = bpy.data.collections.new(wgts_collection_name) + widget_collection.hide_viewport = True + widget_collection.hide_render = True + + widget_layer_collection = None + else: + widget_layer_collection = find_layer_collection_by_collection(view_layer.layer_collection, widget_collection) + + if not widget_layer_collection: + # Add the widget collection to the tree + collection.children.link(widget_collection) + widget_layer_collection = [c for c in layer_collection.children if c.collection == widget_collection][0] + + # Make the widget the active collection for the upcoming added (widget) objects + view_layer.active_layer_collection = widget_layer_collection + return widget_collection |