Skip to content

Instantly share code, notes, and snippets.

@ZipFile
Last active May 1, 2024 13:25
Show Gist options
  • Save ZipFile/16ae630309ff138826e92e8a5e5c7e1f to your computer and use it in GitHub Desktop.
Save ZipFile/16ae630309ff138826e92e8a5e5c7e1f to your computer and use it in GitHub Desktop.
[Blender Add-on] Switch to Weight Paint and select Armature
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <https://unlicense.org>
import bpy
bl_info = {
"name": "Switch to Weight Paint and select Armature",
"author": "ZipFile",
"blender": (2, 80, 0),
"version": (0, 0, 1),
"description": "Switch to Weight Paint Mode and select associated Armature",
"category": "Object",
"location": "Context menu",
}
def find_armature(obj):
for modifier in obj.modifiers:
if modifier.type == "ARMATURE" and modifier.object:
return modifier.object
return None
def is_mesh(obj):
return obj is not None and obj.type == "MESH"
class WeightPaintSelectArmature(bpy.types.Operator):
"""Switch to Weight Paint and select Armature"""
bl_idname = "object.weight_paint_select_armature"
bl_label = "Weight Paint and Select Armature"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return is_mesh(context.active_object)
def execute(self, context):
mesh = context.active_object
armature = find_armature(mesh)
if armature is None:
self.report({"ERROR"}, "No armature connected to the mesh.")
return {"CANCELLED"}
bpy.ops.object.select_all(action="DESELECT")
bpy.context.view_layer.objects.active = armature
armature.select_set(True)
bpy.context.view_layer.objects.active = mesh
bpy.ops.object.mode_set(mode="WEIGHT_PAINT")
return {"FINISHED"}
def menu_func(self, context):
self.layout.operator(WeightPaintSelectArmature.bl_idname)
def register():
bpy.utils.register_class(WeightPaintSelectArmature)
bpy.types.VIEW3D_MT_object_context_menu.append(menu_func)
def unregister():
bpy.types.VIEW3D_MT_object_context_menu.remove(menu_func)
bpy.utils.unregister_class(WeightPaintSelectArmature)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment