Script Idea. anyone fancy tackling ?

Hi

One of the features that I badly miss with Blender is the ability to UV map seperate objects to one uv texture file. I typically have say an aeroplane model that has 50 or so objects that I need to map to a single tga texture file, but I cannot easily do this in Blender. To workaround the problem I have to join all the objects together, UV map it, then manually separate the verts back into the 50 separate objects at the end. All very tedious and time consuming

This process I think could be done with 2 python scripts ?

Join script
All object names eg “left_wing” etc are taken and applied as a vertex group name to those vertices. Then at the end, all objects are joined into one object (equivalent of CTRL J)

Separate script
After the uv mapping has been completed, this script can be run to reverse the process, creating new objects, from all the vertex group names.

Does this make sense ? Is there maybe an easier way to accomplish the same thing ?
These 2 scripts sound “doable”, but sadly past my level of Blender/Python skills.

Does anyone fancy tackling this as a chance to practice their script coding? It would be great if possible ?

Thanks Geoff

Do you really need a script for this? If you don’t have too many objects, you could skip the script and do the whole thing with vertex groups. For that, you just need to assign all the vertices in an object to a vertex group. Then CTRL-J to join, and do the UV mapping. After completing that. Just separate everything apart using the vertex groups. Of course, this would still be a hassle if you have too many objects.

Probably not needed. After you’re done why not just separate “All loose parts”, or “by Material”?

nope, for 50 parts or so it really is too tedious and slow to do manually, and then also when you realise you made mistake and need to change model a bit, its all repeated again

The seperate by material wont work as there is no material, just a uv map

The separate loose parts doesnt work either, its lock up Blender, maybe model is too big ? Anyways that is not a sound solution either as one original object may contain several non joined meshes, so wouldnt get back what I started with

Geoff

Well I don’t necessarily agree with your reasoning here. What good is a UV map without a material for one thing. Separate has never crashed on me for another. But anyway…

Works on the selected mesh. Uses the experimental Bpy module so may not work in the future. Heck, may not work now but tested ok here – your mileage may vary and I make no guarantees this code is fit for the requested or any other purpose.

Be sure to “Apply Rotation” or the resulting objects may not be in the same orientation as the starting mesh (although, if you don’t, the pieces should still be in the proper relation to one another).

And finally, probably not the most efficient code in the world but you get what you pay for.

Separate by Vertex Group


import Blender
from Blender import Mesh, Window
import bpy

print 'Start'

sce = bpy.data.scenes.active
ob_act = sce.objects.active
me = ob_act.getData(mesh=1)

print me
print me.verts
me.sel = 0

for n1 in me.getVertGroupNames():
    print 'Vertex Group', n1
    
    rmvList = []
    for n2 in me.getVertGroupNames():
        if n1<>n2:
            rmvList = rmvList + me.getVertsFromGroup(n2)
        #end if
    #end for
    
    print 'keep', me.getVertsFromGroup(n1)
    print 'delete', rmvList
    
    nme = me.__copy__()
    nme.verts.delete(rmvList)
    sce.objects.new(nme, 'sbvg_'+n1)
#end for

Window.Redraw()

print 'End'

** EDIT ** I’m assuming the UV coords are maintained during all this. Don’t know, did not check.

Hi

Thanks for taking the time to write this script. I have just tried it with quite a big model 26k vertices, it works beautifully, does exactly what I wanted, and yes the UV coords are preserved.

I would be really grateful if you could finish it off by making the inverse script, that takes a multi object model and creates the vertex groups from the object names, and then glues all the objects into 1 single object. I am not sure if that would be harder to write ?

Hoping you can find the time for the inverse script fourMadMen ? Thanks again mate.

   Geoff

OK, but don’t ever say I never did anything for you…


import Blender
from Blender import Object, Mesh, Window
import bpy

print 'Start'

objects = Object.GetSelected()

if len(objects)>0:
    for object in objects:
        name = object.getName()
        me = object.getData(mesh=1)
        
        try:
            me.removeVertGroup(name)
        except AttributeError:
            pass
        #end try
        
        me.addVertGroup(name)
        
        me.assignVertsToGroup(name, [vert.index for vert in me.verts], 1.0, Mesh.AssignModes.REPLACE)
    #end for
    
    sce = bpy.data.scenes.active
    me = Mesh.New('JoinUs')
    
    object = sce.objects.new(me, 'JoinUs')
    
    object.join(objects)
    
    Window.Redraw()
#end if

print 'End'

Hello Four

Thanks again mate, another super bit of work, the 2nd script also worked perfectly for me, and did exactly what I needed. It will save me lots of wasted time in the future. I was quite surprised how little code it was, not many more lines than it took me to describe the requirement.

I was having a study of the 1st script, I wish my Blender/Python skills were stronger, :frowning: although the code was short I still found it tough to understand. I must admit I didnt understand why there are 2 iteration loops for the me.getVertGroupNames. I am sure it will be obvious once the penny drops, so far it has me stumped.

  Thanks Geoff

Read the console output and go over that section again, it might make things more clear.

For any one iteration of the out loop: The outer loop represents the vertices that will be kept and the inner loop builds a combined vertex list of all the other groups.