简体   繁体   English

生成UV球的搅拌器性能问题

[英]Blender performance issues with generating UV spheres

When I run this script: 当我运行此脚本时:

import bpy, time
t0 = time.time()

for i in range(1000):
    bpy.ops.mesh.primitive_uv_sphere_add()

    if i % 100 == 0:
        print(time.time()-t0)
        t0 = time.time()

This is the output (exponential growth vs. time): 这是输出(指数增长与时间的关系):

1.1920928955078125e-05
0.44658803939819336
0.46373510360717773
0.5661759376525879
0.7258329391479492
0.9994637966156006
1.381392002105713
1.8257861137390137
2.4634311199188232
3.2817111015319824

Why does this happen? 为什么会这样? Is there a better approach? 有没有更好的方法?

I am running this on a server with ample memory, and I know Blender can expand to use most of it (it does in rendering). 我正在具有足够内存的服务器上运行此程序,并且我知道Blender可以扩展以使用其中的大部分功能(它在渲染中确实如此)。

The quick answer: 快速答案:

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.mesh.primitive_uv_sphere_add()
sphere = bpy.context.object

for i in range(1000):
    ob = sphere.copy()
    ob.data = sphere.data.copy()
    bpy.context.scene.objects.link(ob)
bpy.context.scene.update()

Explanation: 说明:

Anything in bpy.ops.* causes a scene redraw with each call. bpy.ops.*任何内容都会导致每次调用都重绘场景。 You want to avoid calling these in loops. 您要避免在循环中调用它们。 The above script calls lower-level copy() methods, which don't redraw. 上面的脚本调用较低级别的copy()方法,该方法不会重绘。 If you want linked duplicates, you can remove the sphere.data.copy() line. 如果要链接重复项,可以删除sphere.data.copy()行。

This solution is not my own. 这个解决方案不是我自己的。 Kudos go to CoDEmanX over at BlenderArtists for this answer! 荣誉,请转到BlenderArtists的 CoDEmanX获得此答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM