[Addon] Invert Magnitudes

I made this little addon this evening out of curiosity, it takes the magnitudes of the vertices of the active objects mesh, then makes the large ones small and the small ones large.
I wasn’t going to write a post or put it anywhere (because its only a little thing) but it effectively extends the number of basic meshes you can add… Have a look, and and let me know if it is worth uploading somewhere??? It’s found as a menu option under objects.
Try adding a cube, subdivide once then invert magnitudes. Do the same for the cone etc. If you do this with an IcoSphere (one subdivision) it makes a Icosidodecahedron, which was a surprise for me!!..
also try moving the objects origin first…

# -*- coding: utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  Blender script - Invert Magnitudes
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#  The Original Code is Copyright (C) Feb 2012 by John Michael Palmer
#  All rights reserved.
#
#  Contact:      john53john + at + gmail + dot + com
#  Information:  none yet  ###
#
#  The Original Code is: all of this file.
#
#  Contributor(s): none yet.
#
#  ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Invert Magnitudes",
    "author": "John Michael Palmer (jump)",
    "version": (0, 1),
    "blender": (2, 6, 3),
    "location": "",
    "description": "Invert mesh Magnitudes",
    "warning": "",
    "wiki_url": "",
    "tracker_url": ""\
        "",
    "category": "Mesh"}

import bpy

class FractionInvert(bpy.types.Operator):
    bl_idname = 'invertmops.frac'
    bl_label = 'Change Magnitudes based on a Fraction'
   
    def execute(self, context):
        sc = context.scene
        mesh = sc.objects.active.data
        mags = [v.co.magnitude for v in mesh.vertices]
        mmin = min(mags)
        mmax = max(mags)
        mrange = mmax - mmin
        if(mrange != 0):
            for vert in mesh.vertices:
                mag = mags[vert.index]
                unitvec = vert.co.normalized()
                minvec = unitvec * mmin 
                frac = (mag - mmin) / mrange
                newfrac = 1 - frac # that's magic bit
                vert.co = (newfrac * mrange * unitvec) + minvec
        return {'FINISHED'}
        
def menu_draw(self, context):
    self.layout.operator('invertmops.frac', text="Invert Magnitudes", icon = "PLUGIN")
    
def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_object.prepend(menu_draw)
    
def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_object.remove(menu_draw)

if __name__ == "__main__":
    register()

That’s pretty nifty and interesting. :slight_smile:

What do you think about adding it to the Specials menu (W-key)? I didn’t even know about vertex magnitudes!

One small critique: can you really have a “Copyright, All rights reserved” statement along with a GPL GNU license? Kind of contrasting, don’t you think? :wink:

Aaron, your right, I need to have a look at the license statement… its the result of cutting, pasting and changing fields, when first started with the blender python stuff (which is why the date is wrong also!!! eeek! I wanted to get on with the coding and so gave it less attention than I ought to have done!!)… thanks for pointing it out :slight_smile:

Ah okay, no worries. I understand about wanting to get on with the coding and stuff. :wink:

Interesting script jomp.
I tried it and it looks very similar to already existing function:
Mesh menu -> Transform -> Push/Pull
However the script is great for learning.
Thanks for sharing!

thanks Syziph :slight_smile: I’m still newish to blender and learning from the coding point of view so I am not aware of all the operators etc… I just had a quick look at the manual here http://wiki.blender.org/index.php/Doc:2.6/Manual/Modeling/Meshes/Editing/Basics and it looks like it is the same apart form the script above uses one fixed value to flip the magnitudes around and the mesh origin, where as the push/pull can take more values but uses the average vertices position. I guess the optimal solution would be for the push/pull to use the mesh origin?

Push/Pull uses selection center or 3D cursor, depending on the pivot mode.
It is great that you can use the mouse interactively and to see the change in real time.
Your script is great, but maybe you can add some original feature to it and make it stand up?

oh! well then! mmm…I could project the 3D profile to another mesh…
If you look at the code above, I have calculated the range of magnitude differences for all the vertices. The variable ‘mmin’ represents the radius of a sphere that the profile sits on top. This is to say that each vertex is made of the sphere radius vector plus the profile vector. If the sphere radius vector was calculated using another mesh and object.closest_point_on_mesh() then I suppose our original mesh would take the overall shape of the second mesh but keep its surface… i don’t know if that would be useful!!!

I originally only implemented the code above because i was thinking about this shape profile thing and it occurred to me that it wouldn’t take a lot to simply invert the profile… Little did i know i it was already done!!! :eek: :smiley: