addPolyline

DzFacetMesh addPolyline is not exposed in the sdk?
How we can create polyline in C++?

Comments

  • Richard HaseltineRichard Haseltine Posts: 107,858

    You can use the scripting methods, either executing a script or via QMetaObject::invokeMethod()

  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 11

    I tried but dosn't work, would you please show me a C++ working example? super simple, minimal, just a single polyline. This should work, But does nothing:

     

                    QList vertexIndices;
                    int vertexCount = mesh->getNumVertices();
                    for (int i = 0; i < vertexCount; i++)
                    {
                        vertexIndices.append(i);
                    }
                    int flags = 0;
                    QVariant varVertexIndices = QVariant::fromValue(vertexIndices);
                    QList uvIndices;
                    uvIndices.append(0);
                    uvIndices.append(0);
                    QVariant varUVIndices = QVariant::fromValue(uvIndices);
                    QMetaObject::invokeMethod(reinterpret_cast(mesh), "addPolyline",
                        Qt::DirectConnection,
                        Q_ARG(QVariant, varVertexIndices),
                        Q_ARG(QVariant, varUVIndices),
                        Q_ARG(int, flags));
    
    Post edited by MehdiZangenehBar on
  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 12

    ok, I have serious problem on this, this is super minimal dazscript to create a single polyline, which works perfect:
     

    Scene.clear();
    
    var mesh = new DzFacetMesh();
    var shape = new DzFacetShape();
    var object = new DzObject();
    var node = new DzNode();
    
    mesh.beginEdit();
    var vertices = [];
    var v0 = mesh.addVertex(new DzVec3(0, 0, 0));
    var v1 = mesh.addVertex(new DzVec3(0, 100, 0));
    vertices.push(v0);
    vertices.push(v1);
    mesh.addPolyline(vertices, [0, 0]);
    mesh.finishEdit();
    
    var material = new DzDefaultMaterial();
    material.name = "material";
    material.setDiffuseColor(new Color(255, 125, 0));
    material.getLineStartWidthControl().setValue(20);
    material.getLineEndWidthControl().setValue(15);
    
    var tessellation_modifier = new DzLineTessellationModifier();
    tessellation_modifier.getViewportSidesControl().setValue(3);
    tessellation_modifier.getRenderSidesControl().setValue(3);
    
    shape.setFacetMesh(mesh);
    shape.addMaterial(material);
    object.addShape(shape);
    object.addModifier(tessellation_modifier);
    node.setObject(object);
    node.setLabel("Polyline");
    Scene.addNode(node);
    

     

     

    and this is converted C++ version, that I tested all parts works, ecxept addPolyline method:

     

     

    #include "Kodak.h"
    #include "QColor"
    #include "QObject"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    #include "dzfloatproperty.h"
    #include "dzintproperty.h"
    #include "dzmodifier.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        dzScene->clear();
    
        DzFacetMesh* mesh = new DzFacetMesh();
        DzFacetShape* shape = new DzFacetShape();
        DzObject* object = new DzObject();
        DzNode* node = new DzNode();
    
        mesh->beginEdit();
        QList vertices;
        int v0 = mesh->addVertex(DzVec3(0, 0, 0));
        int v1 = mesh->addVertex(DzVec3(0, 100, 0));
        vertices.append(v0);
        vertices.append(v1);
        QList uvIndices;
        uvIndices.append(0);
        uvIndices.append(0);
        QMetaObject::invokeMethod
        (
            mesh,
            "addPolyline",
            Q_ARG(QList, vertices),
            Q_ARG(QList, uvIndices),
            Q_ARG(int, 0)
        );
        mesh->finishEdit();
    
        DzDefaultMaterial* material = new DzDefaultMaterial();
        material->setName("material");
        material->setDiffuseColor(QColor(255, 125, 0));
        qobject_cast(material->findProperty("Line Start Width"))->setDoubleValue(0, 20);
        qobject_cast(material->findProperty("Line End Width"))->setDoubleValue(0, 15);
    
        DzModifier* tessellation_modifier = qobject_cast(dzApp->findClassFactory("DzLineTessellationModifier")->createInstance());
        qobject_cast(tessellation_modifier->findPropertyByLabel("Viewport Line Tessellation Sides"))->setDoubleValue(0, 3);
        qobject_cast(tessellation_modifier->findPropertyByLabel("Render Line Tessellation Sides"))->setDoubleValue(0, 3);
    
        shape->setFacetMesh(mesh);
        shape->addMaterial(material);
        object->addShape(shape);
        object->addModifier(tessellation_modifier);
        node->setObject(object);
        node->setLabel("Polyline");
        dzScene->addNode(node);
    }
    
    Post edited by MehdiZangenehBar on
  • Richard HaseltineRichard Haseltine Posts: 107,858

    Several issues have been pointed out - a general lack of error checking (so that you can verify each step and add a log entry or pop-up telling you where things went wrong), properties should, if possible, be found by name rather than label (harder for the user, or a product, to change), you should look more closely at the example in https://doc.qt.io/archives/qt-4.8/qobject.html#qobject_cast, qobject_cast<DzModifier*>(...), and note that addPolyline does not take QList, it takes QVariantList

  • Richard Haseltine said:

    Several issues have been pointed out - a general lack of error checking (so that you can verify each step and add a log entry or pop-up telling you where things went wrong), properties should, if possible, be found by name rather than label (harder for the user, or a product, to change), you should look more closely at the example in https://doc.qt.io/archives/qt-4.8/qobject.html#qobject_cast, qobject_cast<DzModifier*>(...), and note that addPolyline does not take QList, it takes QVariantList

    Thank You for your response Richard.
    I undrstand your concern about null check, as this is a test project, I temporary skipped them, but in final version I will check them all, But all works without any crash or problem, except the addpolyline that will not crash, but does NOTHING.
    I created a test: EVERYTHING was done in C++, except addpolyline, then run the code, node was created without polyline. Then using DazScript, I get the mesh in the scene and added polyline, and yea, polyline was created, I checked all other settings was OK, this means all steps was working using C++, except the adding polyline.
    I just tested QVariantList and also dosn't work. but if you can show me a complete working sample code, I appreciate it.

  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 15

    I just realized my posted code was wrong, I don't want to edit it, just updated the code based on your suggestions, Still dosn't work:
     

    #include "Kodak.h"
    #include "QColor"
    #include "QObject"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    #include "dzfloatproperty.h"
    #include "dzintproperty.h"
    #include "dzmodifier.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        dzScene->clear();
    
        DzFacetMesh* mesh = new DzFacetMesh();
        DzFacetShape* shape = new DzFacetShape();
        DzObject* object = new DzObject();
        DzNode* node = new DzNode();
    
        mesh->beginEdit();
        QVariantList vertices;
        int v0 = mesh->addVertex(DzVec3(0, 0, 0));
        int v1 = mesh->addVertex(DzVec3(0, 100, 0));
        vertices.append(v0);
        vertices.append(v1);
        QVariantList uv_indices;
        uv_indices.append(0);
        uv_indices.append(0);
        QMetaObject::invokeMethod
        (
            mesh,
            "addPolyline",
            Q_ARG(QVariantList, vertices),
            Q_ARG(QVariantList, uv_indices),
            Q_ARG(int, 0)
        );
        mesh->finishEdit();
    
        DzDefaultMaterial* material = new DzDefaultMaterial();
        material->setName("material");
        material->setDiffuseColor(QColor(255, 125, 0));
        DzFloatProperty* prop_line_start_width = qobject_cast(material->findProperty("Line Start Width"));
        if (prop_line_start_width)
        {
            prop_line_start_width->setDoubleValue(0, 20);
        }
        DzFloatProperty* prop_line_end_width = qobject_cast(material->findProperty("Line End Width"));
        if (prop_line_end_width)
        {
            prop_line_end_width->setDoubleValue(0, 15);
        }
    
        DzModifier* tessellation_modifier = qobject_cast(dzApp->findClassFactory("DzLineTessellationModifier")->createInstance());
        DzFloatProperty* prop_viewport_line_tessellation_sides = qobject_cast(tessellation_modifier->findPropertyByLabel("Viewport Line Tessellation Sides"));
        if (prop_viewport_line_tessellation_sides)
        {
            prop_viewport_line_tessellation_sides->setDoubleValue(0, 3);
        }
        DzFloatProperty* prop_render_line_tessellation_sides = qobject_cast(tessellation_modifier->findPropertyByLabel("Render Line Tessellation Sides"));
        if (prop_render_line_tessellation_sides)
        {
            prop_render_line_tessellation_sides->setDoubleValue(0, 3);
        }
    
        shape->setFacetMesh(mesh);
        shape->addMaterial(material);
        object->addShape(shape);
        object->addModifier(tessellation_modifier);
        node->setObject(object);
        node->setLabel("Polyline");
        dzScene->addNode(node);
    }
    
    
    Post edited by MehdiZangenehBar on
  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 15

    ok, I finaly found a working code, we have some methods for the addpolyline:
     

    addPolyline(QVariantList,QVariantList,unsigned char)
    addPolyline(QVariantList,QVariantList)
    addPolyline(QVariantList)
    addPolyline(int,const int*,const int*,unsigned char)
    addPolyline(int,const int*,const int*)
    addPolyline(int,const int*)
    

    no where explained correctly what these are, but based on the dazscript document, one QVariantList is vertexes and one is uvs, I don't know other unsigned char flag and I don't know what is int argument. maybe ints are vertex indexes to add polylines between two vertexes. however I used simplest version which is addPolyline(QVariantList), and magically it works!
    I am not sure why addPolyline(QVariantList,QVariantList) dosn't work.

    Post edited by MehdiZangenehBar on
  • Richard HaseltineRichard Haseltine Posts: 107,858

    The signature in the docs/header is QVariantList, QVariantList, unsigned char but you are sending it QVariantList, QVariantList, int - the last type does not match, so the function you are calling is not found (which is something that error checking, checking the return type, would have been able to find for you). Scripting isn't type dependent to anything like the extent that C++ is, which is wht you can't readily transalte withoutn modification/checking.

    QList<T> on its own is not script compatible - it is too generic and can contain many types that are not script compatible. The typedef for QList<QVariant> as QVariantList is compatible where there are registered converters for QVariant that convert between script/ECMA types and the respective C++ equivalent. The ECMA Number type can be converted/coerced into an unsigned char. The int*, or const int*, C++ type is not script compatible.

    which is, I think, why you are getting into trouble using the scripting docs here. Remember that things which are no documented are not supported, and may chnage without notice - which is why their use is stronly discouraged.

  • Got it, Thanks.

Sign In or Register to comment.