__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # ----------------------------------------------------------
  2. # File __init__.py
  3. # ----------------------------------------------------------
  4. # Imports
  5. # Check if this add-on is being reloaded
  6. if "bpy" in locals():
  7. # reloading .py files
  8. import bpy
  9. import importlib
  10. # from . import zs_renderscene as zsrs
  11. # importlib.reload(zsrs)
  12. # or if this is the first load of this add-on
  13. else:
  14. import bpy
  15. import json
  16. from pathlib import Path
  17. # from . import zs_renderscene as zsrs # noqa
  18. # Addon info
  19. bl_info = {
  20. "name": "ZS Stable Diffusion Connection V0.0.1",
  21. "author": "Sergiu <sergiu@zixelise.com>",
  22. "Version": (0, 0, 1),
  23. "blender": (4, 00, 0),
  24. "category": "Scene",
  25. "description": "Stable Diffusion Connection",
  26. }
  27. # -------------------------------------------------------------------
  28. # Functions
  29. # -------------------------------------------------------------------
  30. def load_scene():
  31. print("Loading Scene")
  32. # load scene data
  33. # create parent collections
  34. create_parent_collections("01_Products")
  35. create_parent_collections("02_Elements")
  36. create_parent_collections("03_Shapes")
  37. # append products
  38. # append elements
  39. # append shapes
  40. # set lighting
  41. # set camera
  42. def append_objects():
  43. print("Appending Objects")
  44. # append objects
  45. # set transforms
  46. def create_parent_collections(group_name: str):
  47. if collection_exists(group_name):
  48. remove_collection_and_objects(group_name)
  49. else:
  50. create_collection(group_name)
  51. def remove_collection_and_objects(collection_name):
  52. oldObjects = list(bpy.data.collections[collection_name].all_objects)
  53. for obj in oldObjects:
  54. bpy.data.objects.remove(obj, do_unlink=True)
  55. old_collection = bpy.data.collections[collection_name]
  56. if old_collection is not None:
  57. old_collection_names = get_subcollection_names(old_collection)
  58. else:
  59. print("Collection not found.")
  60. # print line break
  61. print("-----------------------------------------------------------------")
  62. print(old_collection_names)
  63. print("-----------------------------------------------------------------")
  64. for old_collection_name in old_collection_names:
  65. for collection in bpy.data.collections:
  66. if collection.name == old_collection_name:
  67. bpy.data.collections.remove(collection)
  68. bpy.ops.outliner.orphans_purge(
  69. do_local_ids=True, do_linked_ids=True, do_recursive=True
  70. )
  71. def get_subcollection_names(collection):
  72. subcollection_names = []
  73. for child in collection.children:
  74. subcollection_names.append(child.name)
  75. subcollection_names.extend(get_subcollection_names(child))
  76. return subcollection_names
  77. # function that checks if a collection exists
  78. def collection_exists(collection_name):
  79. return collection_name in bpy.data.collections
  80. # function that creates a new collection and adds it to the scene
  81. def create_collection(collection_name):
  82. new_collection = bpy.data.collections.new(collection_name)
  83. bpy.context.scene.collection.children.link(new_collection)
  84. # -------------------------------------------------------------------
  85. # Operators
  86. # -------------------------------------------------------------------
  87. # load scene operator
  88. class ZSSD_OT_LoadScene(bpy.types.Operator):
  89. bl_idname = "zs_sd_loader.load_scene"
  90. bl_label = "Load Scene"
  91. bl_description = "Load Scene"
  92. def execute(self, context):
  93. load_scene()
  94. return {"FINISHED"}
  95. # parent class for panels
  96. class ZSSDPanel:
  97. bl_space_type = "VIEW_3D"
  98. bl_region_type = "UI"
  99. bl_category = "ZS SD Loader"
  100. # -------------------------------------------------------------------
  101. # Draw
  102. # -------------------------------------------------------------------
  103. # Panels
  104. class ZSSD_PT_Main(ZSSDPanel, bpy.types.Panel):
  105. bl_label = "SD Loader"
  106. def draw(self, context):
  107. layout = self.layout
  108. scene = context.scene
  109. col = layout.column()
  110. self.is_connected = False
  111. col.label(text="Stable Diffusion Connection")
  112. # load scene button
  113. col.operator("zs_sd_loader.load_scene", text="Load Scene")
  114. # modify after making products
  115. blender_classes = [
  116. ZSSD_PT_Main,
  117. ZSSD_OT_LoadScene,
  118. ]
  119. def register():
  120. # register classes
  121. for blender_class in blender_classes:
  122. bpy.utils.register_class(blender_class)
  123. # Has to be afqter class registering to correctly register property
  124. # register global properties
  125. # register list
  126. # list data
  127. # bpy.types.Scene.zs_product_list = bpy.props.CollectionProperty(
  128. # type=ZS_Product_ListItem)
  129. # current item in list
  130. def unregister():
  131. # unregister classes
  132. for blender_class in blender_classes:
  133. bpy.utils.unregister_class(blender_class)
  134. # unregister global properties
  135. # unregister list items
  136. # del bpy.types.Scene.my_list
  137. # del bpy.types.Scene.product_product_list_index