Fix add-ons with Python 3.12 by replacing "imp" with "importlib"
[blender-addons.git] / io_coat3D / folders.py
blob4acec77843da262aa6485e62b6c8ac4fbe77df8f
1 # SPDX-FileCopyrightText: 2020-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 import os
8 def InitFolders():
10 platform = os.sys.platform
11 coat3D = bpy.context.scene.coat3D
13 # Global variable foundExchangeFolder (True / False) guides these steps
14 # 1. Read Exchange_folder.txt, if not success ->
15 # 2. Try to find exchange folder from system hard drive, if not success -->
16 # 3. Leave foundExchangeFolder = False
18 # 1. #################################################################
20 if(platform == 'win32' or platform == 'darwin'):
21 DC2Folder = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3DC2Blender'
22 else:
23 DC2Folder = os.path.expanduser("~") + os.sep + '3DC2Blender'
25 exchangeFile = DC2Folder + os.sep + 'Exchange_folder.txt'
27 if(not os.path.isdir(DC2Folder)):
28 os.mkdir(DC2Folder)
30 if(not os.path.isfile(exchangeFile)):
31 file = open(exchangeFile, 'w')
32 file.close()
33 else:
34 savedExchangePath = ''
35 folderPath = open(exchangeFile)
37 for line in folderPath:
38 savedExchangePath = line
39 break
40 folderPath.close()
43 coat3D.exchangeFolder = savedExchangePath
44 return True, coat3D.exchangeFolder
47 # 2. #################################################################
49 if(platform == 'win32' or platform == 'darwin'):
50 exchangeFolder = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + 'Applinks' + os.sep + '3D-Coat' + os.sep +'Exchange'
51 else:
52 exchangeFolder = os.path.expanduser("~") + os.sep + '3D-CoatV4' + os.sep + 'Exchange'
53 if not(os.path.isdir(exchangeFolder)):
54 exchangeFolder = os.path.expanduser("~") + os.sep + '3D-CoatV3' + os.sep + 'Exchange'
55 if(os.path.isdir(exchangeFolder)):
56 coat3D.exchangeFolder = exchangeFolder
58 Blender_folder = ("%s%sBlender"%(exchangeFolder,os.sep))
60 if(not(os.path.isdir(Blender_folder))):
61 os.makedirs(Blender_folder, mode = 0o666)
62 Blender_folder1 = os.path.join(Blender_folder,"run.txt")
63 file = open(Blender_folder1, "w")
64 file.close()
66 Blender_folder2 = os.path.join(Blender_folder, "extension.txt")
67 file = open(Blender_folder2, "w")
68 file.write("fbx")
69 file.close()
71 Blender_folder3 = os.path.join(Blender_folder, "preset.txt")
72 file = open(Blender_folder3, "w")
73 file.write("Blender Cycles")
74 file.close()
76 file = open(exchangeFile, "w")
77 file.write("%s"%(os.path.abspath(coat3D.exchangeFolder)))
78 file.close()
80 return True, coat3D.exchangeFolder
82 return False, ''
85 def updateExchangeFile(newPath):
87 platform = os.sys.platform
89 if(platform == 'win32' or platform == 'darwin'):
90 exchangeFile = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3DC2Blender' + os.sep + 'Exchange_folder.txt'
91 else:
92 exchangeFile = os.path.expanduser("~") + os.sep + '3DC2Blender' + os.sep + 'Exchange_folder.txt'
93 if(os.path.isfile(exchangeFile)):
94 folderPath = ''
96 if(os.path.isfile(exchangeFile)):
97 file = open(exchangeFile, "w")
98 file.write("%s"%(newPath))
99 file.close()
101 def loadExchangeFolder():
103 platform = os.sys.platform
104 coat3D = bpy.context.scene.coat3D
106 if(platform == 'win32' or platform == 'darwin'):
107 DC2Folder = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3DC2Blender'
108 else:
109 DC2Folder = os.path.expanduser("~") + os.sep + '3DC2Blender'
111 exchangeFile = DC2Folder + os.sep + 'Exchange_folder.txt'
113 if(not os.path.isdir(DC2Folder)):
114 os.mkdir(DC2Folder)
116 if(not os.path.isfile(exchangeFile)):
117 file = open(exchangeFile, 'w')
118 file.close()
119 else:
120 savedExchangePath = ''
121 folderPath = open(exchangeFile)
123 for line in folderPath:
124 savedExchangePath = line
125 break
126 folderPath.close()
127 coat3D.exchangeFolder = savedExchangePath
130 def set_working_folders():
132 platform = os.sys.platform
133 coat3D = bpy.context.scene.coat3D
135 if(platform == 'win32' or platform == 'darwin'):
136 if (coat3D.defaultfolder != '' and os.path.isdir(coat3D.defaultfolder)):
137 return coat3D.defaultfolder
138 else:
139 folder_objects = os.path.expanduser("~") + os.sep + 'Documents' + os.sep + '3DC2Blender' + os.sep + 'ApplinkObjects'
140 if(not(os.path.isdir(folder_objects))):
141 os.makedirs(folder_objects, mode = 0o666)
142 else:
143 if (coat3D.defaultfolder != '' and os.path.isdir(coat3D.defaultfolder)):
144 return coat3D.defaultfolder
145 else:
146 folder_objects = os.path.expanduser("~") + os.sep + '3DC2Blender' + os.sep + 'ApplinkObjects'
147 if(not(os.path.isdir(folder_objects))):
148 os.makedirs(folder_objects, mode = 0o666)
150 return folder_objects