switch between flat and smooth shading

I think Ive been told before that as this depends on the normals, you need to modify the geom’s normals to change between smooth and flat shading, but Ive found this render attrib, ShadeModelAttrib, that seems to do that

import direct.directbase.DirectStart
from pandac.PandaModules import *

base.cam.setPos(0, -30, 5)

dlight = DirectionalLight('dlight')
dlnp = render.attachNewNode(dlight)
dlnp.setHpr(0,-60,0)
render.setLight(dlnp)

model = loader.loadModel('panda')
model.reparentTo(render)
base.textureOff()

type = 1
def changeShading():
	global type
	
	if type == 1:
		model.node().setAttrib(ShadeModelAttrib.make(ShadeModelAttrib.MFlat))
		type = 0
	else:
		model.node().setAttrib(ShadeModelAttrib.make(ShadeModelAttrib.MSmooth))
		type = 1
			
base.accept('enter', changeShading)

run()

Let me know if this isnt recommended.

ShadeModelAttrib.MFlat is really designed to be applied to a model that was created with this attribute in mind. It makes each polygon render with the lighting indicated by its first (or sometimes last) vertex. A model that was created with this attribute in mind will store the polygon normal in that vertex position. However, a model that was created with smooth-shading in mind will store the appropriate vertex normal for each vertex position; the vertex normal will be close to, but not exactly, the polygon normal.

So, if you apply this attrib to a model that was designed for smooth shading, you will get a faceted appearance which will be close to, but not exactly, the appearance it would have if it were actually created as a faceted model.

But if all you’re looking for is a visual effect, there’s no problem with achieving it this way.

David