Index  | Recent Threads  | List Attachments  | Search
 Welcome Guest  |  Register  |  Login
Login Name  Password
 

Sweet Home 3D Forum



No member browsing this thread
Thread Status: Active
Total posts in this thread: 5
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 4730 times and has 4 replies Next Thread
Mitsaki
Member




Joined: Jul 26, 2011
Post Count: 62
Status: Offline
Reply to this Post  Reply with Quote 
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?
[Jan 13, 2012, 1:41:37 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Mitsaki
Member




Joined: Jul 26, 2011
Post Count: 62
Status: Offline
Reply to this Post  Reply with Quote 
Re: How to export to .obj all furniture seperately.

No need to try to answer this question. Tomorrow, I will try to explain what I did finally:)
[Jan 14, 2012, 11:41:34 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Mitsaki
Member




Joined: Jul 26, 2011
Post Count: 62
Status: Offline
Reply to this Post  Reply with Quote 
Re: How to export to .obj all furniture seperately.

So, what I finally did and it worked is:

I first added in the HomePane.java

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

public static String objFile
;


Afterwards, I changed the HomeController.java and especially exporttoObj

public void exportToOBJ() {

// 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
//-------------------------------COUNT THE FURNITURE------------------------------------
HomePane.m = 0;
HomePane.objFile = "";
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
// In the end m shows how many furniture there are
HomePane.m++;
}
}

// I add one, so that I can store "Room" in it.
HomePane.m = HomePane.m + 1;
System.out.printf (HomePane.m + "\n");
//------------------------------------SAVE THE ARRAY------------------------------------
// A table used to save the names of the furniture with the "m" that we counted before
String [] temp = new String[HomePane.m + 1];
temp[0] = "Room";

HomePane.o = 1;
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible()) {
temp[HomePane.o] = piece.getName();
//System.out.printf (HomePane.o + "\n");
// System.out.printf(temp[HomePane.o] + "\n");
HomePane.o++;
}
}

String [] Furniture = new String[HomePane.m];
int z;
for (z = 0; z < HomePane.m ; z++)
{
Furniture[z] = temp[z];
}

// l is used for the array Furniture[m] and it will be used again
HomePane.o = 0;

// For the room
//-----------------------------------CALL THE FUNCTION----------------------------------
for (HomePane.o = 0; HomePane.o < HomePane.m ; HomePane.o++)
{
// System.out.printf (HomePane.o + "\n");
// System.out.printf(Furniture[HomePane.o] + "\n");
HomePane.a = Furniture[HomePane.o];
HomePane.objFile = Furniture[HomePane.o];
// System.out.printf(objFile + "\n");
// Use a clone of home to ignore selection
final String objName = getView().showExportToOBJDialog(this.home.getName());
if (objName != null) {
// Export 3D view in a threaded task
Callable<Void> exportToObjTask = new Callable<Void>() {
public Void call() throws RecorderException {
getView().exportToOBJ(objName);
return null;
}
};
ThreadedTaskController.ExceptionHandler exceptionHandler =
new ThreadedTaskController.ExceptionHandler() {
public void handleException(Exception ex) {
if (!(ex instanceof InterruptedRecorderException)) {
if (ex instanceof RecorderException) {
String message = preferences.getLocalizedString(
HomeController.class, "exportToOBJError", objName);
getView().showError(message);
} else {
ex.printStackTrace();
}
}
}
};
new ThreadedTaskController(exportToObjTask,
this.preferences.getLocalizedString(HomeController.class, "exportToOBJMessage"), exceptionHandler,
this.preferences, this.viewFactory).executeTask(getView());
}
}

}


and then I changed the HomePane.java and especially private static class OBJExporter

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);
// System.out.printf(objFile + "\n");
if (objFile.equals("./My_Home/Room.obj"))
{
System.out.printf(objFile + "\n");
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");
}
}

// Write 3D walls
int i;


// The first time, we want to export the only the walls and the room
if (objFile.equals("./My_Home/Room.obj"))
{
i = 0;
System.out.printf("Bhka sto walls\n");
System.out.printf(objFile + "\n");
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);
}

// Write 3D rooms
i = 0;
for (Room room : home.getRooms()) {
System.out.printf("Bhka sto 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);
}
}

i = 0;
System.out.printf(objFile + "\n");
if (!objFile.equals("./My_Home/Room.obj"))
{
System.out.printf("Bhka sto Furniture1\n");
//Write 3D furniture
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isVisible() && objFile.equals("." + File.separator + "My_Home" + File.separator + piece.getName() + ".obj")) {
System.out.printf("Bhka sto Furniture2\n");
// System.out.printf(objFile + "\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, home, true, true);
writer.writeNode(pieceNode);
}
}
}
} 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);
}
}
}
}


That's more or less it.
[Jan 20, 2012, 5:08:46 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
chicosRV
Newbie




Joined: May 25, 2012
Post Count: 14
Status: Offline
Reply to this Post  Reply with Quote 
confused Re: How to export to .obj all furniture seperately.

hello I used the last code you posted and yes, everything is exported seperately but all the .obj are in blank, only with a header and i am confused about it, are you sure this code work allright???, please i need a solution.
[May 25, 2012, 5:16:11 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
Mitsaki
Member




Joined: Jul 26, 2011
Post Count: 62
Status: Offline
Reply to this Post  Reply with Quote 
Re: How to export to .obj all furniture seperately.

Well, maybe you should check it again, because I use these furniture to import them in a program called blender and all of them appear perfectly.
[Jun 4, 2012, 2:14:23 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread

  Get Sweet Home 3D at SourceForge.net. Fast, secure and Free Open Source software downloads  
© Copyright 2024 Space Mushrooms - All rights reserved