Rotate 3D Model



  • I had a custom QML object "Model.qml" to load and rotate 3D model.

    Entity {
    id: root
    property Material material
    property alias myRoll : transform.rollAngle
    components: [ transform, mesh, root.material ]
    Transform {
        id: transform
        objectName: "MyModel"
        property real rollAngle : 0
        property real pitchAngle : 20
        Translate { id: translation }
        Scale { id: scaleTransform }
        Rotate { 
            objectName: "rotateRoll"
            axis : Qt.vector3d(1, 0, 0)
            angle : transform.rollAngle
        }
    }
    Mesh {
        id: mesh
        source: "qrc:/3dmodel/Drone.obj"
    }
    }
    

    And in drone.cpp i update property "rollAngle" to rotate model whenever this property changed but it doesn't work anyway. Here is the code I use to update "rollAngle"

    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl("qrc:/src/Model.qml"));
    QObject *object = component.create();    
    QObject *rotateObject = object->findChild<QObject *>("rotateRoll");  
    rotateObject->setProperty("angle", this->roll);    
    qDebug() << "Property value:" << rotateObject->property("angle").toFloat();
    engine.destroyed();
    

    "rollAngle" changes but 3D model doesn't rotate. I use SequenceAnimation instead but it can't run too. Can anyone give me some advice?

    Thks.


  • Moderators

    Hi @nothing and Welcome
    One thing I have noticed is you dont set a visual parent to the object that you create using component.create(). It can be done using setParentItem. For that you will ned to cast it to QQuickItem. I dont know that will make your problem solve but it is something that should be done.

    P.S: I have formatted your code by adding tags. You can edit the post to see how it is done.



  • Hi @p3c0 ,

    Can you give some tutorial about "set visual parent" or a simple example, I'm confuse how to apply this into my codes.

    P.S: Thks for editing my post :)


  • Moderators

    @nothing Can you post your complete code ? I tried the same with this example (http://doc.qt.io/qt-5/qt3drenderer-simple-qml-example.html) and it worked. However I was not able to use your example. Loading Model.qml in QQmlComponent didn't work.



  • @p3c0 I dont use setProperty anymore. I create Q_PROPERTY attribute roll in Drone.h that links to attribute roll of object Model.qml and set rootContext to Drone. Then Q_EMIT the function rollchange() notify with roll (in Drone.h) and the Rotate object in Model.qml we change to drone.roll . And finally it works :)


  • Moderators

    @nothing Congratulations and thanks for sharing :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.