def bone(context, armature, name="", edit=None, pose=None, overwrite=False): "Insert a bone into an armature object" if getattr(armature, 'type', None) != 'ARMATURE': return # active = Get.active(context) # Set.active(context, armature) # Set.select(armature, True) # Set.visible(context, armature, True) # mode = armature.mode.replace('EDIT_ARMATURE', 'EDIT') Set.in_scene(context, armature) is_visible = Is.visible(context, armature) Set.visible(context, armature, True) mode = armature.mode # mode = context.mode.replace('EDIT_ARMATURE', 'EDIT') # Go into Edit mode and create a new bone ebones = armature.data.edit_bones if armature.mode != 'EDIT': Set.mode(context, 'EDIT', armature) mirror = armature.data.use_mirror_x armature.data.use_mirror_x = False children = list() if overwrite and name in ebones: for child in ebones[name].children: children.append((child, child.use_connect)) child.use_connect = False ebones.remove(ebones[name]) bone = ebones.new(name) for child, child_connect in children: child.parent = bone child.use_connect = child_connect bone.tail = ((0, 0, 1)) # If the bone's head AND tail stay at 0, # it gets deleted when leaving edit mode name = bone.name if edit: edit(bone) armature.data.use_mirror_x = mirror pbones = armature.pose.bones if pose: Set.mode(context, 'POSE', armature) bone = pbones[name] pose(bone) # Revert mode change if mode != armature.mode: # Set.active(active) Set.mode(context, mode, armature) Set.visible(context, armature, is_visible) if armature.mode == 'EDIT': bone = ebones[name] else: bone = pbones[name] return bone
def editWidget(context, active_bone): widget = active_bone.custom_shape armature = active_bone.id_data Set.mode(context, 'OBJECT', armature) Set.select(armature, False) ''' # 2.7 if context.space_data.lock_camera_and_layers == False : visibleLayers = numpy.array(bpy.context.space_data.layers)+widget.layers-armature.layers bpy.context.space_data.layers = visibleLayers.tolist() else : visibleLayers = numpy.array(bpy.context.scene.layers)+widget.layers-armature.layers bpy.context.scene.layers = visibleLayers.tolist() ''' "Commented because the new version only uses get_collection" if widget.users_collection: collection = widget.users_collection[0] else: collection = get_collection(context) collection.objects.link(widget) Set.in_scene(context, collection) Set.visible(context, collection) "Commented because new version uses operator" # get_collection_view_layer(collection).hide_viewport = False if getattr(context.space_data, 'local_view', None): bpy.ops.view3d.localview() # select object and make it active Set.select(widget, True) Set.active(context, widget) Set.mode(context, 'EDIT', widget)