Skip to content

Commit d1219f1

Browse files
committed
BE refactoring
1 parent 13ff635 commit d1219f1

3 files changed

Lines changed: 116 additions & 98 deletions

File tree

‎BEngine-Py/bengine/BERunNodes.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def RunNodes(context, js_inputs, node_tree, process_gn_obj, geom_mod, be_base_st
112112

113113
process_gn_obj.data.update()
114114

115-
js_output_data = BEUtils.SaveBlenderOutputs(context, [process_gn_obj], be_base_stuff.be_type, True)
115+
js_output_data = BEUtils.GetBlenderOutputs(context, [process_gn_obj], be_base_stuff.be_type, True)
116116
return js_output_data
117117

118118
# If SV
@@ -128,7 +128,7 @@ def RunNodes(context, js_inputs, node_tree, process_gn_obj, geom_mod, be_base_st
128128
context.view_layer.update()
129129
node_tree.process_ani(True, False)
130130

131-
js_output_data = BEUtils.SaveBlenderOutputs(context, BEUtils.GetSVOutputObjects(node_tree),
131+
js_output_data = BEUtils.GetBlenderOutputs(context, BEUtils.GetSVOutputObjects(node_tree),
132132
be_base_stuff.be_type, False)
133133
return js_output_data
134134

‎BEngine-Py/bengine/Utils/BEGeoUtils.py‎

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -203,55 +203,55 @@ def TerrainMeshFromJSON(js_obj, engine_type: EngineType):
203203
return new_mesh
204204

205205

206-
def MeshToJSONData(process_obj):
206+
def MeshToJSONData(mesh):
207207
mesh_dict = {} # Instances Dictionary
208208

209209
# Get Points
210-
np_verts = np.zeros(len(process_obj.data.vertices) * 3, dtype=np.float32)
211-
process_obj.data.vertices.foreach_get('co', np_verts)
212-
np_verts.shape = (len(process_obj.data.vertices), 3)
210+
np_verts = np.zeros(len(mesh.vertices) * 3, dtype=np.float32)
211+
mesh.vertices.foreach_get('co', np_verts)
212+
np_verts.shape = (len(mesh.vertices), 3)
213213
mesh_dict["Verts"] = np_verts.tolist()
214214

215215
# GET MESHES
216216
# MAIN Attributes
217217

218218
# Get Polygons
219-
poly_indices = [tuple(poly.vertices) for poly in process_obj.data.polygons]
219+
poly_indices = [tuple(poly.vertices) for poly in mesh.polygons]
220220
mesh_dict["PolyIndices"] = poly_indices
221221

222222
# # Get Polygons Loops
223-
# poly_loops = [tuple(poly.loop_indices) for poly in process_obj.data.polygons]
223+
# poly_loops = [tuple(poly.loop_indices) for poly in mesh.polygons]
224224
# mesh_dict["Loops"] = poly_loops
225225

226226
# Get Polygons Loop Start
227-
np_loop_start = np.empty(len(process_obj.data.polygons), dtype=np.int32)
228-
process_obj.data.polygons.foreach_get("loop_start", np_loop_start)
227+
np_loop_start = np.empty(len(mesh.polygons), dtype=np.int32)
228+
mesh.polygons.foreach_get("loop_start", np_loop_start)
229229
mesh_dict["LoopStart"] = np_loop_start.tolist()
230230

231231
# # Get Triangles Only
232-
# process_obj.data.calc_loop_triangles()
233-
# np_tris = np.zeros(len(process_obj.data.loop_triangles) * 3, dtype=np.int32)
234-
# process_obj.data.loop_triangles.foreach_get('vertices', np_tris)
235-
# np_tris.shape = (len(process_obj.data.loop_triangles), 3)
232+
# mesh.calc_loop_triangles()
233+
# np_tris = np.zeros(len(mesh.loop_triangles) * 3, dtype=np.int32)
234+
# mesh.loop_triangles.foreach_get('vertices', np_tris)
235+
# np_tris.shape = (len(mesh.loop_triangles), 3)
236236
# mesh_dict["PolyIndices"] = np_tris.tolist()
237237

238238
# # Get Loops of Triangles
239-
# np_tris_loops = np.zeros(len(process_obj.data.loop_triangles) * 3, dtype=np.int32)
240-
# process_obj.data.loop_triangles.foreach_get('loops', np_tris_loops)
241-
# np_tris_loops.shape = (len(process_obj.data.loop_triangles), 3)
239+
# np_tris_loops = np.zeros(len(mesh.loop_triangles) * 3, dtype=np.int32)
240+
# mesh.loop_triangles.foreach_get('loops', np_tris_loops)
241+
# np_tris_loops.shape = (len(mesh.loop_triangles), 3)
242242
# mesh_dict["Loops"] = np_tris_loops.tolist()
243243

244244
# Get Normals
245-
if BESettings.BENGINE_NORMAL in process_obj.data.attributes.keys():
246-
if process_obj.data.attributes[BESettings.BENGINE_NORMAL].domain == 'CORNER':
247-
np_normals = np.zeros(len(process_obj.data.attributes[BESettings.BENGINE_NORMAL].data) * 3, dtype=np.float32)
248-
process_obj.data.attributes[BESettings.BENGINE_NORMAL].data.foreach_get('vector', np_normals)
249-
np_normals.shape = (len(process_obj.data.attributes[BESettings.BENGINE_NORMAL].data), 3)
245+
if BESettings.BENGINE_NORMAL in mesh.attributes.keys():
246+
if mesh.attributes[BESettings.BENGINE_NORMAL].domain == 'CORNER':
247+
np_normals = np.zeros(len(mesh.attributes[BESettings.BENGINE_NORMAL].data) * 3, dtype=np.float32)
248+
mesh.attributes[BESettings.BENGINE_NORMAL].data.foreach_get('vector', np_normals)
249+
np_normals.shape = (len(mesh.attributes[BESettings.BENGINE_NORMAL].data), 3)
250250
else:
251-
print("Attribute " + BESettings.BENGINE_NORMAL + " must have FACECORNER domain!!!")
252-
np_normals = GetMeshNormalsNumpy(process_obj)
251+
print("Attribute " + BESettings.BENGINE_NORMAL + " must have FACECORNER domain. Taken Default Normals Instead!!!")
252+
np_normals = GetMeshNormalsNumpy(mesh)
253253
else:
254-
np_normals = GetMeshNormalsNumpy(process_obj)
254+
np_normals = GetMeshNormalsNumpy(mesh)
255255

256256
mesh_dict["Normals"] = np_normals.tolist()
257257

@@ -260,25 +260,25 @@ def MeshToJSONData(process_obj):
260260
np_col_attrib = None
261261

262262
# Get Attributes
263-
for attrib_name in process_obj.data.attributes.keys():
263+
for attrib_name in mesh.attributes.keys():
264264
if attrib_name in BESettings.UV_NAMES:
265265

266-
if process_obj.data.attributes[attrib_name].domain == 'CORNER':
267-
if (len(process_obj.data.attributes[attrib_name].data) > 0):
266+
if mesh.attributes[attrib_name].domain == 'CORNER':
267+
if (len(mesh.attributes[attrib_name].data) > 0):
268268

269-
# if process_obj.data.attributes[attrib_name].data_type == 'FLOAT':
269+
# if mesh.attributes[attrib_name].data_type == 'FLOAT':
270270
# uv_vec_len = 2
271271
# else:
272-
uv_vec_len = len(process_obj.data.attributes[attrib_name].data[0].vector)
272+
uv_vec_len = len(mesh.attributes[attrib_name].data[0].vector)
273273

274-
np_uv_attrib = np.zeros(len(process_obj.data.attributes[attrib_name].data) * uv_vec_len, dtype=np.float32)
274+
np_uv_attrib = np.zeros(len(mesh.attributes[attrib_name].data) * uv_vec_len, dtype=np.float32)
275275

276-
# if process_obj.data.attributes[attrib_name].data_type == 'FLOAT':
277-
# process_obj.data.attributes[attrib_name].data.foreach_get('value', np_uv_attrib)
276+
# if mesh.attributes[attrib_name].data_type == 'FLOAT':
277+
# mesh.attributes[attrib_name].data.foreach_get('value', np_uv_attrib)
278278
# else:
279-
process_obj.data.attributes[attrib_name].data.foreach_get('vector', np_uv_attrib)
279+
mesh.attributes[attrib_name].data.foreach_get('vector', np_uv_attrib)
280280

281-
np_uv_attrib.shape = (len(process_obj.data.attributes[attrib_name].data), uv_vec_len)
281+
np_uv_attrib.shape = (len(mesh.attributes[attrib_name].data), uv_vec_len)
282282

283283
uvs_dict[attrib_name] = np_uv_attrib.tolist()
284284

@@ -287,22 +287,22 @@ def MeshToJSONData(process_obj):
287287

288288
elif attrib_name == BESettings.BENGINE_COLOR:
289289

290-
if process_obj.data.attributes[attrib_name].domain == 'CORNER':
290+
if mesh.attributes[attrib_name].domain == 'CORNER':
291291
# col_attrib = [tuple(uv_attr.color) for uv_attr in process_obj_ev.data.attributes[attrib_name].data]
292292
# mesh_dict["VertexColor"] = col_attrib
293293

294-
np_col_attrib = np.zeros(len(process_obj.data.attributes[attrib_name].data) * 4, dtype=np.float32)
295-
process_obj.data.attributes[attrib_name].data.foreach_get('color', np_col_attrib)
296-
np_col_attrib.shape = (len(process_obj.data.attributes[attrib_name].data), 4)
294+
np_col_attrib = np.zeros(len(mesh.attributes[attrib_name].data) * 4, dtype=np.float32)
295+
mesh.attributes[attrib_name].data.foreach_get('color', np_col_attrib)
296+
np_col_attrib.shape = (len(mesh.attributes[attrib_name].data), 4)
297297
mesh_dict["VertexColors"] = np_col_attrib.tolist()
298298
else:
299299
print("Attribute " + attrib_name + " must have FACECORNER domain!!!")
300300

301301
# Setup bengine_material Value
302302
elif attrib_name == BESettings.BENGINE_MATERIAL:
303-
if process_obj.data.attributes[attrib_name].domain == 'FACE':
304-
np_mat_attrib = np.zeros(len(process_obj.data.attributes[attrib_name].data), dtype=np.uint8)
305-
process_obj.data.attributes[attrib_name].data.foreach_get('value', np_mat_attrib)
303+
if mesh.attributes[attrib_name].domain == 'FACE':
304+
np_mat_attrib = np.zeros(len(mesh.attributes[attrib_name].data), dtype=np.uint8)
305+
mesh.attributes[attrib_name].data.foreach_get('value', np_mat_attrib)
306306
mesh_dict["Materials"] = np_mat_attrib.tolist()
307307
else:
308308
print("Attribute " + attrib_name + " must have FACE domain!!!")
@@ -313,14 +313,14 @@ def MeshToJSONData(process_obj):
313313
return mesh_dict
314314

315315

316-
def GetMeshNormalsNumpy(process_obj):
316+
def GetMeshNormalsNumpy(mesh):
317317
# Calc Split Nomrals
318-
process_obj.data.calc_normals_split()
318+
mesh.calc_normals_split()
319319

320320
# GET NORMALS
321-
np_normals = np.zeros(len(process_obj.data.loops) * 3, dtype=np.float32)
322-
process_obj.data.loops.foreach_get("normal", np_normals)
323-
np_normals.shape = (len(process_obj.data.loops), 3)
321+
np_normals = np.zeros(len(mesh.loops) * 3, dtype=np.float32)
322+
mesh.loops.foreach_get("normal", np_normals)
323+
np_normals.shape = (len(mesh.loops), 3)
324324

325325
return np_normals
326326

‎BEngine-Py/bengine/Utils/BEUtils.py‎

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -493,89 +493,107 @@ def SetTransformFromJSON(js_obj, be_obj, engine_type: EngineType):
493493
be_obj.scale = js_obj["Scale"]
494494

495495

496-
def RecordObjectOutputToJSON(inst_dict, the_object, is_instance: bool):
496+
def RecordObjectOutputToJSON(objects_sets_dict, the_object, is_instance: bool,
497+
js_meshes: list, meshes_tmp_list: list, meshes_tmp_set: set):
497498

498499
if is_instance:
499-
the_mesh = the_object.object.data
500+
mesh = the_object.object.data
500501
obj_type = the_object.object.type
501502
true_obj = the_object.object
502503
else:
503-
the_mesh = the_object.data
504+
mesh = the_object.data
504505
obj_type = the_object.type
505506
true_obj = the_object
506507

507-
if the_mesh not in inst_dict.keys():
508-
inst_dict[the_mesh] = {}
509-
inst_dict[the_mesh]["Transforms"] = []
508+
# Add Default Keys
509+
if mesh not in objects_sets_dict.keys():
510+
objects_sets_dict[mesh] = {}
511+
objects_sets_dict[mesh]["Transforms"] = []
510512

511-
cur_inst_data = inst_dict[the_mesh]
513+
js_object_data = objects_sets_dict[mesh]
512514

513515
# Setup Transforms
514516
inst_pos, inst_rot, inst_scale = the_object.matrix_world.decompose()
517+
js_object_data["Transforms"].append((tuple(inst_pos), tuple(inst_rot.to_euler('XYZ')), tuple(inst_scale)))
515518

516-
cur_inst_data["Transforms"].append((tuple(inst_pos), tuple(inst_rot.to_euler('XYZ')), tuple(inst_scale)))
517-
518-
# Setup bengine_instance Value and Mesh
519+
# Get Mesh or BENGINE_INSTANCE
519520
if obj_type == "MESH":
520-
if BESettings.BENGINE_INSTANCE in the_mesh.attributes.keys():
521-
if BESettings.BENGINE_INSTANCE not in cur_inst_data.keys():
522-
if len(the_mesh.attributes[BESettings.BENGINE_INSTANCE].data) > 0:
523-
cur_inst_data["Bengine_Instance"] = the_mesh.attributes[BESettings.BENGINE_INSTANCE].data[0].value
524-
else:
525-
if "Mesh" not in cur_inst_data.keys():
526-
# GET INSTANCE MESH
527-
cur_inst_data["Mesh"] = BEGeoUtils.MeshToJSONData(true_obj)
521+
# Get BENGINE_INSTANCE Value
522+
if BESettings.BENGINE_INSTANCE in mesh.attributes.keys():
523+
if BESettings.BENGINE_INSTANCE not in js_object_data.keys():
524+
if len(mesh.attributes[BESettings.BENGINE_INSTANCE].data) > 0:
525+
526+
be_inst_id = mesh.attributes[BESettings.BENGINE_INSTANCE].data[0].value
527+
528+
if type(be_inst_id) is int:
529+
js_object_data["Bengine_Instance"] = be_inst_id
530+
else:
531+
print(BESettings.BENGINE_INSTANCE + " Attribute is not Integer!!!")
532+
533+
# Get Mesh to JSON
534+
elif "Mesh" not in js_object_data.keys():
535+
if mesh not in meshes_tmp_set:
536+
js_mesh = BEGeoUtils.MeshToJSONData(mesh)
537+
js_meshes.append(js_mesh)
538+
539+
js_object_data["Mesh"] = len(meshes_tmp_list)
540+
541+
meshes_tmp_list.append(mesh)
542+
meshes_tmp_set.add(mesh)
543+
544+
else:
545+
js_object_data["Mesh"] = meshes_tmp_list.index(mesh)
528546

529547

530-
def SaveBlenderOutputs(context, process_objs: list, engine_type: EngineType, is_GN: bool):
548+
def GetBlenderOutputs(context, process_objs: list, engine_type: EngineType, is_GN: bool):
531549

532550
depsgraph = context.evaluated_depsgraph_get()
533551

534552
# MAIN Data
535553
js_output_data = {} # Output JSON Data
536-
inst_dict = {} # Instances Dictionary
537-
meshes = [] # All Meshes
538-
539-
# parse_objs = [process_objs]
540-
541-
ev_objs = []
554+
objects_sets_dict = {} # Instances Dictionary
542555

543-
# GET Evaluated Objects
544-
for obj in process_objs:
545-
if obj.name in depsgraph.objects.keys():
546-
ev_objs.append(depsgraph.objects[obj.name])
547-
elif obj.name in bpy.data.objects.keys():
548-
ev_objs.append(bpy.data.objects[obj.name])
556+
meshes_tmp_list = []
557+
meshes_tmp_set = set()
558+
js_meshes_list = [] # All Parsed JSON Meshes
549559

560+
process_ev_objs = [depsgraph.objects[obj.name] for obj in process_objs]
561+
process_ev_objs_set = set(process_ev_objs)
550562

551-
ev_objs_set = set(ev_objs)
563+
# # GET MAIN MESH
564+
# if is_GN:
565+
# # GET MAIN MESH (FOR GN ONLY)!!!
566+
# main_mesh_dict = BEGeoUtils.MeshToJSONData(process_objs[0])
552567

553-
if is_GN:
554-
# GET MAIN MESH (FOR GN ONLY)!!!
555-
main_mesh_dict = BEGeoUtils.MeshToJSONData(ev_objs[0])
568+
# # If the main mesh has Verts/Polygons
569+
# # Only one mesh at the moment
570+
# if len(main_mesh_dict["Verts"]) > 0:
571+
# # meshes.insert(0, main_mesh_dict)
572+
# meshes.append(main_mesh_dict) # Add mesh to a list
556573

557-
# If the main mesh has Verts/Polygons
558-
# Only one mesh at the moment
559-
if len(main_mesh_dict["Verts"]) > 0:
560-
# meshes.insert(0, main_mesh_dict)
561-
meshes.append(main_mesh_dict) # Add mesh to a list
574+
# if meshes:
575+
# js_output_data["Meshes"] = meshes
576+
# else:
562577

563-
if meshes:
564-
js_output_data["Meshes"] = meshes
565-
else:
566-
# GET SVERCHOK OBJECS AND MESHES
567-
for obj in ev_objs:
568-
RecordObjectOutputToJSON(inst_dict, obj, False)
578+
# GET OBJECS AND MESHES
579+
for obj in process_ev_objs:
580+
RecordObjectOutputToJSON(objects_sets_dict, obj, False,
581+
js_meshes_list, meshes_tmp_list, meshes_tmp_set)
569582

570583
# GET INSTANCES
571584
for obj_instance in depsgraph.object_instances:
572-
if obj_instance.parent in ev_objs_set and obj_instance.is_instance:
573-
574-
RecordObjectOutputToJSON(inst_dict, obj_instance, True)
585+
if obj_instance.parent in process_ev_objs_set and obj_instance.is_instance:
586+
RecordObjectOutputToJSON(objects_sets_dict, obj_instance, True,
587+
js_meshes_list, meshes_tmp_list, meshes_tmp_set)
575588

576589
# Record Objects/Instances
577-
if inst_dict:
578-
js_output_data["Instances"] = list(inst_dict.values())
590+
if objects_sets_dict:
591+
js_output_data["ObjectsSets"] = list(objects_sets_dict.values())
592+
else:
593+
js_output_data["ObjectsSets"] = []
594+
595+
# Record Meshes
596+
js_output_data["Meshes"] = js_meshes_list
579597

580598
# gn_js_path = be_paths.be_tmp_folder + BESettings.OUTPUT_JSON_NAME
581599
# SaveJSON(gn_js_path, js_output_data)

0 commit comments

Comments
 (0)