[Bf-python] hints for optimizing python exporters, using Blender2Radiance for eg.

Campbell Barton cbarton at metavr.com
Mon Mar 21 06:59:34 CET 2005


Hi Francesco, just had a look at Brad geometry export code.
I havnt got enough time to edit code, but thaught it might help to make 
some comments on optimizing an exporter, so others can use it too.
Looking through at what I have written it could be taken offensivly, I 
can only say that its not ment to cryt your coding style and Im just 
trying make brad better!.
points 1&2 will give the most bang for buck, probably worth commining to 
BRad when the next blender is released, only in cvs now.

I base my optimizations on my obj_export script that I have used as a 
testbed for benchmarking exporter functionality.
Worth noting then blender obj_export if older then the one on my pc :)

There are some fairly easy optimizations Id recommend, some are using 
functions I have added (with help from Willian)


      1. Dont Get Data anymore then you need to.

    First-  Line 1330, brad_radiance.py
            for obj in objs:
              if obj.getType()=="Mesh":
                  if datablocks.count(obj.getData().name)==0:
                    datablocks.append(obj.getData().name)

    This can be very slow since each obj.getData() converts an entire
    mesh into NMesh data, on large meshs this can suck up too much time.
    Instead use* obj.getData(1) ... meaning *obj.getData(1=name_only),
    this is nice because it works for all datatypes, its just that
    Mesh's arnt just thin wrappers like most of the other Types in
    blender so you want to avoid mesh.getData(), its best practice only
    calling each mesh once.

            for datablock in meshdatablocks:
                    meshdata = datablock.getData()
                    mesh = Blender.NMesh.GetRaw(meshdata.name)


      2. Transfrom mesh's using new (cvs only) mesh.transform()

    Of course this wasnt in python until 2 days ago :) but its very
    usefull and on my PC is over 100 times faster then transforming each
    vert's location and normal by a matrix within python.

    First-  Function 84, brad_radiance.py
        def applyTransformation4(self, vertex, matrix):
            vertCopy = Blender.Mathutils.CopyVec(vertex)
            vertCopy.resize4D()
            return Blender.Mathutils.VecMultMat(vertCopy, matrix)
          
    Any calls to applyTransformation4() where mesh vertex
    locations/normals are concerned (line 674, brad_radiance.py), can be
    replaced with. *mesh.transfrom(matrix)*, make sure that if you are
    only exporting locations you add a 1,* mesh.transfrom(matrix,
    1=location_only)*  This avoids un-needed vertex normal transfroming.


      3. Avoid mixing Blender pathnames with os.isfile.. open().. etc.

    Line 823 I noticed you are using an os command on a blender pathname.
    os.path.basename(face.image.getFilename())

    This can throw errors if the user has relitive paths in there filename.
    eg- an image filename may be //testures/myimage.png

    os.* functions dont understand blenders // as being the cerrent file
    dir in blender.

    The following function provides a workaround.

    #===================#
    # Path Functions    #
    #===================#
    def stripFileFromPath(file): # Unix only?
        return '/'.join(file.split('/')[:-1])+'/'
       
    def rel2absPath(pathName):
        if pathName.startswith('//'):
            filename = Get('filename')
            print filename
            filename = stripFileFromPath(filename)
            return filename + pathName[2:]
           
        else:
            return pathName



      4. Misc stuff, you probably alredy know.

    Avoid using *try/except*, in recursive code *if/thens* are a LOT
    faster, The try on brad_radiance.py line 822 could slow the export
    process if there are a few missing images.
    for face in mesh.faces:
        ...
        for uvco in face.uv:
           ...
            try:
            img
    =Blender.Image.Get(os.path.basename(face.image.getFilename()))
    A possible solution would be to loop through all blenders images and
    tag images that are missing. then this can be used to see if the
    image is there.

    Avoid string adding when recursive writes, this will only be
    marginly faster Im guessing  between 2 & 10%?
    objfile.write("vt " + str(uvco[0]*(float(w)/float(h))) + " " +
    str(uvco[1]) + "\n")

    rather
    objfile.write("v %f %f %f\n" % (x, y, z))
    I see you have done both, It might be worth going thriough all
    recursive writes and converting the format.


      5. Be ware of python hogging memory.

    You may ignore this point though It has forced my into having 4 gig
    of ram, so I take it seriously :).

    A List in python (and therefor blender) is never? deallocated,
    python can re-usethe memory whilst the script is running.

    Basicly making large lists in python and a lot of recursive variable
    assignment can leak memory that can only be regained witha restart.
    - *If you can- avoid making large lists.

    This is Fixed in Python 2.4rc1, and will compile with blender.

    *

**Hope this helps :)
- Cam

Francesco Anselmo wrote:

>Hi Cam!
>
>I was thinking of writing you to ask about measuring tools
>in Blender, but you've been faster than me!!!
>
>Happy to hear from you!
>
>I don't think you'll find anything special in my scripts,
>I've put them together without knowing very much about Python
>programming at the beginning, and the Blender Python API
>has improved a lot and I still haven't had the time to study
>it properly.
>
>The Radiance users are very interested about the exporter,
>so I'm trying to get more people involved into its development,
>especially from a lighting designer perspective (improve calculation
>methods, add pdf/html report export, enhance libraries, etc.).
>
>I'm sure you will give me a lot of good feedback about it.
>
>So you're still travelling around the world ...
>
>See you!
>
>Francesco
>
>On Mon, 2005-01-10 at 01:47, cpbarton at iinet.net.au wrote:
>  
>
>>Hi Francesco, Good to here from you!, remember me from Bconf??? I did the OBJ 
>>i/o (not like its a big deal of anything)
>>
>>Id love to have a look at your script, I have been developing a set of generic 
>>impoerter/exporter functions that do commonly done tasks in an optimal way OR 
>>manage corner cases better then blender does. so Ill have a look at your code 
>>and see if there are any improvements.
>>Im in sunny Boston right now so I cant realy do this yet but look fwd to 
>>kicking your code about, BTW I was very impressed with your exporter and am 
>>not infering that you code is bad or anything like that.
>>
>>Any Boston blender users out there :), 
>>Oh well, see you guy's later...
>>- Cam
>>
>>    
>>
>
>
>_______________________________________________
>Bf-python mailing list
>Bf-python at projects.blender.org
>http://projects.blender.org/mailman/listinfo/bf-python
>
>
>  
>


-- 
Campbell J Barton

133 Hope Street
Geelong West, Victoria 3218 Australia

URL:    http://www.metavr.com
e-mail: cbarton at metavr.com
phone: AU (03) 5229 0241




More information about the Bf-python mailing list