Skip to content

Commit aac32da

Browse files
committed
BEngine Python module for BEngine For Unity V0.3
1 parent cafe243 commit aac32da

File tree

4 files changed

+840
-0
lines changed

4 files changed

+840
-0
lines changed

BEngine-Py/BEGetBlenderInputs.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Main Stuff
2+
try:
3+
4+
import bpy
5+
6+
import traceback
7+
# from unicodedata import decimal
8+
9+
import sys
10+
import os
11+
12+
# import json
13+
# from json.decoder import JSONDecodeError
14+
15+
# import numpy
16+
17+
import time
18+
19+
import pathlib
20+
21+
try:
22+
from .Utils import BEUtils
23+
except:
24+
# Load BEUtils
25+
PACKAGE_PARENT = pathlib.Path(__file__).parent
26+
#PACKAGE_PARENT = pathlib.Path.cwd().parent # if on jupyter notebook
27+
SCRIPT_DIR = PACKAGE_PARENT
28+
sys.path.append(str(SCRIPT_DIR) + "/Utils")
29+
30+
import BEUtils
31+
32+
33+
def MainWork():
34+
context = bpy.context
35+
36+
BEUtils.ClearScene()
37+
38+
be_paths = BEUtils.BEPaths()
39+
40+
process_obj, geom_mod, bengine_GN = BEUtils.LoadGN(context, be_paths)
41+
42+
if (process_obj and bengine_GN):
43+
44+
# ORIGINAL STUFF
45+
# Get GN Data
46+
js_output_data = {}
47+
gn_inputs_data = BEUtils.GetGNInputsData(bengine_GN)
48+
49+
js_output_data['Inputs'] = gn_inputs_data
50+
51+
# If No JSON File
52+
gn_js_path = be_paths.blendfolder + be_paths.blendfile_name + '_' + be_paths.node_sys_name + '.json'
53+
BEUtils.SaveJSON(gn_js_path, js_output_data)
54+
55+
return True
56+
57+
else:
58+
print("Geometry Nodes were not Loaded! Please check Paths!")
59+
return False
60+
61+
62+
# start = time.time()
63+
64+
is_done = MainWork()
65+
66+
# end = time.time()
67+
# print(end - start)
68+
69+
if is_done:
70+
print("!PYTHON DONE!")
71+
else:
72+
print("!PYTHON ERROR!")
73+
74+
except Exception as e:
75+
print(traceback.format_exc())

BEngine-Py/BERunNodes.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
try:
2+
3+
import bpy
4+
5+
import traceback
6+
# from unicodedata import decimal
7+
8+
import sys
9+
import os
10+
11+
import json
12+
from json.decoder import JSONDecodeError
13+
14+
import time
15+
16+
import pathlib
17+
18+
try:
19+
from .Utils import BEUtils
20+
except:
21+
# Load BEUtils
22+
PACKAGE_PARENT = pathlib.Path(__file__).parent
23+
#PACKAGE_PARENT = pathlib.Path.cwd().parent # if on jupyter notebook
24+
SCRIPT_DIR = PACKAGE_PARENT
25+
sys.path.append(str(SCRIPT_DIR) + "/Utils")
26+
27+
import BEUtils
28+
29+
30+
def IsInstanceFromSelected(object_instance):
31+
# For instanced objects we check selection of their instancer (more accurately: check
32+
# selection status of the original object corresponding to the instancer).
33+
if object_instance.parent:
34+
return object_instance.parent.original.select_get()
35+
# For non-instanced objects we check selection state of the original object.
36+
return object_instance.object.original.select_get()
37+
38+
39+
def MainWork():
40+
context = bpy.context
41+
42+
BEUtils.ClearScene()
43+
44+
be_paths = BEUtils.BEPaths()
45+
46+
process_obj, geom_mod, bengine_GN = BEUtils.LoadGN(context, be_paths)
47+
48+
if (process_obj and bengine_GN):
49+
50+
# ORIGINAL STUFF
51+
with open(be_paths.be_tmp_folder + "BEngineInputs.json") as js_file:
52+
try:
53+
js_input_data = json.load(js_file)
54+
except JSONDecodeError:
55+
js_input_data = {}
56+
57+
engine_type = js_input_data["BEngineType"]
58+
59+
# Set Transform
60+
process_obj.location = js_input_data["Pos"]
61+
BEUtils.SetRotationFromJSON(process_obj, js_input_data["Rot"], engine_type)
62+
process_obj.scale = js_input_data["Scale"]
63+
64+
# Setup inputs
65+
BEUtils.SetupInputsFromJSON(context, bengine_GN, geom_mod,
66+
js_input_data["BEngineInputs"], be_paths, engine_type)
67+
# geom_mod.show_viewport = True
68+
69+
# Set the GN Object Active and Selected
70+
bpy.ops.object.select_all(action='DESELECT')
71+
process_obj.select_set(True)
72+
context.view_layer.objects.active = process_obj
73+
74+
process_obj.data.update()
75+
76+
# Save Node Outputs
77+
BEUtils.SaveBlenderOutputs(context, process_obj, be_paths, engine_type)
78+
79+
return True
80+
81+
else:
82+
print("Geometry Nodes were not Loaded! Please check Paths!")
83+
return False
84+
85+
86+
# start = time.time()
87+
88+
is_done = MainWork()
89+
90+
# end = time.time()
91+
# print(end - start)
92+
93+
if is_done:
94+
print("!PYTHON DONE!")
95+
else:
96+
print("!PYTHON ERROR!")
97+
98+
except Exception as e:
99+
print(traceback.format_exc())

0 commit comments

Comments
 (0)