try: import poser except ImportError: poser = SCENE = None raise RuntimeWarning("Script should run in Poser-Python.") else: SCENE = poser.Scene() def object_from_path(path, base=SCENE): """ Presetting base makes shortcuts possible. """ def extract_data(n): """:type n: basestring""" i_r = n.find("]") if i_r == -1: # first found closing bracket return n, None i_l = n.rfind("[", i_r-1) if i_l == -1: return n, None # i_l == first found "[" left from "]" # important to implement [...[...]] return n[:i_l], n[i_l + 1:i_r] def convert_data(d): if isinstance(d, basestring): if "," in d: d = d.split(",") for i, dd in enumerate(d): d[i] = convert_data(dd.strip()) else: try: d = float(d) except: pass if hasattr(d, "InternalName"): d = d.InternalName() return d if isinstance(path, basestring): path = [s.strip() for s in path.split("/")] if isinstance(path, list): while path[0] in "poser Scene SCENE": base = globals()[path.pop(0)] # Assume other iterator are already prepared to work. # E.g. you provided the right base to start with. assert hasattr(path, "__iter__") try: for p in path: cmd, data = extract_data(p) if data is None: base = getattr(base, cmd)() else: data = convert_data(data) if hasattr(data, "__iter__"): base = getattr(base, cmd)(*data) else: base = getattr(base, cmd)(data) except Exception as err: return "ERR: " + str(err) return convert_data(base)