8
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – CharlesL Jul 6 '13 at 17:49
8
$\begingroup$

Animatable (custom property) toggle

import bpy

def toggle_shading(self, context=bpy.context):
    """Toggle smooth shading"""
    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):
    """Update shading on frame change"""
    for mesh in bpy.data.objects.data.meshes:
        try:
            toggle_shading(mesh)
        except KeyError:
            pass

bpy.types.Mesh.use_smooth = bpy.props.BoolProperty(
    description=toggle_shading.__doc__, update=toggle_shading)
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.

Added property

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Sielan Jul 7 '13 at 8:08
  • $\begingroup$ @Sielan, You added the property to Object not Object Data. $\endgroup$ – Aldrik Jul 7 '13 at 11:20
  • $\begingroup$ Wow, thanks! Now it works! :) BTW. Is there a way to run this script automatically each time when I load the scene? $\endgroup$ – Sielan Jul 7 '13 at 16:55
  • $\begingroup$ @Sielan, that's what the Register check-box does. This feature requires the name of the text block to end with ".py". $\endgroup$ – Aldrik Jul 7 '13 at 17:24
  • $\begingroup$ @Sielan feel free to accept this as the answer that worked when you are ready. $\endgroup$ – iKlsR Jul 7 '13 at 19:18
7
$\begingroup$

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 One:

Frame One

Frame Six:

Frame Six

$\endgroup$
4
$\begingroup$

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

frames_to_flatshade = [[20,30],[45,54]]
obj_name = 'Cube'
frames = list_to_frames(frames_to_flatshade)

bpy.app.handlers.frame_change_pre.append(my_handler)

Here's a compacted version of this script

$\endgroup$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.