2
$\begingroup$

I am having a really hard time trying to create a diffuse colored plane by script. The contours of the plane I do get right, but I can't seem to get the surface filled.

g_d = bpy.data.meshes.new(name="GroundM")
g_o = bpy.data.objects.new(name="GroundO", object_data = g_d)
scene.objects.link(g_o)
g_o.location = (0,0,0)
g_o.select = True
scene.objects.active = g_o

# Fill in the data for the mesh
b = 10
v1 = (-b,b,0)
v2 = (b,b,0)
v3 = (b,-b,0)
v4 = (-b,-b,0)
verts = [v1, v2, v3, v4]

g_d.vertices.add(4)
g_d.edges.add(4)
g_d.faces.add(1)

g_d.vertices[0].co = (-b,b,0)
g_d.vertices[1].co = (b,b,0)
g_d.vertices[2].co = (b,-b,0)
g_d.vertices[3].co = (-b,-b,0)

g_d.edges[0].vertices = (0,1)
g_d.edges[1].vertices = (1,2)
g_d.edges[2].vertices = (2,3)
g_d.edges[3].vertices = (3,0)

g_d.faces[0].vertices = (0,1,2,3)

As a result I get the edges of a square, and an error message "Mesh object has no attribute "faces"." But one should be able to define faces in a mesh..

$\endgroup$
1
  • $\begingroup$ Even if changing faces to polygons, g_d.polygons[0].vertices = (0,1,2,3) will not populate the g_d.polygons[0].vertices collection, nor throw an error. $\endgroup$
    – batFINGER
    Commented Oct 2, 2017 at 20:22

2 Answers 2

3
$\begingroup$

Trivial case with bpy.ops.mesh.primitive_plane_add()

import bpy
context = bpy.context

b = 10
bpy.ops.mesh.primitive_plane_add(radius=b)
g_o = context.object
g_o.name = "GroundO"
g_d = g_o.data
g_d.name = "GroundM"

Use bmesh Strongly recommend getting familiar with bmesh as soon as possible. Trying to use the mesh methods (esp. to create), other than from_pydata(..) as pointed out in answer from @atomicbezierslinger, will drive you nuts.

import bpy
import bmesh
scene = bpy.context.scene
g_d = bpy.data.meshes.new(name="GroundM")
g_o = bpy.data.objects.new(name="GroundO", object_data = g_d)
scene.objects.link(g_o)
g_o.location = (0,0,0)
g_o.select = True
scene.objects.active = g_o

bm = bmesh.new()
bm.from_mesh(g_d)
# Fill in the data for the mesh
b = 10
v1 = (-b,b,0)
v2 = (b,b,0)
v3 = (b,-b,0)
v4 = (-b,-b,0)
verts = [v1, v2, v3, v4]
bmverts = [bm.verts.new(co) for co in verts]

bm.faces.new(bmverts)
bm.to_mesh(g_d)
scene.update()

or easier still with bmesh.ops.create_grid(...)

import bpy
import bmesh
scene = bpy.context.scene
g_d = bpy.data.meshes.new(name="GroundM")
g_o = bpy.data.objects.new(name="GroundO", object_data = g_d)
scene.objects.link(g_o)
g_o.location = (0,0,0)
g_o.select = True
scene.objects.active = g_o

bm = bmesh.new()
bm.from_mesh(g_d)
# Fill in the data for the mesh
b = 10
bmesh.ops.create_grid(bm, x_segments=1, y_segments=1, size=b)
bm.to_mesh(g_d)
scene.update()
$\endgroup$
1
$\begingroup$

See polygons

enter image description here

  • Above is the console inquiry for a plane in a variable p.

  • from_pydata is a convenience function

enter image description here

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .