Add Easy_Weight to Addons #47

Merged
Nick Alberelli merged 48 commits from feature/easy_weights into main 2023-05-17 22:13:57 +02:00
Showing only changes of commit 40cfa1e968 - Show all commits

View File

@ -5,16 +5,15 @@ import bpy
# It registers an operator called "Toggle Weight Paint Mode" that does the following:
# Set active object to weight paint mode
# Set shading mode to a white MatCap, Single Color shading
# Set shading mode to a Flat, Single Color, White shading
# Find first armature via the object's modifiers.
# Ensure it is visible by moving it to a temporary collection and changing its visibility settings.
# Enable "In Front" option
# Set it to pose mode.
# When running the operator again, it should restore all modes and shading settings, delete the temporary collection, and restore the armature's visibility settings.
# You need to set up your own keybind for this operator.
# NOTE: If you exit weight paint mode with this operator while in wireframe mode, the studio light shading setting won't be restored.
coll_name = "temp_weight_paint_armature"
coll_name = "Weight Paint"
class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
"""Toggle weight paint mode properly with a single operator. """
@ -40,10 +39,10 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
# Finding armature.
armature = None
for m in obj.modifiers:
if(m.type=='ARMATURE'):
if m.type=='ARMATURE':
armature = m.object
if(enter_wp):
if enter_wp:
### Entering weight paint mode. ###
# If the mesh object's display mode was anything other than Solid or Textured, store it, as we'll have to change it. and then change it back.
@ -54,11 +53,11 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
obj.data.use_mirror_topology = False
# Store old shading settings in a dict custom property
if('wpt' not in context.screen):
if 'wpt' not in context.screen:
context.screen['wpt'] = {}
wpt = context.screen['wpt'].to_dict()
# Set modes.
if(armature):
if armature:
context.screen['wpt']['armature_enabled'] = armature.hide_viewport
context.screen['wpt']['armature_hide'] = armature.hide_get()
context.screen['wpt']['armature_in_front'] = armature.show_in_front
@ -82,12 +81,12 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
bpy.ops.object.mode_set(mode='POSE')
context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
if('last_switch_in' not in wpt or wpt['last_switch_in']==False): # Only save shading info if we exitted weight paint mode using this operator.
if 'last_switch_in' not in wpt or wpt['last_switch_in']==False: # Only save shading info if we exitted weight paint mode using this operator.
context.screen['wpt']['shading_type'] = context.space_data.shading.type
if context.space_data.shading.type=='SOLID':
context.screen['wpt']['light'] = context.space_data.shading.light
context.screen['wpt']['color_type'] = context.space_data.shading.color_type
context.screen['wpt']['studio_light'] = context.space_data.shading.studio_light
context.screen['wpt']['single_color'] = context.space_data.shading.single_color
context.screen['wpt']['active_object'] = obj
context.screen['wpt']['last_switch_in'] = True # Store whether the last time the operator ran, were we switching into or out of weight paint mode.
@ -95,9 +94,9 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
# Set shading
if context.space_data.shading.type=='SOLID':
context.space_data.shading.light = 'MATCAP'
context.space_data.shading.light = 'FLAT'
context.space_data.shading.color_type = 'SINGLE'
context.space_data.shading.studio_light = 'basic_1.exr'
context.space_data.shading.single_color = (1,1,1)
else:
### Leaving weight paint mode. ###
@ -107,7 +106,7 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
obj.display_type = obj['wpt_display_type']
del obj['wpt_display_type']
if('wpt' in context.screen):
if 'wpt' in context.screen:
info = context.screen['wpt'].to_dict()
# Restore mode.
bpy.ops.object.mode_set(mode=info['mode'])
@ -118,10 +117,10 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
if context.space_data.shading.type=='SOLID':
context.space_data.shading.light = info['light']
context.space_data.shading.color_type = info['color_type']
context.space_data.shading.studio_light = info['studio_light']
context.space_data.shading.single_color = info['single_color']
# If the armature was un-hidden, hide it again.
if(armature):
if armature:
armature.hide_viewport = info['armature_enabled']
armature.hide_set(info['armature_hide'])
armature.show_in_front = info['armature_in_front']