[Bf-committers] Proper way to handle Libdata and ListBases?

Ken Hughes khughes at pacific.edu
Tue Oct 18 23:26:24 CEST 2005


Don't know of a better place to ask this, since it's not a real BPython 
questions....

I'm trying to add "copy from object" functionality to the Mesh type, 
similar to NMesh.GetRawFromObject().  I'm trying to reuse existing 
Blender code, mainly from convert (ALT-C) and duplicate (SHIFT-D).  My 
goal is to take an existing mesh and replace it with a copy from another 
object:

import Blender

me = Blender.Mesh.Get('oldmesh')
me.getFromObject('Font')

The problem is I also want to have
(1) all objects linked to the mesh get updated automatically
(2) all BPython objects derived from the mesh get updated automatically
(3) not leave any extra data allocated
(4) have the mesh's ID (most specifically the name) remain the same

My solution (currently for curves and fonts) is to create a temporary 
mesh from the object, copy its data only (not its ID) into the original 
mesh, and delete the temp mesh.  The code below mimics ALT-C for a 
curve/font:

      ID tmpid;
      void *olddata;
      Mesh *mesh = ((BPy_Mesh *)self)->mesh;
      /* make a temp object for the curve */
      Object *tmpobj = alloc_libblock( &( G.main->object ), ID_OB, "" );
      tmpobj->id.us = 1;
      tmpobj->flag = 0;
      tmpobj->type = OB_CURVE; /* or OB_FONT */

      /* copy original curve */
      tmpobj->data = copy_curve( (Curve *) ob->data );

      /* convert curve object to a temp mesh object */
      makeDispListCurveTypes( tmpobj, 0 );
      nurbs_to_mesh( tmpobj );

      /* BEGIN QUESTIONABLE CODE */
      /* free all old mesh data */
      free_mesh( self->mesh );

      /* copy temp mesh to old, except for ID */
      tmpid = self->mesh->id;
      memcpy( self->mesh, tmpobj->data, sizeof( Mesh ) );
      self->mesh->id= tmpid;

      /* delete temp mesh and temp object */
      olddata = tmpobj->data;
      free_libblock_us( &G.main->object, tmpobj );
      BLI_remlink( &G.main->mesh, olddata );
      MEM_freeN( olddata );
      /* END QUESTIONABLE CODE */


This works, I don't get any strange runtime error messages, if I link 
the resulting mesh to another object the usage count goes to 2 as it 
should, the temporary mesh doesn't appear in the list of materials from 
the UI, etc.   But is this a good (or correct) way to handle this?  I've 
  read Ton's "Blender Architecture" webpage and think I understand 
better the underlying organization, but then again maybe I don't :-)

Ken








More information about the Bf-committers mailing list