Blender model have two materials,uv maps and textures, how switch texture?

Since you forgot the texture, then an example without textures.

	Spatial Model = assetManager.loadModel("player-test-fix.j3o");
	rootNode.attachChild(Model);

	SceneGraphVisitor searchGeomMat = new SceneGraphVisitor() {

		public void visit(Spatial spatial) {

			System.out.println("Geometry =" + spatial);

			if (spatial instanceof Geometry) {

				Geometry geom = (Geometry) spatial;

				System.out.println("Material =" + geom.getMaterial());

			}

		}

	};

	Model.depthFirstTraversal(searchGeomMat);
1 Like

Geometry =base.obj1 (Geometry)
Material =Army
Geometry =base.obj2 (Geometry)
Material =null
Geometry =base.obj3 (Geometry)
Material =null
Geometry =base.obj4 (Geometry)
Material =null
Geometry =base.obj (Node)
Geometry =Armature (Node)
Geometry =Lamp (LightNode)
Geometry =Camera (CameraNode)

Thank’s.

model have only assigned in blender material.

I think need:

  1. assign material
  2. save (serialize) material
  3. load needed material
1 Like

Here’s an example of replacing a material, if you want to do it later, just assign it a variable.

	Spatial Model = assetManager.loadModel("player-test-fix.j3o");
	rootNode.attachChild(Model);

	SceneGraphVisitor searchGeomMat = new SceneGraphVisitor() {

		public void visit(Spatial spatial) {

			if (spatial instanceof Geometry) {

				Geometry geom = (Geometry) spatial;

				if (geom.getMaterial().getName().equals("Spec")) {

					Material mat = geom.getMaterial();
					Model.setMaterial(mat);

				}

			}

		}

	};

	Model.depthFirstTraversal(searchGeomMat);

EDIT: This can be interesting.

https://jmonkeyengine.github.io/wiki/jme3/advanced/save_and_load.html

1 Like

Thanks. Have null and army:

Think use this:

  1. get material
  2. serialize
  3. load material

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package custom;

import com.jme3.material.Material;
import java.io.Serializable;

/**
*

  • @author nn
    */
    public class MaterisalSerializable extends Material implements Serializable {

}

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package custom;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Base64;

/**
*

  • @author nn
    */
    public class Serialize {

    private static Object fromString( String s ) throws IOException, ClassNotFoundException {
    byte data = Base64.getDecoder().decode( s );
    ObjectInputStream ois = new ObjectInputStream(
    new ByteArrayInputStream( data ) );
    Object o = ois.readObject();
    ois.close();
    return o;
    }

/** Write the object to a Base64 string. */
private static String toString( Serializable o ) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream( baos );
    oos.writeObject( o );
    oos.close();
    return Base64.getEncoder().encodeToString(baos.toByteArray()); 
}

}

1 Like

I do not think it’s right, it’s better to get all the parameters of the material and make the file readable in text form. You can edit it later with your hands. And it’s better to create it manually at once.

1 Like

Material file contains big lists and hash maps.

Write material with hands - big work.

Simple serialize object to string

1 Like

Node node = (Node) assetManager.loadModel(modelPath);
Geometry myGeo = (Geometry)node.getChild(“base.obj1”);

        Material material = myGeo.getMaterial();
        
        MaterisalSerializable materialSerializ = (MaterisalSerializable) material;

java.lang.RuntimeException: Uncompilable source code - incompatible types: com.jme3.material.Material cannot be converted to custom.MaterisalSerializable

But MaterisalSerializable extends Material

1 Like

As you propose to do?

1 Like

System.out.println(mat.getParams());

Based on them, create a material file: j3m

Material My shiny custom material : Common/MatDefs/Light/Lighting.j3md {
     MaterialParameters {
        DiffuseMap : Textures/Terrain/Pond/Pond.jpg
        NormalMap : Textures/Terrain/Pond/Pond_normal.png
        UseMaterialColors : true
        Specular : 1.0 1.0 1.0 1.0
        Diffuse : 1.0 1.0 1.0 1.0
        Shininess : 64.0
     }
}

The problem can arise with obtaining all the parameters.

1 Like

Maybe i get all parameters from material.
Add to class for save (serrialize).
Add set when it need?

1 Like

I have nothing to say.

1 Like

Thank’s i believe i solve this problem. I post answer later

1 Like

Next error

1 Like

I try to:

  1. create material
  2. add map
  3. add change in scene composer

material switch but uv coordinates no asinged

1 Like

This video:

1 Like

I generate tangents change texture position on model
not by uv in blender

1 Like