[Bf-committers] Render Plugin Development

Jonathan Merritt j.merritt at pgrad.unimelb.edu.au
Thu Aug 16 03:21:02 CEST 2007

Campbell Barton wrote:
> Jonathan Merritt wrote:
>> Campbell Barton wrote:
>>> Can you give an example of why a C render plugin would need to call a 
>>> python script?
>> I'd imagine that one use for this could be procedural generation of 
>> render output.
>> <snip>
> This still dosnt make sense to me, are you saying the render API should 
> provide some special function to run a python script? - C can alredy do 
> this without too much trouble.

Well, following the RenderMan example, the problem is that the renderer 
itself doesn't know about Python.  Remember that RenderMan renderers can 
be linked directly into a compiled binary.

Let's say I have some C code (the render plugin), and I want to 
implement a piece of procedural geometry using the standard procedural 
interface provided by PRMan or Aqsis (or ...).  To do this, I'd create 
two functions in my C program:
    void refineproc(RtPointer data, RtFloat levelofdetail);
    void freeproc(RtPointer data);
The purpose of the refineproc() function is to spit out more geometry 
(which can also be procedural in nature), while freeproc() is called at 
the end to free any working data.  To instantiate the geometry, I'd then 
    RiProcedural(data, bound, refineproc, freeproc)
data is an arbitrary pointer that the refineproc() uses to generate the 
geometry, and bound is a bounding box.

What I'm suggesting is that the render plugin would have a *single* 
refineproc() and a *single* freeproc().  The data pointer passed to 
these functions would refer to a struct that specifies which Python 
script to call, and with what arguments.  Thus, the external renderer 
could end up calling right back into a Python script inside Blender.

The Python scripts would then need a way to pass their generated 
geometry back down to the renderer.  Again, this must be done by calling 
Ri functions which are provided by the renderer itself.  So, the easiest 
way is to bind those Ri functions to Python.  The de-facto standard 
RenderMan interface in Python is provided by CGKit 
(cgkit.sourceforge.net), so it would be a case of implementing that 
interface as a binding to the render API.

To make all of this a clean interface, the render API would need to 
provide links to be able to call Python scripts.  As you mention, the 
plugin could try digging around in Blender's internals to get everything 
it needs to be able to invoke a script.  However, this would be a Bad 
Thing (TM).

I should also mention that procedural geometry can be implemented as a 
Python script *outside* Blender, which is run by invoking an external 
instance of the Python interpreter.  This method already works well for 
Aqsis (there are two examples in the Aqsis test suite).  However, the 
dis-advantage here is that the external Python script must have all of 
its data passed to it, while a Python script executed inside Blender 
could use the Blender Python API to get everything it needs... much more 
powerful and flexible.

Regardless, all of this is hypothetical, and a long-term idea.  I'm not 
sure it really matters at all to Aaron's current work. :-)

Jonathan Merritt.

More information about the Bf-committers mailing list