@@ -313,6 +313,8 @@ def LoadNodesTreeFromJSON(context, be_paths: BEPaths):
313313def SetupInputsFromJSON (context , node_tree , GN_mod , js_input_data ,
314314 be_paths : BEPaths , engine_type : str ):
315315
316+ js_inputs = js_input_data ["BEngineInputs" ]
317+
316318 is_GN = node_tree .bl_idname == TYPE_GN
317319
318320 coll_idx = 0
@@ -324,32 +326,34 @@ def SetupInputsFromJSON(context, node_tree, GN_mod, js_input_data,
324326 else :
325327 input_items = GetSVInputNodes (node_tree )
326328
329+ instanced_meshes = {}
330+
327331 for input in input_items :
328332
329333 if is_GN :
330334 node_id = input [1 ].identifier
331335 else :
332336 node_id = input [1 ].node_id
333337
334- if node_id in js_input_data .keys ():
338+ if node_id in js_inputs .keys ():
335339
336340 # Check if the same type
337341 if is_GN :
338- is_same_type = (input [1 ].type == js_input_data [node_id ]["Type" ])
342+ is_same_type = (input [1 ].type == js_inputs [node_id ]["Type" ])
339343
340344 if not is_same_type :
341- print ("!Input " + input [1 ].name + " is not the same type! " + input [1 ].type + " " + js_input_data [node_id ]["Type" ])
345+ print ("!Input " + input [1 ].name + " is not the same type! " + input [1 ].type + " " + js_inputs [node_id ]["Type" ])
342346 continue
343347
344348 else :
345- is_same_type = (input [0 ] == js_input_data [node_id ]["Type" ])
349+ is_same_type = (input [0 ] == js_inputs [node_id ]["Type" ])
346350
347351 if not is_same_type :
348- print ("!Input " + input [1 ].name + " is not the same type! " + input [0 ] + " " + js_input_data [node_id ]["Type" ])
352+ print ("!Input " + input [1 ].name + " is not the same type! " + input [0 ] + " " + js_inputs [node_id ]["Type" ])
349353 continue
350354
351355 # Run Main Stuff
352- js_prop = js_input_data [node_id ]
356+ js_prop = js_inputs [node_id ]
353357
354358 if "Value" in js_prop .keys () and js_prop ["Value" ] is not None :
355359
@@ -394,7 +398,9 @@ def SetupInputsFromJSON(context, node_tree, GN_mod, js_input_data,
394398 case "OBJECT" :
395399 js_obj_val = js_prop ["Value" ]
396400
397- be_objs , has_mesh = ParseObjectFromJSON (context , js_obj_val , engine_type , False , True )
401+ be_objs , has_mesh = ParseObjectFromJSON (context , js_obj_val ,
402+ js_input_data , instanced_meshes ,
403+ engine_type , False , True )
398404
399405 # Join Meshes
400406 bpy .ops .object .select_all (action = 'DESELECT' )
@@ -427,7 +433,9 @@ def SetupInputsFromJSON(context, node_tree, GN_mod, js_input_data,
427433 be_coll = bpy .data .collections .new ('BEngine_' + str (coll_idx ))
428434 context .scene .collection .children .link (be_coll )
429435
430- be_coll_objs , has_mesh = ParseObjectFromJSON (context , js_coll_val , engine_type , True , False )
436+ be_coll_objs , has_mesh = ParseObjectFromJSON (context , js_coll_val ,
437+ js_input_data , instanced_meshes ,
438+ engine_type , True , False )
431439
432440 for obj in be_coll_objs :
433441 be_coll .objects .link (obj )
@@ -471,16 +479,21 @@ def SetupInputsFromJSON(context, node_tree, GN_mod, js_input_data,
471479 # GN_mod[input[1].identifier] = prop_value
472480
473481
474- def ParseObjectFromJSON (context , js_obj_val , engine_type : str , isCollection : bool , convert_to_meshes = False ):
482+ def ParseObjectFromJSON (context , js_obj_val , js_input_data , instanced_meshes ,
483+ engine_type : str , isCollection : bool ,
484+ convert_to_meshes = False ):
475485 has_mesh = False
476486
477487 be_objs = []
478488
479489 # Check If it has Mesh
480490 for js_obj in js_obj_val :
481- if "Mesh" in js_obj and "Verts" in js_obj ["Mesh" ] and js_obj ["Mesh" ]["Verts" ]:
482- has_mesh = True
483- break
491+ if "Mesh" in js_obj :
492+ tmp_jsmesh = js_input_data ["Meshes" ][js_obj ["Mesh" ]]
493+
494+ if "Verts" in tmp_jsmesh and tmp_jsmesh ["Verts" ]:
495+ has_mesh = True
496+ break
484497
485498 # if hasMesh:
486499 for i , js_obj in enumerate (js_obj_val ):
@@ -490,21 +503,34 @@ def ParseObjectFromJSON(context, js_obj_val, engine_type: str, isCollection: boo
490503
491504 # Import Mesh
492505 if "Mesh" in js_obj :
493- be_mesh_obj = ImportMeshFromJSON (context , js_obj , engine_type )
506+ if js_obj ["Mesh" ] not in instanced_meshes .keys ():
507+ js_mesh = js_input_data ["Meshes" ][js_obj ["Mesh" ]]
508+ be_mesh = MeshFromJSON (js_mesh , engine_type )
509+
510+ instanced_meshes [js_obj ["Mesh" ]] = be_mesh
511+ else :
512+ be_mesh = instanced_meshes [js_obj ["Mesh" ]]
513+
514+ be_mesh_obj = ObjectFromJSON (js_obj , be_mesh , engine_type , True )
515+
494516 be_objs .append (be_mesh_obj )
495517
496518 # Import Curves
497519 if "Curves" in js_obj :
498520 if (convert_to_meshes ):
499- be_curves_obj = ImportCurvesFromJSON ( context , js_obj , engine_type , bool (1 - has_mesh ))
521+ be_curves_data = CurvesFromJSON ( js_obj , engine_type , bool (1 - has_mesh ))
500522 else :
501- be_curves_obj = ImportCurvesFromJSON (context , js_obj , engine_type , True )
523+ be_curves_data = CurvesFromJSON (js_obj , engine_type , True )
524+
525+ be_curves_obj = ObjectFromJSON (js_obj , be_curves_data , engine_type , True )
502526
503527 be_objs .append (be_curves_obj )
504528
505529 # Import Terrain
506530 if "Terrain" in js_obj :
507- be_terr_obj = ImportTerrainFromJSON (context , js_obj , engine_type )
531+ be_terr_mesh = TerrainMeshFromJSON (js_obj , engine_type )
532+ be_terr_obj = ObjectFromJSON (js_obj , be_terr_mesh , engine_type , False )
533+
508534 be_objs .append (be_terr_obj )
509535
510536 if be_mesh_obj is None and be_curves_obj is None and be_terr_obj is None :
@@ -532,11 +558,12 @@ def ParseObjectFromJSON(context, js_obj_val, engine_type: str, isCollection: boo
532558
533559 be_objs .append (be_empty_main_obj )
534560
561+ instanced_meshes = None # Clear Meshes
562+
535563 return be_objs , has_mesh
536564
537565
538- def ImportMeshFromJSON (context , js_obj , engine_type : str ):
539- js_mesh = js_obj ["Mesh" ]
566+ def MeshFromJSON (js_mesh , engine_type : str ):
540567
541568 if "Verts" in js_mesh and js_mesh ["Verts" ]:
542569 verts_len = len (js_mesh ["Verts" ])
@@ -597,16 +624,12 @@ def ImportMeshFromJSON(context, js_obj, engine_type: str):
597624 else :
598625 new_mesh = bpy .data .meshes .new ('BESubMesh' )
599626
600- be_mesh_obj = bpy .data .objects .new (js_obj ["Name" ], new_mesh )
601-
602- SetTransformFromJSON (js_obj , be_mesh_obj , engine_type )
603-
604627 # context.collection.objects.link(be_mesh_obj)
605628
606- return be_mesh_obj
629+ return new_mesh
607630
608631
609- def ImportCurvesFromJSON ( context , js_obj , engine_type : str , import_as_curve : bool ):
632+ def CurvesFromJSON ( js_obj , engine_type : str , import_as_curve : bool ):
610633
611634 js_curv = js_obj ["Curves" ]
612635 js_curve_elems = js_curv ["CurveElements" ]
@@ -660,15 +683,10 @@ def ImportCurvesFromJSON(context, js_obj, engine_type: str, import_as_curve: boo
660683
661684 be_curv_data .from_pydata (verts , edges , [])
662685
663- # Add Curve Object
664- be_curve_obj = bpy .data .objects .new (js_obj ["Name" ], be_curv_data )
686+ return be_curv_data
665687
666- SetTransformFromJSON (js_obj , be_curve_obj , engine_type )
667688
668- return be_curve_obj
669-
670-
671- def ImportTerrainFromJSON (context , js_obj , engine_type : str ):
689+ def TerrainMeshFromJSON (js_obj , engine_type : str ):
672690 js_terr = js_obj ["Terrain" ]
673691
674692 if "Verts" in js_terr and js_terr ["Verts" ]:
@@ -687,16 +705,12 @@ def ImportTerrainFromJSON(context, js_obj, engine_type: str):
687705 new_mesh = bpy .data .meshes .new ("BESubMesh" )
688706 bm .to_mesh (new_mesh )
689707 bm .free ()
690-
691- print (verts_len , len (np_verts ), len (new_mesh .vertices ))
692708
693709 new_mesh .vertices .foreach_set ('co' , np_verts )
694710
695711 new_mesh .update ()
696712
697- be_mesh_obj = bpy .data .objects .new (js_obj ["Name" ], new_mesh )
698-
699- return be_mesh_obj
713+ return new_mesh
700714
701715
702716# Create Mesh
@@ -734,6 +748,15 @@ def CreateMesh(verts_len, polys_len, np_verts, np_poly_indices, np_normals):
734748 return mesh
735749
736750
751+ def ObjectFromJSON (js_obj , mesh , engine_type , do_transform : bool ):
752+ be_mesh_obj = bpy .data .objects .new (js_obj ["Name" ], mesh )
753+
754+ if do_transform :
755+ SetTransformFromJSON (js_obj , be_mesh_obj , engine_type )
756+
757+ return be_mesh_obj
758+
759+
737760def SetTransformFromJSON (js_obj , be_obj , engine_type : str ):
738761 be_obj .location = js_obj ["Pos" ]
739762 SetRotationFromJSON (be_obj , js_obj ["Rot" ], engine_type )
0 commit comments