8

I would like to change the default settings in 3D View.

  • No symbols to Single Symbol
  • Altitude clamping from Relative to Absolute
  • Change colors for Diffuse, Ambient and Specular

Changing by hand works fine but is impractical for 10+ layers. I would like to automate it with Python. I have looked into the 3D Library Python API but I have not been able to get this to work.

I have written some code:

import qgis._3d as d

wkt = "MULTIPOLYGON Z(((0 0 0, 0 0 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 0 0 0)))"

symbol = d.QgsPolygon3DSymbol()
symbol.setAddBackFaces(False)
symbol.setAltitudeBinding(1)
symbol.setAltitudeClamping(0)
symbol.setCullingMode(0)

renderer = d.QgsVectorLayer3DRenderer()
renderer.setSymbol(symbol)

materialSettings = d.QgsPhongMaterialSettings()
materialSettings.setAmbient(QColor(255,0,0))
materialSettings.setDiffuse(QColor(255,0,0))
materialSettings.setSpecular(QColor(255,0,0))
symbol.setMaterial(materialSettings)

layer = QgsVectorLayer("multipolygon?crs=EPSG:31469", "MULTIPOLYGON Z", "memory")
renderer.setLayer(layer)
feature = QgsFeature()
geometry = QgsGeometry.fromWkt(wkt)
feature.setGeometry(geometry)
layer.dataProvider().addFeatures([feature])
QgsProject.instance().addMapLayers([layer])

It shows no errors, it changes nothing.

I am using QGIS 3.12.0 Python console.

7

You should use setRenderer3D method of QgsMapLayer.

layer.setRenderer3D(renderer)

QgsVectorLayer inherits that method from QgsMapLayer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.