Auto_smooth / precise smooth in coding an export script?

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Auto_smooth / precise smooth in coding an export script?

Post by dom107 » Tue Jul 17, 2012 7:19 pm

Hello,

In exporting a Blender mode, by checking use_smooth, the smooth effect can be exported, using:

Code: Select all

                        if face.use_smooth:
                            # use vertex normal (full smooth effect)
                        else:
                            # use face normal (no smooth effect)
However, like in the 3D view port, the effect seems binary: I have all or none smooth effect.

Is there a way to use the auto_smooth option (use_auto_smooth in python) and the related angle (auto_smooth_angle in python) to set some more precise smooth effect in the export?
From reading the 2 values, I should recompute the vertex normals?

Thanks in advance for your help.

dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Re: Auto_smooth / precise smooth in coding an export script?

Post by dom107 » Tue Jul 17, 2012 7:20 pm

In the above "code", the normals are applied to the exported vertex being porcessed.

dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Re: Auto_smooth / precise smooth in coding an export script?

Post by dom107 » Thu Jul 19, 2012 10:59 am

I found some interesting code on this matter (trainz exporter):

Code: Select all

def get_autosmooth_normal(mesh, fop, mesh_vi):
 '''check if smoothing has to be applied and return the depending normal'''
##  is at least one neighbourface smooth, the we return the vertex
##  normal(smooth applied), else the face normal will be returned
    result = fop.normal  # init with the normal for the un-smooth case

    ##  faulty (none-planar) faces may have a zero-length normal, and without
    ##  direction you can't calculate direction difference
    if fop.normal.length > 0.0:
		for p in mesh.polygons:
			if ( p != fop ) and ( mesh_vi in p.vertices ) and ( p.normal.length > 0.0):
				angle = int(round(math.degrees(fop.normal.angle(p.normal))))
				if  angle <= mesh.auto_smooth_angle:
					result = mesh.vertices[mesh_vi].normal
    return result
	
-------------------

	if face.use_smooth:
		if mesh.use_auto_smooth:
			no = mathutils.Vector(obj_prop[OBJ.ROT] * get_autosmooth_normal(mesh, face, face.vertices[face_vi]))
		else:
			no = mathutils.Vector(obj_prop[OBJ.ROT] * mesh.vertices[face.vertices[face_vi]].normal)
	else:
		no = mathutils.Vector(obj_prop[OBJ.ROT] * face.normal)

dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Re: Auto_smooth / precise smooth in coding an export script?

Post by dom107 » Thu Jul 19, 2012 9:54 pm

I found 2 problems in the above code :
1) for p in mesh.polygons should be replaced by for p in mesh.tessfaces:
2) mesh.auto_smooth_angle is in radians. Thus angle has to be computed as:
angle = fop.normal.angle(p.normal)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests