【STM32】无法运行它,但我看到一些缩进错误

[解决方案1]:

我无法运行它,但我看到一些缩进错误 – 这可能会产生问题,代码可能会在无限循环中运行并冻结程序。

您将 if 与 while 放在同一列中,因此它完成了 while 循环并在退出循环后执行其余部分。但是这个循环检查 i 退出,现在你有 i += 1 在这个循环之外,所以它永远不会改变这个 i。

如果您更改缩进,if 将在循环内,i += 1 也将在循环内,这应该可以解决冻结问题。

def main(context):
    target_dir = "C:/Users/Arpit/Desktop/UV/" 
    selObj = []
    for obj in bpy.context.selected_objects:
        selObj.append(obj.name)
        bpy.ops.object.select_all(action='TOGGLE')
    # --- while-loop ---

图片[1]-【STM32】无法运行它,但我看到一些缩进错误-唐朝资源网

i = 0 while i < len(selObj): obj = bpy.context.window.scene.objects[0] bpy.context.view_layer.objects.active = obj bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action='TOGGLE') bpy.ops.mesh.select_all(action='TOGGLE') full_file_name = target_dir + bpy.data.objects[selObj[i]].name dirname = os.path.dirname(full_file_name)

图片[2]-【STM32】无法运行它,但我看到一些缩进错误-唐朝资源网

# inside `while`-loop if not os.path.exists(dirname): os.makedirs(dirname) bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6) bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.select_all(action='TOGGLE') i += 1

但坦率地说,我会使用 for 循环来完成,它不需要 i = 0、i += 1 和 selObj[i],而只需要 item

    # --- for-loop ---

图片[3]-【STM32】无法运行它,但我看到一些缩进错误-唐朝资源网

# without `i = 0` for item in selObj: obj = bpy.context.window.scene.objects[0] bpy.context.view_layer.objects.active = obj bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action='TOGGLE') bpy.ops.mesh.select_all(action='TOGGLE') # `item` instead of `selObj[i]` full_file_name = target_dir + bpy.data.objects[ item ].name dirname = os.path.dirname(full_file_name) # inside `while`-loop if not os.path.exists(dirname): os.makedirs(dirname) bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', size=(4096, 4096), opacity=0.6) bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.select_all(action='TOGGLE') # without `i += 1`

【讨论】:

© 版权声明
THE END
喜欢就支持一下吧
点赞81赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容