"Parent" Reference

It has become common when creating DC’s to reference attributes in a parent component by using the “Parent” keyword rather than the name of the parent Component. I have been recommending its use because it allows more flexibility in replacing sub-components of a DC.

This “trick” does not appear to be documented anywhere. Is it officially supported by SketchUp? Is using “Parent” a best practice? If so, it should be put in the DC documentation somewhere.

4 Likes

This was new to me. But what happens if you name the component or one of its subcomponents “Parent”?

Anssi

Excellent question. It seems SketchUp has thought of it.

1 Like

I’ve done it that way on quite a few occasions. are you sure it’s not documented? (ie- I learned it, I didn’t just discover it on my own)

otherwise, I suppose I could of just picked it up from an individual along the way… maybe it was you :wink:

No error messages for me, but in the Attributes dialog the Parent component becomes listed as “ParentEntity”

Anssi

Right. Double-click the name to try renaming it to “Parent”. You should get the error message.

I’ve learned this from Eric in his great Basecamp video:

2 Likes

Well, I tried to use “Parent” for one thing and now i have a question to anybody who might know the answer.

I am trying to make an attribute formula, that would allow a component
to recieve a string data containing a name of parent component or group.
First thing I did is I wrote a formula “=Parent!Name” but that didn’t
work. I’ve tried several formulas, but still didn’t get a desired result.
Maybe you know just the right answer?

I cannot do tests right now, but your formula refers to a parameter called “Name” in the “parent”, so if the parameter is not defined, you should get an error message. I am not sure if there is a formula that will get the component definition or instance name and put it into a parameter.

Anssi

Thank you for reply. Well, this works if I add an attribute “Name” to a parent group/component and type in some value, e.g. “A”. Then child component recieves “A” value through formula “=Parent!Name”.
Looks like my first question was incorrect.
The point is, I’m looking for a way to make child component automatically recieve definition value without need to add attribute “Name” to parent group/component.

_name ?

Wow, Thank you! This works!!

Still it works from time to time, thats weird.

‘Built-in’ keys are usually prefixed by an _
So the displayed
name
is actually
'_name`
???
Dump the selected DC instance’s keys/values using:

Sketchup.active_model.selection[0].definition.attribute_dictionaries['dynamic_attributes'].each_pair{|k,v| puts"#{k} = #{v}"}

This will return a list if it’s a DC…

the ‘parent!attributename’ operator is very useful - I use it a lot - it works like this in the formula:

=parent!lenx
Returns the value of the parent component/group attribute called ‘lenx’

=parent!mycustomattribute
Returns the value of the parents attribute called ‘mycustomattribute’

The attribute must exist on the parent, so ‘=parent!name’ will only work if the parent has an attribute called ‘name’
‘name’ is not the name of the definition/instance, but the name attribute of the dynamic component - which is a predefined attribute you can add to your dynamic component.

It can be used in formulas also like:
=IF(parent!lenx<100,0,100)

1 Like

Well, Looks like I’ll have to learn Ruby to make things work. I dont even understand where to put the code you gave.

Select one DC component-instance.
Open the Ruby Console.
Edit>Copy the one line of text I gave you:

Sketchup.active_model.selection[0].definition.attribute_dictionaries['dynamic_attributes'].each_pair{|k,v| puts"#{k} = #{v}"}

Edit>Paste the text into the Console + [enter].
That then runs and looks at the component’s definition and finds its DC attributes.
It then prints the key and value of each of those.
That way you can see that what you thought would be called ‘name’ is actually ‘_name,’ and so on…

It’s not so hard to do.

3 Likes