@ -38,7 +38,6 @@ import java.util.logging.Level;
import java.util.logging.Logger ;
import java.util.logging.Logger ;
import com.jme3.asset.AssetInfo ;
import com.jme3.asset.AssetInfo ;
import com.jme3.asset.AssetLoader ;
import com.jme3.asset.BlenderKey ;
import com.jme3.asset.BlenderKey ;
import com.jme3.asset.BlenderKey.FeaturesToLoad ;
import com.jme3.asset.BlenderKey.FeaturesToLoad ;
import com.jme3.asset.BlenderKey.LoadingResults ;
import com.jme3.asset.BlenderKey.LoadingResults ;
@ -67,38 +66,31 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper;
/ * *
/ * *
* This is the main loading class . Have in notice that asset manager needs to have loaders for resources like textures .
* This is the main loading class . Have in notice that asset manager needs to have loaders for resources like textures .
* @author Marcin Roguski
* @author Marcin Roguski ( Kaelthas )
* /
* /
public class BlenderLoader implements Asset Loader {
public class BlenderLoader extends AbstractBlender Loader {
private static final Logger LOGGER = Logger . getLogger ( BlenderLoader . class . getName ( ) ) ;
private static final Logger LOGGER = Logger . getLogger ( BlenderLoader . class . getName ( ) ) ;
/** Converter for blender structures. */
protected JmeConverter converter ;
/** The blender context. */
protected BlenderContext blenderContext ;
/** The blender key to use. */
protected BlenderKey blenderKey ;
/** The blocks read from the file. */
/** The blocks read from the file. */
protected List < FileBlockHeader > blocks ;
protected List < FileBlockHeader > blocks ;
/** Blender input stream. */
protected BlenderInputStream inputStream ;
@Override
@Override
public Spatial load ( AssetInfo assetInfo ) throws IOException {
public Spatial load ( AssetInfo assetInfo ) throws IOException {
try {
try {
this . setup ( assetInfo ) ;
this . setup ( assetInfo ) ;
BlenderKey blenderKey = blenderContext . getBlenderKey ( ) ;
LoadingResults loadingResults = blenderKey . prepareLoadingResults ( ) ;
LoadingResults loadingResults = blenderKey . prepareLoadingResults ( ) ;
WorldData worldData = null ; // a set of data used in different scene aspects
WorldData worldData = null ; // a set of data used in different scene aspects
for ( FileBlockHeader block : blocks ) {
for ( FileBlockHeader block : blocks ) {
switch ( block . getCode ( ) ) {
switch ( block . getCode ( ) ) {
case FileBlockHeader . BLOCK_OB00 : // Object
case FileBlockHeader . BLOCK_OB00 : // Object
Object object = converter . toObject ( block . getStructure ( blenderContext ) ) ;
Object object = this . toObject ( block . getStructure ( blenderContext ) ) ;
if ( object instanceof Node ) {
if ( object instanceof Node ) {
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . OBJECTS ) ! = 0 ) {
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . OBJECTS ) ! = 0 ) {
LOGGER . log ( Level . INFO , "{0}: {1}--> {2}" , new Object [ ] { ( ( Node ) object ) . getName ( ) , ( ( Node ) object ) . getLocalTranslation ( ) . toString ( ) , ( ( Node ) object ) . getParent ( ) = = null ? "null" : ( ( Node ) object ) . getParent ( ) . getName ( ) } ) ;
LOGGER . log ( Level . INFO , "{0}: {1}--> {2}" , new Object [ ] { ( ( Node ) object ) . getName ( ) , ( ( Node ) object ) . getLocalTranslation ( ) . toString ( ) , ( ( Node ) object ) . getParent ( ) = = null ? "null" : ( ( Node ) object ) . getParent ( ) . getName ( ) } ) ;
if ( ( ( Node ) object ) . getParent ( ) = = null ) {
if ( this . isRootObject ( loadingResults , ( Node ) object ) ) {
loadingResults . addObject ( ( Node ) object ) ;
loadingResults . addObject ( ( Node ) object ) ;
}
}
}
}
@ -114,12 +106,12 @@ public class BlenderLoader implements AssetLoader {
break ;
break ;
case FileBlockHeader . BLOCK_MA00 : // Material
case FileBlockHeader . BLOCK_MA00 : // Material
if ( blenderKey . isLoadUnlinkedAssets ( ) & & ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . MATERIALS ) ! = 0 ) {
if ( blenderKey . isLoadUnlinkedAssets ( ) & & ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . MATERIALS ) ! = 0 ) {
loadingResults . addMaterial ( converter . toMaterial ( block . getStructure ( blenderContext ) ) ) ;
loadingResults . addMaterial ( this . toMaterial ( block . getStructure ( blenderContext ) ) ) ;
}
}
break ;
break ;
case FileBlockHeader . BLOCK_SC00 : // Scene
case FileBlockHeader . BLOCK_SC00 : // Scene
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . SCENES ) ! = 0 ) {
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . SCENES ) ! = 0 ) {
loadingResults . addScene ( converter . toScene ( block . getStructure ( blenderContext ) ) ) ;
loadingResults . addScene ( this . toScene ( block . getStructure ( blenderContext ) ) ) ;
}
}
break ;
break ;
case FileBlockHeader . BLOCK_WO00 : // World
case FileBlockHeader . BLOCK_WO00 : // World
@ -127,7 +119,7 @@ public class BlenderLoader implements AssetLoader {
Structure worldStructure = block . getStructure ( blenderContext ) ;
Structure worldStructure = block . getStructure ( blenderContext ) ;
String worldName = worldStructure . getName ( ) ;
String worldName = worldStructure . getName ( ) ;
if ( blenderKey . getUsedWorld ( ) = = null | | blenderKey . getUsedWorld ( ) . equals ( worldName ) ) {
if ( blenderKey . getUsedWorld ( ) = = null | | blenderKey . getUsedWorld ( ) . equals ( worldName ) ) {
worldData = converter . toWorldData ( worldStructure ) ;
worldData = this . toWorldData ( worldStructure ) ;
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . LIGHTS ) ! = 0 ) {
if ( ( blenderKey . getFeaturesToLoad ( ) & FeaturesToLoad . LIGHTS ) ! = 0 ) {
loadingResults . addLight ( worldData . getAmbientLight ( ) ) ;
loadingResults . addLight ( worldData . getAmbientLight ( ) ) ;
}
}
@ -136,11 +128,7 @@ public class BlenderLoader implements AssetLoader {
break ;
break ;
}
}
}
}
try {
blenderContext . dispose ( ) ;
inputStream . close ( ) ;
} catch ( IOException e ) {
LOGGER . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
}
return loadingResults ;
return loadingResults ;
} catch ( BlenderFileException e ) {
} catch ( BlenderFileException e ) {
LOGGER . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
LOGGER . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
@ -148,6 +136,30 @@ public class BlenderLoader implements AssetLoader {
return null ;
return null ;
}
}
/ * *
* This method indicates if the given spatial is a root object . It means it
* has no parent or is directly attached to one of the already loaded scene
* nodes .
*
* @param loadingResults
* loading results containing the scene nodes
* @param spatial
* spatial object
* @return < b > true < / b > if the given spatial is a root object and
* < b > false < / b > otherwise
* /
protected boolean isRootObject ( LoadingResults loadingResults , Spatial spatial ) {
if ( spatial . getParent ( ) = = null ) {
return true ;
}
for ( Node scene : loadingResults . getScenes ( ) ) {
if ( spatial . getParent ( ) . equals ( scene ) ) {
return true ;
}
}
return false ;
}
/ * *
/ * *
* This method sets up the loader .
* This method sets up the loader .
* @param assetInfo
* @param assetInfo
@ -158,6 +170,7 @@ public class BlenderLoader implements AssetLoader {
protected void setup ( AssetInfo assetInfo ) throws BlenderFileException {
protected void setup ( AssetInfo assetInfo ) throws BlenderFileException {
// registering loaders
// registering loaders
ModelKey modelKey = ( ModelKey ) assetInfo . getKey ( ) ;
ModelKey modelKey = ( ModelKey ) assetInfo . getKey ( ) ;
BlenderKey blenderKey ;
if ( modelKey instanceof BlenderKey ) {
if ( modelKey instanceof BlenderKey ) {
blenderKey = ( BlenderKey ) modelKey ;
blenderKey = ( BlenderKey ) modelKey ;
} else {
} else {
@ -166,7 +179,7 @@ public class BlenderLoader implements AssetLoader {
}
}
// opening stream
// opening stream
inputStream = new BlenderInputStream ( assetInfo . openStream ( ) , assetInfo . getManager ( ) ) ;
BlenderInputStream inputStream = new BlenderInputStream ( assetInfo . openStream ( ) , assetInfo . getManager ( ) ) ;
// reading blocks
// reading blocks
blocks = new ArrayList < FileBlockHeader > ( ) ;
blocks = new ArrayList < FileBlockHeader > ( ) ;
@ -217,7 +230,5 @@ public class BlenderLoader implements AssetLoader {
int lay = ( ( Number ) sceneFileBlock . getStructure ( blenderContext ) . getFieldValue ( "lay" ) ) . intValue ( ) ;
int lay = ( ( Number ) sceneFileBlock . getStructure ( blenderContext ) . getFieldValue ( "lay" ) ) . intValue ( ) ;
blenderContext . getBlenderKey ( ) . setLayersToLoad ( lay ) ; // load only current layer
blenderContext . getBlenderKey ( ) . setLayersToLoad ( lay ) ; // load only current layer
}
}
converter = new JmeConverter ( blenderContext ) ;
}
}
}
}