Adjusting the pivot for proper object replacement

Adjusting the pivot for proper object replacement

SzakacsMark
Explorer Explorer
5,006 Views
9 Replies
Message 1 of 10

Adjusting the pivot for proper object replacement

SzakacsMark
Explorer
Explorer

Hello everyone, I have a task that I can't seem to solve.
The situation is as follows: I have a model consisting of 600-800 elements, each made up of 6 polygons. I would like to replace these with a model that has 2 polygons. If I break them down into 600-800 objects, their pivot points are all in one place. If I simply set the pivot to the center of each object, I can't maintain their current positions during the replacement process.
I would like to set their pivots according to their current positions (maybe to one polygon surface) so that during the replacement, the new model will be placed in the correct position. How can I do this? I searched online and tried writing a script, but I haven't succeeded. Does anyone have a solution? Could I be approaching this the wrong way? Thanks!

0 Likes
Accepted solutions (1)
5,007 Views
9 Replies
Replies (9)
Message 2 of 10

rendermaster
Advisor
Advisor

how about Substitute modifier

 


Ronel Katigbak Pabico

3D Generalist / Animator / Renderer/ Graphics Designer

Autodesk Student Ambassador Platinum
YouTube Channel l Linked l Facebook l Blogs

0 Likes
Message 3 of 10

MartinBeh
Advisor
Advisor

Is the vertex order the same for each element?

 

I.e. is, for example, vertex #1 at the same place within element #1, as vertex #21 is within element #2 (no idea how many vertices make up one of your element, but let's assume 20)? If so, maybe you could explode all elements into objects and then write a little MAXScript to place the pivot for each now object at the relative location of vertex #1 with each new object?

 

PS: If you could share a screenshot or a .max file, this would help to better understand your request.

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 4 of 10

strdr
Contributor
Contributor

I don't know how accurate you need it, but you can place pivot with some script in center, bottom/top then you need to reset xform on all the objects you need to be replaced and then replace them with the new object that has its pivot in center or bottom/top. But make sure you reset xform in order to use the new pivot positions.

0 Likes
Message 5 of 10

SzakacsMark
Explorer
Explorer

Unfortunately, the substitute modifier also uses the given pivot position. However, the vertices are in the same order, so it's worth a try. In the images, you can see that they are in a large pile, showing basically what position they are in; the objects I want to replace with, and roughly the pivot position I would like to achieve. I tried aligning to the bottom, center, and top, but it needs to be more precise, especially for the rectangular shapes. And 800 times. Has anyone encountered a situation like this? 🙂 Isn't there already a script for this?

 

0 Likes
Message 6 of 10

MartinBeh
Advisor
Advisor

Not 100% sure I understood your latest comments but give this a try:

 

1. Convert the object containing all the elements to Editable Mesh

2. In Modify panel of Editable Mesh set the angle next to the "Explode" button to 180 degrees and the "to:" radio button below it to "Objects"

3. Click "Explode" --> this should convert all your elements to separate objects

4. Select all newly exploded objects and convert them all to EPoly

5. With all new objects selected, run this piece of MAXScript code:

 

for o in selection do (
	in coordSys world p = polyop.getVert o 1
	o.pivot = p
	resetXform o
	collapseStack o
)

 

 This should set all the pivots to the location of the first vertex for each exploded element.

6. Now model your new geometry such that its pivot also is at the first vertex.

7. Replace all the exploded objects with your new geometry (again, a little MAXScript code might be what you want here; or the Substitute modifier)

 

There might be opportunities for using instances (instead of copies) in all this, and maybe you also need to set the pivot orientation (not only position), e.g. by using the first edge of vertex 1 as pivot x axis, and world +Z as pivot z axis. 

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 7 of 10

SzakacsMark
Explorer
Explorer

Yesss, thank you, that's close, the orientation is definitely needed; this is what I wasn't able to achieve.

0 Likes
Message 8 of 10

MartinBeh
Advisor
Advisor
Accepted solution

Maybe this works better for you?

 

Select your exploded objects, then run:

fn setPivotRotation obj tm = (
-- code by DenisT: https://forums.autodesk.com/t5/3ds-max-programming/align-object-s-pivot-to-object-s-orientation/td-p/12657785/page/2
	worldAlignPivot obj
	local rot = inverse tm.rotationpart
	
	animate off in coordsys local obj.rotation *= rot
	obj.objectoffsetrot *= rot
	obj.objectoffsetpos *= rot
	obj.objecttransform
)

fn getVecBetween obj vidx edgeidx = (	-- return normalized vector from vertex vidx along edge #edgeidx
	local p = polyop.getVert obj vidx
	-- get vector from first vertex along first egde
	local edg = (polyop.getEdgesUsingVert obj #{vidx} as array)[edgeidx] 		-- get n-th edge of vert #1
	local vrt2 = (((polyop.getVertsUsingEdge obj edg) - #{vidx}) as array)[1]	-- remaining (second) vertex of n-th edge
	local p2 = polyop.getVert obj vrt2	-- position of remaining vertex
	-- use vector as pivot x axis 
	local n = normalize (p2-p)
)

for o in selection do (
	format "Setting pivot on %...\n" o.name
	if (classOf o)!=Editable_Poly then ( convertTo o Editable_Poly)
	
	theVert = 1							-- use first vertex in mesh
	local p = polyop.getVert o theVert
	local n = getVecBetween o theVert 1		-- vector from first vertex along first edge
	
	-- use z axis as up vector to calculate remaining axes
	-- side = cross z_axis n
	-- better: use vector along second edge of vert #1
	local n2 = getVecBetween o theVert 2
	local side = cross n2 n

	-- calculate remaining orthognal vector
	local up = cross n side

	local tm = matrix3 n side up p
	setPivotRotation o tm
	o.pivot = p	
)

 

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
Message 9 of 10

SzakacsMark
Explorer
Explorer

Noooo wayyyy! You did it! It works! Thank you so much for your time, knowledge, and willingness to help! Tank you!

0 Likes
Message 10 of 10

MartinBeh
Advisor
Advisor

You are welcome, glad it worked.

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes