Virtual Mesh Instance in Python?

I’ve been trying to figure out how to make an array of objects in a script without actually adding new meshes to the scene.

I tried this in Lua elsewhere before, where I iterate an object adding values to the position etc. for each instance, but can’t seem to easily replicate this in Blender and Python without resorting to adding multiple meshes or group instances. Is this even possible?

so do you wanna add linked duplicates (different object, but same mesh data) or duplicate the geometry within a single mesh datablock?

It’s basically the same functionality as the array modifier, but from a script and with more options, like randomising materials or size etc. (like in the Blender Cookie scripting tutorials).

I know someone is woking on an updated array modifier, but it seems such a simple thing to script that it’s silly to wait for that. Isn’t it possible with python?

modifiers in python aren’t possible. You could create arrays with python, but it would be like adding an array modifier and applying it. Would that be ok? Distribute many copies once?

I don’t want to really use or apply modifiers. So from what you say it’s not possible to generate arrays of something without then applying them and actually making new individual meshes? I guess it makes sense that blender is oriented towards a more final approach, with clearly defined and numbered meshes and materials etc.

Thanks for your answers CoDEmanx!

Don’t get me wrong, it is possible to make duplicates of a single mesh datablock, either by using dupli groups or by adding individual objects (but ob.data will be the same mesh!). What isn’t possible, at least not without a high amount of hacks, is to make these non-destructive, like the array modier when not applied.

Quick example:


import bpy

ob = bpy.context.object
scene = bpy.context.scene

if ob is not None and ob.type == 'MESH':
    for x in range(5):
        for y in range(5):
            ob_copy = ob.copy()
            ob_copy.location = x*3-3, y*4-4, 2
            scene.objects.link(ob_copy)
            scene.update()

Thanks. That still adds meshes to the scene though. It’s weird because it’s something I do in Lua all the time, even on my iPad with Codea. Something like:

(pseudocode)

for i = 0, 10 do

x = 0 + (object_size * i)
draw_object( x, y, width, height)

end

you know what I mean? And it just draws it, so I guess I expected the same out of the box in Blender. It’s not a big deal anyway, just a bit boring to have to undo every time you want to go back and try some different settings.

@Superflea,
Since blender doesnt use python for its drawing/rendering, Python has to add the object into the scene. Its not language specific - if we used lua it would have the same limit.

It works this way because blender is a tool where the objects can be edited and manipulated, rather then a game engine which can just draw data and continue.

If you want to make the tool avoid having to undo, you could make it re-use objects from previous execution, add in its own group for eg.

If you really just want to draw…
You can have python draw using OpenGL into the 3D viewport, but then you need to do all drawing in py and it wont export, render … etc.

I can live with lack of export, but if something appears in the viewport and it’s openGL, wouldn’t I be able to render it to a file with the openGl render button?

No, it doesn’t? It adds objects, but no meshes.