[Closed] Export explicit normals
replacement proof mesh rebuilding routine adds in vertex colour, uv verts and normals.
--*****************************************************************************************
-- test the routine by building a new mesh based on the data
fn BuildMesh verts faces =
(
nverts = verts.count;
nfaces = faces.count/3;
msh = mesh numverts:nverts numfaces:nfaces;
-- geometry
for v = 1 to nverts do setvert msh v verts[v].pos;
fi = 1;
for f = 1 to faces.count by 3 do
(
setface msh fi faces[f] faces[f+1] faces[f+2];
setEdgeVis msh fi 1 true;
setEdgeVis msh fi 2 true;
setEdgeVis msh fi 3 true;
fi += 1;
)
-- color per vertex
meshop.setMapSupport msh 0 true;
meshop.setNumMapVerts msh 0 nverts;
meshop.setNumMapFaces msh 0 nfaces;
for v = 1 to nverts do meshop.setMapVert msh 0 v ((verts[v].col as point3)/255.0);
fi = 1;
for f = 1 to faces.count by 3 do
(
meshop.setMapFace msh 0 fi [faces[f], faces[f+1], faces[f+2]]
fi += 1;
)
-- texture verts
meshop.setMapSupport msh 1 true;
meshop.setNumMapVerts msh 1 nverts;
meshop.setNumMapFaces msh 1 nfaces;
for v = 1 to nverts do meshop.setMapVert msh 1 v verts[v].uvw;
fi = 1;
for f = 1 to faces.count by 3 do
(
meshop.setMapFace msh 1 fi [faces[f], faces[f+1], faces[f+2]]
fi += 1;
)
update msh;
-- add the normals
select msh;
modpanel.addmodtoselection (Nmod = edit_normals displayLength:0)
disableRefMsgs();
for v = 1 to nverts do
(
Nmod.SetNormal v verts[v].normal;
Nmod.SetNormalExplicit v explicit:true;
)
fi = 1;
for f = 1 to faces.count by 3 do
(
for c = 1 to 3 do Nmod.SetNormalID fi c faces[f + c - 1];
fi += 1;
)
enableRefMsgs();
ConverttoMesh msh;
)
you also need to add
select msh;
after the call to buildmesh in the ExportMesh function
added very rudimentary obj export
--**************************************************************************************
fn ExportAsObj objName verts find =
(
numverts = verts.count;
fname = (GetDir #export) + "/" + objName + ".obj";
fstream = openFile fname mode:"w+";
if fstream != undefined then
(
format "# 3ds Max Wavefront OBJ Exporter v100001 - (c)whenever klunkware
" to:fstream;
format "# File Created:%
" localTime to:fstream;
format "
# verts
" to:fstream;
for v = 1 to numverts do format "v % % %
" verts[v].pos.x verts[v].pos.y verts[v].pos.z to:fstream;
format "
# normals
" to:fstream;
for v = 1 to numverts do format "vn % % %
" verts[v].normal.x verts[v].normal.y verts[v].normal.z to:fstream;
format "
# uvw
" to:fstream;
for v = 1 to numverts do format "vt % % %
" verts[v].uvw.x verts[v].uvw.y verts[v].uvw.z to:fstream;
format "
# faces
" to:fstream
format "g %
" objName to:fstream;
for f = 1 to find.count by 3 do
format "f %/%/% %/%/% %/%/%
" find[f] find[f] find[f] find[f+1] find[f+1] find[f+1] find[f+2] find[f+2] find[f+2] to:fstream;
close fstream;
)
)
--**************************************************************************************
fn ExportMesh mObj = if canConvertTo mObj Editable_Mesh then
(
-- grab the mesh
msh = snapshot mObj;
-- compute the inverse tm so we can get positions in local space
localTM = (inverse (msh.transform));
-- check for color per vertex if not there create a default
if getNumCPVVerts msh == 0 then defaultVCFaces msh;
-- get the number of mat id's
matids = GetMeshMatIDs msh;
-- get normals
select msh;
setCommandPanelTaskMode mode:#modify
modpanel.addmodtoselection (norm = edit_normals displayLength:0);
for m in matids do
(
faces = GetFacesWithMatID msh m;
raw = CollectRawVerts msh faces norm localTM;
opt = OptimizeVerts raw;
indices = CreateFaces raw opt;
ExportAsObj (mObj.name + "_" + m as string) opt indices;
)
delete msh;
)
so the answer was staring at me in the face the entire time
EDITNORMAL=Edit_Normals()
addModifier trimesh EDITNORMAL
for EN = 1 to trimesh.Edit_Normals.GetNumNormals() do print ( trimesh.Edit_Normals.GetNormal EN )
I now have a new problem, maybe its just that im really tired and I cant see it but ive been looking at this for about an hour now and I cant figure out why its only printing out the explicit normals on a selected editable mesh
OK sorry for the vagueness… What I mean is that im doing an export and I want to add these modifiers in the background, use their data and collapse the stack at the end of the script, here is what I got which technically works but only on 1 selected mesh and I don’t know why.
addModifier geometry(Edit_Normals())
for obj in geometry do(
for v = 1 to obj.numverts do(
zz=obj.edit_normals.getnormal v
print zz
)
)
collapsestack geometry
In the code I do not want to have to select the meshes and deselect them.
I feel like the problem is obj. before edit_normals but I have tried “edit_normals.getnormal obj v” and it gives me an error.
the object must be selected and you must have the command panel in modify mode otherwise the edit normals modifier cannot be accessed with maxscript.
ah well I at least tried to avoid selecting things with my script but I guess it cant be avoided, at least it works. Is there at least a way to automatically select the mod panel so that it doesn’t confuse people whenever I decide to release my exporter?
*edit, I discovered the answer
max modify mode