Skip to content

Commit

Permalink
Updated Onion Skin material handling
Browse files Browse the repository at this point in the history
- Updated logic for handling materials of Onion Skin object

- The addon will now update the onion skin objects material and colors
  appropriately, similar to how it would create the material

- Updated logic of clearing onion skin. Disabling and Enabling of the
  Onion Skin feature caused onion objects with similar name to be
  created, causing blender to rename it with an incremental number.
  This has been fixed by purging unused data when the onion skin is
  disabled
  • Loading branch information
aldrinmathew committed Feb 17, 2022
1 parent 80e9669 commit 9760858
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
51 changes: 46 additions & 5 deletions stopmagic/functions/handle_onion_skin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def handle_onion_skin(dummy):
#
# Onion Skin objects visibility
#
pobject.color = bpy.context.scene.stopmagic_past_color
fobject.color = bpy.context.scene.stopmagic_future_color
pmaterial = bpy.data.materials.get("stopmagic_material_past")
fmaterial = bpy.data.materials.get("stopmagic_material_future")
if pmaterial is None:
Expand All @@ -89,11 +91,13 @@ def handle_onion_skin(dummy):
].default_value = bpy.context.scene.stopmagic_past_color
opacity = bpy.context.scene.stopmagic_past_color[3]
if opacity > 0 and opacity < 1:
p_principled.inputs[21].default_value = bpy.context.scene.stopmagic_past_color[3]
p_principled.inputs[
21
].default_value = bpy.context.scene.stopmagic_past_color[3]
else:
p_principled.inputs[21].default_value = 0.3
pmaterial.diffuse_color = bpy.context.scene.stopmagic_past_color
pmaterial.blend_method = 'HASHED'
pmaterial.blend_method = "HASHED"
if fmaterial is None:
fmaterial = bpy.data.materials.new(name="stopmagic_material_future")
fobject.active_material = fmaterial
Expand All @@ -108,11 +112,13 @@ def handle_onion_skin(dummy):
].default_value = bpy.context.scene.stopmagic_future_color
opacity = bpy.context.scene.stopmagic_future_color[3]
if opacity > 0 and opacity < 1:
f_principled.inputs[21].default_value = bpy.context.scene.stopmagic_future_color[3]
f_principled.inputs[
21
].default_value = bpy.context.scene.stopmagic_future_color[3]
else:
f_principled.inputs[21].default_value = 0.3
fmaterial.diffuse_color = bpy.context.scene.stopmagic_future_color
fmaterial.blend_method = 'HASHED'
fmaterial.blend_method = "HASHED"
#
# Check and collect meshes within the range specified by the user
#
Expand Down Expand Up @@ -239,16 +245,51 @@ def change_onion_color(past: bool, color: bpy.props.FloatVectorProperty):
if past:
pobject = get_past_object(past_exists)
pobject.color = list(color)
pmaterial = bpy.data.materials.get("stopmagic_material_past")
if pmaterial is not None:
pmaterial.diffuse_color = color
if not pmaterial.use_nodes:
pmaterial.use_nodes = True
if pmaterial.node_tree.get("Principled BSDF") is not None:
p_principled = pmaterial.node_tree.get("Principled BSDF")
p_principled.inputs[0] = list(color)
opacity = color[3]
if opacity > 0 and opacity < 1:
p_principled.inputs[21].default_value = color[3]
else:
p_principled.inputs[21].default_value = 0.3
else:
fobject = get_future_object(future_exists)
fobject.color = list(color)
fmaterial = bpy.data.materials.get("stopmagic_material_future")
if fmaterial is not None:
fmaterial.diffuse_color = color
if not fmaterial.use_nodes:
fmaterial.use_nodes = True
if fmaterial.node_tree.get("Principled BSDF") is not None:
f_principled = fmaterial.node_tree.get("Principled BSDF")
f_principled.inputs[0] = list(color)
opacity = color[3]
if opacity > 0 and opacity < 1:
f_principled.inputs[21].default_value = color[3]
else:
f_principled.inputs[21].default_value = 0.3


def clear_onion_data():
collection = bpy.data.collections.get("Stopmagic")
pmaterial = bpy.data.materials.get("stopmagic_material_past")
fmaterial = bpy.data.materials.get("stopmagic_material_future")
if pmaterial is not None:
bpy.data.materials.remove(pmaterial, do_unlink=True)
if fmaterial is not None:
bpy.data.materials.remove(fmaterial, do_unlink=True)
if collection.get("objects") is not None:
for obj in collection.objects:
mesh = obj.data
bpy.data.objects.remove(obj, do_unlink=True)
obj.name = "removed"
bpy.context.scene.objects.unlink(obj)
bpy.data.meshes.remove(mesh, do_unlink=True)
bpy.data.objects.remove(obj, do_unlink=True)
bpy.data.collections.remove(collection)
bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=False, do_recursive=False)
6 changes: 3 additions & 3 deletions stopmagic/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def register_properties() -> None:
description="Change the type of display for the onion skin",
options=set(),
default=get_preferences(__package__).onion_display_type,
update=handle_display
update=handle_display,
)
bpy.types.Scene.stopmagic_past_count = bpy.props.IntProperty(
name="Poses",
Expand All @@ -212,7 +212,7 @@ def register_properties() -> None:
soft_min=1,
soft_max=10,
step=1,
update=handle_display
update=handle_display,
)
bpy.types.Scene.stopmagic_past_offset = bpy.props.IntProperty(
name="Offset",
Expand All @@ -238,7 +238,7 @@ def register_properties() -> None:
soft_min=1,
soft_max=10,
step=1,
update=handle_display
update=handle_display,
)
bpy.types.Scene.stopmagic_future_offset = bpy.props.IntProperty(
name="Offset",
Expand Down

0 comments on commit 9760858

Please sign in to comment.