<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: polyop.attach obj1 obj2 in SDK in 3ds Max Programming Forum</title>
    <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5550880#M15518</link>
    <description>&lt;P&gt;Here's an untested example for both poly attach and poly detach.&amp;nbsp;Make sure to have a look at the documentation of classes &lt;A href="http://help.autodesk.com/view/3DSMAX/2015/ENU/?guid=__cpp_ref_class_poly_object_html" target="_self"&gt;PolyObject&lt;/A&gt;&amp;nbsp;and &lt;A href="http://help.autodesk.com/view/3DSMAX/2015/ENU/?guid=__cpp_ref_class_m_n_mesh_html" target="_self"&gt;MNMesh&lt;/A&gt;. I don't actually know if this keeps maps and UVWs intact. But it should give you a decent starting point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The polyAttach function takes a node that must reference an poly object and an INodeTab containing nodes that should be attached to the node. All attached nodes are then deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;bool polyAttach(INode* targetNode, const INodeTab&amp;amp; sourceNodes)
{
    if (!targetNode || sourceNodes.Count() == 0)
        return false;

    // make sure we have a valid poly object

    auto targetObj = targetNode-&amp;gt;GetObjectRef();
    if (!targetObj || targetObj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || targetObj-&amp;gt;ClassID() != Class_ID(POLYOBJ_CLASS_ID, 0))
        return false;

    // cast the object to a poly object

    auto targetPolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(targetObj);

    // get the poly mesh we want the other poly meshes to attach to

    auto targetPolyMesh = &amp;amp;targetPolyObj-&amp;gt;GetMesh();

    // iterate all source nodes

    for (auto i = 0; i &amp;lt; sourceNodes.Count(); ++i)
    {
        auto sourceNode = sourceNodes[i];
        if (!sourceNode)
            continue;

        // get and check if the object can be converted to a poly object

        auto sourceObj = sourceNode-&amp;gt;EvalWorldState(0).obj;
        if (!sourceObj || sourceObj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || !sourceObj-&amp;gt;CanConvertToType(Class_ID(POLYOBJ_CLASS_ID, 0)))
            continue;

        // convert the object to a poly object

        auto sourcePolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(sourceObj-&amp;gt;ConvertToType(GetCOREInterface()-&amp;gt;GetTime(), Class_ID(POLYOBJ_CLASS_ID, 0)));
        if (!sourcePolyObj)
            continue;

        // get the mesh and attach it to the target mesh

        auto sourcePolyMesh = &amp;amp;sourcePolyObj-&amp;gt;GetMesh();
        *targetPolyMesh += *sourcePolyMesh;

        // delete the temporary source poly object (only if ConvertToType() created a new object)

        if (sourcePolyObj != sourceObj)
            sourcePolyObj-&amp;gt;DeleteThis();

        // finally delete the node we took the mesh from as we usually don't need it anymore

        sourceNode-&amp;gt;DeleteMe();
    }

    return true;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The polyDetach function takes a node which must reference a poly object. It then detaches the currently selected element to a new node.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;bool polyDetach(INode* node)
{
    if (!node)
        return false;

    // make sure we have a valid (poly) object

    auto obj = node-&amp;gt;GetObjectRef();
    if (!obj || obj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || obj-&amp;gt;ClassID() != Class_ID(POLYOBJ_CLASS_ID, 0))
        return false;

    // cast the object to a poly object

    auto polyObj = static_cast&amp;lt;PolyObject*&amp;gt;(obj);

    // create a new poly object that will hold the detached mesh

    auto detachedPolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(CreateInstance(GEOMOBJECT_CLASS_ID, Class_ID(POLYOBJ_CLASS_ID, 0)));
    if (!detachedPolyObj)
        return false;

    // detach the SELECTED element to the new poly object's mesh (see class MNMesh)

    if (!polyObj-&amp;gt;GetMesh().DetachElementToObject(detachedPolyObj-&amp;gt;GetMesh()))
    {
        detachedPolyObj-&amp;gt;DeleteThis();
        return false;
    }

    // create a new node referencing the detached poly object

    auto newNode = GetCOREInterface()-&amp;gt;CreateObjectNode(detachedPolyObj);
    if (!newNode)
    {
        detachedPolyObj-&amp;gt;DeleteThis();
        return false;
    }

    return true;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Mar 2015 19:00:06 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2015-03-20T19:00:06Z</dc:date>
    <item>
      <title>polyop.attach obj1 obj2 in SDK</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5550719#M15517</link>
      <description>&lt;P&gt;How would you attach 2 or more objects in the sdk and how would you detach keeping their maps and UVWs&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2015 17:10:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5550719#M15517</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-03-20T17:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: polyop.attach obj1 obj2 in SDK</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5550880#M15518</link>
      <description>&lt;P&gt;Here's an untested example for both poly attach and poly detach.&amp;nbsp;Make sure to have a look at the documentation of classes &lt;A href="http://help.autodesk.com/view/3DSMAX/2015/ENU/?guid=__cpp_ref_class_poly_object_html" target="_self"&gt;PolyObject&lt;/A&gt;&amp;nbsp;and &lt;A href="http://help.autodesk.com/view/3DSMAX/2015/ENU/?guid=__cpp_ref_class_m_n_mesh_html" target="_self"&gt;MNMesh&lt;/A&gt;. I don't actually know if this keeps maps and UVWs intact. But it should give you a decent starting point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The polyAttach function takes a node that must reference an poly object and an INodeTab containing nodes that should be attached to the node. All attached nodes are then deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;bool polyAttach(INode* targetNode, const INodeTab&amp;amp; sourceNodes)
{
    if (!targetNode || sourceNodes.Count() == 0)
        return false;

    // make sure we have a valid poly object

    auto targetObj = targetNode-&amp;gt;GetObjectRef();
    if (!targetObj || targetObj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || targetObj-&amp;gt;ClassID() != Class_ID(POLYOBJ_CLASS_ID, 0))
        return false;

    // cast the object to a poly object

    auto targetPolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(targetObj);

    // get the poly mesh we want the other poly meshes to attach to

    auto targetPolyMesh = &amp;amp;targetPolyObj-&amp;gt;GetMesh();

    // iterate all source nodes

    for (auto i = 0; i &amp;lt; sourceNodes.Count(); ++i)
    {
        auto sourceNode = sourceNodes[i];
        if (!sourceNode)
            continue;

        // get and check if the object can be converted to a poly object

        auto sourceObj = sourceNode-&amp;gt;EvalWorldState(0).obj;
        if (!sourceObj || sourceObj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || !sourceObj-&amp;gt;CanConvertToType(Class_ID(POLYOBJ_CLASS_ID, 0)))
            continue;

        // convert the object to a poly object

        auto sourcePolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(sourceObj-&amp;gt;ConvertToType(GetCOREInterface()-&amp;gt;GetTime(), Class_ID(POLYOBJ_CLASS_ID, 0)));
        if (!sourcePolyObj)
            continue;

        // get the mesh and attach it to the target mesh

        auto sourcePolyMesh = &amp;amp;sourcePolyObj-&amp;gt;GetMesh();
        *targetPolyMesh += *sourcePolyMesh;

        // delete the temporary source poly object (only if ConvertToType() created a new object)

        if (sourcePolyObj != sourceObj)
            sourcePolyObj-&amp;gt;DeleteThis();

        // finally delete the node we took the mesh from as we usually don't need it anymore

        sourceNode-&amp;gt;DeleteMe();
    }

    return true;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The polyDetach function takes a node which must reference a poly object. It then detaches the currently selected element to a new node.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;bool polyDetach(INode* node)
{
    if (!node)
        return false;

    // make sure we have a valid (poly) object

    auto obj = node-&amp;gt;GetObjectRef();
    if (!obj || obj-&amp;gt;SuperClassID() != GEOMOBJECT_CLASS_ID || obj-&amp;gt;ClassID() != Class_ID(POLYOBJ_CLASS_ID, 0))
        return false;

    // cast the object to a poly object

    auto polyObj = static_cast&amp;lt;PolyObject*&amp;gt;(obj);

    // create a new poly object that will hold the detached mesh

    auto detachedPolyObj = static_cast&amp;lt;PolyObject*&amp;gt;(CreateInstance(GEOMOBJECT_CLASS_ID, Class_ID(POLYOBJ_CLASS_ID, 0)));
    if (!detachedPolyObj)
        return false;

    // detach the SELECTED element to the new poly object's mesh (see class MNMesh)

    if (!polyObj-&amp;gt;GetMesh().DetachElementToObject(detachedPolyObj-&amp;gt;GetMesh()))
    {
        detachedPolyObj-&amp;gt;DeleteThis();
        return false;
    }

    // create a new node referencing the detached poly object

    auto newNode = GetCOREInterface()-&amp;gt;CreateObjectNode(detachedPolyObj);
    if (!newNode)
    {
        detachedPolyObj-&amp;gt;DeleteThis();
        return false;
    }

    return true;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2015 19:00:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5550880#M15518</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-03-20T19:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: polyop.attach obj1 obj2 in SDK</title>
      <link>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5551425#M15519</link>
      <description>&lt;P&gt;Thanks, that's great.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just what I was after.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Mar 2015 11:22:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/3ds-max-programming-forum/polyop-attach-obj1-obj2-in-sdk/m-p/5551425#M15519</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-03-21T11:22:04Z</dc:date>
    </item>
  </channel>
</rss>

