# How to toggle between Flat and Smooth shading during animation?

The Edge Split modifier doesn't give me desired results (even with angle set to 0 it looks different).

• What do you mean it looks different? Can you post a screenshot comparing the two? There isn't a way to insert keyframes for Smooth/Flat shading, so edge split is your only choice. – CharlesL Jul 6 '13 at 17:49

# Animatable (custom property) toggle

import bpy

value = self["use_smooth"]
if self.polygons[0].use_smooth is not value:
for poly in self.polygons:
poly.use_smooth = value

def frame_handler(scene):
for mesh in bpy.data.objects.data.meshes:
try:
except KeyError:
pass

bpy.types.Mesh.use_smooth = bpy.props.BoolProperty(
bpy.app.handlers.frame_change_pre.append(frame_handler)

1. Paste the script above in the Text Editor, click Register and Run Script.
• Text block name must end with ".py" to activate the register functionality (run script on load).
2. In the Properties Window, under Object Data, Add a custom property with Property Name use_smooth and Property Value of 0.
• You can now animate this value the toggle smooth shading.

• Thank you, guys! The second one seems to better suits my needs. But I can't force it to work. img849.imageshack.us/img849/5430/cjzv.jpg – Sielan Jul 7 '13 at 8:08
• @Sielan, You added the property to Object not Object Data. – Aldrik Jul 7 '13 at 11:20
• Wow, thanks! Now it works! :) BTW. Is there a way to run this script automatically each time when I load the scene? – Sielan Jul 7 '13 at 16:55
• @Sielan, that's what the Register check-box does. This feature requires the name of the text block to end with ".py". – Aldrik Jul 7 '13 at 17:24
• – iKlsR Jul 7 '13 at 19:18

# I still think that edge split is the way to go:

• Make sure edge split is the last modifier.
• Make sure that it is smooth shaded without it.
• Set edge split angle to zero and check the angle.
• Add desired keyframes for the angle checkbox.

# Frame Six:

Let me preface this with saying that this method won't let you set key-frames with the UI, Aldrik's approach does.

My method lends itself better to a procedural approach to animation where key-framing isn't important. This script accepts a list of start-end ranges, which map to the frame numbers that should be shaded flat or smooth.

• Hit run on this script, then start the animation render

script:

import bpy

def list_to_frames(ranges):
k = set()
for begin, finish in ranges:
k = k | {i for i in range(begin, finish)}
return k

def my_handler(scene):
frame = scene.frame_current

obj = bpy.data.objects[obj_name]

# this assumes you are OK with doing a full sweep
# and setting all to either smooth or flat.
if not frame in frames:
if obj.data.polygons[0].use_smooth == True:
return
for poly in obj.data.polygons:
poly.use_smooth = True
else:
if obj.data.polygons[0].use_smooth == False:
return
for poly in obj.data.polygons:
poly.use_smooth = False