Results 1 to 6 of 6

Thread: Mesh cleaning script

  1. #1
    Member noidtluom's Avatar
    Join Date
    Aug 2005
    Location
    Malaysia
    Posts
    638

    Mesh cleaning script

    I have this little snippet which cleans all the objects in a file:

    Code:
    import bpy
    
    selected_objects = bpy.context.selected_objects
    for selected_object in selected_objects:
        bpy.context.scene.objects.active = selected_object
        bpy.ops.object.editmode_toggle()
        bpy.ops.mesh.remove_doubles()
        bpy.ops.mesh.tris_convert_to_quads()
        bpy.ops.mesh.normals_make_consistent()
        bpy.ops.object.editmode_toggle()
    Unfortunately it is quite slow for a file with, say, several thousand objects. Is there a way to parallelise, or make this faster?
    The ThoughtScore Project - My current project
    thinkMoult.com - My blog
    WIPUP.org - a fun, open-source, easy way to share your works-in-progresses for the community! Try it out today!



  2. #2
    I tried it out, but it doesn't seem to actually remove any double verts? Always says Zero removed.

    EDIT: Seems to work in some files but not others. No clue why.
    Last edited by Oscalon; 06-Sep-17 at 01:25.
    Non-Photorealistic Rendering and basic character tutorials:
    http://www.aversionofreality.com/



  3. #3
    Member FreeAccess's Avatar
    Join Date
    Jul 2013
    Location
    FRANCE
    Posts
    62
    Hello,

    To convert FBX files containing many objects, I use this script that runs in few seconds:
    - convert tri to quad
    - [Addon HardOPs] ==> Operation / Clean Mesh


    This may be useful . . .

    CleanMesh.jpg
    Win 7 x64, I7 -5930K, 16 Go,GTX 750 TI



  4. #4
    Member
    Join Date
    Sep 2015
    Location
    Canada
    Posts
    186
    Remove double works on a distance basis. If 2 verts has a larger distance than the threshold given to the 'remove double' operator, it won't remove anything.

    Converting tris to quads using the automatic algorithm might not be a good idea unless you know the result is going to be what you want.

    It also might depend on the scene size.



  5. #5
    It should be worth noting that using operators often forces a whole scene reload.

    https://blender.stackexchange.com/qu...nder-operators

    If you can write the equivalent code with low-level access, you can bypass those updates and do one update in the end.
    Basically, you would have to rewrite the code of each operator using "normal" data access. This will inevitably involve writing much more code, but could be worth the effort if you need to do such a task often.



  6. #6
    Member dudecon's Avatar
    Join Date
    Sep 2007
    Location
    Camarillo, CA, USA
    Posts
    117
    Originally Posted by -HENDRIX- View Post
    ... If you can write the equivalent code with low-level access, you can bypass those updates and do one update in the end. Basically, you would have to rewrite the code of each operator using "normal" data access. This will inevitably involve writing much more code, but could be worth the effort if you need to do such a task often.
    True. Though there's some low hanging fruit to harvest first.
    The code as-written operates on each object regardless of whether they are even meshes at all.
    It also operates on every instance of each block of mesh data, so if you have lots of duplicates (meshes that share the same data) it will waste a bunch of time cleaning the same data over and over.
    And finally, the failure to remove some doubles is because the "remove doubles" operation only operates on selected vertices. So we need to select all vertices first.
    My version, incorporating these changes, is as follows:
    Code:
    import bpy
    checked = set()
    selected_objects = bpy.context.selected_objects
    for selected_object in selected_objects:
        if selected_object.type != 'MESH':
            continue
        meshdata = selected_object.data
        if meshdata in checked:
            continue
        else:
            checked.add(meshdata)
        bpy.context.scene.objects.active = selected_object
        bpy.ops.object.editmode_toggle()
        bpy.ops.mesh.select_all(action='SELECT')
        bpy.ops.mesh.remove_doubles()
        bpy.ops.mesh.tris_convert_to_quads()
        bpy.ops.mesh.normals_make_consistent()
        bpy.ops.object.editmode_toggle()
    If this still runs too slowly for you, consider -HENDRIX- suggestion to re-bind the ops calls. The "remove_doubles" is probably the main culprit, as it prints to the console, which takes forever.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •