diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2020-12-07 22:11:00 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2020-12-07 22:43:22 +0300 |
commit | f8d6489fb6e4b8f3c1029d3468ebbe6781f599dd (patch) | |
tree | d9b2cddabba936c2327389176a763ec78fd3fd95 /rigify/utils | |
parent | c93dc355888ec2f9a4376ad6cf0422277c69d565 (diff) |
Rigify: use separate widget collection for each generated rig.
As pointed out in comments to T73114, using the same name has downsides.
In addition, remove the widget parent object that was inherited
from the old pre-collection way Rigify worked and has no other
purpose than grouping objects like the collection already does.
Rename the widgets and the collection when renaming the rig.
Finally as an aside add a couple of options to create_widget.
Diffstat (limited to 'rigify/utils')
-rw-r--r-- | rigify/utils/collections.py | 6 | ||||
-rw-r--r-- | rigify/utils/widgets.py | 37 |
2 files changed, 20 insertions, 23 deletions
diff --git a/rigify/utils/collections.py b/rigify/utils/collections.py index 5682ec64..a172b984 100644 --- a/rigify/utils/collections.py +++ b/rigify/utils/collections.py @@ -65,9 +65,7 @@ def filter_layer_collections_by_object(layer_collections, obj): return [lc for lc in layer_collections if obj in lc.collection.objects.values()] -def ensure_widget_collection(context): - wgts_collection_name = "Widgets" - +def ensure_widget_collection(context, wgts_collection_name): view_layer = context.view_layer layer_collection = bpy.context.layer_collection collection = layer_collection.collection @@ -89,6 +87,8 @@ def ensure_widget_collection(context): collection.children.link(widget_collection) widget_layer_collection = [c for c in layer_collection.children if c.collection == widget_collection][0] + widget_layer_collection.exclude = True + # Make the widget the active collection for the upcoming added (widget) objects view_layer.active_layer_collection = widget_layer_collection return widget_collection diff --git a/rigify/utils/widgets.py b/rigify/utils/widgets.py index 04e176c6..f198afda 100644 --- a/rigify/utils/widgets.py +++ b/rigify/utils/widgets.py @@ -54,7 +54,7 @@ def obj_to_bone(obj, rig, bone_name, bone_transform_name=None): obj.matrix_basis = rig.matrix_world @ bone.bone.matrix_local @ Matrix.Scale(scale, 4) -def create_widget(rig, bone_name, bone_transform_name=None): +def create_widget(rig, bone_name, bone_transform_name=None, *, widget_name=None, widget_force_new=False): """ Creates an empty widget object for a bone, and returns the object. """ assert rig.mode != 'EDIT' @@ -64,37 +64,34 @@ def create_widget(rig, bone_name, bone_transform_name=None): if bone.custom_shape: return None - obj_name = WGT_PREFIX + rig.name + '_' + bone_name + obj_name = widget_name or WGT_PREFIX + rig.name + '_' + bone_name scene = bpy.context.scene - collection = ensure_widget_collection(bpy.context) + collection = ensure_widget_collection(bpy.context, 'WGTS_' + rig.name) # Check if it already exists in the scene - if obj_name in scene.objects: - # Move object to bone position, in case it changed - obj = scene.objects[obj_name] - obj_to_bone(obj, rig, bone_name, bone_transform_name) + if not widget_force_new: + if obj_name in scene.objects: + # Move object to bone position, in case it changed + obj = scene.objects[obj_name] + obj_to_bone(obj, rig, bone_name, bone_transform_name) + + return None - return None - else: # Delete object if it exists in blend data but not scene data. # This is necessary so we can then create the object without # name conflicts. if obj_name in bpy.data.objects: - bpy.data.objects[obj_name].user_clear() bpy.data.objects.remove(bpy.data.objects[obj_name]) - # Create mesh object - mesh = bpy.data.meshes.new(obj_name) - obj = bpy.data.objects.new(obj_name, mesh) - collection.objects.link(obj) + # Create mesh object + mesh = bpy.data.meshes.new(obj_name) + obj = bpy.data.objects.new(obj_name, mesh) + collection.objects.link(obj) - # Move object to bone position and set layers - obj_to_bone(obj, rig, bone_name, bone_transform_name) - wgts_group_name = 'WGTS_' + rig.name - if wgts_group_name in bpy.data.objects.keys(): - obj.parent = bpy.data.objects[wgts_group_name] + # Move object to bone position and set layers + obj_to_bone(obj, rig, bone_name, bone_transform_name) - return obj + return obj def create_circle_polygon(number_verts, axis, radius=1.0, head_tail=0.0): |