Line Art in Cycles via Python Script

Hey all,
I have finally achieved node materials which along with cycles give me the cartoon / manga look that I am after. The shading is great, and now I have a method for creating the line art outlines just like I want them. Something like BI edges, but better. It goes something like this.

Setup your mesh with a toon shaded material
Dup mesh and scale it out (shift-S) 0.01
Apply a material which is transparent, ink color on the backside, and only renders on direct camera rays and only on the backside.

So, I can do this, and it works great, but now I want to animate it. This means I have to work with 2x as many vertices as I really need, and I have to worry about armature parenting, layer collisions and so on. Not ideal, just to get edges…

Finally: Question?
Can I write a python script which will do this for me at final render for each frame. I can program, that’s not the question. The question is regarding the capabilities of the Blender Python API.

  1. Is there a pre-render per frame hook which would call my script at the right time?
  2. Is it possible (I assume this should be easy) to dup / scale / apply material to an object.

Sorry, I’m pretty new to blender, so I hope this makes some sense.


Moved from “General Forums > Blender and CG Discussions” to “Coding > Python Support”

nihildeb, any chance of a video/picture based tutorial on this? I’m wanting to use 3d for my backgrounds and props to supplement my hand-drawn comic pages… so this would be tremendously helpful.

All what you are doing is possible to be replicated/automated via scripting.
Have a look at the python api in general
and in particular into handlers:
there you can simply hook your stuff into and do on frame change/pre render or something alon those lines.

If you have problems join #blenderpython for scripting help or #blendercoders for more general blender coding help (on freenode).

Thanks very much testscreenings, guess I’ll start reading then :slight_smile:

4tonmantis: I’m pretty new here, and don’t think I’m really in a position to be teaching. I can tell you what helped me though

Here is a very tunable node material setup for getting the toon shading. I used a couple tweaks of my own, but it’s based on this:

For the line art, I can’t find the link anymore but it’s like this:
select your mesh, go into edit mode, duplicate all your verts and Shift-S to scale out from normal.
apply a node material which is like this

Geometry - Backfacing -> Math - Minimum
LightPath - Is Camera Ray -> Math - Minimum
Math - Minimum -> Mix Shader - Fac
Transparent BSDF -> Mix Shader - Shader1
Translucent BSDF (ink color) -> Mix Shader - Shader2
Mix Shader - Shader Out -> Material - Shader

That’s it. I wish I could find the link to credit the guy, but it seems to be buried in the internets

Yes. render_pre apphandler:

You can execute whatever after adding the handler before each render, and can also make a render_post deleting the scaled duplicates again.


thanks arexma, I hadn’t considered the fact that the duped verts would be in the next frame. thanks for the tip. render_pre/post is exactly what I’m looking for then :slight_smile: