Browse Source

Fixes #21. A way tighter integration with .fbx, .dae and .3ds by providing double click actions and a context menu for -> blend and -> j3o conversions.

MeFisto94 9 years ago
parent
commit
50c9d42002

+ 85 - 0
jme3-blender/src/com/jme3/gde/blender/ConvertToBlendAction.java

@@ -0,0 +1,85 @@
+/*
+ *  Copyright (c) 2009-2016 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.blender;
+
+import com.jme3.gde.blender.filetypes.AbstractBlenderImportDataObject;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.List;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionReferences;
+import org.openide.awt.ActionRegistration;
+import org.openide.filesystems.FileObject;
+import org.openide.loaders.MultiDataObject;
+import org.openide.util.NbBundle.Messages;
+
+@ActionID(
+        category = "jMonkeyPlatform",
+        id = "com.jme3.gde.blender.ConvertToBlendAction"
+)
+@ActionRegistration(
+        displayName = "#CTL_ConvertToBlendAction"
+)
+@ActionReferences({
+    @ActionReference(path = "Loaders/application/fbx/Actions", position = 150),
+    @ActionReference(path = "Loaders/model/vnd.collada+xml/Actions", position = 150),
+    @ActionReference(path = "Loaders/application/x-3ds/Actions", position = 150)
+})
+@Messages("CTL_ConvertToBlendAction=Convert to .blend file")
+
+/**
+ * The "Convert to .blend" Action is there for all files which are converted by the blender importers.
+ * It allows you to convert an .fbx/.3ds file into a .blend file so that you can modify the file before the final .j3o import.
+ * 
+ * @author MeFisto94
+ */
+public final class ConvertToBlendAction implements ActionListener {
+
+    private final List<MultiDataObject> context;
+
+    public ConvertToBlendAction(List<MultiDataObject> context) {
+        this.context = context;
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent ev) {
+        /* We need a context list because you can select multiple objects */
+        for (MultiDataObject multiDataObject : context) {
+            if (multiDataObject instanceof AbstractBlenderImportDataObject) {
+                AbstractBlenderImportDataObject obj = (AbstractBlenderImportDataObject)multiDataObject;
+                FileObject file = obj.importFile();
+                // BlenderTool.openInBlender(file);
+            }
+        }
+    }
+}

+ 117 - 0
jme3-blender/src/com/jme3/gde/blender/ConvertToJ3OAction.java

@@ -0,0 +1,117 @@
+/*
+ *  Copyright (c) 2009-2016 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.blender;
+
+import com.jme3.gde.blender.filetypes.AbstractBlenderImportDataObject;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.beans.PropertyChangeListener;
+import java.util.List;
+import javax.swing.Action;
+import org.openide.awt.ActionID;
+import org.openide.awt.ActionReference;
+import org.openide.awt.ActionReferences;
+import org.openide.awt.ActionRegistration;
+import org.openide.loaders.MultiDataObject;
+import org.openide.util.NbBundle.Messages;
+
+@ActionID(
+        category = "jMonkeyPlatform",
+        id = "com.jme3.gde.blender.ConvertToJ3OAction"
+)
+@ActionRegistration(
+        displayName = "#CTL_ConvertToJ3OAction"
+)
+@ActionReferences({
+    @ActionReference(path = "Loaders/application/fbx/Actions", position = 150),
+    @ActionReference(path = "Loaders/model/vnd.collada+xml/Actions", position = 150),
+    @ActionReference(path = "Loaders/application/x-3ds/Actions", position = 150)
+})
+@Messages("CTL_ConvertToJ3OAction=Convert to .j3o binary")
+
+/**
+ * The "Convert to .j3o" Action is there for all files which are converted by the blender importers.
+ * It allows you to convert an .fbx/.3ds file into a .j3o file using blenders import capabilites.
+ * 
+ * @author MeFisto94
+ */
+public final class ConvertToJ3OAction implements Action {
+
+    private final List<MultiDataObject> context;
+
+    public ConvertToJ3OAction(List<MultiDataObject> context) {
+        this.context = context;
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent ev) {
+        /* We need a context list because you can select multiple objects */
+        for (MultiDataObject multiDataObject : context) {
+            if (multiDataObject instanceof AbstractBlenderImportDataObject) {
+                AbstractBlenderImportDataObject obj = (AbstractBlenderImportDataObject)multiDataObject;
+                obj.loadAsset();
+            }
+        }
+    }
+
+    @Override
+    public Object getValue(String key) {
+        return null;
+    }
+
+    @Override
+    public void putValue(String key, Object value) {
+        
+    }
+
+    @Override
+    public void setEnabled(boolean b) {
+        
+    }
+
+    @Override
+    public boolean isEnabled() {
+        return true;
+    }
+
+    @Override
+    public void addPropertyChangeListener(PropertyChangeListener listener) {
+        
+    }
+
+    @Override
+    public void removePropertyChangeListener(PropertyChangeListener listener) {
+        
+    }
+    
+    
+}

+ 127 - 31
jme3-blender/src/com/jme3/gde/blender/filetypes/AbstractBlenderImportDataObject.java

@@ -1,29 +1,66 @@
 /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
+ *  Copyright (c) 2009-2016 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package com.jme3.gde.blender.filetypes;
 
 import com.jme3.asset.BlenderKey;
 import com.jme3.gde.blender.BlenderTool;
+import com.jme3.gde.blender.ConvertToJ3OAction;
+import com.jme3.gde.core.assets.AssetDataNode;
 import com.jme3.gde.core.assets.ProjectAssetManager;
 import com.jme3.gde.core.assets.SpatialAssetDataObject;
 import com.jme3.gde.core.util.Beans;
 import com.jme3.gde.core.util.SpatialUtil;
 import com.jme3.scene.Spatial;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.logging.Level;
+import javax.swing.Action;
 import org.openide.DialogDisplayer;
 import org.openide.NotifyDescriptor;
-import org.openide.filesystems.FileLock;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataObject;
 import org.openide.loaders.DataObjectExistsException;
+import org.openide.loaders.MultiDataObject;
 import org.openide.loaders.MultiFileLoader;
+import org.openide.nodes.Children;
+import org.openide.nodes.Node;
 import org.openide.util.Exceptions;
+import org.openide.util.Lookup;
+import org.openide.util.lookup.ProxyLookup;
 
 /**
- *
+ * The AbstractBlenderImportDataObject handles all possible Filetypes which should be automatically converted by Blender's Importer.
+ * You have to extend this class and just set the correct suffix. See: {@link BlenderFbxDataObject}
  * @author normenhansen
  */
 public abstract class AbstractBlenderImportDataObject extends SpatialAssetDataObject {
@@ -39,38 +76,12 @@ public abstract class AbstractBlenderImportDataObject extends SpatialAssetDataOb
         if (savable != null) {
             return (Spatial) savable;
         }
-        if (SUFFIX == null) {
-            throw new IllegalStateException("Suffix for blender filetype is null! Set SUFFIX = \"sfx\" in constructor!");
-        }
         ProjectAssetManager mgr = getLookup().lookup(ProjectAssetManager.class);
         if (mgr == null) {
             DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("File is not part of a project!\nCannot load without ProjectAssetManager."));
             return null;
         }
-        //make sure its actually closed and all data gets reloaded
-        closeAsset();
-        FileObject mainFile = getPrimaryFile();
-        if (!BlenderTool.runConversionScript(SUFFIX, mainFile)) {
-            logger.log(Level.SEVERE, "Failed to create model, running blender caused an error");
-            return null;
-        }
-        mainFile.getParent().refresh();
-        FileObject outFile = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX);
-        if (outFile == null) {
-            logger.log(Level.SEVERE, "Failed to create model, blend file cannot be found");
-            return null;
-        }
-        int i = 1;
-        FileObject blend1File = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX + i);
-        while (blend1File != null) {
-            try {
-                blend1File.delete();
-            } catch (IOException ex) {
-                Exceptions.printStackTrace(ex);
-            }
-            i++;
-            blend1File = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX + i);
-        }
+        FileObject outFile = importFile(mgr);
         String assetKeyName = mgr.getRelativeAssetPath(outFile.getPath());
         BlenderKey key = new BlenderKey(assetKeyName);
         Beans.copyProperties(key, getAssetKey());
@@ -84,6 +95,7 @@ public abstract class AbstractBlenderImportDataObject extends SpatialAssetDataOb
             logger.log(Level.INFO, "Loaded asset {0}", getName());
             return spatial;
         } catch (Exception ex) {
+            logger.log(Level.SEVERE, ex.getMessage(), ex);
             Exceptions.printStackTrace(ex);
         } finally {
             try {
@@ -116,4 +128,88 @@ public abstract class AbstractBlenderImportDataObject extends SpatialAssetDataOb
             }
         }
     }
+    
+    /**
+     * Use this method to convert this file into a blender model
+     * Note: This requires a bit of additional code (creating the PAM) so use the version without parameters if you don't plan to modify the blend further
+     * @param mgr The AssetManager which provides access to the Project Files
+     * @return The FileObject of the .blend file
+     */
+    public FileObject importFile(ProjectAssetManager mgr) {
+        if (SUFFIX == null) {
+            throw new IllegalStateException("Suffix for blender filetype is null! Set SUFFIX = \"sfx\" in constructor!");
+        }
+        
+        //make sure its actually closed and all data gets reloaded
+        closeAsset();
+        FileObject mainFile = getPrimaryFile();
+        
+        if (FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX) != null) {
+            logger.log(Level.SEVERE, "Cannot convert " + getName() + " to .blend because there is already a file with this name. Delete it and try again!");
+            return null;
+        }
+        
+        if (!BlenderTool.runConversionScript(SUFFIX, mainFile)) {
+            logger.log(Level.SEVERE, "Failed to create model, running blender caused an error");
+            return null;
+        }
+        mainFile.getParent().refresh();
+        FileObject outFile = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX);
+        if (outFile == null) {
+            logger.log(Level.SEVERE, "Failed to create model, blend file cannot be found");
+            return null;
+        }
+        int i = 1;
+        FileObject blend1File = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX + i);
+        while (blend1File != null) {
+            try {
+                blend1File.delete();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+            i++;
+            blend1File = FileUtil.findBrother(mainFile, BlenderTool.TEMP_SUFFIX + i);
+        }
+        
+        return outFile;
+    }
+    
+    /**
+     * Use this method to simply convert any supported file into a .blend file.
+     * @return The FileObject for the converted .blend file
+     */
+    public FileObject importFile() {
+        ProjectAssetManager mgr = getLookup().lookup(ProjectAssetManager.class);
+        if (mgr == null) {
+            DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("File is not part of a project!\nCannot load without ProjectAssetManager."));
+            return null;
+        }
+        FileObject outFile = importFile(mgr);
+        return outFile;
+    }
+
+    @Override
+    protected Node createNodeDelegate() {
+        MyAssetDataNode node = new MyAssetDataNode(this, Children.LEAF, new ProxyLookup(getCookieSet().getLookup(), contentLookup));
+        node.setIconBaseWithExtension("com/jme3/gde/core/icons/model.gif");
+        return node;
+    }
+    
+    private class MyAssetDataNode extends AssetDataNode {
+        public MyAssetDataNode(DataObject obj, Children ch) {
+            super(obj, ch);
+        }
+
+        public MyAssetDataNode(DataObject obj, Children ch, Lookup lookup) {
+            super(obj, ch, lookup);
+        }
+
+        @Override
+        public Action getPreferredAction()
+        {
+            ArrayList<MultiDataObject> ctx = new ArrayList<MultiDataObject>();
+            ctx.add((MultiDataObject)getDataObject());
+            return new ConvertToJ3OAction(ctx);
+        }   
+    }
 }