Notifications
Clear all

[Closed] Applying parenting in a When construct

I’m trying to parent a bunch of objects to a shape object when I move it, with the following:


    when transform spinners change id:#spinnerRotate obj  do
    	(
    		[color=DarkOrange]-- if the spinner object isn't already being affected[/color]
    		if tempObj != obj then
    		(
    			tempObj = obj 
    			spinnerTransform = obj.transform.position
    
    			rotationGroup = #()
    			for i = 1 to cubeArray.count do
    			(
    				--if a cube is on the same z plane as the spinner object
    				if (cubeArray[i].transform.position.z) == (spinnerTransform.z) then 
    				(
    					append rotationGroup cubeArray[i] --keep the group for later
    					append obj.children cubeArray[i] --parent the cube to the spinner
    				)
    			)
    					    
    		)
    	)
    )
    
    
    
If I check the assignment with a print statement, it shows that the object is parented, but when I move the spinner object, it doesn't move the children, nor does it show any form of parenting has taken place in the properties dialog.

I’ve also tried to parent the other way, using:


 cubeArray[i].parent = obj
Is this parenting only occuring in the when construct then being removed?  I'm slightly confused.

I should add that a normal parenting process, such as


   selection.parent = $Box01

works fine.

4 Replies

Here’s a simplified example that people can copy/paste/run:


s1 = sphere pos: [20,0,0]
s2 = sphere pos: [-20,0,0]
spheres = #(s1,s2)
b1 = box()

tempObj = 0
when transform spheres change obj do
(
	if tempObj !=Obj then
	(
		for i in obj.children do deleteItem obj.children[i]
		b1.parent =Obj
		
		print s1.children
		print s2.children	
		tempObj = Obj
	)
)

You’ll notice that when you move one of the spheres, the box stays stationary and the object properties stay unlinked, even though the children print statement is telling you that the Box is a child of either object.

This happens with or without the children deletion statement.

Well, dunno if this is what you want, but it works with your simpler example.

 
s1 = sphere pos: [20,0,0]
s2 = sphere pos: [-20,0,0]
aspheres = #(s1,s2)
b1 = box()
when select aspheres change obj do
( 
 b1.parent=obj
)

Hope it helps.

Artur, not what I wanted, but exactly what I needed! It actually makes a lot more sense to do it the way you have implemented. The code is working now.
Thank you very much.

Now can anyone explain why the transfom method wouldn’t be working? I have a few inklings, but i’d love to hear from the switched-on people.

At a very first glance, isn’t this the problem:

for i in obj.children do deleteItem obj.children[i]

You’re going in a loop through actual objects (not indexes) while at the same time you try to delet a obj.children[i], which accepts an integer value (an index), but instead, you supply it with an actual object.

So, to make it clear what I mean, let’s assume the loop is in progress and we bumped into the first object:

i = $myBox01

then you want it to perform this method:

deleteItem $myBox01[$myBox01]

This doesn’t make sense.

But I didn’t try your code, I only noticed this mistake at first glance, so it might not be it, but I’d guess that might have been the problem.