Browse Source

updates material. null check if mesh names have changed (#315)

* updates material. null check if mesh names have changed

* using Material.clone() instead.

* fixing codacy issues

* combining if statements

* fixing codacy issues

* fixing codacy issues
Rickard Edén 3 years ago
parent
commit
d9fe5b9a49

+ 2 - 0
jme3-core/src/com/jme3/gde/core/assets/ExternalChangeScanner.java

@@ -128,7 +128,9 @@ public class ExternalChangeScanner implements AssetDataPropertyChangeListener, F
         try {
             Spatial original = loadOriginalSpatial();
             Spatial spat = (Spatial) assetDataObject.loadAsset();
+
             SpatialUtil.updateMeshDataFromOriginal(spat, original);
+            SpatialUtil.updateMaterialDataFromOriginal(spat, original);
             if (SpatialUtil.hasAnimations(original)) {
                 NotifyDescriptor.Confirmation mesg = new NotifyDescriptor.Confirmation("Model appears to have animations, try to import as well?\nCurrently this will unlink attachment Nodes and clear\nadded effects tracks.",
                         "Animations Available",

+ 30 - 0
jme3-core/src/com/jme3/gde/core/util/SpatialUtil.java

@@ -220,6 +220,32 @@ public class SpatialUtil {
         });
     }
 
+    /**
+     * Updates material of existing objects from an original file.
+     *
+     * @param root
+     * @param original
+     */
+    public static void updateMaterialDataFromOriginal(final Spatial root,
+                                                      final Spatial original) {
+        //loop through original to also find new geometry
+        original.depthFirstTraversal(new SceneGraphVisitorAdapter() {
+            @Override
+            public void visit(Geometry geom) {
+                //will always return same class type as 2nd param, so casting is safe
+                Geometry spat = (Geometry) findTaggedSpatial(root, geom);
+                if (spat != null && spat.getMaterial() != null
+                        && geom.getMaterial() != null
+                        && !spat.getMaterial().equals(geom.getMaterial())) {
+                    spat.setMaterial(geom.getMaterial().clone());
+                    logger.log(LogLevel.USERINFO,
+                            "Updated material for Geometry {0}", geom.getName()
+                    );
+                }
+            }
+        });
+    }
+
     /**
      * Adds a leaf to a spatial, including all nonexisting parents.
      *
@@ -241,6 +267,10 @@ public class SpatialUtil {
                 logger.log(Level.INFO, "Attaching {0} to {1} in root {2} to add leaf {3}", new Object[]{s, other, root, leaf});
                 //set original path data to leaf and new parents
                 for (Spatial spt = leaf; spt != parent; spt = spt.getParent()) {
+                    if (spt == null) {
+                        // this is to avoid a crash when changing mesh names
+                        return;
+                    }
                     spt.setUserData(ORIGINAL_NAME, spt.getName());
                     spt.setUserData(ORIGINAL_PATH, getSpatialPath(spt));
                     spt = spt.getParent();