made scn.objects more flexible... you can now things like...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 28 Dec 2006 11:09:36 +0000 (11:09 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 28 Dec 2006 11:09:36 +0000 (11:09 +0000)
scn.objects.selected = [] # deselect all
scn.objects.selected = scn.objects # select all
scn.objects.context = [ob1, ob2...]

Added epydoc examples and updates importer scripts to use this de-select-all method.

release/scripts/3ds_import.py
release/scripts/animation_trajectory.py
release/scripts/bvh_import.py
release/scripts/import_obj.py
release/scripts/lightwave_import.py
release/scripts/mesh_edges2curves.py
source/blender/python/api2_2x/Scene.c
source/blender/python/api2_2x/Scene.h
source/blender/python/api2_2x/doc/Scene.py

index 4c37b75b02a07ce733d0946a407e83db633cadac..8014186b637de885d70a0140c3ba7deaa0cc4946 100644 (file)
@@ -794,8 +794,7 @@ def load_3ds(filename, PREF_UI= True):
        
        scn= Scene.GetCurrent()
        SCN_OBJECTS = scn.objects
-       for ob in SCN_OBJECTS:
-               ob.sel= 0
+       SCN_OBJECTS.selected = [] # de select all
        
        importedObjects= [] # Fill this list with objects
        process_next_chunk(file, current_chunk, importedObjects)
index ba20ee68a970825a5e3c48d90de934c7ab0c9ea2..0bdcd719c063d14aee05080e37239b3040f81544 100644 (file)
@@ -548,9 +548,8 @@ Blender.Text.unlink(scripting)
                                                        #Go to frame that correspond to selected "vertex"
                                                        Blender.Set('curframe', TestFRAME[k])
                                                        
-                                                       #un select all objects
-                                                       for _ob in Blender.Object.GetSelected():
-                                                               _ob.sel= False
+                                                       scene.objects.selected = [] #un select all objects
+                                                       
                                                        #FIXED TestLIST[j][0].sel=0, but no j. So ob.sel and above variable changed in obj
                                                        ob.sel= True
                                                        Blender.Run('Edit_Trajectory')
index 2993cd088a7f26947f4a5c2cce35180387ec7b6a..30e42095d242ec872c725cfc3c57c7997ee8f820 100644 (file)
@@ -311,8 +311,7 @@ def bvh_node_dict2objects(bvh_nodes, IMPORT_START_FRAME= 1):
                IMPORT_START_FRAME= 1
                
        scn= Blender.Scene.GetCurrent()
-       for ob in scn.objects:
-               ob.sel= 0
+       scn.objects.selected = []
        
        objects= []
        
@@ -444,14 +443,10 @@ def bvh_node_dict2armature(bvh_nodes, IMPORT_START_FRAME= 1):
        
        scn= Blender.Scene.GetCurrent()
        
-       for ob in scn.objects:
-               ob.sel= 0
+       scn.objects.selected = []
        
        scn.link(arm_ob)
-       arm_ob.sel= 1
-       arm_ob.Layers= scn.Layers
-       
-       
+       scn.objects.context = [arm_ob]
        
        # Now Apply the animation to the armature
        
index ed379ac52abea2caae4fd6beb21d232f85259aa5..e611669d1c9d44ce03cc1c770f7e3b4a848e8f2d 100644 (file)
@@ -672,7 +672,7 @@ def load_obj(filepath, CLAMP_SIZE= 0.0, CREATE_FGONS= True, CREATE_SMOOTH_GROUPS
        
        
        # deselect all
-       for ob in Scene.GetCurrent().objects: ob.sel= False
+       Scene.GetCurrent().objects.selected = []
        new_objects= [] # put new objects here
        
        print '\tbuilding geometry;\n\tverts:%i faces:%i materials: %i smoothgroups:%i ...' % ( len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups) ),
index b80498d455b719163e28e7889f672fe01e8ef329..0cf544e174405868a492009520acbfe80abf7eea 100644 (file)
@@ -222,8 +222,7 @@ def read(filename):
        tobj.logcon ("Importing file:")
        tobj.logcon (filename)
 
-       for ob in Blender.Scene.GetCurrent().objects:
-               ob.sel= 0
+       Blender.Scene.GetCurrent().objects.selected = []
        
        start = Blender.sys.time()
        file = open(filename, "rb")
index 9c8007b00544f28e845202cb053aa7fbcc1cd11b..70be7524b5cb100bf34d5d5f44bc9218e6c0febe 100644 (file)
@@ -118,8 +118,8 @@ def polysFromMesh(me):
 
 def mesh2polys():
        scn= Scene.GetCurrent()
-       for ob in scn.objects:
-               ob.sel= 0
+       scn.objects.selected = []
+       
        meshOb= scn.objects.active
        if meshOb==None or meshOb.type != 'Mesh':
                Draw.PupMenu( 'ERROR: No Active Mesh Selected, Aborting' )
index ee254df8bc17f62c45c4df00bd06deccdad71c5b..8e737f1f4969747d1986cb2e466cc1421a7d68bb 100644 (file)
@@ -83,6 +83,14 @@ PyObject *M_Object_Get( PyObject * self, PyObject * args ); /* from Object.c */
 #define SCENE_DEL_CHECK_PY(bpy_scene) if (!(bpy_scene->scene)) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "Scene has been removed" ) )
 #define SCENE_DEL_CHECK_INT(bpy_scene) if (!(bpy_scene->scene)) return ( EXPP_ReturnIntError( PyExc_RuntimeError, "Scene has been removed" ) )
 
+
+enum obj_consts {
+       EXPP_OBSEQ_NORMAL = 0,
+       EXPP_OBSEQ_SELECTED,
+       EXPP_OBSEQ_CONTEXT,
+};
+
+
 /*-----------------------Python API function prototypes for the Scene module--*/
 static PyObject *M_Scene_New( PyObject * self, PyObject * args,
                              PyObject * keywords );
@@ -1118,6 +1126,71 @@ static PyObject *SceneObSeq_getObjects( BPy_SceneObSeq *self, void *mode)
        return SceneObSeq_CreatePyObject(self->bpyscene, NULL, (int)((long)mode));
 }
 
