Retrieving the transform values used with the scatter compound object

Retrieving the transform values used with the scatter compound object

braisJSL2A
Participant Participant
2,087 Views
7 Replies
Message 1 of 8

Retrieving the transform values used with the scatter compound object

braisJSL2A
Participant
Participant

Hi all 🙂

 

I'm using the Scatter compound object to randomly place an object on top of another (I'm specifically scattering flowers on the branches of a plant). What I'd love to do is to be able to write down the transform values used to scatter the flowers, so basically the position, rotation and scale of the different instances that I'm scattering, just so I can do the same on a different program. Problem is, I have no idea how to do this!

 

Would you guys know of a way to tackle this problem?

 

Best, 

 

Brais

0 Likes
Accepted solutions (1)
2,088 Views
7 Replies
Replies (7)
Message 2 of 8

leeminardi
Mentor
Mentor

I don't have a full solution but have some ideas.  

First, be sure that the display units are the same as the system units in your file.  If they are not you may get numerical confusing numerical values.

 

This link provides some information about finding the position of a vertex in local or world coordinates.

 

It's interesting to note that each vertex of an editable meshed object has a transform associated with it although the vertex is just a point and conceptually does not have an orientation.   I could not determine the Maxscript syntax to use to get the transform of the numbered vertex of an editable mesh.    As a kludge I tried the following.

 

I converted a small box (0.1 pre side) to an editable mesh.   I then noted where vertices 1, 2, 3, and 5 were. A vector from 1 to 2 to defines the x axis, 1 to 3 the Y axis and 1 to 5 the z axis. 

To experiment with your task I meshed a teapot object and then attached it to the meshed box I previously created.  The result is a meshed object that I know I can use the first three vertices to build the transformation matrix.  I also noted the total number of vertices in the object. In my case it was 538.

 

Compound Scatter was then used to create a model with multiple instance of the special meshed teapot.  The resulting object was then meshed.  The position of the various instances can be determined by looking at vertices 1, 539 (1 + 538), 1077 (1 + 2 x 538), 1296, etc.   

The position of the vertices can be found with

v_pos = (in coordys world getvert obj n)

 where n is the number of the vertex.

  

To build the matrix for the first row of the transform matric just define a unit* vector for vertex 2 - vertex 2, row 2 is 3 -1, row 3 is 5 - 1, and row 4 is vertex 1.

 

The process is repeated for the other instance by adding multiples of the total vertex count (538 in my example) to the four vertices 1, 2, 3, and 5.

 

image.png

A script could be written that asks the user for the number of vertices per instance and the number of instances.  From this information it could output a series of transformation matrices.

 

* Note, usually the transformation matrix has unit vectors for rows 1 through 3.  If the instance is scaled then the scale factor can be determined by the length of the three vector 1 to 2, 1 to 3, and 1 to 5.

lee.minardi
Message 3 of 8

braisJSL2A
Participant
Participant

Hi @leeminardi! Thanks a lot for your reply, your approach reads very plausible and I'm looking forward to testing it. Thanks again, I'll try to put your ideas into practice and see how I manage - I'll come back to the thread as soon as I make some progress! 

0 Likes
Message 4 of 8

leeminardi
Mentor
Mentor

Glad to hear my suggestion may have potential for you.

I noticed a few typos in my post.

 

“...use the first three vertices to build the transformation matrix”

 

Should read:

“...use the first three and fifth vertices to build the transformation matrix”

 

and

 

“To build the matrix for the first row of the transform matric just define a unit* vector for vertex 2 - vertex 2, row 2 is 3 -1, row 3 is 5 - 1, and row 4 is vertex 1.”

 

Should be

 

“The first row of the transform matrix is vertex 2 - vertex 1, row 2 is vertex 3 - vertex 1, row 3 is vertex 5 - vertex 1, and row 4 is vertex 1.”  

 

Note, these vectors should be normalized if you do not want scaling.

lee.minardi
Message 5 of 8

leeminardi
Mentor
Mentor
Accepted solution

I created a draft Maxscript to output the transforms as outlined above. The input is primitive as the number of vertices per "instance" and the number of instances is hard coded.  You can test it on the attached Max file.

 

Here is the script.

 

-- creates a file of the transformed of scattered objects converted to a single meshed object. 
--  draft version  9/20/2021
nV = 538 as integer  -- number of vertices per instance
obj= selection[1]
outputFile = getSaveFileName()
if outputFile != undefined then
(outputFile = createfile outputFile)
nObj = 4 as integer  -- number of scattered objects
		vi = 1 as integer
			for k = 1 to nObj do (
				v1 = (in coordsys world getvert obj vi)
				v2 = (in coordsys world getvert obj (vi + 1))
				v3 = (in coordsys world getvert obj (vi + 2))
				v5 = (in coordsys world getvert obj (vi + 4))
				vx = v2 - v1
				vy = v3 - v1
				vz	= v5 - v1				
				m = (matrix3 vx vy vz v1)
				format "\n %" m to:outputFile
				
				vi = vi +  nV
				format "\n vi = % " vi
			)
	format "\nDone\n"
	close outputFile

 

 

lee.minardi
Message 6 of 8

braisJSL2A
Participant
Participant

Thanks a lot for that @leeminardi, I was able to take a look at the script and the scene you sent last night and it looked very promising. I think I understand how everything works, it should now be a matter of me formatting the first three rows in a way that lets me send this data from one program to the next - we have the vectors in rows 1 through 3 that can be used to determine the rotation of the instances, I should now be able to re purpose that information and create a rotator out of it.

 

I'll see if I can continue to work on this and will post any results once I have them 🙂

0 Likes
Message 7 of 8

braisJSL2A
Participant
Participant

Hi @leeminardi! Just getting back to this thread as I managed to take your script and use it for the purposes that I wanted - export the data out of 3ds Max to be able to replicate the scatter behaviour with other instances. This is the spreadsheet that I managed to create using your script: 

 

braisJSL2A_0-1632317752388.png

 

The location value is something that your script already provided me with, and the only thing that I needed to do was to calculate the Rotation value (as you see, that field is in a different format to the one that the script outputs) from the transform matrix. I only used the first row thrown by your script, as I found that the X vector was enough to calculate the orientation of the object (or the amount by which I need to rotate something in order for that something to be facing in the same direction as the original scattered instance).

 

All in all, I just wanted to say thanks for your help once again.

 

Best, 

 

Brais

0 Likes
Message 8 of 8

leeminardi
Mentor
Mentor

@braisJSL2A  thanks for the feedback.  It's nice to know I was of help.  It was a fun and interesting challenge to find a solution.

 

Best regards,

Lee

lee.minardi
0 Likes