Added scn.objects.new(obbdata) as a way of adding object data to a scene and createing the object data at once.

This functionality will probably be moved later on after discussion.
This commit is contained in:
Campbell Barton 2006-08-27 16:27:34 +00:00
parent b39f4b788d
commit 5dbc4c5f8f
5 changed files with 206 additions and 46 deletions

View File

@ -129,9 +129,9 @@ static PyObject *BPy_Group_copy( BPy_Group * self )
* Python BPy_Object attributes
*
************************************************************************/
static PyObject *M_Group_getObjects( BPy_Group * self )
static PyObject *Group_getObjects( BPy_Group * self )
{
BPy_MGroupObSeq *seq = PyObject_NEW( BPy_MGroupObSeq, &MGroupObSeq_Type);
BPy_GroupObSeq *seq = PyObject_NEW( BPy_GroupObSeq, &GroupObSeq_Type);
seq->bpygroup = self; Py_INCREF(self);
return (PyObject *)seq;
}
@ -151,7 +151,7 @@ static void add_to_group_wraper(Group *group, Object *ob) {
}
/* only for internal use Blender.Group.Get("MyGroup").objects= []*/
static int M_Group_setObjects( BPy_Group * self, PyObject * args )
static int Group_setObjects( BPy_Group * self, PyObject * args )
{
int i, list_size;
Group *group;
@ -171,7 +171,7 @@ static int M_Group_setObjects( BPy_Group * self, PyObject * args )
add_to_group_wraper(group, blen_ob);
}
/*
} else if( args->ob_type == &MGroupObSeq_Type ) {
} else if( args->ob_type == &GroupObSeq_Type ) {
*/
/* todo, handle sequences here */
@ -275,7 +275,7 @@ static PyGetSetDef BPy_Group_getseters[] = {
"Number of group users",
NULL},
{"objects",
(getter)M_Group_getObjects, (setter)M_Group_setObjects,
(getter)Group_getObjects, (setter)Group_setObjects,
"objects in this group",
NULL},
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
@ -512,7 +512,7 @@ PyObject *Group_Init( void )
PyObject *submodule;
if( PyType_Ready( &Group_Type ) < 0 )
return NULL;
if( PyType_Ready( &MGroupObSeq_Type ) < 0 )
if( PyType_Ready( &GroupObSeq_Type ) < 0 )
return NULL;
submodule = Py_InitModule3( "Blender.Group", M_Group_methods,
@ -636,10 +636,10 @@ static PyObject *Group_repr( BPy_Group * self )
*
************************************************************************/
/*
* create a thin MGroupOb object
* create a thin GroupOb object
*/
static PyObject *MGroupObSeq_CreatePyObject( Group *group, int i )
static PyObject *GroupObSeq_CreatePyObject( Group *group, int i )
{
int index=0;
PyObject *bpy_obj;
@ -661,25 +661,25 @@ static PyObject *MGroupObSeq_CreatePyObject( Group *group, int i )
}
static int MGroupObSeq_len( BPy_MGroupObSeq * self )
static int GroupObSeq_len( BPy_GroupObSeq * self )
{
return BLI_countlist( &( self->bpygroup->group->gobject ) );
}
/*
* retrive a single MGroupOb from somewhere in the GroupObex list
* retrive a single GroupOb from somewhere in the GroupObex list
*/
static PyObject *MGroupObSeq_item( BPy_MGroupObSeq * self, int i )
static PyObject *GroupObSeq_item( BPy_GroupObSeq * self, int i )
{
return MGroupObSeq_CreatePyObject( self->bpygroup->group, i );
return GroupObSeq_CreatePyObject( self->bpygroup->group, i );
}
static PySequenceMethods MGroupObSeq_as_sequence = {
( inquiry ) MGroupObSeq_len, /* sq_length */
static PySequenceMethods GroupObSeq_as_sequence = {
( inquiry ) GroupObSeq_len, /* sq_length */
( binaryfunc ) 0, /* sq_concat */
( intargfunc ) 0, /* sq_repeat */
( intargfunc ) MGroupObSeq_item, /* sq_item */
( intargfunc ) GroupObSeq_item, /* sq_item */
( intintargfunc ) 0, /* sq_slice */
( intobjargproc ) 0, /* sq_ass_item */
( intintobjargproc ) 0, /* sq_ass_slice */
@ -688,7 +688,7 @@ static PySequenceMethods MGroupObSeq_as_sequence = {
/************************************************************************
*
* Python MGroupObSeq_Type iterator (iterates over GroupObjects)
* Python GroupObSeq_Type iterator (iterates over GroupObjects)
*
************************************************************************/
@ -696,17 +696,17 @@ static PySequenceMethods MGroupObSeq_as_sequence = {
* Initialize the interator index
*/
static PyObject *MGroupObSeq_getIter( BPy_MGroupObSeq * self )
static PyObject *GroupObSeq_getIter( BPy_GroupObSeq * self )
{
self->iter = self->bpygroup->group->gobject.first;
return EXPP_incr_ret ( (PyObject *) self );
}
/*
* Return next MGroupOb.
* Return next GroupOb.
*/
static PyObject *MGroupObSeq_nextIter( BPy_MGroupObSeq * self )
static PyObject *GroupObSeq_nextIter( BPy_GroupObSeq * self )
{
PyObject *object;
if( !(self->iter) || !(self->bpygroup->group) )
@ -719,7 +719,7 @@ static PyObject *MGroupObSeq_nextIter( BPy_MGroupObSeq * self )
}
static PyObject *MGroupObSeq_append( BPy_MGroupObSeq * self, PyObject *args )
static PyObject *GroupObSeq_add( BPy_GroupObSeq * self, PyObject *args )
{
PyObject *pyobj;
Object *blen_ob;
@ -739,7 +739,7 @@ static PyObject *MGroupObSeq_append( BPy_MGroupObSeq * self, PyObject *args )
static PyObject *MGroupObSeq_remove( BPy_MGroupObSeq * self, PyObject *args )
static PyObject *GroupObSeq_remove( BPy_GroupObSeq * self, PyObject *args )
{
PyObject *pyobj;
Object *blen_ob;
@ -770,40 +770,40 @@ static PyObject *MGroupObSeq_remove( BPy_MGroupObSeq * self, PyObject *args )
}
static struct PyMethodDef BPy_MGroupObSeq_methods[] = {
{"append", (PyCFunction)MGroupObSeq_append, METH_VARARGS,
static struct PyMethodDef BPy_GroupObSeq_methods[] = {
{"add", (PyCFunction)GroupObSeq_add, METH_VARARGS,
"add object to group"},
{"remove", (PyCFunction)MGroupObSeq_remove, METH_VARARGS,
{"remove", (PyCFunction)GroupObSeq_remove, METH_VARARGS,
"remove object from group"},
{NULL, NULL, 0, NULL}
};
/************************************************************************
*
* Python MGroupObSeq_Type standard operations
* Python GroupObSeq_Type standard operations
*
************************************************************************/
static void MGroupObSeq_dealloc( BPy_MGroupObSeq * self )
static void GroupObSeq_dealloc( BPy_GroupObSeq * self )
{
Py_DECREF(self->bpygroup);
PyObject_DEL( self );
}
/*****************************************************************************/
/* Python MGroupObSeq_Type structure definition: */
/* Python GroupObSeq_Type structure definition: */
/*****************************************************************************/
PyTypeObject MGroupObSeq_Type = {
PyTypeObject GroupObSeq_Type = {
PyObject_HEAD_INIT( NULL ) /* required py macro */
0, /* ob_size */
/* For printing, in format "<module>.<name>" */
"Blender MGroupObSeq", /* char *tp_name; */
sizeof( BPy_MGroupObSeq ), /* int tp_basicsize; */
"Blender GroupObSeq", /* char *tp_name; */
sizeof( BPy_GroupObSeq ), /* int tp_basicsize; */
0, /* tp_itemsize; For allocation */
/* Methods to implement standard operations */
( destructor ) MGroupObSeq_dealloc,/* destructor tp_dealloc; */
( destructor ) GroupObSeq_dealloc,/* destructor tp_dealloc; */
NULL, /* printfunc tp_print; */
NULL, /* getattrfunc tp_getattr; */
NULL, /* setattrfunc tp_setattr; */
@ -813,7 +813,7 @@ PyTypeObject MGroupObSeq_Type = {
/* Method suites for standard classes */
NULL, /* PyNumberMethods *tp_as_number; */
&MGroupObSeq_as_sequence, /* PySequenceMethods *tp_as_sequence; */
&GroupObSeq_as_sequence, /* PySequenceMethods *tp_as_sequence; */
NULL, /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */
@ -847,11 +847,11 @@ PyTypeObject MGroupObSeq_Type = {
/*** Added in release 2.2 ***/
/* Iterators */
( getiterfunc) MGroupObSeq_getIter, /* getiterfunc tp_iter; */
( iternextfunc ) MGroupObSeq_nextIter, /* iternextfunc tp_iternext; */
( getiterfunc) GroupObSeq_getIter, /* getiterfunc tp_iter; */
( iternextfunc ) GroupObSeq_nextIter, /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/
BPy_MGroupObSeq_methods, /* struct PyMethodDef *tp_methods; */
BPy_GroupObSeq_methods, /* struct PyMethodDef *tp_methods; */
NULL, /* struct PyMemberDef *tp_members; */
NULL, /* struct PyGetSetDef *tp_getset; */
NULL, /* struct _typeobject *tp_base; */

View File

@ -38,10 +38,10 @@
/* The Group PyTypeObject defined in Group.c */
extern PyTypeObject Group_Type;
extern PyTypeObject MGroupObSeq_Type;
extern PyTypeObject GroupObSeq_Type;
#define BPy_Group_Check(v) ((v)->ob_type == &Group_Type)
#define BPy_MGroupObSeq_Check(v) ((v)->ob_type == &MGroupObSeq_Type)
#define BPy_GroupObSeq_Check(v) ((v)->ob_type == &GroupObSeq_Type)
/*****************************************************************************/
/* Python BPy_Group structure definition. */
@ -57,7 +57,7 @@ typedef struct {
PyObject_VAR_HEAD /* required python macro */
BPy_Group *bpygroup; /* link to the python group so we can know if its been removed */
GroupObject *iter; /* so we can iterate over the objects */
} BPy_MGroupObSeq;
} BPy_GroupObSeq;
PyObject *Group_Init( void );
PyObject *Group_CreatePyObject( struct Group *group );

View File

@ -38,10 +38,13 @@ struct View3D;
#include "BKE_main.h"
#include "MEM_guardedalloc.h" /* for MEM_callocN */
#include "DNA_screen_types.h" /* SPACE_VIEW3D, SPACE_SEQ */
#include "DNA_userdef_types.h" /* U.userdefs */
#include "DNA_object_types.h" /* SceneObSeq_new */
#include "BKE_depsgraph.h"
#include "BKE_library.h"
#include "BKE_scene.h"
#include "BLI_blenlib.h"
#include "BKE_font.h"
#include "BLI_blenlib.h" /* only for SceneObSeq_new */
#include "BSE_drawview.h" /* for play_anim */
#include "BSE_headerbuttons.h" /* for copy_scene */
#include "BIF_drawscene.h" /* for set_scene */
@ -49,8 +52,21 @@ struct View3D;
#include "BIF_screen.h" /* curarea */
#include "mydevice.h" /* for #define REDRAW */
#include "DNA_view3d_types.h"
/* python types */
#include "Object.h"
#include "Camera.h"
/* only for SceneObSeq_new */
#include "BKE_material.h"
#include "BLI_arithb.h"
#include "Armature.h"
#include "Lamp.h"
#include "Curve.h"
#include "NMesh.h"
#include "Mesh.h"
#include "Lattice.h"
#include "Metaball.h"
#include "Text3d.h"
#include "gen_utils.h"
#include "sceneRender.h"
#include "sceneRadio.h"
@ -795,7 +811,7 @@ static PyObject *Scene_link( BPy_Scene * self, PyObject * args )
base->lay = object->lay;
base->flag = object->flag;
object->id.us += 1; /* incref the object user count in Blender */
//object->id.us += 1; /* incref the object user count in Blender */
BLI_addhead( &scene->base, base ); /* finally, link new base to scene */
}
@ -1209,11 +1225,154 @@ static PyObject *SceneObSeq_nextIter( BPy_SceneObSeq * self )
static PyObject *SceneObSeq_add( BPy_SceneObSeq * self, PyObject *pyobj )
{
if( !(self->bpyscene->scene) )
return (EXPP_ReturnPyObjError( PyExc_RuntimeError,
"Blender Scene was deleted!" ));
/* this shold eventually replace Scene_link */
return Scene_link(self->bpyscene, pyobj);
}
/* This is buggy with new object data not alredy linked to an object, for now use the above code */
static PyObject *SceneObSeq_new( BPy_SceneObSeq * self, PyObject *args )
{
void *data = NULL;
int type;
struct Object *object;
Base *base;
PyObject *py_data;
Scene *scene= self->bpyscene->scene;
if( !(scene) )
return (EXPP_ReturnPyObjError( PyExc_RuntimeError,
"Blender Scene was deleted!" ));
if( !PyArg_ParseTuple( args, "O", &py_data ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected an object as argument" );
if( ArmatureObject_Check( py_data ) ) {
data = ( void * ) PyArmature_AsArmature((BPy_Armature*)py_data);
type = OB_ARMATURE;
} else if( Camera_CheckPyObject( py_data ) ) {
data = ( void * ) Camera_FromPyObject( py_data );
type = OB_CAMERA;
} else if( Lamp_CheckPyObject( py_data ) ) {
data = ( void * ) Lamp_FromPyObject( py_data );
type = OB_LAMP;
} else if( Curve_CheckPyObject( py_data ) ) {
data = ( void * ) Curve_FromPyObject( py_data );
type = OB_CURVE;
} else if( NMesh_CheckPyObject( py_data ) ) {
data = ( void * ) NMesh_FromPyObject( py_data, NULL );
type = OB_MESH;
if( !data ) /* NULL means there is already an error */
return NULL;
} else if( Mesh_CheckPyObject( py_data ) ) {
data = ( void * ) Mesh_FromPyObject( py_data, NULL );
type = OB_MESH;
} else if( Lattice_CheckPyObject( py_data ) ) {
data = ( void * ) Lattice_FromPyObject( py_data );
type = OB_LATTICE;
} else if( Metaball_CheckPyObject( py_data ) ) {
data = ( void * ) Metaball_FromPyObject( py_data );
type = OB_MBALL;
} else if( Text3d_CheckPyObject( py_data ) ) {
data = ( void * ) Text3d_FromPyObject( py_data );
type = OB_FONT;
}
/* have we set data to something good? */
if( !data )
return EXPP_ReturnPyObjError( PyExc_AttributeError,
"link argument type is not supported " );
object = alloc_libblock( &( G.main->object ), ID_OB, ((ID *)data)->name + 2 );
object->data = data;
((ID *)object->data)->us++;
object->flag = 0;
object->type = (short)type;
/* Object properties copied from M_Object_New */
/* creates the curve for the text object */
if (type == OB_FONT)
text_to_curve(object, 0);
/* transforms */
QuatOne( object->quat );
QuatOne( object->dquat );
object->col[3] = 1.0; /* alpha */
object->size[0] = object->size[1] = object->size[2] = 1.0;
object->loc[0] = object->loc[1] = object->loc[2] = 0.0;
Mat4One( object->parentinv );
Mat4One( object->obmat );
object->dt = OB_SHADED; /* drawtype*/
object->empty_drawsize= 1.0;
object->empty_drawtype= OB_ARROWS;
if( U.flag & USER_MAT_ON_OB ) {
object->colbits = -1;
}
switch ( object->type ) {
case OB_CAMERA: /* fall through. */
case OB_LAMP:
object->trackflag = OB_NEGZ;
object->upflag = OB_POSY;
break;
default:
object->trackflag = OB_POSY;
object->upflag = OB_POSZ;
}
object->ipoflag = OB_OFFS_OB + OB_OFFS_PARENT;
/* duplivert settings */
object->dupon = 1;
object->dupoff = 0;
object->dupsta = 1;
object->dupend = 100;
/* Gameengine defaults */
object->mass = 1.0;
object->inertia = 1.0;
object->formfactor = 0.4f;
object->damping = 0.04f;
object->rdamping = 0.1f;
object->anisotropicFriction[0] = 1.0;
object->anisotropicFriction[1] = 1.0;
object->anisotropicFriction[2] = 1.0;
object->gameflag = OB_PROP;
G.totobj++;
/* link to scene */
base = MEM_callocN( sizeof( Base ), "pynewbase" );
if( !base )
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"couldn't allocate new Base for object" );
base->object = object; /* link object to the new base */
base->lay= object->lay = scene->lay; /* Layer, by default visible*/
base->flag = 0;
object->id.us = 1; /* we will exist once in this scene */
BLI_addhead( &(scene->base), base ); /* finally, link new base to scene */
/* make sure data and object materials are consistent */
test_object_materials( (ID *)object->data );
return Object_CreatePyObject( object );
}
static PyObject *SceneObSeq_remove( BPy_SceneObSeq * self, PyObject *args )
{
@ -1253,9 +1412,11 @@ static PyObject *SceneObSeq_remove( BPy_SceneObSeq * self, PyObject *args )
static struct PyMethodDef BPy_SceneObSeq_methods[] = {
{"add", (PyCFunction)SceneObSeq_add, METH_VARARGS,
"add object to group"},
"add object to the scene"},
{"new", (PyCFunction)SceneObSeq_new, METH_VARARGS,
"add object data to this scene and return the object"},
{"remove", (PyCFunction)SceneObSeq_remove, METH_VARARGS,
"remove object from group"},
"remove object from the scene"},
{NULL, NULL, 0, NULL}
};
@ -1354,4 +1515,4 @@ PyTypeObject SceneObSeq_Type = {
NULL, /* PyObject *tp_subclasses; */
NULL, /* PyObject *tp_weaklist; */
NULL
};
};

View File

@ -95,6 +95,6 @@ class Group:
@ivar objects: Objects that this group uses.
This is an iterator with list like access so use list(gp.objects) if you need to use a list. (where gp is a group object).
The groups objects can be set by assigning a list or iterator of objects to the groups objects.
objects.append() and objects.remove() also work with the the objects iterator just like with lists.
objects.add() and objects.remove() also work with the the objects iterator just like with lists.
"""

View File

@ -102,8 +102,7 @@ class Scene:
scene.layers = scene.layers.append(1)
print scene.layers # will print: [1, 3]
@type objects: list of integers
@ivar objects: An iterator for the scenes objects with set like functionality,
.add() and .remove() to add and remove objects.
@ivar objects: An iterator for the scenes objects with set like functionality.
"""
def getName():