Duplicate Linked Object

I’m trying to duplicate a cube with a Python script. It has to be a linked object. all the cubes need to have the same physics. Please help me :frowning:

Hi


bpy.ops.object.duplicate(linked=True)

will duplicate the context object, linked=True will give the object the same mesh

test file… if you have an object named Cube it will dupe it


import bpy
context = bpy.context
scene = context.scene
cube = scene.objects.get("Cube") # Will return None if there is no obj named CUBe

if cube is not None:
    scene.objects.active = cube
    # dupe it
    bpy.ops.object.duplicate(linked=True)

1 Like

Thanks so much :smiley: . Your my hero

I’m trying to change the location of every cube that i generate but, i always have to select the original cube. How can i change that ?

Code:

import bpy
import random

context = bpy.context
scene = context.scene
cube = scene.objects.get(“Cube.001”) # Will return None if there is no obj named CUBe

if cube is not None:
scene.objects.active = cube
# dupe it
bpy.ops.object.duplicate(linked=True)

xachse=random.uniform(-0.76, 2.89)
yachse=random.uniform(-4.84, 2.94)
zachse=4.28663

bpy.context.object.location = [xachse, yachse, zachse]

there’s no need for an operator, this should be much faster if you plan to duplicate many objects:

import bpy
from random import uniform

scene = bpy.context.scene
ob = scene.objects.get("Cube")

if ob is not None:
    obs = []
    for i in range(20):
        obs.append(ob.copy())

    for ob in obs:
        ob.location = (uniform(-0.76, 2.89), uniform(-4.84, 2.94), 4.28663)
        scene.objects.link(ob)
    scene.update()

Thank you :slight_smile: … I’m trying to make a cube fall each time the frame changes so that it looks like it’s raining cubes. But it’s not working

Code:

import bpy
from random import uniform

def cuberain():

scene = bpy.context.scene
ob = scene.objects.get("Cube.001")

if ob is not None:
    obs = []
    for i in range(1):
        obs.append(ob.copy())

    for ob in obs:
        ob.location = (uniform(-0.76, 2.89), uniform(-4.84, 2.94), 4.28663)
        ob.rotation_euler =(uniform(0.00, 360.00), uniform(0.00, 360.00),uniform(0.00, 360.00))
        scene.objects.link(ob)
    frame=scene.frame_current
    scene.update()

bpy.app.handlers.frame_change_pre.append(cuberain)

on a related subject can somebody port the script from here http://forums.osgrid.org/viewtopic.php?f=5&t=1409
to Blender??

beyond,

handlers expect one argument, the scene


import bpy
from random import uniform


def cuberain(<b>scene</b>):


    ob = scene.objects.get("Cube")


    if ob is not None:
        obs = []
    for i in range(1):
        obs.append(ob.copy())


    for ob in obs:
        ob.location = (uniform(-0.76, 2.89), uniform(-4.84, 2.94), 4.28663)
        ob.rotation_euler =(uniform(0.00, 360.00), uniform(0.00, 360.00),uniform(0.00, 360.00))
        scene.objects.link(ob)
        frame=scene.frame_current
        scene.update()


bpy.app.handlers.frame_change_pre.append(cuberain)

btw wrap code in [noparse]


[/noparse] tags

EDIT:

Check out this puppy… much better cube rain.


import bpy
from random import uniform


def cuberain(scene):


    ob = scene.objects.get("Cube")


    if ob is not None:
        obs = []
    for i in range(1):
        bpy.ops.object.duplicate(linked=True)
        obs.append( scene.objects.active)


    for ob in obs:
        ob.location = (uniform(-0.76, 2.89), uniform(-4.84, 2.94), 4.28663)
        ob.rotation_euler =(uniform(0.00, 360.00), uniform(0.00, 360.00),uniform(0.00, 360.00))




bpy.app.handlers.frame_change_pre.append(cuberain)


remember to do a few bpy.app.handlers.frame_change_pre.oop() 's in the console to clear them out.

The ob.copy() gives me a vector trail type thingy the op.object.dupe gives me rain. Also duping with linked=True only gives me one mesh.

@batFINGER: little typo: oop -> pop :slight_smile:

bpy.app.handlers.frame_change_pre.pop()

There’s also
bpy.app.handlers.frame_change_pre.clear()
to remove all!

Thanks for the help :slight_smile: … but the cubs are falling at the same time and not after each other. I wan’t them to fall for 5 minutes so that means for 1 frame, 1 cube so 9000 cubes. Is this even possible ?

isn’t it rather something for the particle system?

http://pasteall.org/blend/25637

Hmm Yes. But can’t i just adjust something in the script. It should be possible right ?