繁体   English   中英

当我使用 Python 脚本时,Blender 崩溃

[英]Blender Crashes when i use Python Script

我有一个运行良好的 python 脚本,但它无法运行 Blender 在安装插件时冻结并执行它,因为我已经提到代码本身运行良好。 这是最终代码:

def main(context):
     # You should change this varibale in "here" to match your own directory path
     # use '/' or '\\' for hirecacy
    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 ---
    
    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)

        # 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
  

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

您将if放在与while相同的列中,以便它完成while-loop并在退出此循环后执行 rest。 但是这个循环检查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 ---
    
    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)

        # 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 -loop 来做,它不需要i = 0i += 1selObj[i]但只需要item

    # --- for-loop ---

    # 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`

暂无
暂无

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

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