3
$\begingroup$

I have a handful of mesh objects and I'd like to display one of them per frame. Thus far, I've been switching out entire mesh objects like this:

myObject.data = meshes[frame_number]

Unfortunately, the object's shader is tied to each mesh, so when I change the mesh, the shader disappears.

I know that I can use myMesh.from_pydata() to edit the actual mesh, but I'm not quite sure how to get the vertices, edges, and faces out of a mesh and copy them into another. I tried this, but got an error:

#gives an error:
myMesh.from_pydata(otherMesh.vertices, otherMesh.edges, otherMesh.polygons)

Any ideas on how I can swap meshes but retain the object's shader?

$\endgroup$
1
  • $\begingroup$ Answered the why does from_pydata give an error part of your question. Can you elaborate on the shader? $\endgroup$
    – batFINGER
    Commented Jun 4, 2016 at 7:58

1 Answer 1

3
$\begingroup$

One way is, as you are doing, Swapping the mesh

ob1.data = mesh # assign mesh to ob1
ob1.data = mesh.copy() # assign a copy of the mesh to ob1

how to use another meshes verts, edges and polygons to pass to the mesh.from_pydata method.

cone = context.object # some other object
mymesh = bpy.data.meshes.new("mymesh")
vertices = [v.co for v in cone.data.vertices]
faces = [[v for v in p.vertices] for p in cone.data.polygons]
# let faces take care of edges
mymesh.from_pydata(vertices=vertices, edges=[], faces=faces)
# could make edges
edges = [[v for v in e.vertices] for e in cone.data.edges]

Using from_pydata isn't going to fix the shader issue.

$\endgroup$
2
  • $\begingroup$ This worked for a brand-new mesh. Why doesn't this work for an existing mesh? I got the error: Traceback (most recent call last): File "<blender_console>", line 1, in <module> File "C:\Program Files\Blender Foundation\Blender\2.77\scripts\modules\bpy_types.py", line 417, in from_pydata self.vertices.foreach_set("co", vertices_flat) RuntimeError: internal error setting the array $\endgroup$
    – Justin
    Commented Jun 6, 2016 at 18:04
  • $\begingroup$ It's designed to use on a new mesh, otherwise the indices won't match between verts and edges / faces, passed to method and those already existing in mesh. If you want to replace with a new mesh via from_pydata you will need to clear the old one first. $\endgroup$
    – batFINGER
    Commented Jun 7, 2016 at 3:38

You must log in to answer this question.

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