How to generate objects based on a .csv or excel file

How to generate objects based on a .csv or excel file

Anonymous
Not applicable
6,057 Views
8 Replies
Message 1 of 9

How to generate objects based on a .csv or excel file

Anonymous
Not applicable

Hi all,

 

I need to create >1000 objects in 3DSMax. All of them are cube shaped, but with different dimensions and textures.

 

I have an excel sheet with the object name and the dimensions in XYZ. I also have a folder with the textures for each face of the object, named in a way that corresponds with the excel file.

 

Is there a way to write a script to automatically generate these objects based on the data?

 

I've tried Google, but without much luck.

 

Many thanks in advance

0 Likes
Accepted solutions (1)
6,058 Views
8 Replies
Replies (8)
Message 3 of 9

leeminardi
Mentor
Mentor
Accepted solution

Creating objects with data from an Excel file is fairly straightforward.  The goal is to create 3ds Max instructions in Excel that can be run as a script.

 

The first thing I do is to turn on the Max Script Listener Scripting -->  MaxScript Listener... 

 

The next step is to do the action you want to duplicate with the script.  Since you want to add a bunch of boxes I would add one box and see what the Max Script Listener shows.  Here's an example"

 

Box lengthsegs:1 widthsegs:1 heightsegs:1 length:4.64166 width:7.40208 height:7.6287 mapcoords:on pos:[74.7074,-24.8917,0] isSelected:on

 

Let's assume my Excel file has the following layout for the box data:

ExcelBoxes1.JPG

 

By using the Excel concatenate function we can build the box creation command in G3.  For this example it would look like this:

 

=CONCATENATE("Box lengthsegs:1 widthsegs:1 heightsegs:1  length:",A3," width:",B3," height:",C3," mapcoords:on pos:[",D3,",",E3,",",F3,"]")

 

Excel cell G3 would show:


Box lengthsegs:1 widthsegs:1 heightsegs:1 length:11 width:5 height:17.8 mapcoords:on pos:[25.738,24.075,16.81]

 

Fill this expression down to the other rows of data and then copy and paste the contents of column G to Notepad.

 

ExcelBoxes2.JPG

 

Save the Notepad file as a .txt file and then change the name of the file so that its file extension is .ms (for Max Script).

 

You can now use Scripting -->  Run Script to execute the script and see the boxes!

 

Use listener to see how materials are assigned to objects and use concatenate to duplicate that process.

 

Good luck!

 

~Lee

 

lee.minardi
Message 4 of 9

Anonymous
Not applicable

Hi,

 

This will be perfect for what I need to do!

 

Thank you very much for your detailed response. It works in practice, I'll let you know how it goes when I implement it for everything.

 

Thanks again.

0 Likes
Message 5 of 9

leeminardi
Mentor
Mentor

Be careful with the commas in Concatenate and that the result duplicates the the Max format.

 

I'd like to hear how this works out for you.

 

Lee 

lee.minardi
0 Likes
Message 6 of 9

Alfred.DeFlaminis
Alumni
Alumni

Hello @Anonymous and welcome to the community,

 

I just wanted to follow up on this, did this work for you?  (Excellent post btw @leeminardi!)  If this did work for you, can you please let me know so I (or you) can mark the post as a solution for the next person who needs this information?  Thanks very much!

Best Regards,

Message 7 of 9

zvonimirpetrovic89
Participant
Participant

How to generate objects with x, y, z coordinates and rotation from an excel file?

Maybe it requires just a small modification of this script? Thank you in advance.

0 Likes
Message 8 of 9

leeminardi
Mentor
Mentor

Working with rotations in 3D is tricky. The order in which you do rotations can affect an object’s final orientation. For example, if a box is rotated first by 30° about the x axis, then 25° about the y axis and then 10° it will have a different 3D orientation than a box that is first rotated by 10° about the z axis then 25° about the y axis and finally 30° about the z axis. For this reason I would rather deal with an object’s transformation matrix. This requires that you know the direction vectors for each of the local axes of the object.

 

However, if you want to work with angle rotations then you can just use the following statements to rotate box Box001 by 30° about the x axis, then 25 about y and 10 about z:

Rotate $Box001 30.0 [1,0,0]
Rotate $Box001 25.0 [0,1,0]
Rotate $Box001 10.0 [0,0,1]


If you know the transformation matrix that you would like to assign to an object you can do that directly. For example, let’s say we would like to give Box002 the same orientation as Box001. The following will assign the transformation matrix of Box001 to the variable m.

 

m = $Box001.transform

 


The matrix m will look something like this:

 

(matrix3 [0.892539,0.157379,-0.422618] [0.0577152,0.889562,0.453154] [0.447262,-0.428849,0.784886] [200,200.0,100.83])

 

It consists of 4 vectors. The first three define the directions of the object’s local x, y, and z axes. These vectors are usually unit vectors if the object has not been scaled. The 4th vector is the position vector.
If we assign m to Box002 it will be at the same location as Box001 as well as the same orientation. To keep the position unchanged we extract the location of Box002 with:

 

m.pos = $Box002.pos

 

and now assign m to Box002:

 

$Box002.transform = m

 

Now Box002 has the same orientation as Box001 but its position is unchanged.
Depending on whether you would like to work with angles (knowing their limitations) or matrices is up to you. If you provide a sample Excel file with data for a few boxes and how you want to set their orientation I can take a shot at adding a column that generates a Maxscript.

lee.minardi
0 Likes
Message 9 of 9

leeminardi
Mentor
Mentor

@zvonimirpetrovic89

 I've added the option to include rotation angles in an Excel worksheet that will be referenced when creating a Maxscript.

The content of cell J3 is:

=CONCATENATE("Box length:",A3," width:",B3," height:",C3," mapcoords:on pos:[",D3,",",E3,",",F3,"] isSelected:on",CHAR(13),"rotate $ ",G3," [1,0,0]",CHAR(13),"rotate $ ",H3," [0,1,0]",CHAR(13),"rotate $ ",I3," [0,0,1]")

Note that the CHAR(13) is used to add a carriage return to create a new line for the rotate functions.

image.png

To use the Excel file as the source of a Maxscript copy the contents of cells J3 down to a text editor. I use Notepad++.  Before saving the file BE SURE TO DELETE ALL " MARKS (use replace " with nothing) then save with the .ms filename extension.

image.png

For best results, keep rotation angles between -90° and +90°.

lee.minardi
0 Likes