'Blender Crashes when i use Python Script
I have a python script which works fine when I run in text editor. But I want to create Addon for that script. I am not from coding background so do not have much knowledge in python. I have seen a YouTube video (https://www.youtube.com/watch?v=Y67eCfiqJQU&ab_channel=chocofur) to create addon but it is not working Blender freezes as install the addon and Execute it as I have mentioned the code itself works fine. Here Is the Final code:
bl_info = {
"name": "New_Object",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > New Object",
"description": "Adds a new Mesh Object",
"warning": "",
"doc_url": "",
"category": "Add Mesh",
}
import bpy
import os
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')
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)
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
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
main(context)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)
class LayoutDemoPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "Layout Demo"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
scene = context.scene
# Big render button
layout.label(text="Big Button:")
row = layout.row()
row.scale_y = 2.0
row.operator("object.simple_operator")
def register():
bpy.utils.register_class(LayoutDemoPanel)
bpy.utils.register_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
bpy.utils.unregister_class(SimpleOperator)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if __name__ == "__main__":
register()
Please Guide me. Main code is under def main(context): Rest is the template
Solution 1:[1]
I can't run it but I see some mistakes with indentations - and this can make problem and code may run in endless loop and freeze program.
You put if in the same column as while so it finishs while-loop and rest is executed after exiting this loop. But this loop check i to exit and now you have i += 1 outside this loop so it never change this i.
If you change indentations then if will be inside loop and i += 1 will be also inside loop and this should resolve problem with freezing.
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
But frankly I would do it with for-loop and it wouldn't need i = 0, i += 1 and selObj[i] but only 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`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | furas |
