Print at Dec 20, 2025, 12:16:21 AM

Posted by Mitsaki at Jan 13, 2012, 1:41:37 PM
confused   How to export to .obj all furniture seperately.
Hello,

what I am trying to do now (you can also see my previous question) is to export first the home with the walls and then one by one all the furniture. So, what I have changed is the script HomePane.java, first by setting 4 public variables.

// Used in the export
public static int k = 0;
public static int m = 0;
public static int l = 0;
public static String a;


and afterwards I have changed the following

/**
* Exports the objects of the 3D view to the given OBJ file.
*/
public void exportToOBJ(String objFile) throws RecorderException {
System.out.printf("Exporttoobj\n");
String header = this.preferences != null
? this.preferences.getLocalizedString(HomePane.class,"exportToOBJ.header", new Date()): "";

// Added from this point...
// In the beginning k=0, l=0, m=0
// Count how many pieces of furniture are placed in the house during the first control
objFile = "";
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// In the end m shows how many furniture there are
m++;
}
}

// A table used to save the names of the furniture with the "m" that we counted before
String [] Furniture = new String[m];

a = "";
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// "a" is used to save the name of the furniture piece
a = piece.getName();
Furniture[l] = a;
//System.out.printf(Furniture[m]);
l++;
}
}
// l is used for the array Furniture[m]
l = 0;

//for(k=0;k<l+1;k++)
//{
// Use a clone of home to ignore selection
// For the room
if(k==0)
{
objFile = "room";
//System.out.printf(objFile + "\n");
OBJExporter.exportHomeToFile(home.clone(), objFile, header);
}
if(k>0 && k<=m)
{
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// "a" is used to save the name of the furniture piece
String a = piece.getName();
Furniture[l] = a;
objFile = Furniture[l];
OBJExporter.exportHomeToFile(home.clone(), objFile, header);
//System.out.printf(Furniture[m]);
l++;
}
//}
}
}
}


/**
* Export to OBJ in a separate class to be able to run HomePane without Java 3D classes.
*/
private static class OBJExporter {
public static void exportHomeToFile(Home home, String objFile, String header) throws RecorderException {
OBJWriter writer = null;
boolean exportInterrupted = false;
try {
writer = new OBJWriter(objFile, header, -1);

List<Selectable> emptySelection = Collections.emptyList();
home.setSelectedItems(emptySelection);
if (home.getWalls().size() > 0) {
// Create a not alive new ground to be able to explore its coordinates without setting capabilities
Rectangle2D homeBounds = getExportedHomeBounds(home);
Ground3D groundNode = new Ground3D(home,
(float)homeBounds.getX(), (float)homeBounds.getY(),
(float)homeBounds.getWidth(), (float)homeBounds.getHeight(), true);
writer.writeNode(groundNode, "ground");
}
int i = 0;
// The first time, we want to export the only the walls and the room
if (k==0)
{
System.out.printf("Walls\n");


// Write 3D walls

for (Wall wall : home.getWalls()) {
// Create a not alive new wall to be able to explore its coordinates without setting capabilities
Wall3D wallNode = new Wall3D(wall, home, true, true);
writer.writeNode(wallNode, "wall_" + ++i);
}
}
System.out.println(m);
i = 0;
if (k>0 && k<=m)
{
//Write 3D furniture
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible() && a == piece.getName()) {
System.out.print(k + "\n");
System.out.printf("Furniture\n");
// Create a not alive new piece to be able to explore its coordinates without setting capabilities
// I set as home = null
HomePieceOfFurniture3D pieceNode = new HomePieceOfFurniture3D(piece, null, true, true);
writer.writeNode(pieceNode);
k++;
}
}
}

if (k==0)
{
// Write 3D rooms
i = 0;
for (Room room : home.getRooms()) {
System.out.printf("Room\n");
// Create a not alive new room to be able to explore its coordinates without setting capabilities
Room3D roomNode = new Room3D(room, home, false, true, true);
writer.writeNode(roomNode, "room_" + ++i);
k++;
}
}
} catch (InterruptedIOException ex) {
exportInterrupted = true;
throw new InterruptedRecorderException("Export to " + objFile + " interrupted");
} catch (IOException ex) {
throw new RecorderException("Couldn't export to OBJ in " + objFile, ex);
} finally {
if (writer != null) {

try {
writer.close();
// Delete the file if exporting is interrupted
if (exportInterrupted) {
new File(objFile).delete();
}
} catch (IOException ex) {
throw new RecorderException("Couldn't export to OBJ in " + objFile, ex);
}
}
}
}


The result is that everything is exported, I mean all the furniture and the room seperately, but the saved file has kind: Document, according to info. Furthermore, I can open it with txt.

Could give me a hint for what I am doing wrong?