Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


BlazeCell

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

Posts I've Made

In Topic: Little Inferno - Fire Algorithm

30 May 2013 - 11:37 AM

Thanks guys. Those are some good tips on creating a realtime fire effect.


In Topic: How do I align vertices along a arbitrary axis?

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)

In Topic: How do I align vertices along a arbitrary axis?

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


In Topic: How do I align vertices along a arbitrary axis?

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. =)


In Topic: How do I align vertices along a arbitrary axis?

21 May 2013 - 01:29 AM

Wow! After a couple of days, REPLIES! =D
 

You see, Blender has an API bound with Python, so you can build your own modelling tools by coding Python scripts. You can find educational material both on Python and on making scripted tools for Blender, so it'd be a great idea if you could dedicate some time to study this. 

If you learn to code Python tools for Blender then you can pretty much add most "simple" features like these yourself. I myself am very tempted to do this too.

 

Yeah, that's actually exactly what I've been doing the last couple of days. I already have the operation part of the code figured out and working. The part I'm stuck at is getting the python script to be processed in a modal manner so that I can toggle which axis coordinates to preserve. The Blender Python API describes using a modal() function in the custom operator class that gets called after execute() or invoke() return {'RUNNING_MODAL'}. Only problem is that my modal() function is never getting called. I've posted on the Blender forums regarding this issue:

http://www.blender.org/forum/viewtopic.php?t=27384

 

 

 

I just remembered something else(duh).  You delete all the vertices except for the end points.  Select those end points, hit 'F" to create an edge, and the either subdivide that edge.  This way you recreate the vertices all along that single edge.

 

 

I don't know much of Blender, but if you manually move the vertices you inserted into the edge, the edge won't be a straight line anymore (if you zoom in really close you'll see it's a bit sloppy from not being mathematically perfect). It's something that really annoyed me for a while until I discovered 'constraints'.

So you need to use something like Vertex Slide or Custom Transform Orientation to let your vertices slide mathematically perfect along the edge. In 3DS Max, this is called "edge constraint".

 

 

Yes, my first point was the actual creation of the vertices on the straight line.  Once you do that, then yes indeed you would be best to do the vertex slide tool to keep things straight.  It is the easiest way I know to slide the verts along the straight edge, considering it takes more time/effort to set up the contraints and/or the custom transform orientation.

 

Thanks for the alternative. I'm not sure if that will get the precision that I'm looking for, but constraints seem to be the best alternative that I've run across so far. I'll keep those tools in mind in the future.


PARTNERS