+int SceneObSeq_setObjects( BPy_SceneObSeq *self, PyObject *value, void *_mode_) 
+{
+       /*
+       ONLY SUPPORTS scn.objects.selected and scn.objects.context 
+       cannot assign to scn.objects yet!!!
+       */
+       PyObject *item;
+       Scene *scene= self->bpyscene->scene;
+       Object *blen_ob;
+       Base *base;
+       int size, mode = (int)_mode_;
+       
+       SCENE_DEL_CHECK_INT(self->bpyscene);
+       
+       /* scn.objects.selected = scn.objects  - shortcut to select all */
+       if (BPy_SceneObSeq_Check(value)) {
+               BPy_SceneObSeq *bpy_sceneseq = (BPy_SceneObSeq *)value;
+               if (self->bpyscene->scene != bpy_sceneseq->bpyscene->scene)
+                       return EXPP_ReturnIntError( PyExc_ValueError,
+                                       "Cannot assign a SceneObSeq type from another scene" );
+               if (bpy_sceneseq->mode != EXPP_OBSEQ_NORMAL)
+                       return EXPP_ReturnIntError( PyExc_ValueError,
+                                       "Can only assign scn.objects to scn.objects.context or scn.objects.selected" );
+               
+               for (base= scene->base.first; base; base= base->next) {
+                       base->flag |= SELECT;
+                       base->object->flag |= SELECT;
+                       
+                       if (mode==EXPP_OBSEQ_CONTEXT && G.vd) {
+                               base->object->lay= base->lay= G.vd->lay;
+                       }
+               }
+               return 0;
+       }
+       
+       if (!PySequence_Check(value))
+               return EXPP_ReturnIntError( PyExc_ValueError,
+                               "Error, must assign a sequence of objects to scn.objects.selected" );
+       
+       /* for context and selected, just deselect, dont remove */
+       for (base= scene->base.first; base; base= base->next) {
+               base->flag &= ~SELECT;
+               base->object->flag &= ~SELECT;
+       }
+       
+       size = PySequence_Length(value);
+       while (size) {
+               size--;
+               item = PySequence_GetItem(value, size);
+               if ( PyObject_TypeCheck(item, &Object_Type) ) {
+                       blen_ob= ((BPy_Object *)item)->object;
+                       base = object_in_scene( blen_ob, scene );
+                       if (base) {
+                               blen_ob->flag |= SELECT;
+                               base->flag |= SELECT;
+                               if (mode==EXPP_OBSEQ_CONTEXT && G.vd) {
+                                       blen_ob->lay= base->lay= G.vd->lay;
+                               }
+                       }
+               }
+               Py_DECREF(item);
+       }
+       return 0;
+}
+
 
 static PyObject *SceneObSeq_CreatePyObject( BPy_Scene *self, Base *iter, int mode )
 {
@@ -1133,9 +1206,9 @@ static int SceneObSeq_len( BPy_SceneObSeq * self )
        Scene *scene= self->bpyscene->scene;
        SCENE_DEL_CHECK_INT(self->bpyscene);
        
-       if (self->mode == 0) /* all obejcts */
+       if (self->mode == EXPP_OBSEQ_NORMAL)
                return BLI_countlist( &( scene->base ) );
-       else if (self->mode == 1) { /* selected obejcts */
+       else if (self->mode == EXPP_OBSEQ_SELECTED) {
                int len=0;
                Base *base;
                for (base= scene->base.first; base; base= base->next) {
@@ -1144,7 +1217,7 @@ static int SceneObSeq_len( BPy_SceneObSeq * self )
                        }
                }
                return len;
-       } else if (self->mode == 2) { /* user context */
+       } else if (self->mode == EXPP_OBSEQ_CONTEXT) {
                int len=0;
                Base *base;
                
@@ -1176,15 +1249,15 @@ static PyObject *SceneObSeq_item( BPy_SceneObSeq * self, int i )
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
        /* objects */
-       if (self->mode==0)
+       if (self->mode==EXPP_OBSEQ_NORMAL)
                for (base= scene->base.first; base && i!=index; base= base->next, index++) {}
        /* selected */
-       else if (self->mode==1)
+       else if (self->mode==EXPP_OBSEQ_SELECTED)
                for (base= scene->base.first; base && i!=index; base= base->next)
                        if (base->flag & SELECT)
                                index++;
        /* context */
-       else if (self->mode==2)
+       else if (self->mode==EXPP_OBSEQ_CONTEXT)
                if (G.vd)
                        for (base= scene->base.first; base && i!=index; base= base->next)
                                if ((base->flag & SELECT) && (base->lay & G.vd->lay))
@@ -1233,10 +1306,10 @@ static PyObject *SceneObSeq_getIter( BPy_SceneObSeq * self )
        
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
-       if (self->mode==1) /* selected */
+       if (self->mode==EXPP_OBSEQ_SELECTED)
                while (base && !(base->flag & SELECT))
                        base= base->next;
-       else if (self->mode==2) { /* context */
+       else if (self->mode==EXPP_OBSEQ_CONTEXT) {
                if (!G.vd)
                        base= NULL; /* will never iterate if we have no */
                else
@@ -1269,10 +1342,10 @@ static PyObject *SceneObSeq_nextIter( BPy_SceneObSeq * self )
        object= Object_CreatePyObject( self->iter->object ); 
        base= self->iter->next;
        
-       if (self->mode==1) /* selected */
+       if (self->mode==EXPP_OBSEQ_SELECTED)
                while (base && !(base->flag & SELECT))
                        base= base->next;
-       else if (self->mode==2) { /* context */
+       else if (self->mode==EXPP_OBSEQ_CONTEXT) {
                if (!G.vd)
                        base= NULL; /* will never iterate if we have no */
                else
@@ -1280,7 +1353,6 @@ static PyObject *SceneObSeq_nextIter( BPy_SceneObSeq * self )
                                base= base->next;       
        }
        self->iter= base;
-       
        return object;
 }
 
@@ -1290,7 +1362,7 @@ static PyObject *SceneObSeq_link( BPy_SceneObSeq * self, PyObject *pyobj )
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
        /* this shold eventually replace Scene_link */
-       if (self->mode != 0)
+       if (self->mode != EXPP_OBSEQ_NORMAL)
                return (EXPP_ReturnPyObjError( PyExc_TypeError,
                                              "Cannot link to objects.selection or objects.context!" ));        
        
@@ -1317,7 +1389,7 @@ static PyObject *SceneObSeq_new( BPy_SceneObSeq * self, PyObject *args )
        
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
-       if (self->mode != 0)
+       if (self->mode != EXPP_OBSEQ_NORMAL)
                return EXPP_ReturnPyObjError( PyExc_TypeError,
                                        "Cannot add new to objects.selection or objects.context!" );    
 
@@ -1455,16 +1527,16 @@ typeError:
 
 }
 
-
 static PyObject *SceneObSeq_unlink( BPy_SceneObSeq * self, PyObject *args )
 {
        PyObject *pyobj;
        Object *blen_ob;
+       Scene *scene;
        Base *base= NULL;
        
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
-       if (self->mode != 0)
+       if (self->mode != EXPP_OBSEQ_NORMAL)
                return (EXPP_ReturnPyObjError( PyExc_TypeError,
                                              "Cannot add new to objects.selection or objects.context!" ));     
        
@@ -1473,23 +1545,25 @@ static PyObject *SceneObSeq_unlink( BPy_SceneObSeq * self, PyObject *args )
                                "expected a python object as an argument" ) );
        
        blen_ob = ( ( BPy_Object * ) pyobj )->object;
-
+       
+       scene = self->bpyscene->scene;
+               
        /* is the object really in the scene? */
-       base = object_in_scene( blen_ob, self->bpyscene->scene );
+       base = object_in_scene( blen_ob, scene);
 
-       if( base ) {            /* if it is, remove it */
+       if( base ) { /* if it is, remove it */
                /* check that there is a data block before decrementing refcount */
-               if( (ID *)blen_ob->data )
-                       ((ID *)blen_ob->data)->us--;
+               if( (ID *)base->object->data )
+                       ((ID *)base->object->data)->us--;
                else if( blen_ob->type != OB_EMPTY )
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-                                             "Object has no data!" );
+                                                 "Object has no data!" );
                
-               if (self->bpyscene->scene->basact==base)
-                       self->bpyscene->scene->basact= NULL;    /* in case the object was selected */
+               if (scene->basact==base)
+                       scene->basact= NULL;    /* in case the object was selected */
                
-               BLI_remlink( &(self->bpyscene->scene)->base, base );
-               blen_ob->id.us--;
+               BLI_remlink( &scene->base, base );
+               base->object->id.us--;
                MEM_freeN( base );
        }
        Py_RETURN_NONE;
@@ -1503,7 +1577,7 @@ PyObject *SceneObSeq_getActive(BPy_SceneObSeq *self)
        
        SCENE_DEL_CHECK_PY(self->bpyscene);
        
-       if (self->mode!=0)
+       if (self->mode!=EXPP_OBSEQ_NORMAL)
                        return (EXPP_ReturnPyObjError( PyExc_TypeError,
                                                "cannot get active from objects.selected or objects.context" ));
        
@@ -1526,7 +1600,7 @@ static int SceneObSeq_setActive(BPy_SceneObSeq *self, PyObject *value)
        
        SCENE_DEL_CHECK_INT(self->bpyscene);
        
-       if (self->mode!=0)
+       if (self->mode!=EXPP_OBSEQ_NORMAL)
                        return (EXPP_ReturnIntError( PyExc_TypeError,
                                                "cannot set active from objects.selected or objects.context" ));
        
@@ -1586,10 +1660,10 @@ static PyObject *SceneObSeq_repr( BPy_SceneObSeq * self )
 {
        if( !(self->bpyscene->scene) )
                return PyString_FromFormat( "[Scene ObjectSeq Removed]" );
-       else if (self->mode==1)
+       else if (self->mode==EXPP_OBSEQ_SELECTED)
                return PyString_FromFormat( "[Scene ObjectSeq Selected \"%s\"]",
                                                self->bpyscene->scene->id.name + 2 );
-       else if (self->mode==2)
+       else if (self->mode==EXPP_OBSEQ_CONTEXT)
                return PyString_FromFormat( "[Scene ObjectSeq Context \"%s\"]",
                                                self->bpyscene->scene->id.name + 2 );
        
@@ -1600,13 +1674,13 @@ static PyObject *SceneObSeq_repr( BPy_SceneObSeq * self )
 
 static PyGetSetDef SceneObSeq_getseters[] = {
        {"selected",
-        (getter)SceneObSeq_getObjects, (setter)NULL,
+        (getter)SceneObSeq_getObjects, (setter)SceneObSeq_setObjects,
         "sequence of selected objects",
-        (void *)1},
+        (void *)EXPP_OBSEQ_SELECTED},
        {"context",
-        (getter)SceneObSeq_getObjects, (setter)NULL,
+        (getter)SceneObSeq_getObjects, (setter)SceneObSeq_setObjects,
         "sequence of user context objects",
-        (void *)2},
+        (void *)EXPP_OBSEQ_CONTEXT},
        {"active",
         (getter)SceneObSeq_getActive, (setter)SceneObSeq_setActive,
         "active object",
index ee4c45739831ea0cad47a2d67ae60f54c7c5a720..4d294700c63e25369dde83f581ad80c7c83025fb 100644 (file)
@@ -42,6 +42,8 @@ extern PyTypeObject SceneObSeq_Type;
 
 #define BPy_Scene_Check(v) \
     ((v)->ob_type == &Scene_Type)
+#define BPy_SceneObSeq_Check(v) \
+    ((v)->ob_type == &SceneObSeq_Type)
 
 /*---------------------------Python BPy_Scene structure definition----------*/
 typedef struct {
@@ -63,7 +65,7 @@ typedef struct {
 
 PyObject *Scene_Init( void );
 PyObject *Scene_CreatePyObject( Scene * cam );
-Scene *Scene_FromPyObject( PyObject * pyobj );
+/*Scene *Scene_FromPyObject( PyObject * pyobj );*/  /* not used yet */
 int Scene_CheckPyObject( PyObject * pyobj );
 Scene *GetSceneByName( char *name );
 
index 2ada306282660b683f40e1f926d52e61d4a3ebe0..4fd9f896edcccbcb9caba8042f80d89f4bf742a0 100644 (file)
@@ -315,6 +315,27 @@ class SceneObjects:
   =========================================
    This object gives access to the Objects in a Scene in Blender.
 
+   Example::
+     from Blender import Scene
+     scn = Scene.GetCurrent()
+     
+     scn.objects.selected = [] # select none
+     scn.objects.selected = scn.objects # select all
+     scn.objects.context = scn.objects # select all and move into the scenes display layer
+     
+     # get a list of mesh objects
+     obs = [ob for ob in scn.objects if ob.type == 'Mesh']
+     
+     # Select only these mesh objects
+     scn.objects.selected = obs
+     
+     # print all object names
+     for ob in scn.objects: print ob.name
+     
+     # make a list of objects that you can add and remove to
+     # will not affect the current scene
+     scene_obs = list(scn.objects)
+
   @ivar selected: an iterator over all the selected objects in a scene.
   @type selected: sequence of L{Object}
   @ivar context: an iterator over all the visible selected objects in a scene.