Previous Thread  Next Thread

chat icon Auto_smooth / precise smooth in coding an export script?

dom107

Posted: Tue Jul 17, 2012 6:19 pm
Joined: 18 Apr 2012
Posts: 35
Hello,

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

Code:
                        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.
Reply with quote


dom107

Posted: Tue Jul 17, 2012 6:20 pm
Joined: 18 Apr 2012
Posts: 35
In the above "code", the normals are applied to the exported vertex being porcessed.
Reply with quote


dom107

Posted: Thu Jul 19, 2012 9:59 am
Joined: 18 Apr 2012
Posts: 35
I found some interesting code on this matter (trainz exporter):
Code:
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)
Reply with quote


dom107

Posted: Thu Jul 19, 2012 8:54 pm
Joined: 18 Apr 2012
Posts: 35
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)
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group