Toon Material Conversion Script

TOON MATERIAL CONVERSION SCRIPT

This is something I made for a project I was working on. Maybe someone can find it useful too :slight_smile:

This script quickly converts all materials in a currently loaded .blend file to the diffuse and specular types of Toon, resulting “toon shaded” render results.

The script also revises specific diffuse/specular settings (e.g. spec size, smooth) for all materials, allowing for more uniform control over all current shaders.

Instructions: Load a test .blend scene, load this script in a text window, edit values in script, then run the script (ALT P with mouse over script text window).

Check Blender Console window for script messages. When script is done, render your scene. You will need to edit materials and lighting later to achieve best results.

If you tweak any of the changeable values in the script, rerun it and render again to see the changes applied to all materials. Afterwards, you can manually edit individual material settings as you normally would in Blender.

Editable settings and valid value ranges are listed in the script below.

Download the script here or copy/paste the script below into a text file called toonMatConvert.py:


# TOON MATERIAL BATCH CONVERSION SCRIPT
#
# BY: ROBERTT
# DATE CREATED: JANUARY 2006
# SCRIPT VERSION: 1.0
# DESIGNED/TESTED ON BLENDER VERSION: 2.40
#
# WHAT THIS BLENDER PYTHON SCRIPT CAN DO:
#
# THIS SCRIPT CHANGES *ALL* MATERIALS IN YOUR
# CURRENTLY OPEN BLEND FILE TO TOON MATERIALS.
# AFTER EXECUTING, CHECK THE BLENDER CONSOLE
# FOR SCRIPT OUTPUT.
#
# AS THIS SCRIPT ALTERS *ALL* OF YOUR MATERIAL
# SETTINGS IN YOUR CURRENTLY OPENED BLEND FILE,
# YOU SHOULD *NOT* RUN IT ON A PROJECT THAT HAS
# NOT BEEN SAVED YET!  CHANGES MADE TO MATERIALS
# WHILE USING THIS SCRIPT CANNOT BE UNDONE!
#
# NOTE: CHANGES ARE NOT PERMANENTLY COMMITED
# TO YOUR BLEND FILE UNLESS YOU CHOOSE TO SAVE
# YOUR FILE AFTER EXECUTING THIS SCRIPT.
#
# USAGE TERMS:  USE THIS SCRIPT FREELY,
# AT YOUR OWN RISK, AND ADAPT AS YOU WISH.


import Blender

from Blender import Material, Scene
from Blender.Scene import Render

print "
TOON MATERIAL CONVERSION SCRIPT V1.0 STARTED...
"

# Get list of active materials from Blender
materials = Blender.Material.Get()

# Get render information needed for edge setting
scn = Scene.GetCurrent()
context = scn.getRenderingContext()

print "PROGRESS: CONVERTING ALL MATERIALS TO TOON TYPE..."

# Change materials to Toon Diffuse/Specular
for m in materials:

	# Diffuse Shader (2 = Toon)
	m.setDiffuseShader(2)
	
	# Specular Shader (3 = Toon)
	m.setSpecShader(3)

	# THE FOLLOWING SETTINGS CAN
	# BE CHANGED TO DIFFERENT
	# VALUES WITHIN THE SPECIFIED
	# RANGE OF ACCEPTABLE NUMBERS:

	# Diffuse Size (0 to 3.14)
	m.setDiffuseSize(1.5)

	# Diffuse Smooth (0 to 1.0)
	m.setDiffuseSmooth(.5)

	# Reflect Amount (0 to 1.0)
	# - optionally here to help you
	# with any necessary batch changes
	# to all material reflection values
	# Remove "#" from line below to use:
	# m.setRef(.75)

	# Specular (0 to 2.0)
	m.setSpec(.3)

	# Specular Smooth (0 to 1.0)
	m.setSpecSmooth(.5)

	# Specular Size (0 to 3.14)
	m.setSpecSize(.4)

	# Enable toon edge: 0 = off, 1 = on
	context.enableToonShading(1)

	# Edge Intension (0 to 255)
	context.edgeIntensity(30)

print "PROGRESS: CONVERSION FINISHED!
TWEAK MATERIALS AND LIGHTING AS NECESSARY."

Blender.Redraw()

RobertT

Do you mind if I include this in the cookbook?
better still, you add it in :slight_smile:

http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Cookbook

Thanks cambo :slight_smile: I added it:

http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Cookbook#Toon_Material_Batch_Conversion_Script

RobertT

You think you can post some previews?
P

Here’s some sample output using the default values. Your mileage will vary depending on the lighting of your scene and other factors. Part of the fun/challenge of this script is to experiment with settings (in the script and in Blender) to see what kinds of results you can get.

In the example below, I disabled ambient occlusion and replaced lights used in the first blend with a simple spotlight. I also deactivated raytraced reflections in the materials for the toon render.

The input image is actually an entry of mine for a recent CGTalk challenge (model provided by them), for which I created the lighting, shaders, and render.

Image hosted at imageshack:

http://img294.imageshack.us/img294/9895/toonscriptexamplerjt20062jj.jpg

(alternate direct link to image)

RobertT