[Closed] setup texture & uv to PolyObject using max sdk
How to make this operation correct?
I’m trying to use MNMap, but texture doesnt appear on my object in viewport
PolyObject *planeObj = CreateEditablePolyObject();
MNMesh &mm = planeObj->GetMesh();
mm.NewVert(Point3(pt_list[0].x(), pt_list[0].y(), pt_list[0].z())); mm.NewVert(Point3(pt_list[1].x(), pt_list[1].y(), pt_list[1].z())); mm.NewVert(Point3(pt_list[2].x(), pt_list[2].y(), pt_list[2].z())); mm.NewVert(Point3(pt_list[3].x(), pt_list[3].y(), pt_list[3].z()));
mm.CreateFace(3, &VECTOR(2, 1, 0)[0]);
mm.CreateFace(3, &VECTOR(3, 2, 0)[0]);
mm.SetMapNum(1);
MNMap *map = mm.M(0);
map->NewVert(UVVert(0.0f, 0.0f, 1.0f));
map->NewVert(UVVert(1.0f, 0.0f, 1.0f));
map->NewVert(UVVert(1.0f, 1.0f, 1.0f));
map->NewVert(UVVert(0.0f, 1.0f, 1.0f));
map->NewFace(3, &VECTOR(2, 1, 0)[0]);
map->NewFace(3, &VECTOR(3, 2, 0)[0]);
INode *plane_node = u->ip->CreateObjectNode(planeObj);
plane_node->SetShowFrozenWithMtl(true);
plane_node->SetName((char*)name.c_str());
plane_node->SetMtl(plane_mat);
And it’s alright in case of using ApplyUVWMap for example.
Another way is in using maxscipt call inside sdk.
select $image_plane_0
addModifier $image_plane_0 (unwrap_UVW())
dialogMonitorOps.unRegisterNotification id:#unwrap_reset
fn confirmReset =
(
local hwnd = dialogMonitorOps.getWindowHandle()
if UIAccessor.GetWindowText hwnd == "Unwrap UVW" then
(
uiAccessor.pressDefaultButton()
true
)
else
false
)
dialogMonitorOps.enabled = true
dialogMonitorOps.interactive = false
dialogMonitorOps.registerNotification confirmReset id:#unwrap_reset
$image_plane_0.modifiers[#unwrap_uvw].reset()
dialogMonitorOps.enabled = false
$image_plane_0.modifiers[1].SetVertexPosition 0 1 (point3 0.0 1.0 1.0)
$image_plane_0.modifiers[1].SetVertexPosition 0 2 (point3 1.0 1.0 1.0)
$image_plane_0.modifiers[1].SetVertexPosition 0 3 (point3 1.0 0.0 1.0)
$image_plane_0.modifiers[1].SetVertexPosition 0 4 (point3 0.0 0.0 1.0)
It doesnt work and corrupted something inside max gui. But if i’m using this code alone in maxscript->new listener then all is ok.
It’s impossible to setup uv into unwrap_UVW without reseting.
sorry but i can’t understand what you want to do. do you want to build map channel and set tverts at specified positions? you can’t control uvws of geometry primitive so you have to convert it first to mesh or mnmesh (editable mesh or poly), or apply a uvw modifier.
please make your question clearer.
Ok. We will focus only on max sdk implementation.
Steps:
- Programmatically create geometry.
- Programmatically create uv coords for this geometry.
- Apply existing materail with bitmap to geometry and render them in viewport.
first step:
PolyObject *planeObj = CreateEditablePolyObject();
MNMesh &mm = planeObj->GetMesh();
mm.NewVert(Point3(pt_list[0].x(), pt_list[0].y(), pt_list[0].z()));
mm.NewVert(Point3(pt_list[1].x(), pt_list[1].y(), pt_list[1].z()));
mm.NewVert(Point3(pt_list[2].x(), pt_list[2].y(), pt_list[2].z()));
mm.NewVert(Point3(pt_list[3].x(), pt_list[3].y(), pt_list[3].z()));
mm.CreateFace(3, &VECTOR(2, 1, 0)[0]);
mm.CreateFace(3, &VECTOR(3, 2, 0)[0]);
second step:
mm.SetMapNum(1);
MNMap *map = mm.M(0);
map->NewVert(UVVert(0.0f, 0.0f, 1.0f));
map->NewVert(UVVert(1.0f, 0.0f, 1.0f));
map->NewVert(UVVert(1.0f, 1.0f, 1.0f));
map->NewVert(UVVert(0.0f, 1.0f, 1.0f));
map->NewFace(3, &VECTOR(2, 1, 0)[0]);
map->NewFace(3, &VECTOR(3, 2, 0)[0]);
third step (with existing material):
INode *plane_node = u->ip->CreateObjectNode(planeObj);
plane_node->SetShowFrozenWithMtl(true);
plane_node->SetName((char*)name.c_str());
plane_node->SetMtl(plane_mat);
Looks like all steps are correct, but texture doesnt appear in viewport. Have I missed something.
In max sdk there is a howto VS solution with ObjImporter project which uses TriObject class.
But unfortunately uvs was not created int this project too, when importing obj file.
Looks like official example doesnt work correctly.
I’ve found solution. The main problem – i have’nt initialized texture faces
Here is correct code
TriObject *planeObj = CreateNewTriObject();
Mesh &mm = planeObj->GetMesh();
mm.setNumVerts(4);
mm.setNumTVerts(4);
mm.setNumFaces(2);
mm.setNumTVFaces(2);
for(int j = 0; j < pt_list.size(); j++)
mm.setVert(j, pt_list[j].x(), pt_list[j].y(), pt_list[j].z());
mm.setTVert(0, 0.0f, 1.0f, 1.0f);
mm.setTVert(1, 1.0f, 1.0f, 1.0f);
mm.setTVert(2, 1.0f, 0.0f, 1.0f);
mm.setTVert(3, 0.0f, 0.0f, 1.0f);
mm.faces[0].setVerts(2, 1, 0);
mm.tvFace[0].setTVerts(2, 1, 0);
mm.faces[1].setVerts(3, 2, 0);
mm.tvFace[1].setTVerts(3, 2, 0);
mm.buildNormals();
INode *plane_node = u->ip->CreateObjectNode(planeObj);
plane_node->SetShowFrozenWithMtl(true);
plane_node->SetMtl(plane_mat);