discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Argument to children()

ZK
Zoltán Kócsi
Mon, Jan 27, 2025 2:56 AM

I know that I can't pass a module name as an argument, and that there
is the children() module as a work-around for that.

However, I could not find a way to pass an argument to a child.

I wonder whether there is a way, or it is really impossible?

What I would need it for is that I have a few different objects.
They all have a parameter.
I need to generate these objects, with different positions/orientations
and parameter values, all of which are calculated by an external
program. Something like this:

data.scad:

q_data = [ [[rot_vect], [tras_vect], para ], ... ];
p_data = [ [[rot_vect], [tras_vect], para ], ... ];

code.scad:

include <data.scad>

module q_object( para )
{
...
}

module p_object( para )
{
...
}

module generate( list )
{
for ( a = list ) {
rotate( a[0] )
translate( a[1] )
children( 0 )( a[2] ); // This is not valid code
}
}

generate( q_data ) q_object();
generate( p_data ) p_object();

The real generate() is a lot more complex and actually needs to
generate multiple objects with locally adjusted values, but the general
gist of the problem is there.

Thanks,

Zoltan

I know that I can't pass a module name as an argument, and that there is the children() module as a work-around for that. However, I could not find a way to pass an argument to a child. I wonder whether there is a way, or it is really impossible? What I would need it for is that I have a few different objects. They all have a parameter. I need to generate these objects, with different positions/orientations and parameter values, all of which are calculated by an external program. Something like this: data.scad: q_data = [ [[rot_vect], [tras_vect], para ], ... ]; p_data = [ [[rot_vect], [tras_vect], para ], ... ]; code.scad: include <data.scad> module q_object( para ) { ... } module p_object( para ) { ... } module generate( list ) { for ( a = list ) { rotate( a[0] ) translate( a[1] ) children( 0 )( a[2] ); // This is not valid code } } generate( q_data ) q_object(); generate( p_data ) p_object(); The real generate() is a lot more complex and actually needs to generate multiple objects with locally adjusted values, but the general gist of the problem is there. Thanks, Zoltan
JB
Jordan Brown
Mon, Jan 27, 2025 8:07 AM

You can set a $ variable that the child module can look at.

module repeat(n, dx) {
for (i=[1:n]) {
translate([i*dx,0,0]) {
$i = i;
children();
}
}
}

repeat(5, 20) text(str($i));

You can set a $ variable that the child module can look at. module repeat(n, dx) { for (i=[1:n]) { translate([i*dx,0,0]) { $i = i; children(); } } } repeat(5, 20) text(str($i));
ZK
Zoltán Kócsi
Tue, Jan 28, 2025 3:53 AM

Thanks for the tip!

Zoltan

Thanks for the tip! Zoltan