4
$\begingroup$

If I create a plane, and want to subdivide it, I can issue the Python command:

>>> bpy.ops.mesh.subdivide()

But if I want to subdivide 20 times, using:

>>> for i in range(20):
...   bpy.ops.mesh.subdivide()

this causes Blender to get stuck in an infinite loop, eventually taking up 75% of all available RAM.

Why would this happen, and how can I execute said loop?

$\endgroup$
0

3 Answers 3

12
$\begingroup$

Well, one problem I can think of is that you're asking blender to subdivide a mesh by cutting everything in half 20 times in a row. That's 2**20 = around 1 million cuts on every edge, meaning a square face would be cut into 1 trillion faces.

If this is really what you want, you can do it in a single step using a single

bpy.ops.mesh.subdivide(number_cuts=1048576)

My suspicion is that this is not really what you want to do. Mostly because I can't think of any machines that can stash the vertices and face loops for several trillion faces.

$\endgroup$
5
$\begingroup$

Try subdividing a plane 20 times manually by successively hitting W ->Subdivide. You won't get past iteration 9 (or 10, 11.. maybe 12) before your machine starts to break out in a sweat. This isn't so much a Python problem as misunderstanding about what subdivide does.

The default single cut per subdivide turns each face into 4 faces. here a short snippet to print out the numbers involved.

prev = [1]

for i in range(20):
    last = prev[-1] * 4    
    print(i+1, prev[-1], '->', last)
    prev.append(last)

outputs (here -> means subdivide):

1 1 -> 4
2 4 -> 16
3 16 -> 64
4 64 -> 256
5 256 -> 1024
6 1024 -> 4096
7 4096 -> 16384
8 16384 -> 65536
9 65536 -> 262144
10 262144 -> 1048576
11 1048576 -> 4194304
12 4194304 -> 16777216
13 16777216 -> 67108864
14 67108864 -> 268435456
15 268435456 -> 1073741824
16 1073741824 -> 4294967296
17 4294967296 -> 17179869184
18 17179869184 -> 68719476736
19 68719476736 -> 274877906944
20 274877906944 -> 1099511627776

after 20 iterations you would have 1 099 511 627 776 faces.

$\endgroup$
2
$\begingroup$

As the other answers mention, you are subidiving a progressively subdivided mesh over and over, which makes polycount grow exponentially. If you want to subdivide 20 times, the proper code would be:

bpy.ops.mesh.subdivide(number_cuts=20)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.