Blender For AmigaOS 4.1

Two Techniques For Creating Images Using A 'Hybrid' Blender Python / Arexx Script

This short article will describe two tecniques for combining blender with other aplications via script based automation, using python and arexx.

In this case blender and ImageFX4.5 will be used to create a final image (shown right). The task for the two script will be:

  • Render the blender default cube
  • Export the image with alpha channel
  • Generate a simple background image in ImageFX
  • Load the render exported from blender into ImageFX to create a simple composite image
The design has deliberatly been kept as simple as possible for the sake of a hopefully clear example. The scripts are "naive" in that they assume that both blender amd ImageFX have allready been started and that blender it showing the default scene.
  • Approach 1:

    In the first approach an ARexx script passes a python script to blender via it's ARexx port and then controls ImageFX to utilise the results.
    00:
    01:
    02:
    03:
    04:
    05:
    06:
    07:
    08:
    09:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    31:
    32:
    33:
    34:
    35:
    36:
    37:
    38:
    39:
    40:
    41:
    42:
    43:
    44:
    45:
    46:
    47:
    48:
    49:
    50:
        
    /* An Arexx script to render a cube in blender and pass it to ImageFX4.5 */
    
    options results
    
    NEWLINE = "*N"
    
    
    myscript = "";
    
    
    
    call buildscript("import Blender")
    call buildscript("from Blender import **")
    call buildscript("from Blender.Scene import Render")
    call buildscript("scn = Scene.GetCurrent()")
    call buildscript("context = scn.getRenderingContext()")
    call buildscript("context.extensions = True")
    call buildscript("context.renderPath = 'ram:'")
    call buildscript("context.sizePreset(Render.PC)")
    call buildscript("context.imageType = Render.PNG")
    call buildscript("context.enableRGBAColor()")
    call buildscript("context.sFrame = 2")
    call buildscript("context.eFrame = 2")
    call buildscript("context.renderAnim()")
    call buildscript("Render.CloseRenderWindow()")
    
    address BLENDER.1
    
    'RUNPYTHON "' || myscript || '"'
    
    ADDRESS IMAGEFX.1
    
    CreateBuffer 640 480 0 0 0 1 1 100 100 FORCE
    ActiveColor 2
    EdgeMode Normal 0
    EdgeMode FeatherIn 0
    EdgeMode FeatherIn 28
    FloodFill 312 227 30
    LoadLayer "RAM:0002.png" ALPHA
    
    
    exit
    
    buildscript:
    
    procedure expose myscript NEWLINE
    
    PARSE ARG line
    
    myscript = myscript || line || NEWLINE
    return ""
      

    NOTES:
    An ARexx string 'mysript' is built by repeated calls to the procedure buildscript defined at line 43:. Note that at line 12 the python expression "from Blender import *" contains a "*" so this must be "escaped" with a second "*" as the string will be passed through the dos function readargs. Any whitespace indenting required by the created python script must be included in the string!

    Lines 11 to 13 import the built in Render module in the Blender python interpreter.
    Lines 14 to 15 obtain a handle for the currentrendering context.
    Lines 16 to 20 set up the desired save format PNG with alpha channel.
    Lines 21 to 24 create a single frame animation as a convenient way of saving the rendered file.

    At line 26 we set up the ARexx host to BLENDER.1 and line 28 calls the blender arexx call RUNPYTHON, note the quotation required.

    At line 30 we swap to controlling ImageFX.

    Line 32 sets up a new buffer.
    Lines 33 to 37 creates our simple background.
    Line 38 loads the render result from blender into a layer in the current buffer.


  • Approach 2:

    In the second aproach the primary script is a python script run in the blender interpreter, making calls to ImageFX's ARexx port via the python arexx module. blendfile with script embeded.
    00:
    01:
    02:
    03:
    04:
    05:
    06:
    07:
    08:
    09:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    31:
        
    
    import Blender
    from Blender import *
    from Blender.Scene import Render
    
    import arexx
    
    # Here goes the blender side 
    
    scn = Scene.GetCurrent()
    context = scn.getRenderingContext()
    context.extensions = True
    context.renderPath = 'ram:'
    context.sizePreset(Render.PC)
    context.imageType = Render.PNG
    context.enableRGBAColor()
    context.sFrame = 2
    context.eFrame = 2
    context.renderAnim()
    Render.CloseRenderWindow()
    
    
    
    # Now for the ImageFX part
    
    arexx.dorexx('IMAGEFX.1','CreateBuffer 640 480 0 0 0 1 1 100 100 FORCE')
    arexx.dorexx('IMAGEFX.1','ActiveColor 2')
    arexx.dorexx('IMAGEFX.1','EdgeMode Normal 0')
    arexx.dorexx('IMAGEFX.1','EdgeMode FeatherIn 0')
    arexx.dorexx('IMAGEFX.1','EdgeMode FeatherIn 28')
    arexx.dorexx('IMAGEFX.1','FloodFill 312 227 30')
    arexx.dorexx('IMAGEFX.1','LoadLayer "RAM:0002.png" ALPHA')
    
    
    NOTES:
    As you can see the working code is very similar.

    At line 4 we import the arexx module. This is provided as part of the AmigaOS4 python distribution, and allows access to a prgrams ARexx host from python.

    Lines 8 to 18 implement the same python code as before, but this time directly in python, so obviously no escaping required.

    Lines 24 to 30 perform the ImageFX ARexx calls using the dorexx method. The first argument is the port address exactly as woukld be set by the 'ADDRESS' instruction in ARexx. The second argument is the arexx command that you wnt to pass to the host.


If you wish to encourage me in developement of this project you can donate via paypal. Please leave a message or send me an associated email to andy@broad.ology.org.uk