Normals, when changed in python window, do not update on mes

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Normals, when changed in python window, do not update on mes

Postby caibbor » Sun Dec 23, 2012 10:30 pm

whether I edit the mesh's normal in python console, or even if I manually reverse the order of the vertices in python.

how do I get my changes to the mesh data in python to take effect on the actual blender model/mesh/scene/whatever?

here's a video to illustrate my problem:
bcaptain.net/blender-normals.mpeg

and here's the code I wrote to reverse a quad's vertex order:

Code: Select all


sce = bpy.context.scene #sce = bpy.data.scenes.active
ob = sce.objects.active
mesh = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW')
# ** show current normal
mesh.polygons[0].normal
# ** flip vertices of a quad
tmp=mesh.polygons[0].vertices[0]
mesh.polygons[0].vertices[0]=mesh.polygons[0].vertices[3]
mesh.polygons[0].vertices[3]=tmp
tmp=mesh.polygons[0].vertices[1]
mesh.polygons[0].vertices[1]=mesh.polygons[0].vertices[2]
mesh.polygons[0].vertices[2]=tmp
mesh.update( calc_edges=True )
# ** show new normal
mesh.polygons[0].normal

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Postby caibbor » Wed Dec 26, 2012 3:52 pm

This seems like a pretty simple thing, somebody must have an answer to this.

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Postby caibbor » Wed Dec 26, 2012 4:04 pm

After some thinking, I presume that I am not editing the mesh data directly, but instead a copy.

mesh = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW')

how to edit the mesh directly.. that is the question!

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Wed Dec 26, 2012 9:06 pm

there can be issues with the "old" api, you should try the bmesh module. It has a special function to update normals:

http://www.blender.org/documentation/bl ... mal_update

but if you really wanna use the old api, switch from edit to object mode, apply changes, then switch back. This may last a while for complex models and eat memory, but otherwise changes are likely ignored (in edit mode)
I'm sitting, waiting, wishing, building Blender in superstition...

caibbor
Posts: 10
Joined: Thu Sep 20, 2012 11:43 pm

Postby caibbor » Thu Dec 27, 2012 2:29 am

CoDEmanX wrote:there can be issues with the "old" api, you should try the bmesh module. It has a special function to update normals:

http://www.blender.org/documentation/bl ... mal_update

but if you really wanna use the old api, switch from edit to object mode, apply changes, then switch back. This may last a while for complex models and eat memory, but otherwise changes are likely ignored (in edit mode)


I didn't realize there was an "old" api. I wrote an export script using roughly the same python code and it took a while to wade through what was "old blender" and what was "new blender" because 2.63 is so new. But... I guess I've got some homework to do.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 2 guests