Notifications
Clear all

[Closed] [SDK] Help with rewriting script function into c++ max code

Hello, i have this function from export script

fn GetSmoothByOpenEdge f obj =
(
	local Opened = meshop.getOpenEdges obj
	local s = #(4,2,1)
	local i, j, k
	
	for i = 1 to obj.numfaces do
	(
		sg = 0
		
		for j = 1 to 3 do
		(
			ei = (i-1)*3 + j
			if Opened[ei] then sg += s[j]
		)

		writelong f sg
	)
)

fn GetSmoothByEdge2 f Sob =
(
	local obj = copy Sob
	local Opened = meshop.getOpenEdges obj
	local s = #(4,2,1)
	local i, j, k
	local FGroups = for i = 1 to 32 collect #{}
	
	for i = 1 to obj.numfaces do
	(
		sg = GetSG(getFaceSmoothGroup obj i) + 1
		append FGroups[sg] i
	)
	
	for g in FGroups where g.numberset > 0 do
		meshop.detachFaces obj g delete:true asMesh:false
	
	GetSmoothByOpenEdge f obj
	delete obj
)

I am trying to import smoothing after export from this script, i wrote similar code in my import plugin but failed because don’t understand how to edges working

function from import script:

SGa = for i = 1 to NumFaces collect readlong f #unsigned
local SGUsed = #{}
local SGroup = 0
local SGStack = #(), SGStackPos = 0
local SGFaces = #(#()), var = #{}

for i = 1 to m.numfaces where not SGUsed[i] do
(
	SGroup += 1
	SGStackPos += 1; 
	SGStack[SGStackPos] = i
	format "# GLOBAL POS = %\n" i
	local d = i
	SGFaces[1][SGroup] = #{}
	
	while true do
	(
		FaceInd = SGStack[SGStackPos]; 
		SGStackPos -= 1
		SGUsed[FaceInd] = true
		--setFaceSmoothGroup m FaceInd (2^SGroup)
		append SGFaces[1][SGroup] FaceInd
		
		--format "SGroup = %\n" SGroup
		for i = 1 to 3 where bit.and SGa[FaceInd] (2^(3-i)) == 0 do
		(

			ei = (FaceInd - 1)*3 + i					
			ope = meshop.getEdgesReverseEdge m ei
			
			--format "rev edges count = %\n" ope

			for j in ope do 
			(
				k = (j-1)/3 + 1
				if not SGUsed[k] then
				(
					SGStackPos += 1
					SGStack[SGStackPos] = k
					SGUsed[k] = true
				)
			)
		)
		
		if SGStackPos < 1 then exit
	)
	
)

--format "file sg groups count = %\n" SGroup

-- remove undefined
--for i = 1 to SGFaces[1].count do format "%, %\n" i SGFaces[1][i]
for i = SGFaces[1].count to 1 by -1 do if SGFaces[1][i] == undefined then deleteitem SGFaces[1] i
	
-- sort by face count
qsort SGFaces[1] compareSG

for i = 1 to 32 where SGFaces[i]!=undefined and SGFaces[i].count>1 do
(
	var = meshop.getVertsUsingFace m SGFaces[i][1]
	--format "i = %\n" i
	
	for j = 2 to SGFaces[i].count do
	(
		v = meshop.getVertsUsingFace m SGFaces[i][j]
		if (var*v).numberset == 0 then
		(
			join var v
			join SGFaces[i][1] SGFaces[i][j]
			--format "\tjoin %\n" j
		)
		else
		(
			k = i+1
			if SGFaces[k] == undefined then SGFaces[k] = #()
			append SGFaces[k] SGFaces[i][j]
			--format "remove sg from % to %, %\n" i k SGFaces[i][j]
		)
	)
)

for i = 1 to 33 where SGFaces[i] != undefined and SGFaces[i].count > 0 do
(
	if i == 33 then
	(
		for j in SGFaces[i] do
		(
			for k in j do
				setFaceSmoothGroup m k (2^(i-1))
			setfaceselection m  j keep:true
		)
		
		setuserprop m "badsg" SGFaces[i]
		if SGFaces[i].count > 1 then format "some smoothing groups are not correct\n"
	)
	else
		for j in SGFaces[i][1] do setFaceSmoothGroup m j (2^(i-1))
)

my attempt in c++

        int SGroup = -1;
		int SGStackPos = -1;

		std::map<unsigned int, unsigned int> SGStack;
		std::map<unsigned int, bool> SGUsed;
		std::map<unsigned int, std::vector<unsigned int>>; SG_;

		for (int i = 0; i < faces.size(); i++)
		{

			if (SGUsed.count(i) == 0)
			{
				SGroup += 1;
				SGStackPos += 1;
				SGStack[SGStackPos] = i;

				while (true)
				{
					unsigned int FaceInd = SGStack[SGStackPos];
					SGStackPos -= 1;
					SGUsed[FaceInd] = true;

					SG_[SGroup].push_back(FaceInd);

					for (int n = 0; n < 3; n++)
					{
						if ((sgroups[FaceInd] & (4>>n)) == 0)
						{
							unsigned int edge = FaceInd * 3 + n;
							unsigned int fi = (edge / 3), ei = (edge % 3);

							int redge = getReverseEdge(mod->faces, mod->numFaces, fi, faces[fi].v[ei], faces[fi].v[(ei + 1) % 3]);
							//uint32_t k = getAdjFace(mod->faces, mod->numFaces, fi, faces[fi].v[ei], faces[fi].v[(ei + 1) % 3]);

							int k = redge / 3;

							if (SGUsed.count(k) == 0 && k!=UNDEFINED)
							{
								SGStackPos += 1;
								SGStack[SGStackPos] = k;
								SGUsed[k] = true;
							}
						}
					}

					if (SGStackPos < 1) break;
				}
			}			
		}

any idea
was wrong with code?
my code with sort and set smoothing after this is working
i used code for edges from this theme getEdgesReverseEdge