[Closed] attach object/group to all group instances
I have an abject/group selected and I want to attach it to all goup instances!
Here I have a code, and it works, but it still needs some corrections! Let’s say, you have a group of glasses and you want to add it to all the tables that are groups and copied as instances! I have placed my glasses on one of the tables, then pressed btn and picked the table bellow=> this should automatically add it to all tables! The problem is with transform: If you rotated some tables, then the glasses may stay outside allready! Scale, I guess should not be affected by the scale of tables, otherwise, added glasses can get unreal dimesions! There are some errors also, when you have an object selected from an opened group, but I know how to fix- tu detach selection first, in this case! Please, who wants and are interested, you’re wellcome to comment!
fn filterSelect obj =
(
if isOpenGroupMember obj then return obj --and isValidNode obj
if isGroupMember obj then
(
while (obj.parent != undefined) and (not isOpenGroupHead obj.parent ) and (isGroupHead obj.parent) do obj = obj.parent
return obj
)
return obj
)
rollout ...
on instatachbtn click do
(
local objList = #()
local selObjList = #()
local target = pickObject()
target = filterSelect target
append selObjList target
j=1
while j<= objects.count and not keyboard.escPressed do
(
if areNodesInstances target objects[j] then
(
appendIfUnique selObjList objects[j]
--print objects[j]
)
j=j+1
)
-- print "============"
-- print selObjList
-- print "================Selection"
for i in selection do appendIfUnique objList (filterSelect i)
-- print objList
local delta, deltap, deltar, deltas
for i in objList do
(
if isOpenGroupMember i then detachNodesFromGroup i
delta = i.transform - target.transform
deltap = i.position - target.position
deltar = i.rotation-target.rotation
deltas = i.scale - target.scale
for j in selObjList do
(
maxOps.cloneNodes i cloneType:#instance newNodes:&newGroup
tempObj= newGroup[1]
tempObj.transform = j.transform+delta
--tempObj.scale = j.scale+deltas
-- tempObj.rotation = j.rotation + deltar
-- tempObj.position = j.position+deltap
attachNodesToGroup tempObj j
)
delete i
)
)
Here is a short preview of what I was talking about
Here is the code
and max file
first of all you have to name your groups and objects with a name that makes more sense than just “group” or “ZBrush_defualt_group”…
I managed position part only need to add rotation. Open attachment file select two teapots, use pickbutton and pick “table object” below teapots.
try(destroyDialog ::bgaRoll)catch()
rollout bgaRoll " "
(
fn filtGroup o = isGroupMember o
fn getRootGroupHead node = if isgroupmember node do
(
while node != undefined and (not isgrouphead node or isgroupmember node) do (node = node.parent)
node
)
pickbutton pick "PickGroup" pos:[5,5] width:100 height:20 filter:filtGroup
on pick picked obj do if obj != undefined and selection.count !=0 do
(
local rootGroup = getRootGroupHead obj
InstanceMgr.GetInstances rootGroup &groups
selObjs = getCurrentSelection()
if groups.count > 1 then
(
with redraw off
(
for i = 1 to groups.count where groups[i] != rootGroup do
(
maxOps.cloneNodes selObjs cloneType:#instance newNodes:&objs
for o in objs do
(
if o.parent != undefined do o.parent = undefined
offsetPOS = in coordsys rootGroup o.pos
o.pos = [offsetPOS.x,offsetPOS.y,offsetPOS.z/2]*groups[i].transform
attachNodesToGroup o groups[i]
)
)
attachNodesToGroup selObjs rootGroup
)
)
)
)
createDialog bgaRoll 110 30 10 110 style:#(#style_titlebar, #style_sysmenu, #style_toolwindow)
I know. I not like position alignement but … I guess you’ve already got a better solution for position and orientation.
this is not about an alignment. you don’t attach selection correctly. the problem is when a selection is a group member. in this case attachNodesToGroup fails with your code.
Yup. I have limited solution. For “all case” scenario we need to consider many different conditions
if selection have target
is selection is a group member or group head
is selection is child
etc.
correct… if it’s a group member don’t process it.
but… i think that any node should be cloned and attached with its children… so you have to make another little change in your script to support it. (i say “a little”. it means add two words)
Recently I’ve posted this snippet (mirror selection fn) on scriptspot.
-- ARGUMENT OPTIONS
-- axis enums: {#x|#y|#z}. Default X-Axis
-- center (miiror about point3) also you can use any object center. Default: world center
-- clone enums: {#copy|#instance|#reference}. Default instance
fn mirrorSelection axis:#x center:[0,0,0] clone:#instance = if selection.count != 0 do
(
local ax = case axis of (
(#x): [-1,1,1]
(#y): [1,-1,1]
(#z): [1,1,-1]
)
local parentslist = #()
maxOps.CloneNodes (selection as array) offset:[0,0,0] cloneType:clone newNodes:&clones
for i in clones do (append parentslist i.parent ; i.parent = undefined)
about center scale clones ax
for i = 1 to clones.count do clones[i].parent = parentslist[i]
free parentslist
)
mirrorSelection()
In this cases I am trying to clone selected parent object and all unselected children together
and I think that “expandHierarchy” not works here. So I used unparent/parent method
Not undertand?
edit
I do understand you. Well I’m not sure allways in my fn’s.
you’re both awesome, Denis and Branko! Thank you so much for your support! I was dreaming so long for such a tool and finally with your help, it will be possible to finish it! Thank you both for the years of scripting – they now can guide us to the solution!
you are thinking usually in the right direction… but there is no such thing as ‘quality assurance’ for max script developers. you are yours QA. so try all the time play not just a specific case but also around it.