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 08596b85c6 - Show all commits

View File

@ -43,6 +43,7 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
original states so they can be restored when leaving wp mode.""" original states so they can be restored when leaving wp mode."""
obj = context.object obj = context.object
wm = context.window_manager
# Store old display_type setting in a Custom Property on the Object. # Store old display_type setting in a Custom Property on the Object.
obj['wpt_display_type'] = obj.display_type obj['wpt_display_type'] = obj.display_type
@ -51,26 +52,27 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
obj.display_type = 'SOLID' obj.display_type = 'SOLID'
# Store old shading settings in a Custom Property dictionary in the Scene. # Store old shading settings in a Custom Property dictionary in the Scene.
if 'wpt' not in context.screen: if 'wpt' not in wm:
context.screen['wpt'] = {} wm['wpt'] = {}
wpt = context.screen['wpt'].to_dict() wpt = wm['wpt']
wpt_as_dict = wpt.to_dict()
# If we are entering WP mode for the first time or if the last time # If we are entering WP mode for the first time or if the last time
# the operator was exiting WP mode, then save current shading info. # the operator was exiting WP mode, then save current shading info.
if 'last_switch_in' not in wpt or wpt['last_switch_in']==False: if 'last_switch_in' not in wpt_as_dict or wpt_as_dict['last_switch_in']==False:
context.screen['wpt']['shading_type'] = context.space_data.shading.type wpt['shading_type'] = context.space_data.shading.type
if context.space_data.shading.type=='SOLID': if context.space_data.shading.type=='SOLID':
# These properties only exist when the shading type is SOLID. # These properties only exist when the shading type is SOLID.
context.screen['wpt']['light'] = context.space_data.shading.light wpt['light'] = context.space_data.shading.light
context.screen['wpt']['color_type'] = context.space_data.shading.color_type wpt['color_type'] = context.space_data.shading.color_type
context.screen['wpt']['single_color'] = context.space_data.shading.single_color wpt['single_color'] = context.space_data.shading.single_color
context.screen['wpt']['active_object'] = obj wpt['active_object'] = obj
# This flag indicates that the last time this operator ran, we were # This flag indicates that the last time this operator ran, we were
# switching INTO wp mode. # switching INTO wp mode.
context.screen['wpt']['last_switch_in'] = True wpt['last_switch_in'] = True
context.screen['wpt']['mode'] = obj.mode wpt['mode'] = obj.mode
# Set shading # Set shading
if context.space_data.shading.type=='SOLID': if context.space_data.shading.type=='SOLID':
@ -86,21 +88,21 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
if not armature: if not armature:
return {'FINISHED'} return {'FINISHED'}
# Save all object visibility related info so it can be restored later. # Save all object visibility related info so it can be restored later.
context.screen['wpt']['arm_enabled'] = armature.hide_viewport wpt['arm_enabled'] = armature.hide_viewport
context.screen['wpt']['arm_hide'] = armature.hide_get() wpt['arm_hide'] = armature.hide_get()
context.screen['wpt']['arm_in_front'] = armature.show_in_front wpt['arm_in_front'] = armature.show_in_front
context.screen['wpt']['arm_coll_assigned'] = False wpt['arm_coll_assigned'] = False
armature.hide_viewport = False armature.hide_viewport = False
armature.hide_set(False) armature.hide_set(False)
armature.show_in_front = True armature.show_in_front = True
if context.space_data.local_view: if context.space_data.local_view:
context.screen['wpt']['arm_local_view'] = armature.local_view_get(context.space_data) wpt['arm_local_view'] = armature.local_view_get(context.space_data)
armature.local_view_set(context.space_data, True) armature.local_view_set(context.space_data, True)
# If the armature is still not visible, add it to the scene root collection. # If the armature is still not visible, add it to the scene root collection.
if not armature.visible_get() and not armature.name in context.scene.collection.objects: if not armature.visible_get() and not armature.name in context.scene.collection.objects:
context.scene.collection.objects.link(armature) context.scene.collection.objects.link(armature)
context.screen['wpt']['arm_coll_assigned'] = True wpt['arm_coll_assigned'] = True
if not armature.visible_get(): if not armature.visible_get():
self.report({'WARNING'}, "Failed to make the armature visible. Curse you, Blender!!!") self.report({'WARNING'}, "Failed to make the armature visible. Curse you, Blender!!!")
@ -116,51 +118,53 @@ class EASYWEIGHT_OT_toggle_weight_paint(bpy.types.Operator):
that was stored about shading settings in enter_wp().""" that was stored about shading settings in enter_wp()."""
obj = context.object obj = context.object
wm = context.window_manager
# Restore object display type # Restore object display type
if 'wpt_display_type' in obj: if 'wpt_display_type' in obj:
obj.display_type = obj['wpt_display_type'] obj.display_type = obj['wpt_display_type']
del obj['wpt_display_type'] del obj['wpt_display_type']
if 'wpt' not in context.screen or 'mode' not in context.screen['wpt'].to_dict(): if 'wpt' not in wm or 'mode' not in wm['wpt'].to_dict():
# There is no saved data to restore from, nothing else to do. # There is no saved data to restore from, nothing else to do.
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'} return {'FINISHED'}
wpt = context.screen['wpt'].to_dict() wpt = wm['wpt']
wpt_as_dict = wpt.to_dict()
# Restore mode. # Restore mode.
bpy.ops.object.mode_set(mode=wpt['mode']) bpy.ops.object.mode_set(mode=wpt_as_dict['mode'])
# Reset the stored data # Reset the stored data
context.screen['wpt'] = {} wm['wpt'] = {}
# Flag to save that the last time the operator ran we were EXITING wp mode. # Flag to save that the last time the operator ran we were EXITING wp mode.
context.screen['wpt']['last_switch_in'] = False wm['wpt']['last_switch_in'] = False
# Restore shading options. # Restore shading options.
if 'light' in wpt: # If we stored solid view shading settings, restore those settings but don't restore the shading type. if 'light' in wpt_as_dict: # If we stored solid view shading settings, restore those settings but don't restore the shading type.
shading_type = context.space_data.shading.type shading_type = context.space_data.shading.type
context.space_data.shading.type = wpt['shading_type'] context.space_data.shading.type = wpt_as_dict['shading_type']
if context.space_data.shading.type=='SOLID': if context.space_data.shading.type=='SOLID':
context.space_data.shading.light = wpt['light'] context.space_data.shading.light = wpt_as_dict['light']
context.space_data.shading.color_type = wpt['color_type'] context.space_data.shading.color_type = wpt_as_dict['color_type']
context.space_data.shading.single_color = wpt['single_color'] context.space_data.shading.single_color = wpt_as_dict['single_color']
context.space_data.shading.type = shading_type context.space_data.shading.type = shading_type
armature = get_armature_of_meshob(obj) armature = get_armature_of_meshob(obj)
if not armature: if not armature:
return {'FINISHED'} return {'FINISHED'}
# If an armature was un-hidden, hide it again. # If an armature was un-hidden, hide it again.
armature.hide_viewport = wpt['arm_enabled'] armature.hide_viewport = wpt_as_dict['arm_enabled']
armature.hide_set(wpt['arm_hide']) armature.hide_set(wpt_as_dict['arm_hide'])
armature.show_in_front = wpt['arm_in_front'] armature.show_in_front = wpt_as_dict['arm_in_front']
# Restore whether the armature is in local view or not. # Restore whether the armature is in local view or not.
if 'arm_local_view' in wpt and context.space_data.local_view: if 'arm_local_view' in wpt_as_dict and context.space_data.local_view:
armature.local_view_set(context.space_data, wpt['arm_local_view']) armature.local_view_set(context.space_data, wpt_as_dict['arm_local_view'])
# Remove armature from scene root collection if it was moved there. # Remove armature from scene root collection if it was moved there.
if wpt['arm_coll_assigned']: if wpt_as_dict['arm_coll_assigned']:
context.scene.collection.objects.unlink(armature) context.scene.collection.objects.unlink(armature)
return {'FINISHED'} return {'FINISHED'}
@ -187,11 +191,12 @@ class EASYWEIGHT_PT_Debug(bpy.types.Panel):
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
wm = context.window_manager
if not 'wpt' in context.screen: if not 'wpt' in wm:
return return
wpt = context.screen['wpt'].to_dict() wpt = wm['wpt'].to_dict()
for key in wpt.keys(): for key in wpt.keys():
split = layout.split(factor=0.4) split = layout.split(factor=0.4)