Script to duplicate an object and transform it along an axis

Apologies in advance - I am just starting to teach myself Python.

I would like to write a script to take an existing poly object (a road segment/section), and be able to duplicate it n times along either a x or y axis, but positioning each duplicate/copy so that the front of each duplicated road segment butts up against the rear of the prior road segment.  (That is, if I have a single segment of a road model, I would like to duplicate it n times in order to make a longer stretch of road from the duplicated sections. I would like to be able to select either the x or the y axis for this process.) I constructed a standard road segment of exactly 12 meters, so I have a constant length for the poly object.

Would very much appreciate any help with this scripting task.

# jooRequestedStackOneXform.py  0.1
# 
#  Authors:     Janne 'Joojaa' Ojala
#  Testing:     None
#  Request:     Pamela Case
#  Licence:     Creative Commons, Attribution, Share Alike
#  About:
#  
#  Usage:
#     import contained module and run:
#
#     jooRequestedStackOneXform('y+',5)
#
#     other possible directions are, x+, x-, y+, y-, z+ ,z-
#
#  Changelist:
#      24.01.2012  - Request 
 
 
import maya.cmds as cmd
 
def jooListMul(a, b):
    ret = []
    for i in range(len(a)):
        ret.append(a[i]*b[i])
    return ret
 
def jooListSub(a, b):
    ret = []
    for i in range(len(a)):
        ret.append(a[i]-b[i])
    return ret
 
 
def jooMoveOneBB(dir):
    options = {'x': [1,0,0], 'y': [0,1,0], 'z': [0,0,1]}
    dvec = options[dir[0]]
    bb = cmd.xform(q=1, bb=1)
    if (dir[1] == '-'):
        tvec = jooListSub(bb[:3],bb[3:])
    else:
        tvec = jooListSub(bb[3:],bb[:3])
    cmd.move(r=1,*jooListMul(tvec, dvec))
 
 
def jooRequestedStackOneXform(dir, num):
    if not len(cmd.ls(sl=1)) == 1:
        raise Exception('select one object or group')
    for i in range(num-1):
       cmd.duplicate()
       jooMoveOneBB(dir)

tough you probably  get things faster done with hitting x down drag and then shift+D a few times. No scripting needed.

Thank you again, Joojaa!

This function is something I want to add to a larger script. I really appreciate your help on this. Hope it was more or less trivial for you.

Trivial yeah, but not necessarily perfect for your need. took 8 minutes to write (i can read it form files timestamps) a few minutes to decide how to publish the options. Mainly I missed one step that took most f the time, i should refactor jooListMul and jooListSub to be calls to map instead but yeah no problem. Keeps my skill up.

So instead of:

def jooListMul(a, b):
    ret = []
    for i in range(len(a)):
        ret.append(a[i]*b[i])
    return ret

one should use:

from operator import mul, add
 
list(map(mul, [1,2,3], [0,0,1]))

Obviously with right lists, this should be a bit faster and more elegant

Thank you again!!!!!!!!!!!!!!!