【问题标题】:Blender Crashes when i use Python Script当我使用 Python 脚本时,Blender 崩溃
【发布时间】:2022-06-21 14:51:25
【问题描述】:

我有一个 python 脚本,当我在文本编辑器中运行时它运行良好。但我想为该脚本创建插件。我不是编码背景,所以对python没有太多了解。我看过一个 YouTube 视频 (https://www.youtube.com/watch?v=Y67eCfiqJQU&ab_channel=chocofur) 来创建插件,但它不起作用 Blender 在安装插件时冻结并执行它,因为我已经提到代码本身工作正常。 这是最终代码:

【问题讨论】:

  • 首先您可以使用print() 来查看执行了哪部分代码以及变量中有什么。或者您也可以使用模块logging 将这些信息保存在文件中,然后检查该文件。也许这样你会看到哪里有问题。您也可以使用try/except 来捕获错误并写入logging
  • 也许你有错误的缩进——你检查了while i < ...,但你在while之外设置了i = i + 1,所以它永远不会停止它。也许if not os.path.exists(dirname): os.makedirs(dirname) 应该有不同的缩进。或者你可以使用os.makedirs(dirname, exist_ok=True) 而不使用if。或者也许你应该使用for item in selObj: 而不是i = 0whilei += 1。稍后你应该使用item 而不是selObj[i]

标签: python blender


【解决方案1】:

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

您将ifwhile 放在同一列中,因此它完成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`

【讨论】:

  • 谢谢 代码现在开始工作,但它选择的对象不正确。假设我有四个对象,所以它选择像“第一个对象”然后“它正在组合所有 4 个对象”然后再次选择“第一个对象”然后“它正在组合所有 4 个对象”
  • 我不知道 Blender 是如何工作的,也不知道你想选择什么。我解决了主要问题 - 冻结 - 所以现在你应该在有新问题的新页面上创建新问题。你应该更好地描述问题,因为我们无法读懂你的想法。一开始我建议使用print() 来查看变量中的内容以及执行的代码部分——它被称为"print debuging",它有助于查看代码在做什么。
  • 实际上我发现了您忘记在上面的行中添加项目的错误,这就是为什么它无法正常工作于 selObj 中的项目:# Here obj = bpy.context.window.scene.objects[0 ] 应该是这样 obj = bpy.context.window.scene.objects[item]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多