-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathecon_blender.py
1481 lines (1182 loc) · 55.8 KB
/
econ_blender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from typing import Pattern
import bpy
from bpy.types import Operator
# from . helper import Helper
from bpy_extras.io_utils import ImportHelper
# from bpy.props import StringProperty
# import json
import os, subprocess, sys, glob
# from shutil import copyfile,rmtree,move
from os.path import join
from .panel import *
import shutil
### TEST ###
import bpy
import os
import urllib.request
import zipfile
from bpy.props import StringProperty, FloatProperty, BoolProperty, IntProperty
from bpy.types import Operator, Panel, PropertyGroup
from ruamel.yaml import YAML
from shutil import copytree
#############
def simple_gen_exec(context):
econ_prop = context.scene.econ_prop
path_addon = os.path.dirname(os.path.abspath(__file__))
path_venv = econ_prop.str_venv_path
path_venv_full = join(path_venv, econ_prop.str_custom_venv_name)
path_venv_cuda = join(path_venv, econ_prop.str_custom_venv_name, "CUDA")
path_venv_activate = join(path_venv_full, "Scripts", "activate.bat")
drive_addon = path_addon.split(":")[0]
rest_path_addon = path_addon.split(":")[1]
rest_path_stable_diffusion = join(rest_path_addon, "stable-diffusion-main")
if os.path.exists(path_venv_activate):
with open(
path_venv_activate, "rt"
) as fin: # cria um bat com a ativacao do venv e inclui instalacao dos pacotes para Stable diffusion
with open(join(path_addon, "install_econ_reqs_local.bat"), "wt") as fout:
for line in fin:
# fout.write(line.replace('####RESULTS_DIR####', folder_prj))
fout.write(line)
fout.write("\nrem %1 - drive env")
fout.write("\nrem %2 - path to venv (wihtout drive)")
fout.write("\nrem %3 - env_folder")
fout.write("\n" + drive_addon + ":")
# fout.write('\ncd'+rest_path_stable_diffusion)
fout.write("\ncd" + rest_path_addon)
fout.write("\ncd")
fout.write("\necho --------------------------------------------------")
# fout.write('\npython -mpip install -r "'+join(path_addon,'stable-diffusion-main','requirements_test.txt"'))
fout.write("\npython -mpip install -r requirements.txt")
fout.write(
"\npython -mpip install src\\rembg-0+unknown-py3-none-any.whl"
)
fout.write(
"\npython -mpip install src\\voxelize_cuda-0.0.0-cp38-cp38-win_amd64.whl"
)
# # fout.write('\nset python_include=%3\\compilando\\Python\\include')
# # fout.write('\nset python_pc=%3\\compilando\\Python\\PC')
# # fout.write('\nset python_libs=%3\\compilando\\Python\\libs')
# # fout.write('\nset INCLUDE=%python_pc%;%python_include%')
# # fout.write('\nset LIB=%python_libs%')
# # copiado do dreambooth
# fout.write('\nset DISTUTILS_USE_SDK=1') #importante senao da erro "error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/"
# # fout.write('\nset CUDA_HOME=%3\\compilando\\CUDA_v11.8')
# # fout.write('\nset CUDA_PATH=%3\\compilando\\CUDA_v11.8')
# # fout.write('\necho %CUDA_HOME%')
# # fout.write('\nset PATH=%1:%2\\PortableGit\\cmd;%PATH%')
# ## setting caminhos
# # fout.write('\nset CUDA_PATH=%3\\compilando\\CUDA_v11.8')
# fout.write('\nset python_include=%3\\compilando\\Python\\include')
# fout.write('\nset python_pc=%3\\compilando\\Python\\PC')
# fout.write('\nset python_libs=%3\\compilando\\Python\\libs')
# # fout.write('\nset cuda_path=%3\\compilando\\CUDA_v11.8')
# fout.write('\nset msvc_include=%3\\compilando\\MSVC\\include')
# fout.write('\nset msvc_atlmfc_include=%3\\compilando\\MSVC\\ATLMFC\\include')
# fout.write('\nset win_kit_include_ucrt=%3\\compilando\\MSVC\\windows_kits\\include\\ucrt')
# fout.write('\nset win_kit_include_shared=%3\\compilando\\MSVC\\windows_kits\\include\\shared')
# fout.write('\nset win_kit_include_um=%3\\compilando\\MSVC\\windows_kits\\include\\um')
# fout.write('\nset win_kit_include_winrt=%3\\compilando\\MSVC\\windows_kits\\include\\winrt')
# fout.write('\nset win_kit_include_cppwinrt=%3\\compilando\\MSVC\\windows_kits\\include\\cppwinrt')
# fout.write('\nset msvc_atlmfc_lib=%3\\compilando\\MSVC\\ATLMFC\\lib\\x64')
# fout.write('\nset msvc_lib=%3\\compilando\\MSVC\\lib\\x64')
# fout.write('\nset win_kit_ucrt_lib=%3\\compilando\\windows_kits\\lib\\ucrt\\x64')
# fout.write('\nset win_kit_um_lib=%3\\compilando\\windows_kits\\lib\\um\\x64')
# fout.write('\nset win_kit_bin=%3\\compilando\\windows_kits\\bin\\10.0.19041.0\\x64')
# fout.write('\nset todo_msvc_path=%3\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64')
# fout.write('\nset ninja_path=%3\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja')
# # os.environ['CUDA_HOME'] = cuda_path
# fout.write('\nset INCLUDE=%python_pc%;%python_include%;%win_kit_include_ucrt%;%msvc_include%;%msvc_atlmfc_include%;% win_kit_include_shared%;%win_kit_include_um%;%win_kit_include_winrt%;%win_kit_include_cppwinrt%')
# fout.write('\nset LIB=%python_libs%;%msvc_atlmfc_lib%;%msvc_lib%;%win_kit_ucrt_lib%;%win_kit_um_lib%')
# fout.write('\nset LIBPATH=%msvc_atlmfc_lib%;%msvc_lib%')
# fout.write('\nset PATH=%win_kit_bin%;%CUDA_PATH%\\bin;%todo_msvc_path%;%ninja_path%;%PATH%')
# fout.write('\ncd econ\\lib\\common\\libmesh')
# # fout.write('\npython setup.py install')
# fout.write('\npython setup.py build_ext --inplace')
# fout.write('\ncd ..')
# fout.write('\ncd libvoxelize')
# # fout.write('\npython setup.py install')
# fout.write('\npython setup.py build_ext --inplace')
fout.write(
"\necho --------------------------------------------------------------"
)
fout.write("\necho YOU CAN CLOSE THIS WINDOW NOW")
fout.write(
"\necho --------------------------------------------------------------"
)
print("Re created install_econ_reqs_local.bat")
# with open(join(path_addon,'stable-diffusion-main','req_local.txt'), "wt") as fout:
# fout.write('\n-e '+join(path_venv_full,'src','taming-transformers')+'\\.')
# fout.write('\n-e '+join(path_venv_full,'src','CLIP')+'\\.')
# print('Re created stable-diffusion-main/req_local.bat')
with open(
path_venv_activate, "rt"
) as fin: # cria um bat com a ativacao do venv e inclui instalacao dos pacotes para Stable diffusion
with open(join(path_addon, "execute_python_generic.bat"), "wt") as fout:
for line in fin:
# fout.write(line.replace('####RESULTS_DIR####', folder_prj))
fout.write(line)
fout.write("\n" + drive_addon + ":")
fout.write("\ncd" + rest_path_stable_diffusion)
fout.write("\nEcho path of the addon, execute")
fout.write("\ncd")
fout.write("\necho --------------------------------------------------")
fout.write("\npython %*")
# fout.write('\n%*')
print("Re created execute_python_generic.bat")
with open(
path_venv_activate, "rt"
) as fin: # cria um bat com a ativacao do venv e inclui instalacao dos pacotes para Stable diffusion
with open(
join(path_addon, "execute_python_generic_custom_folder.bat"), "wt"
) as fout:
for line in fin:
# fout.write(line.replace('####RESULTS_DIR####', folder_prj))
fout.write(line)
fout.write("\nEcho path of the addon, execute")
fout.write("\ncd")
fout.write("\necho --------------------------------------------------")
fout.write("\npython %*")
# fout.write('\n%*')
print("Re created execute_python_generic_custom_folder.bat")
def download_texture_exec(context):
econ_prop = context.scene.econ_prop
path_addon = os.path.dirname(os.path.abspath(__file__))
path_venv = econ_prop.str_venv_path
path_venv_full = join(path_venv, econ_prop.str_custom_venv_name)
path_venv_activate = join(path_venv_full, "Scripts", "activate.bat")
# archive_ref = "022761263660c5dece411766eefe7735eb2cfd6a"
# src = join(path_addon, "TEXTure-" + archive_ref)
src = join(path_addon, "TEXTure_for_ECON-main")
dest = join(path_addon, "ECON", "TEXTure")
download_path = join(path_addon, "TEXTure_github.zip")
unzip_dir = join(path_addon, "ECON")
renamed_dir = join(unzip_dir, "TEXTure")
url = "https://github.com/kwan3854/TEXTure_for_ECON/archive/refs/heads/main.zip"
torch_ver_cuda_ver = "torch-1.12.1_cu116" # TODO: Update this to match your system's torch and cuda versions
# Download the zip file
with urllib.request.urlopen(url) as response, open(download_path, "wb") as out_file:
shutil.copyfileobj(response, out_file)
print("File downloaded successfully")
# Unzip the file
with zipfile.ZipFile(download_path, "r") as zip_ref:
zip_ref.extractall(unzip_dir)
print("File unzipped successfully")
# Rename the extracted folder
os.rename(os.path.join(unzip_dir, "TEXTure_for_ECON-main"), renamed_dir)
print("Folder renamed successfully")
# Delete the downloaded zip file
os.remove(download_path)
print("Downloaded zip file removed successfully")
if os.path.exists(path_venv_activate):
with open(path_venv_activate, "rt") as fin:
with open(join(path_addon, "install_TEXTure_req.bat"), "wt") as fout:
fout.write('call "' + path_venv_activate + '"\n')
for line in fin:
fout.write(line)
# original requirements.txt has problems. uncommnet line 3 to 17.
fout.write(
f"""
cd "{path_addon}"
echo --------------------------------------------------
echo Installing TEXTure dependencies...
python -mpip install -r "{join(dest, 'requirements.txt')}"
python -mpip install kaolin==0.13.0 -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/{torch_ver_cuda_ver}.html
echo --------------------------------------------------
echo YOU CAN CLOSE THIS WINDOW NOW
echo --------------------------------------------------
"""
)
print("Re created install_TEXTure_req.bat")
class CreateVirtualEnviroment(Operator):
bl_idname = "econ.create_virtual_env"
bl_label = "Create ECON Venv"
bl_description = "Create ECON Virtual Enviroment"
def execute(self, context):
import subprocess
import sys
econ_prop = context.scene.econ_prop
path_addon = os.path.dirname(os.path.abspath(__file__))
path_venv = econ_prop.str_venv_path
env_folder_name = econ_prop.str_custom_venv_name
path_pip_ini = join(path_venv, econ_prop.str_custom_venv_name, "pip.ini")
drive_env = path_venv.split(":")[0]
# rest_path_env = path_venv.split(':')[1]
# install venv
os.chdir(path_venv) # muda diretorio
if os.path.exists(join(path_venv, econ_prop.str_custom_venv_name)):
print("Environment already created")
else:
subprocess.run(
[
join(path_addon, "install_venv_p38.bat"),
drive_env,
path_addon,
path_venv,
env_folder_name,
]
) # Cria o venv
# arquivo criado para poder usar o pip pra instalar tudo de uma so vez
with open(path_pip_ini, "wt") as fout:
fout.write("[global]")
fout.write("\nindex-url=https://pypi.org/simple")
fout.write("\nextra-index-url=https://download.pytorch.org/whl/cu116")
print("pip.ini file created")
simple_gen_exec(context)
# disable the button
econ_prop.bool_step1_create_econ_venv = True
return {"FINISHED"}
class InstallSTLOnVenv(Operator):
bl_idname = "econ.install_econ_on_venv"
bl_label = "Install ECON on venv"
bl_description = "Install ECON Virtual Enviroment"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
path_venv = join(econ_prop.str_venv_path, econ_prop.str_custom_venv_name)
drive = path_venv.split(":")[0]
rest_path = path_venv.split(":")[1]
# os.system('start cmd /k \""'+join(path_addon,'install_econ_reqs_local.bat')+'" '+drive+' '+rest_path+' '+econ_prop.str_venv_path+'\"')
subprocess.run(
[
join(path_addon, "install_econ_reqs_local.bat"),
drive,
rest_path,
econ_prop.str_venv_path,
]
) # Cria o venv
# desabilta o botao
econ_prop.bool_step3_install_econ_venv = True
return {"FINISHED"}
class FinishECONInstall(Operator):
bl_idname = "econ.finish_econ_install"
bl_label = "Finish ECON Install"
bl_description = "Finish ECON Install"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
path_venv = econ_prop.str_venv_path
path_venv_full = join(econ_prop.str_venv_path, econ_prop.str_custom_venv_name)
with open(join(path_addon, "finish_ECON_install.bat"), "wt") as fout:
fout.write('call "' + join(path_venv_full, "Scripts", "activate.bat") + '"')
## setting caminhos
# fout.write('\nset CUDA_PATH=%3\\compilando\\CUDA_v11.8')
fout.write("\nset python_include=%1\\compilando\\Python\\include")
fout.write("\nset python_pc=%1\\compilando\\Python\\PC")
fout.write("\nset python_libs=%1\\compilando\\Python\\libs")
# fout.write('\nset cuda_path=%1\\compilando\\CUDA_v11.8')
fout.write("\nset msvc_include=%1\\compilando\\MSVC\\include")
fout.write(
"\nset msvc_atlmfc_include=%1\\compilando\\MSVC\\ATLMFC\\include"
)
fout.write(
"\nset win_kit_include_ucrt=%1\\compilando\\MSVC\\windows_kits\\include\\ucrt"
)
fout.write(
"\nset win_kit_include_shared=%1\\compilando\\MSVC\\windows_kits\\include\\shared"
)
fout.write(
"\nset win_kit_include_um=%1\\compilando\\MSVC\\windows_kits\\include\\um"
)
fout.write(
"\nset win_kit_include_winrt=%1\\compilando\\MSVC\\windows_kits\\include\\winrt"
)
fout.write(
"\nset win_kit_include_cppwinrt=%1\\compilando\\MSVC\\windows_kits\\include\\cppwinrt"
)
fout.write("\nset msvc_atlmfc_lib=%1\\compilando\\MSVC\\ATLMFC\\lib\\x64")
fout.write("\nset msvc_lib=%1\\compilando\\MSVC\\lib\\x64")
fout.write(
"\nset win_kit_ucrt_lib=%1\\compilando\\windows_kits\\lib\\ucrt\\x64"
)
fout.write(
"\nset win_kit_um_lib=%1\\compilando\\windows_kits\\lib\\um\\x64"
)
fout.write(
"\nset win_kit_bin=%1\\compilando\\windows_kits\\bin\\10.0.19041.0\\x64"
)
fout.write(
"\nset todo_msvc_path=%1\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64"
)
fout.write(
"\nset ninja_path=%1\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja"
)
# os.environ['CUDA_HOME'] = cuda_path
fout.write(
"\nset INCLUDE=%python_pc%;%python_include%;%win_kit_include_ucrt%;%msvc_include%;%msvc_atlmfc_include%;% win_kit_include_shared%;%win_kit_include_um%;%win_kit_include_winrt%;%win_kit_include_cppwinrt%"
)
fout.write(
"\nset LIB=%python_libs%;%msvc_atlmfc_lib%;%msvc_lib%;%win_kit_ucrt_lib%;%win_kit_um_lib%"
)
fout.write("\nset LIBPATH=%msvc_atlmfc_lib%;%msvc_lib%")
# fout.write('\nset PATH=%win_kit_bin%;%CUDA_PATH%\\bin;%todo_msvc_path%;%ninja_path%;%PATH%')
fout.write("\nset PATH=%win_kit_bin%;%todo_msvc_path%;%ninja_path%;%PATH%")
fout.write("\ncd econ\\lib\\common\\libmesh")
# fout.write('\npython setup.py install')
fout.write("\npython setup.py build_ext --inplace")
fout.write("\ncd ..")
fout.write("\ncd libvoxelize")
# fout.write('\npython setup.py install')
fout.write("\npython setup.py build_ext --inplace")
current_folder = os.getcwd()
os.chdir(path_addon)
# os.system('start cmd /k \""'+join(path_addon,'install_pytorch3d.bat')+'" '+drive+' '+rest_path+' '+econ_prop.str_venv_path+'\"')
# os.system('start cmd /k "'+join(path_addon,'finish_ECON_install.bat')+'" '+path_venv)
subprocess.run(
[join(path_addon, "finish_ECON_install.bat"), path_venv]
) # Cria o venv
os.chdir(current_folder)
# disable the button
econ_prop.bool_step5_finish_econ = True
return {"FINISHED"}
class VenvPathSelect(Operator, ImportHelper):
bl_idname = "econ.venv_path_select"
bl_label = "Venv Path"
bl_description = "Select Venv Path"
# filename_ext = ".ckpt"
filter_glob: StringProperty(
# default="*.ckpt",
options={"HIDDEN"},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
econ_prop = context.scene.econ_prop
path_venv_path = os.path.dirname(self.filepath)
econ_prop.str_venv_path = os.path.dirname(self.filepath)
path_addon = os.path.dirname(os.path.abspath(__file__))
path_venv_path_txt = join(path_addon, "venv_path.txt")
with open(path_venv_path_txt, "wt") as fout:
fout.write(path_venv_path)
simple_gen_exec(context)
return {"FINISHED"}
class InstallCompiler(Operator, ImportHelper):
bl_idname = "econ.install_compiler"
bl_label = "Install Compiler"
bl_description = "Install Compiler"
filename_ext = ".7z"
filter_glob: StringProperty(
default="*.7z",
options={"HIDDEN"},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
compiler_zip = self.filepath
destination = econ_prop.str_venv_path
# destination = join(path_addon,'Text2Light','logs')
if not os.path.exists(destination):
os.makedirs(destination, exist_ok=True)
# from pyunpack import Archive
# Archive(compiler_zip).extractall(destination)
# executing the process
current_folder = os.getcwd()
os.chdir(path_addon)
subprocess.run(
[
join(path_addon, "execute_python_generic_custom_folder.bat"),
"unpack_7z.py",
"-i",
compiler_zip,
"-o",
destination,
]
)
os.chdir(current_folder)
# disable the button
econ_prop.bool_step4_install_compiler = True
return {"FINISHED"}
class ExecutePrompt(Operator):
bl_idname = "text2light.execute"
bl_label = "Execute Prompt"
bl_description = "Execute Prompt"
option: IntProperty(name="option") # 1 para 4k, 2 para
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
current_folder = os.getcwd()
os.chdir(join(path_addon, "Text2Light"))
# subprocess.run([join(path_addon,'Execute_midas.bat')])
if self.option == 1:
subprocess.run(
[
join(path_addon, "execute_python_generic_custom_folder.bat"),
"text2light.py",
"-rg",
"logs/global_sampler_clip",
"-rl",
"logs/local_sampler_outdoor",
"--outdir",
"../result",
"--text",
econ_prop.str_sd_prompt,
"--clip",
"clip_emb.npy",
"--sritmo",
"logs/sritmo.pth",
"--sr_factor",
"4",
]
)
if self.option == 2:
subprocess.run(
[
join(path_addon, "execute_python_generic_custom_folder.bat"),
"text2light.py",
"-rg",
"logs/global_sampler_clip",
"-rl",
"logs/local_sampler_outdoor",
"--outdir",
"../result",
"--text",
econ_prop.str_sd_prompt,
"--clip",
"clip_emb.npy",
]
)
os.chdir(current_folder)
return {"FINISHED"}
class UseHDRI(Operator):
bl_idname = "text2light.use_hdri"
bl_label = "Use HDRI"
bl_description = "Use last generated HDRI"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
hdri = join(path_addon, "result", "hdr", "*.exr")
result = sorted(glob.glob(hdri), key=os.path.getmtime)
load_hdri = result[-1]
scene = bpy.context.scene
if scene.world is None:
# create a new world
new_world = bpy.data.worlds.new("Text2Light")
scene.world = new_world
bpy.context.scene.world.use_nodes = True
else:
for w in bpy.data.worlds:
if w.name == "Text2Light":
scene.world = w
# se apos o processo acima ainda nao existir world definido como text2light, criar um (pois algum deles falhou)
if scene.world.name != "Text2Light":
new_world = bpy.data.worlds.new("Text2Light")
scene.world = new_world
bpy.context.scene.world.use_nodes = True
C = bpy.context
scn = C.scene
# Get the environment node tree of the current scene
node_tree = scn.world.node_tree
tree_nodes = node_tree.nodes
# Clear all nodes
tree_nodes.clear()
# Add Background node
node_background = tree_nodes.new(type="ShaderNodeBackground")
# Add Environment Texture node
node_environment = tree_nodes.new("ShaderNodeTexEnvironment")
# Load and assign the image to the node property
node_environment.image = bpy.data.images.load(load_hdri) # Relative path
node_environment.location = -300, 0
# Add Output node
node_output = tree_nodes.new(type="ShaderNodeOutputWorld")
node_output.location = 200, 0
# Link all nodes
links = node_tree.links
link = links.new(
node_environment.outputs["Color"], node_background.inputs["Color"]
)
link = links.new(
node_background.outputs["Background"], node_output.inputs["Surface"]
)
econ_prop.fl_bg_strength = 1
return {"FINISHED"}
class OpenOutputFolder(Operator):
bl_idname = "text2light.open_output_folder"
bl_label = "Open Output Folder"
bl_description = "Open Output Folder"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
output_folder = join(path_addon, "result", "hdr")
os.system('explorer "' + output_folder + '"')
return {"FINISHED"}
class DownloadECON(Operator):
bl_idname = "econ.download_econ"
bl_label = "Download ECON"
bl_description = "Download and Unzip ECON"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
# OLD Jan 9 2023
# archive_ref = '3b5ac63fd59b8c915235c62c981ead113c40d3b7'
# NEW May 14 2023
# archive_ref = '6eec05720031c0a3fe008f00191ae4ef37d4dc00'
# rename folder
# src = join(path_addon,'ECON-'+archive_ref)
src = join(path_addon, "ECON-" + "master")
# Destination
dest = join(path_addon, "ECON")
if not os.path.exists(dest):
# url = 'https://github.com/YuliangXiu/ECON/archive/'+archive_ref+'.zip'
url = "https://github.com/kwan3854/ECON/archive/refs/heads/master.zip"
download_path = join(path_addon, "ECON_github.zip")
urllib.request.urlretrieve(url, download_path)
# Unzip
import zipfile
with zipfile.ZipFile(download_path, "r") as zip_ref:
zip_ref.extractall(path_addon)
# Renaming the file
os.rename(src, dest)
os.remove(download_path)
# disable the button
econ_prop = context.scene.econ_prop
econ_prop.bool_step2_downlad_econ = True
return {"FINISHED"}
class DownloadPytorch3d(Operator):
bl_idname = "econ.download_pytorch3d"
bl_label = "Down&Install Pytorch3D"
bl_description = "Download and Install Pytorch3d"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
path_venv = econ_prop.str_venv_path
path_venv_full = join(path_venv, econ_prop.str_custom_venv_name)
archive_ref = "3388d3f0aa6bc44fe704fca78d11743a0fcac38c"
# rename folder
src = join(path_venv_full, "pytorch3d-" + archive_ref)
# Destination
dest = join(path_venv_full, "pytorch3d")
if not os.path.exists(dest):
import urllib.request
url = (
"https://github.com/facebookresearch/pytorch3d/archive/"
+ archive_ref
+ ".zip"
)
download_path = join(path_addon, "Pytorch3d_github.zip")
urllib.request.urlretrieve(url, download_path)
# Unzip
import zipfile
with zipfile.ZipFile(download_path, "r") as zip_ref:
zip_ref.extractall(path_venv_full)
# Renaming the file
os.rename(src, dest)
with open(join(path_addon, "install_pytorch3d.bat"), "wt") as fout:
fout.write('call "' + join(path_venv_full, "Scripts", "activate.bat") + '"')
## setting caminhos
# fout.write('\nset CUDA_PATH=%3\\compilando\\CUDA_v11.8')
fout.write("\nset python_include=%1\\compilando\\Python\\include")
fout.write("\nset python_pc=%1\\compilando\\Python\\PC")
fout.write("\nset python_libs=%1\\compilando\\Python\\libs")
# fout.write('\nset cuda_path=%1\\compilando\\CUDA_v11.8')
fout.write("\nset msvc_include=%1\\compilando\\MSVC\\include")
fout.write(
"\nset msvc_atlmfc_include=%1\\compilando\\MSVC\\ATLMFC\\include"
)
fout.write(
"\nset win_kit_include_ucrt=%1\\compilando\\MSVC\\windows_kits\\include\\ucrt"
)
fout.write(
"\nset win_kit_include_shared=%1\\compilando\\MSVC\\windows_kits\\include\\shared"
)
fout.write(
"\nset win_kit_include_um=%1\\compilando\\MSVC\\windows_kits\\include\\um"
)
fout.write(
"\nset win_kit_include_winrt=%1\\compilando\\MSVC\\windows_kits\\include\\winrt"
)
fout.write(
"\nset win_kit_include_cppwinrt=%1\\compilando\\MSVC\\windows_kits\\include\\cppwinrt"
)
fout.write("\nset msvc_atlmfc_lib=%1\\compilando\\MSVC\\ATLMFC\\lib\\x64")
fout.write("\nset msvc_lib=%1\\compilando\\MSVC\\lib\\x64")
fout.write(
"\nset win_kit_ucrt_lib=%1\\compilando\\windows_kits\\lib\\ucrt\\x64"
)
fout.write(
"\nset win_kit_um_lib=%1\\compilando\\windows_kits\\lib\\um\\x64"
)
fout.write(
"\nset win_kit_bin=%1\\compilando\\windows_kits\\bin\\10.0.19041.0\\x64"
)
fout.write(
"\nset todo_msvc_path=%1\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64"
)
fout.write(
"\nset ninja_path=%1\\compilando\\FULL_COMMUNITY_MSVC\\2019\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja"
)
# os.environ['CUDA_HOME'] = cuda_path
fout.write(
"\nset INCLUDE=%python_pc%;%python_include%;%win_kit_include_ucrt%;%msvc_include%;%msvc_atlmfc_include%;% win_kit_include_shared%;%win_kit_include_um%;%win_kit_include_winrt%;%win_kit_include_cppwinrt%"
)
fout.write(
"\nset LIB=%python_libs%;%msvc_atlmfc_lib%;%msvc_lib%;%win_kit_ucrt_lib%;%win_kit_um_lib%"
)
fout.write("\nset LIBPATH=%msvc_atlmfc_lib%;%msvc_lib%")
# fout.write('\nset PATH=%win_kit_bin%;%CUDA_PATH%\\bin;%todo_msvc_path%;%ninja_path%;%PATH%')
fout.write("\nset PATH=%win_kit_bin%;%todo_msvc_path%;%ninja_path%;%PATH%")
fout.write("\npython setup.py install")
current_folder = os.getcwd()
os.chdir(dest)
# os.system('start cmd /k \""'+join(path_addon,'install_pytorch3d.bat')+'" '+drive+' '+rest_path+' '+econ_prop.str_venv_path+'\"')
# os.system('start cmd /k "'+join(path_addon,'install_pytorch3d.bat')+'" '+path_venv)
subprocess.run(
[join(path_addon, "install_pytorch3d.bat"), path_venv]
) # Cria o venv
os.chdir(current_folder)
# disable button
econ_prop.bool_step6_install_pytorch = True
return {"FINISHED"}
class DownloadTEXTure(Operator):
bl_idname = "econ.download_texture"
bl_label = "Download TEXTure"
bl_description = "Download TEXTure"
def execute(self, context):
download_texture_exec(context)
path_addon = os.path.dirname(os.path.abspath(__file__))
current_folder = os.getcwd()
subprocess.run([join(path_addon, "install_TEXTure_req.bat")], cwd=path_addon)
# disable the button
econ_prop = context.scene.econ_prop
econ_prop.bool_step7_download_TEXTure = True
return {"FINISHED"}
class ExecuteTEXTure(Operator):
bl_idname = "econ.execute_texture"
bl_label = "Generate TEXTure"
bl_description = "Generate TEXTure"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
path_venv_full = join(econ_prop.str_venv_path, econ_prop.str_custom_venv_name)
path_venv_activate = join(path_venv_full, "Scripts", "activate.bat")
dest = join(path_addon, "ECON", "TEXTure")
# Copy cache directory every time before running TEXTure
src_dir = join(path_addon, "ECON", "results", "econ", "cache")
dest_dir = join(dest, "cache")
if econ_prop.selected_image == "":
self.report({"ERROR"}, "No image selected. Please select an image.")
return {"CANCELLED"}
# check if the obj file exists
if not os.path.exists(join(src_dir, os.path.splitext(econ_prop.selected_image)[0], "mesh.obj")):
self.report({"ERROR"}, "Avatar obj file not found. Run Generate Animatable 3D Model first.")
return {"CANCELLED"}
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
copytree(src_dir, dest_dir)
# Apply configs
config_path = join(
path_addon, "ECON", "TEXTure", "configs", "text_guided", "avatar.yaml"
)
yaml = YAML(typ="rt") # Round trip loading and dumping
with open(config_path, "r") as f:
data = yaml.load(f)
file_name = os.path.splitext(econ_prop.selected_image)[0]
data["log"]["exp_name"] = file_name
data["log"]["save_interval"] = 18
data["guide"]["text"] = econ_prop.stable_diffusion_prompt
data["guide"]["diffusion_name"] = "stabilityai/stable-diffusion-2-depth"
data["guide"]["shape_scale"] = 0.6
data["guide"]["append_direction"] = True
data["guide"]["shape_path"] = os.path.join("cache", file_name, "mesh.obj")
data["guide"]["initial_texture"] = os.path.join(
"cache", file_name, "texture.png"
)
data["guide"]["reference_texture"] = os.path.join(
"cache", file_name, "mask.png"
)
data["guide"]["use_inpainting"] = True
data["guide"]["texture_resolution"] = 1024
data["guide"]["guidance_scale"] = 7
data["guide"]["texture_interpolation_mode"] = "bilinear"
data["optim"]["seed"] = 3
data["render"]["front_offset"] = 0
data["render"]["n_views"] = 4
data["render"]["views_after"] = [[180, 30], [180, 150], [180, 1], [180, 180]]
with open(config_path, "w") as f:
yaml.dump(data, f)
if os.path.exists(path_venv_activate):
with open(path_venv_activate, "rt") as fin:
with open(join(path_addon, "execute_TEXTure.bat"), "wt") as fout:
for line in fin:
fout.write(line)
fout.write(
f"""
call \"{join(path_venv_full,'Scripts','activate.bat')}
cd {dest}
python -m scripts.run_texture --config_path=configs/text_guided/avatar.yaml
"""
)
current_folder = os.getcwd()
os.chdir(dest)
# run TEXTure
# python -m scripts.run_texture --config_path=configs/text_guided/avatar.yaml
subprocess.run([join(path_addon, "execute_TEXTure.bat")], cwd=path_addon)
os.chdir(current_folder)
image_name = os.path.splitext(econ_prop.selected_image)[0]
obj = join(dest, "experiments", image_name, "mesh", "mesh.obj")
bpy.ops.wm.obj_import(filepath=obj)
return {"FINISHED"}
class ExecuteECON(Operator):
bl_idname = "econ.execute_econ"
bl_label = "Generate 3D Model"
bl_description = "Generate 3D model from image"
def execute(self, context):
path_addon = os.path.dirname(os.path.abspath(__file__))
econ_prop = context.scene.econ_prop
# Check if an image is selected
if econ_prop.selected_image == "":
self.report({"ERROR"}, "No image selected. Please select an image.")
return {"CANCELLED"}
path_venv = econ_prop.str_venv_path
path_venv_full = join(path_venv, econ_prop.str_custom_venv_name)
with open(join(path_addon, "execute_econ.bat"), "wt") as fout:
fout.write('call "' + join(path_venv_full, "Scripts", "activate.bat") + '"')
fout.write(
"\npython -m apps.infer -cfg ./configs/econ.yaml -in_dir ./examples -out_dir ./results"
)
result_folder = join(path_addon, "ECON", "results", "econ")
# make sure the result folder exists
if not (
os.path.exists(join(result_folder, "BNI"))
and os.path.exists(join(result_folder, "obj"))
and os.path.exists(join(result_folder, "png"))
and os.path.exists(join(result_folder, "vid"))
):
os.makedirs(result_folder, exist_ok=True)
os.mkdir(join(result_folder, "BNI"))
os.mkdir(join(result_folder, "obj"))
os.mkdir(join(result_folder, "png"))
os.mkdir(join(result_folder, "vid"))
################### Temporary bug resolution #######################
bni = join(result_folder, "BNI", "examples")
obj = join(result_folder, "obj", "examples")
png = join(result_folder, "png", "examples")
vid = join(result_folder, "vid", "examples")
if not os.path.exists(bni):
os.symlink(join(result_folder, "BNI"), bni)
if not os.path.exists(obj):
os.symlink(join(result_folder, "obj"), obj)
if not os.path.exists(png):
os.symlink(join(result_folder, "png"), png)
if not os.path.exists(vid):
os.symlink(join(result_folder, "vid"), vid)
econ_folder = join(path_addon, "ECON")
current_folder = os.getcwd()
os.chdir(econ_folder)
subprocess.run([join(path_addon, "execute_econ.bat")]) # Executa o ECON
os.chdir(current_folder)
# full_obj = join(
# result_folder,
# "obj",
# "examples",
# os.path.splitext(econ_prop.selected_image)[0] + "_0_full.obj",
# )
###############################################################
# =========this code should be used when the ECON Windows version directory structure is fixed===========
full_obj = join(
result_folder,
"obj",
os.path.splitext(econ_prop.selected_image)[0] + "_0_full.obj",
)
# =======================================================================================================
# ################### Temporal bug resolution #######################
# directories = [
# join(result_folder, "BNI", "examples"),
# join(result_folder, "obj", "examples"),
# join(result_folder, "png", "examples"),
# join(result_folder, "vid", "examples"),
# ]
# for directory in directories:
# # Check if directory exists
# if not os.path.exists(directory):
# print(f"The directory {directory} does not exist.")
# continue
# # Get the list of all files
# for filename in os.listdir(directory):
# file_path = os.path.join(directory, filename)
# # Ensure that it's a file, not a directory or a sub-directory
# if os.path.isfile(file_path):
# destination_path = os.path.join(
# os.path.dirname(directory), filename
# )
# # Copy the file
# shutil.copy(file_path, destination_path)
# print(f"Copied {file_path} to {destination_path}")
# ####################################################################
bpy.ops.wm.obj_import(filepath=full_obj)
return {"FINISHED"}