Treeform's feature requests

Could you show me a sample? I tried getting this to work and always failed and you always say its possible.

#!/usr/bin/env python
import cairo
import pygame
import array
import math
import sys

import direct.directbase.DirectStart
from pandac.PandaModules import *
props = WindowProperties()
props.setSize(512,512)
base.win.requestProperties(props)
screen = loader.loadModel('plane.egg')
screen.setScale(2)
Width, Height = 512, 512

cairoTexture = Texture()
cairoTexture.setFormat(cairoTexture.FRgba8)
screen.setTexture(cairoTexture)
screen.reparentTo(render2d)
cairoTexture.setXSize(Width)
cairoTexture.setYSize(Height)

ptu_data = cairoTexture.newSimpleRamImage(Width,Height)
# start of the fail code?
array_data = array.array('c', ptu_data.getData())
print len(array_data)

for i in range(Width*Height):
    array_data[i] = chr(200)
 
def draw(surface):
    x,y, radius = (250,250, 200)
    ctx = cairo.Context(surface)
    ctx.set_line_width(15)
    ctx.arc(x, y, radius, 0, 2.0 * math.pi)
    ctx.set_source_rgb(0.8, 0.8, 0.8)
    ctx.fill_preserve()
    ctx.set_source_rgb(1, 1, 1)
    ctx.stroke()

    
surface = cairo.ImageSurface.create_for_data (
    array_data, 
    cairo.FORMAT_ARGB32,
    Width, 
    Height, 
    Width*4)

 
def drawall(task):
    draw(surface)
    # fail code after this line::::
    #print cairoTexture.getExpectedRamImageSize()
    #cairoTexture.setRamImage(how(ptu_data))
    return task.cont    

taskMgr.add(drawall, 'draw')
run()

Something like:

buffer = PTAUchar.emptyArray(len(string))
buffer.setData(string)
tex = Texture()
tex.setup2DTexture(see apiref)
tex.setRamImage(buffer)

That does not work with the sample above.

In what way does it fail? The code looks fine to me, and I do that sort of thing all the time.

David

This is the code that works for anyone interested.

#!/usr/bin/env python
import cairo
import pygame
import array
import math
import sys

import direct.directbase.DirectStart
from pandac.PandaModules import *
props = WindowProperties()
props.setSize(512,512)
base.win.requestProperties(props)
screen = loader.loadModel('./plane.egg.pz')
screen.setScale(2)
Width, Height = 512, 512

cairoTexture = Texture()
cairoTexture.setFormat(cairoTexture.FRgba8)
cairoTexture.setup2dTexture(Width, Height, Texture.CMDefault, Texture.FRgba)

screen.setTexture(cairoTexture)
screen.reparentTo(render2d)

def draw(surface):
    x,y, radius = (250,250, 200)
    ctx = cairo.Context(surface)
    ctx.set_line_width(15)
    ctx.arc(x, y, radius, 0, 2.0 * math.pi)
    ctx.set_source_rgb(0.8, 0.8, 0.8)
    ctx.fill_preserve()
    ctx.set_source_rgb(1, 1, 1)
    ctx.stroke()


surface = cairo.ImageSurface.create(
    cairo.FORMAT_ARGB32,
    Width,
    Height)


def drawall(task):
    draw(surface)
    cairoTexture.setRamImage(surface.get_data())
    return task.cont

taskMgr.add(drawall, 'draw')
run()

And if you want transparency around the circle:

#!/usr/bin/env python
import cairo
import pygame
import array
import math
import sys

import direct.directbase.DirectStart
from pandac.PandaModules import *
props = WindowProperties()
props.setSize(1024,768)
base.setBackgroundColor(0,1,1,1)
base.cam.setPos(0,-20,0)
base.win.requestProperties(props)
screen = loader.loadModel('./plane.egg.pz')
screen.setTransparency(TransparencyAttrib.MAlpha)
screen.setTwoSided(True)
screen.setScale(2)
Width, Height = 512, 512

cairoTexture = Texture()
cairoTexture.setFormat(cairoTexture.FRgba8)
cairoTexture.setup2dTexture(Width, Height, Texture.CMDefault, Texture.FRgba32)

screen.setTexture(cairoTexture)
screen.reparentTo(render)


def draw(surface):
    x,y, radius = (250,250, 200)
    ctx = cairo.Context(surface)
    ctx.set_operator(cairo.OPERATOR_CLEAR)
    ctx.rectangle(0.0, 0.0, Width, Height)
    ctx.fill()
    ctx.set_operator(cairo.OPERATOR_OVER)
    ctx.set_line_width(15)
    ctx.arc(x, y, radius, 0, 2.0 * math.pi)
    ctx.set_source_rgba(0.8, 0.8, 0.8)
    ctx.fill_preserve()
    ctx.set_source_rgba(1, 1, 1)
    ctx.stroke()


surface = cairo.ImageSurface (
    cairo.FORMAT_ARGB32,
    Width,
    Height)


def drawall(task):
    draw(surface)
    cairoTexture.setRamImage(surface.get_data())
    return task.cont

taskMgr.add(drawall, 'draw')
run()

EDIT: Removed some unnecessary bits to make it less confusing/more efficient.