Jump to content

  • Log In with Google      Sign In   
  • Create Account


BlazeCell

Member Since 30 Nov 2010
Offline Last Active Sep 09 2014 04:58 PM

#5063720 How do I align vertices along a arbitrary axis?

Posted by BlazeCell on 21 May 2013 - 09:37 PM

Sigh, the editor messed up on my last code snippet, it should look like this:

 

 

def menu_func(self, context):
    # Operators triggered by menus have their operator context by default set to
    #   EXEC_REGION_WIN. We need it set to INVOKE_DEFAULT to call invoke() instead of
    #   execute().
    self.layout.operator_context = 'INVOKE_DEFAULT'
    
    # Create the menu button
    self.layout.operator(AlignVerts.bl_idname, text="Align Verts")
def register():
    bpy.utils.register_class(AlignVerts)
    
    # Add "Align Verts" menu to the "Mesh->Vertices" menu.
    bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
def unregister():
    bpy.utils.unregister_class(AlignVerts)
    
    # Remove "Align Verts" menu from the "Mesh->Vertices" menu.
    bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)



#5063717 How do I align vertices along a arbitrary axis?

Posted by BlazeCell on 21 May 2013 - 09:32 PM

Thanks for sharing.

I want to ask you where did you learn pythoning with Blender, and if there are any resources you'd recommend to the total beginner (good how-to guides, articles etc.).

 

Regards.

 

Largely, I just googled around. I think I started with the query "Blender scripting", found a basic tutorial, and went from there.

 

I didn't even know Python before starting this, tho it wasn't hard to figure out the syntax. It's basically the same as javascript except:

  1. You use indentation instead of curly braces to indicate scope.
  2. Colons are put at the end of anything that has an inner scope, such as classes, if statements, or for loops.
  3. You use 'not' instead of '!'.
  4. You use 'and' instead of '&&'.
  5. You use 'or' instead of '||'.

You can access the documentation for Blender scripting by going to:
http://www.blender.org/documentation/blender_python_api_2_66a_release/

or by going to Help -> Python API References from within Blender itself (which is better if you want to ensure that the documentation you're looking at matches the version you're using).

 

Blender has a view for a Text Editor that allows you to edit scripts from within Blender itself. I prefer to use Sublime Text 2 myself as it has more options regarding coding, but the Text Editor within Blender has a menu for templates which will allow you to load up boiler plate scripts that you can examine, test, and modify to help you learn the ropes around Blender scripting.

 

As far as gotchas that I ran into while trying to make this Align Vert operator:

  1. There are 3 key functions for a operator:
    a) execute(self, context):
    This method is what is called when you run your script via the console or a batch process or when your operator isn't setup to be modal. It's a one-shot function that performs an operation based on the current settings and that's it.
    b) invoke(self, context, event):
    This method is an initializer function that allows you to set your settings to default values and whatever preprocessing you need.
    c) modal(self, context, event):
    This method is what gets called when a modal event happens. Modal events happen when execute(), invoke(), or modal() return:
    {'RUNNING_MODAL'}
    

    This starts a modal event loop that allows the modal() method to handle user input (key presses and mouse input). This is the setup that I needed to allow X, Y, or Z to be pressed to toggle modes.
     

  2. You must register your operator class as a handler for modal events if you want your modal() function to be called after invoke() or execute() return {'RUNNING_MODAL'}. The event registration command is:

    context.window_manager.modal_handler_add(self)

    It is recommended that you add this line of code to your invoke() function.
     

  3. When your operator is triggered via SPACEBAR operator menu, it is by default run with the 'INVOKE_DEFAULT' operator context, which tries to call invoke() before execute() if invoke() exists. When you register your operator to a menu via code like:

    def menu_func(self, context):
        # Create the menu button
        self.layout.operator(AlignVerts.bl_idname, text="Align Verts")
    
    def register():
        bpy.utils.register_class(AlignVerts)
    
        # Add "Align Verts" menu to the "Mesh->Vertices" menu.
        bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
    
    def unregister():
        bpy.utils.unregister_class(AlignVerts)
    
        # Remove "Align Verts" menu from the "Mesh->Vertices" menu.
        bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)

     

    The menu will button created will by default run your operator with the operator context of 'EXEC_REGION_WIN', which will cause it to ignore your invoke() function and jump straight to the execute() function. To fix this add a line of code that changes the menu operator context before triggering your operator:
    def menu_func(self, context):
        # Operators triggered by menus have their operator context by default set to 
    #   EXEC_REGION_WIN. We need it set to INVOKE_DEFAULT to call invoke() instead of
    # execute().
    self.layout.operator_context = 'INVOKE_DEFAULT'
        # Create the menu button     self.layout.operator(AlignVerts.bl_idname, text="Align Verts") def register():     bpy.utils.register_class(AlignVerts)     # Add "Align Verts" menu to the "Mesh->Vertices" menu.     bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func) def unregister():     bpy.utils.unregister_class(AlignVerts)     # Remove "Align Verts" menu from the "Mesh->Vertices" menu.     bpy.types.VIEW3D_MT_edit_mesh_vertices.remove(menu_func)

 

I spent many hours learning these nuances. I hope these tips help. =D




#5063650 How do I align vertices along a arbitrary axis?

Posted by BlazeCell on 21 May 2013 - 05:25 PM

Ok, I finally got the script working. You can find the full code here:

http://www.blender.org/forum/viewtopic.php?p=104692#104692

 

Thanks for the recommendations guys. =)




#4971689 Performing a boolean on a 2D Mesh

Posted by BlazeCell on 20 August 2012 - 08:30 PM

If you're performing boolean operations on 2D polygons, the Clipper library has served my needs quite well in the past:
http://www.angusj.com/delphi/clipper.php


PARTNERS