operator_mesh_uv.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import bpy
  2. import bmesh
  3. def main(context):
  4. obj = context.active_object
  5. me = obj.data
  6. bm = bmesh.from_edit_mesh(me)
  7. uv_layer = bm.loops.layers.uv.verify()
  8. # adjust uv coordinates
  9. for face in bm.faces:
  10. for loop in face.loops:
  11. loop_uv = loop[uv_layer]
  12. # use xy position of the vertex as a uv coordinate
  13. loop_uv.uv = loop.vert.co.xy
  14. bmesh.update_edit_mesh(me)
  15. class UvOperator(bpy.types.Operator):
  16. """UV Operator description"""
  17. bl_idname = "uv.simple_operator"
  18. bl_label = "Simple UV Operator"
  19. @classmethod
  20. def poll(cls, context):
  21. obj = context.active_object
  22. return obj and obj.type == 'MESH' and obj.mode == 'EDIT'
  23. def execute(self, context):
  24. main(context)
  25. return {'FINISHED'}
  26. def register():
  27. bpy.utils.register_class(UvOperator)
  28. def unregister():
  29. bpy.utils.unregister_class(UvOperator)
  30. if __name__ == "__main__":
  31. register()
  32. # test call
  33. bpy.ops.uv.simple_operator()