0
import maya.cmds as cmds

sel = cmds.ls(sl = True)
cmds.spaceLocator(n = 'driver')
for i in sel:
    cmds.parentConstraint(i, 'driver', n = 'delPs', mo = False)
#until this line, the 'driver' keeps its position at the centre of i
    cmds.delete('delPs')
#after this line, the 'driver' moves to the pivot point of the last item on the selection. 

I'm trying to keep locator(driver) at the centre of the selected objects after delete constraint node.Can I get some advise?

  • I dont understand what you want to do. Do you want your locator at the center of everything ? Do you want one locator at the center of each object ? At the moment your script is putting the locator at the center of the last object – DrWeeny Jul 29 '19 at 02:36

1 Answers1

0

im not sure to understand your problem but here is some solutions, hope one will help you, im using math instead of constraint

from __future__ import division
import maya.cmds as cmds

# =========================
#just to illustrate an exemple selection of 10 sphere combined
import random
psph = [cmds.polySphere()[0] for i in range(4)]
for i in psph:
    cmds.setAttr(i+'.t', random.uniform(0.0, 10), random.uniform(0.0, 10), random.uniform(0.0, 10))
# sph_comb = cmds.polyUnite(psph)
cmds.delete(psph , ch=True)
# ===============================

def getCentroid(sel):

    obj = cmds.ls(sel, o=True)
    sel = cmds.polyListComponentConversion(sel, tv=True)
    sel = cmds.ls(sel, flatten=1)

    if len(obj) > 1:
        pos = []
        for s in sel:
            p = cmds.xform(s, q=1, t=1, ws=1)
            pos += p
    else:
        pos = cmds.xform(sel, q=1, t=1, ws=1)
    nb = len(sel)
    myCenter = sum(pos[0::3]) / nb, sum(pos[1::3]) / nb, sum(pos[2::3]) / nb

    return myCenter

# example for each sphere
sel= cmds.ls(psph)
for i in sel:
    loc = cmds.spaceLocator(n = 'driver')[0]
    coord = getCentroid(i)
    cmds.setAttr(loc+'.t', *coord)

# for only one at the center of all sphres
sel= cmds.ls(psph)
coord = getCentroid(sel)
loc = cmds.spaceLocator()[0]
cmds.setAttr(loc+'.t', *coord)

EDIT : fix some code issue on my function getCentroid because xform can get multiple object

DrWeeny
  • 2,487
  • 1
  • 14
  • 17