Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Rotate 3D Model
Forum Updated to NodeBB v4.3 + New Features

Rotate 3D Model

Scheduled Pinned Locked Moved QML and Qt Quick
qt 5.5qt3dqml
6 Posts 2 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nothing
    wrote on last edited by nothing
    #1

    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.

    p3c0P 1 Reply Last reply
    0
    • N nothing

      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.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nothing
        wrote on last edited by
        #3

        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 :)

        p3c0P 1 Reply Last reply
        0
        • N nothing

          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 :)

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @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.

          157

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nothing
            wrote on last edited by
            #5

            @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 :)

            p3c0P 1 Reply Last reply
            0
            • N nothing

              @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 :)

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @nothing Congratulations and thanks for sharing :)

              157

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved