[Python] MaxPlus: how to combine two meshes

I’m making a tool for the optimization for scenes with large numbers of objects - I’m using Python in 3ds Max 2016, and MaxPlus specifically. Now there’s a problem i’ve run into that has me running into a wall: I would like to avoid going between Python and MaxScript all the time to execute a mesh combine operation ( never mind the difficulty in passing object references between the two ), and so I am trying to perform the merge operation in Python.
Now, for this purpose, the only available method I’ve found whose name seems to describe it doing what I need, is Mesh.CombineMeshes, as documented here: http://docs.autodesk.com/3DSMAX/16/ENU/3ds-Max-Python-API-Documentation/index.html?url=files/GUID-1AC35645-91D7-4DBE-9714-681C8CC8700F.htm,topicNumber=d30e920

Based on the extremely limited documentation, I think this method needs at minimum three arguments, all three of the Mesh Class. My approach so far is the following:

Make an empty geometry object: ( code lifted directly from the API documentation )

geom = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.TriMeshGeometry)
tri = MaxPlus.TriObject._CastFrom(geom)
mesh = tri.GetMesh()

get meshes from the objects I want to merge: ( note: the two lines with mesh1 and mesh2 are being executed in a for loop that loops over the array “self.objects[1:]” - meaning all objects should be merged with the first object in that array.

def getObjectMesh(self,obj):
    geom = obj.GetObject().AsTriObject()
    mesh = geom.GetMesh()
    return mesh

mesh1 = self.getObjectMesh(self.objects[0])
mesh2 = self.getObjectMesh(obj)

And lastly, perform the merge: ( or try to )

mesh.CombineMeshes(mesh,mesh1,mesh2)

When executing this code, max gives me the following error:

File "C:\Program Files\Autodesk\3ds Max 2016\MaxPlus.py", line 15115, in CombineMeshes
    return _MaxPlus.Mesh_CombineMeshes(*args)
RuntimeError: Unknown MaxPlus Exception

Sadly, when you go look in the given file at that line, the line is just: ( this is literally the entire function )

return _MaxPlus.Mesh_CombineMeshes(*args)

Does anyone know what I’m doing wrong / how to use the method? Would be much appreciated :slight_smile:

I got the same problem with MaxPlus. But pymxs works fine, try this:

import pymxs

mesh_01 = pymxs.runtime.getNodeByName('box_01')
mesh_02 = pymxs.runtime.getNodeByName('box_02')
mesh_01.attach(mesh_02, mesh_01)

Ow, I also need this in MaxPlus. :frowning:

Never mind! :slight_smile:

After 6 hours of research I finally found it.! yeaah.
I found the function hidden in the dark and scary Autodesk SDK docs: http://help.autodesk.com/view/3DSMAX/2017/ENU/?guid=__cpp_ref_mesh_8h_html

Parameters
mesh	The result of the combine operation is stored here. mesh = mesh1+mesh2.
mesh1	The first operand.
mesh2	The second operand.
tm1	If non-NULL then the points of mesh1 will transformed by this matrix before the boolean operation.
tm2	If non-NULL then the points of mesh2 will transformed by this matrix before the boolean operation.
whichInv	If 0, the resulting mesh will be transformed by Inverse(tm1). If 1, the resulting mesh will be transformed by Inverse(tm2). If -1, the mesh will not be transformed back.

EDITMESH_CLASS_ID = MaxPlus.Class_ID(3830386867L, 0L)

def createResultMesh(self):
    geom = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.TriMeshGeometry)
    tri = MaxPlus.TriObject._CastFrom(geom)
    node = MaxPlus.Factory.CreateNode(tri)
    return node

def getMesh(iNode):
    meshObj = iNode.GetObject().AsTriObject().GetMesh()
    return meshObj

def main():
    sel = MaxPlus.SelectionManager

    # get the source iNodes, make sure they are editable meshes!
    sourceNode1 = sel.GetNode(0)
    sourceNode2 = sel.GetNode(1)

    # get the mesh from the source node
    mesh1 = getMesh(sourceNode1)
    mesh2 = getMesh(sourceNode2)

    # create the resultMesh
    meshResult = getMesh(createResultMesh())

    # get the transform matrix.
    mesh1TM = sel.GetNode(0).GetLocalTM()
    mesh2TM = sel.GetNode(1).GetLocalTM()

    #CombineMeshes ( meshResult, mesh1, mesh2, mesh1TM, mesh2TM, whichInv)
    MaxPlus.Mesh.CombineMeshes( meshResult, mesh1, mesh2, mesh1TM, mesh2TM, 0 )


main()



oh man, awesome thread… that help me a lot >___<!!! …