[Bf-python] Object/Mesh constructors

Campbell Barton ideasman42 at gmail.com
Sat Jul 17 00:13:38 CEST 2010


Take a look at the script which adds a torus in blender 2.5, I tried
to keep this a fairly simple example which you can use to base scripts
off.

A search for torus in the api docs shows this.

http://www.blender.org/documentation/250PythonDoc/bpy.ops.mesh.html?highlight=torus#bpy.ops.mesh.primitive_torus_add

File and line number referenced in docs.

On Fri, Jul 16, 2010 at 5:56 PM, David Jagoe <davidjagoe at gmail.com> wrote:
> Hi Campbell,
>
> On 8 July 2010 15:36, Campbell Barton <ideasman42 at gmail.com> wrote:
>> mesh = bpy.data.meshes.new(name="Foo")
>> obj = bpy.data.objects.new(name="Foo", object_data=mesh)
>> bpy.context.scene.objects.link(obj)
>
> Thanks for your quick response, this seems to be exactly what I'm
> after. Your example seems to work as expected, and creates a mesh with
> 0 vertices. I have been experimenting with adding vertices and faces -
> so far without success. I came across the following code:
>
> #               me = bpy.data.meshes.new()
> #               ob = sce.objects.new(me)
>
> #               me.verts.extend([V(bb[0]).resize3D(), V(bb[1]).resize3D(),
> V(bb[2]).resize3D(), V(bb[3]).resize3D()])
> #               me.faces.extend([(0,1,2,3),])
> #               me.verts.extend([V(uv[0]).resize3D(), V(uv[1]).resize3D(),
> V(uv[2]).resize3D()])
> #               me.faces.extend([(4,5,6),])
>
> Which doesn't work in blender 2.5:
>
> Traceback (most recent call last):
>  File "<console>", line 1, in <module>
> AttributeError: 'bpy_prop_collection' object has no attribute 'extend'
>
>
> So I tried the following:
>
>>>> mesh.add_geometry(verts=4, edges=4, faces=1)
>
> After which I can manipulate the vertices but there are no faces or edges:
>
>>>> mesh.faces
> <bpy_collection[0], Mesh.faces>
>
>>>> mesh.edges
> <bpy_collection[0], Mesh.edges>
>
> Any advice? My final goal is to end up with a rectangular plane in
> that same way as doing Add > Mesh > Plane in the GUI would do.
>
>
> Thanks again.
> Cheers,
> David
>
>>
>> On Thu, Jul 8, 2010 at 2:57 PM, David Jagoe <davidjagoe at gmail.com> wrote:
>>> G'day all,
>>>
>>> Is there a better way to add an object than using bpy.ops.object_add
>>> or (e.g) bpy.ops.mesh.primitive_plane_add?
>>>
>>> I've tried to use bpy.types.Mesh.
>>>
>>> Ideally I would like something like a normal factory or constructor
>>> method that returns the object to me so that I can manipulate it
>>> further. I imagine that such a facility must exist but I can't seem to
>>> find it.
>>>
>>>
>>> Thanks,
>>> David
>>> _______________________________________________
>>> Bf-python mailing list
>>> Bf-python at blender.org
>>> http://lists.blender.org/mailman/listinfo/bf-python
>>>
>>
>>
>>
>> --
>> - Campbell
>> _______________________________________________
>> Bf-python mailing list
>> Bf-python at blender.org
>> http://lists.blender.org/mailman/listinfo/bf-python
>>
> _______________________________________________
> Bf-python mailing list
> Bf-python at blender.org
> http://lists.blender.org/mailman/listinfo/bf-python
>



-- 
- Campbell



More information about the Bf-python mailing list