1
$\begingroup$

Right now I am making a import script for my file format. I have the model appearing fine and doing everything I need except I need to get all the meshes created and add them under an empty object I created also.

Here is what I have now: https://paste.ofcode.org/AsYmmmV6tTdcR79UJrTy5X

Now on line 95 you will see I create an empty object. My question is how can I get all the meshes created using the add_mesh_to_scene. So basically any mesh added to the scene will be parented under that empty object. I have went through and tried other peoples answer about parenting but cant find a way to do this no matter what I try. Any help would be greatly appreciated thank you :)

$\endgroup$
1
$\begingroup$

Add an empty object and set as parent

Rather than edit into your code I have added an example of how to add objects and set to have same parent. Have used "dummy" version of your add_mesh_to_scene to illustrate.

Uses the default cube mesh as a proxy for the imported mesh. Adds an empty at the cursor location and num_objects = 4 as the children of that empty. Set the inverse parent matrix to position the imported mesh objects where they would be without an empty parent at cursor location.

import bpy
from mathutils import Matrix

context = bpy.context
scene = context.scene

num_objects = 4
def create_mesh():
    # dummy method returns def cube mesh
    return bpy.data.meshes.get("Cube")

def add_mesh_to_scene(scene, mesh):
    obj = bpy.data.objects.new(mesh.name, mesh)
    scene.objects.link(obj)
    return obj

if num_objects:
    # code to add empty parent
    mt = bpy.data.objects.new("Handle", None)
    scene.objects.link(mt)
    mw = Matrix.Translation(scene.cursor_location)
    mt.matrix_world = mw
    # return as active
    scene.objects.active = mt
    mwi = mw.inverted()

    for i in range(num_objects):
        me = create_mesh()
        ob = add_mesh_to_scene(scene, me)
        ob.parent = mt
        # setting this keeps them in import locs.
        ob.matrix_parent_inverse = mwi
        # set some loc to visualize
        ob.location.x += 1.1 * i * ob.dimensions.x
        # return as selected
        ob.select = True
    # update scene once.    
    scene.update()
$\endgroup$
1
  • $\begingroup$ Thanks for the answer but I still cant seem to figure out how to add it in my code. It seems I am always getting some error. Also I dont know how exactly I am gonna get all objects I have added. For example you variable num_objects. Basically I need a way to add all objects I create in a list or array and then do a for and add them to the parent obj I create. Thanks for helping me! $\endgroup$
    – Skylumz
    Feb 15 '18 at 20:16
0
$\begingroup$

Well looks like I found my own answer. @batFINGER answered it correctly but in my situation with so many functions I couldnt pass all them variables through each one. So I decided to do a for loop and check for the objects name that was the one I wanted all other objects to be parented, then added that object to a variable named parent, like this:

for ob in bpy.data.objects:
        if ob.name == "Empty":
            parent = ob

After that to set all objects that were not named that as a child of the parent variable like so:

for ob in bpy.data.objects:
        if ob.name != "Empty":
            ob.parent = parent 

So yeah with some experimentation I did it. Thanks @batFINGER for trying to help!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.