3
$\begingroup$

I have a working importer that needs to handle large data sets ie. thousands of primitives.

When I import a file containing 2500 objects it takes roughly 90 seconds to import. Now if I import the same scene through the obj importer it takes about 10 -15 seconds.

What can I do to optimize this? Here is part of the code below:

    #read each line of the .ssynth file
for line in file:
    if  objCount % 25000 == 0:
        print ("Import progress report: " + str(objCount) + " objects")
        args = line.split()
        argsIndex = 0
        colR = 1
        colB = 1
        colG = 1

    #begin sphere shape. here we import any spheres
    if args[argsIndex] == "s":
        argsIndex += 1
        cx = float(args[argsIndex])
        argsIndex += 1
        cy = float(args[argsIndex])
        argsIndex += 1
        cz = float(args[argsIndex])
        argsIndex += 1
        radius = float(args[argsIndex])
        argsIndex += 1

        colR = float(args[argsIndex])
        argsIndex += 1
        colG = float(args[argsIndex])
        argsIndex += 1
        colB = float(args[argsIndex])
        argsIndex += 1

        # Set the Alpha - maybe we can use this later
        alpha = 1.0

        meshSphere =  bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=2, size=radius, location=([cx, cy, cz,]))

        #convert rgb values to hex for group naming
        def rgb_to_hex(rgb):
            return '#%02x%02x%02x' % rgb

        # Let's set the Object Color
        bpy.context.object.color = (colR, colG, colB, alpha)
        # Name for grouping
        matName = rgb_to_hex((colR,colG,colB))
        # Add a material slot to each object
        bpy.ops.object.material_slot_add()
        # Find the active material
        bpy.context.object.active_material_index = 0
        # Add that material to the slot of each object
        bpy.context.object.active_material = bpy.data.materials["SSmat"]
$\endgroup$
2
  • 1
    $\begingroup$ Please narrow the scope of your question. Right now, the question is very off-topic. Please ask one specific question. $\endgroup$
    – CharlesL
    Jun 13, 2013 at 12:31
  • $\begingroup$ The terms Python Import Code are a little ambiguous, i was expecting to read a question about importing modules or libraries, which can get messy $\endgroup$
    – zeffii
    Jun 13, 2013 at 14:44

1 Answer 1

3
$\begingroup$

Suggest not adding a new mesh for every object, bpy.ops.mesh.primitive_ico_sphere_add

Instead you can add a sphere once, then reuse the mesh between all objects changing their scale. Its also possible for each object to have their own material so you can still share object data but have different colors.

Though you could even make do with a single material for all and just use object color.

$\endgroup$
2
  • $\begingroup$ I added object color to my answer, not intending to step on toes. $\endgroup$
    – zeffii
    Jun 13, 2013 at 16:18
  • $\begingroup$ Ok, I'm hapy to say that this has worked out MUCH better. I have cut down the import time from 90 sec to around 25 sec so a hugh improvment sharing mesh data from one primitive to all objects. Thanks for the tip! $\endgroup$ Jun 19, 2013 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .