Replace a whole bunch of cubes with a new object?


#1

So I’ve got a mess of cubes that I’ve scattered all over the place, only to realise they’re not quite right. The position/rotation/scale are all correct, I just need to swap them all out with a slightly different cube.

I thought I could do this with Xref’s but it doesn’t seem to work the way I imagined. Any thoughts?

Thanks!

Will


#2

If you made them as render objects it is easy to change the reference object in one go.

odo


#3

Thanks Odo. I’m not sure what you mean. What’s a render object? A render instance?

Anyway, I did a bit playing and came up with this:

Make a tracer linking all the cubes, then clone my new object on the tracer. Works great and now it’s all in a cloner, which is handy too.

Thanks for your help, though. It’s much appreciated.

Will


#4

The Replace With command should do the job, select all your “wrong” cubes.
Then call the command Replace With (Character->Commands), select your
correct cube in the Object Manager and hit enter.


#5

Im curious, how id you keep the rotation values when using the tracer?


#6

I didn’t keep the rotation values, Mash. They didn’t really matter as I was just creating a big cluster of fun.

W


#7

I wrote this script a while back when I had similar problems. Name the new correct object ‘Ref’ (reference), select all the wrong cubes and run the script. It will get swap out the selected objects into instances and make the ‘Ref’ object the instance reference. The instance objects will maintain all the position, scale and rotations of the old cubes including any keyframes.

import c4d
from c4d import gui

def instClone(): # Instance Object Function
    inst = c4d.BaseObject(5126) # Insert Instance Object
    instRef = doc.SearchObject("REF") # Find Object to Referance
    if instRef:
        inst[c4d.INSTANCEOBJECT_LINK] = instRef
        inst[c4d.INSTANCEOBJECT_RENDERINSTANCE] = True
        return inst
    else:
        gui.MessageDialog("Name the object to instance 'REF'.", c4d.GEMB_OK)
        return False
    
def main():
    sel = doc.GetActiveObjects(False) # Get selected objects
    bc = c4d.BaseContainer()
    doc.StartUndo()
    for op in sel:
        inst = instClone() # Get Instance Object Function
        opN = op.GetName() # Get Name
        if opN == "REF":
            break
        doc.InsertObject(inst, op) # Insert into document
        opMatrix = op.GetMl() # Get Matrix of Selected
        inst.SetMl(opMatrix)
        # If Shift is pressed reset scale to 1
        if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD,c4d.BFM_INPUT_CHANNEL,bc):        
            if bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QSHIFT:
                inst.SetRelScale(c4d.Vector(1))
        doc.AddUndo(c4d.UNDOTYPE_NEW, inst) # Undo Instance Object
        target = op.GetDown() # Select the Child (New Instance Ob)
        target.SetName(opN) # Set Name
        target.InsertBefore(op) # Insert Render Instance Above Selected
            
        for track in op.GetCTracks(): # Get Selected Tracks
            newTrack= track.GetClone() # Clone the Tracks
            target.InsertTrackSorted(newTrack) # Place the Tracks into Insts
            
        doc.AddUndo(c4d.UNDOTYPE_DELETE, op)
        op.Remove() # Remove Selected Items
        doc.EndUndo()
    c4d.EventAdd()
    c4d.CallCommand(12147) # Update Scene
if __name__=='__main__':
    main()

#8

Thanks Hypoly!

W


#9

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.