<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Q: Qt3D - [c++] Apply QMaterial on a loaded obj file]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I imported a simple obj-file with the SceneLoader.<br />
The object is purposely visible multiple times inside the 3D Window, but I would like to apply individual colors (red or blue) on them. Best case would also be a alpha parameter, as QPhongAlphaMaterial is supposed to be capable. Is there any chance of applying a QMaterial* ?</p>
<p dir="auto">Unfortunately, no effect of transparency, or individual colors from applied material types (I tried all of the popular ones) is spottable, all the objects are black (but shaded from the white light source).</p>
<p dir="auto">Please help. This is my code:</p>
<pre><code>Qt3DRender::QMaterial *objectMaterial = getAnyQMaterial();

Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(root);

Qt3DRender::QSceneLoader *loader = new Qt3DRender::QSceneLoader(root);
QObject::connect(loader, &amp;Qt3DRender::QSceneLoader::statusChanged, [loader, objectMaterial](Qt3DRender::QSceneLoader::Status s) 
{ 
if (s == Qt3DRender::QSceneLoader::Ready)
    {
        QVector&lt;Qt3DCore::QEntity*&gt; entities = loader-&gt;entities();
        if (entities.size() &gt; 0)
        {
            for (int e = 0; e &lt; entities.size(); e++)
            {
	        entities[e]-&gt;components().clear();
                entities[e]-&gt;addComponent(objectMaterial);
                entities[e]-&gt;setEnabled(true);
            }
        }
    } 
});
    
loader-&gt;setSource(QUrl::fromLocalFile("Resources/Objects/pyramid.obj"));
entity-&gt;addComponent(objectMaterial);
entity-&gt;addComponent(loader);
</code></pre>
]]></description><link>https://forum.qt.io/topic/134729/q-qt3d-c-apply-qmaterial-on-a-loaded-obj-file</link><generator>RSS for Node</generator><lastBuildDate>Tue, 03 Nov 2026 20:59:49 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/134729.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 01 Mar 2022 15:48:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Q: Qt3D - [c++] Apply QMaterial on a loaded obj file on Sat, 05 Mar 2022 09:37:33 GMT]]></title><description><![CDATA[<p dir="auto">I am very happy now. All the materials work.<br />
There were two problems:</p>
<p dir="auto">1.) Never reuse material references. Always create a new material which apply to the individual object.</p>
<p dir="auto">2.) The assignment of materials don't work recursively. As in <a href="http://man.hubwiz.com/docset/Qt_5.docset/Contents/Resources/Documents/doc.qt.io/qt-5/qt3drender-qsceneloader.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://man.hubwiz.com/docset/Qt_5.docset/Contents/Resources/Documents/doc.qt.io/qt-5/qt3drender-qsceneloader.html</a>, it needs to traverse through the complete entities-tree of the loader, eg. with the SceneWalker, referenced here: <a href="https://code.qt.io/cgit/qt/qt3d.git/tree/tests/manual/assimp-cpp/main.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">https://code.qt.io/cgit/qt/qt3d.git/tree/tests/manual/assimp-cpp/main.cpp</a> and assign the material to all entities found.</p>
<p dir="auto">Code here:</p>
<pre><code>void SceneWalker::walk(Qt3DCore::QEntity *ent, int depth)
{
    // get entity in entities ...
    // apply material to entity
    // call walk recursively
}

void Scene::loadAndApply()
{
    QPhongAlphaMaterial *_material= new QPhongAlphaMaterial;
    _material-&gt;setDiffuse(QColor(100,100,200));
    [...]
    SceneWalker *sceneWalker = new SceneWalker(_material);
    QObject::connect(loader, &amp;Qt3DRender::QSceneLoader::statusChanged, [sceneWalker, loader](Qt3DRender::QSceneLoader::Status status)
    {
        if (status = Qt3DRender::QSceneLoader::Ready)
            sceneWalker-&gt;onStatusChanged(loader);
    }
    entity-&gt;addComponent(loader);
}
</code></pre>
<p dir="auto">best regards</p>
]]></description><link>https://forum.qt.io/post/705274</link><guid isPermaLink="true">https://forum.qt.io/post/705274</guid><dc:creator><![CDATA[kevin_d]]></dc:creator><pubDate>Sat, 05 Mar 2022 09:37:33 GMT</pubDate></item><item><title><![CDATA[Reply to Q: Qt3D - [c++] Apply QMaterial on a loaded obj file on Fri, 04 Mar 2022 06:10:43 GMT]]></title><description><![CDATA[<p dir="auto">This is a very crucial problem. Is there any way of coloring the resulting mesh?</p>
]]></description><link>https://forum.qt.io/post/705099</link><guid isPermaLink="true">https://forum.qt.io/post/705099</guid><dc:creator><![CDATA[kevin_d]]></dc:creator><pubDate>Fri, 04 Mar 2022 06:10:43 GMT</pubDate></item></channel></rss>