[Closed] Attach mesh to an empty mesh
I have a textured mesh msh and a script like this:
newmsh=mesh numverts:0 numfaces:0
meshop.attach newmsh msh attachMat:#IDToMat condenseMat:true deleteSourceNode:true
it worcks, but after attaching the UVmaping is completlly f*cked up. My curent solution is using attach without deleteSourceNode option, then copy all avaible UVmaps including vertexColors manualy from msh to newmsh and after that deleting msh. The current solution is really complicated, so is there a way to make meshop.attach worck properlly?
Do you have the same result if you specify the source node of mesh?
meshop.attach newmsh msh sourceNode:Your_msh_SourceNode attachMat:#IDToMat condenseMat:true deleteSourceNode:true
There is little to explain…
You are using “deleteSourceNode:true”, so I guess you have a source node for your mesh. Just wondering if you have the same result with the additional parameter “sourceNode:<put here Your msh SourceNode”
OK, now I see, just copy pasted that comand from some example in maxscript reference page. Howewer if I attach manually the mesh to that empty mesh, have the same rsult with the same problem. With manually I mean not using maxscript, exept for creating the empty mesh.
I don´t think that “deleteSourceNode:true” is the reason for my problem, as when I have 2 not empty meshes msh1 and msh2 and execute:
meshop.attach msh1 msh2 attachMat:#IDToMat condenseMat:true deleteSourceNode:true
Everithing worcks fine.
Posible reason for the problem could be that newmesh has no UVmaping, so UVmaping is completly ignored when attaching. I tried to fixe it by using stuff from reference page, like enable mapSuport, but was not really succesfull. Like already mentioned, my maxscript is very low, so posibly I understood something wrong?
Why do you want to attach n empty mesh to another mesh, what are you trying to achieve?
Not wanting to attach empty mesh to an other mesh, as this make no sence in my opinion, but attach a mash to an empty mesh, which is complete diferent case. There are many reasons for attaching a mesh to empty one. For me the main reason is fixing rotation or position of a mesh. Here an example:
rotate msh rot_obj
newmsh=mesh numverts:0 numfaces:0
meshop.attach newmsh msh attachMat:#IDToMat condenseMat:true deleteSourceNode:true
Other reason is to remoove unused materials from mesh.
Does this work?
newmsh = mesh numverts:3 numfaces:1
meshop.attach newmsh msh attachMat:#IDToMat condenseMat:true deleteSourceNode:true
meshop.deleteverts newmsh #{1..3}
For node transform does this work for you?
msh.pivot = [0,0,0]
resetxform msh
converttomesh msh
I´ll try it, thanks.
Have a question about the code for node transform, what it´s doing? In my example I am fixing rotation, but this one seems to be related to position.
Also don´t I need to enable UVmap suport for newmsh?
OK, thanks it seems to worck perfect, at least for examples I tried. Your solution for node transform is also much better that mine. Is there posibly a beter solution for remooving unused materials, then my one?
In your example, when you create a new mesh “newmsh”, it is created with an identity transformation and everything you attach to it will inherit that transformation. This is not always necessary, but as you used it I tried to get the same result.
msh.pivot = [0,0,0]
Moves the pivot to the center of the world so it is the same as in your example. Then resetXForm() will reset all the transformations of the object.
I think the problem was that the mesh was empty. If it has at least 1 face everything seems to work well.
OK, thanks. However the empty mesh not always using identity transformation, an other exaple is when I am having a helper object (dummy) and want to attach meshes to it. The dummy in general can have any transformation, which I copy to the empty mesh.
Honestly, I’ve never seen a case where a mesh holds a material that does not use.
Do you have an example that can be reproduced or do you want to remove unused materials from the scene instead?
Sorry my english is really bad, however i try to explain:
I am importin a mesh from a file. The mesh has a multimaterial. In max I am editing the mesh, so some faces can get deleted and as posible result the material(s) used by that faces could become useles. Hope my explanation is clear enough.
So you want to remove unused sub-materials. I wouldn’t trust attaching to an empty mesh would do a good job. If two or more objects uses the same Multimaterial, things will be messed up.
As this is a very common task, there have been several proposed approaches over the years. Max also has a Utility for that “Clean Multimaterial”, at least since Max 2010.
You may want to do a search on the forum or google for the solution, I am sure more than one answer will come up.
Sorry, posibly I misunderstood something again, but the empty mesh has no materials, so same Multimaterial should not be posible. However I agree that my solution is not really good, OK to be honest it´s bad, but the only one I found. That “Clean Multimaterial” is useles for me, since it not worcking with older max versions.
(
/* Delete all objects */
delete objects
/* Create a multimaterial with 4 submaterials */
mat = multimaterial numsubs:4
mat[1].diffusecolor = red
mat[2].diffusecolor = green
mat[3].diffusecolor = blue
mat[4].diffusecolor = yellow
/* Create an object and assign the multimaterial */
obj1 = converttomesh (plane length:100 width:100 pos:[0,0,10] lengthsegs:2 widthsegs:2 material:mat)
/* Set the faces material IDs */
for j = 1 to 8 by 2 do
(
id = (j/2)+1
setfacematid obj1 j id
setfacematid obj1 (j+1) id
)
/* Copy the object so [b]we have two objects using the same material[/b] */
obj2 = copy obj1 pos:[150,0,10]
/* Delete some faces of the object, so we have something to clen up */
meshop.deletefaces obj2 #{1..6}
/* YOUR CODE TO CLEAN UP MATERIALS ---------------------------------------------- */
newmsh = mesh numverts:3 numfaces:1
meshop.attach newmsh obj2 attachMat:#IDToMat condenseMat:true deleteSourceNode:true
meshop.deleteverts newmsh #{1..3}
/* ------------------------------------------------------------------------------ */
)
The result is:
- Not all unused materials are cleaned up
- The second object using the same multilateral lost half of them
If you need it to work with very old Max versions. it would help if you specify for what version you need the code to work.
You can still search for the solution to this problem. It has been addressed many times.
I see that point, since I am not making copys of meshes it was never a problem for me.
When I am tallking about older max versions I mean posibly all versions, but at least version 5 or 6. I know that versions are really old.
No meter what some people think or even say about me, I am not such an idiot, that unable to use google, but my search was succesles.
You don’t have to copy an object to have two objects usingthe same material, it was just an example.
OK, but for me personally it play no role, since I never have 2 meshes using the same materials, exept the example with the copy.