Sharing custom properties between scenes

General discussion about the R.U.B.E editor
Post Reply
FizzyChicken
Posts: 7
Joined: Wed Jan 23, 2013 10:14 pm

Sharing custom properties between scenes

Post by FizzyChicken »

I'm potentially going to have hundreds of separate RUBE scenes which all need to share the same set of custom properties. Is there a way I can do this ?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Sharing custom properties between scenes

Post by iforce2d »

Custom properties can be created by script, so right now this is the only way you could 'import' them. For example:

addCustomProperty('body','mycustomfloat','My custom float','float');

Unfortunately, while script is not able to obtain the value type for existing properties, otherwise you could make a script to print out the above commands, and then paste that script into another scene.

Copying the 'customPropertyDefs' element of the .rube JSON directly would also be one (pita) way to do it.

I think what you really need is for scripts to be able to read and write files, then you could keep an external file from which every scene can read the custom property definitions. I'm thinking of adding this in the next update.
FizzyChicken
Posts: 7
Joined: Wed Jan 23, 2013 10:14 pm

Re: Sharing custom properties between scenes

Post by FizzyChicken »

Thanks for the help - much appreciated.
nikcain
Posts: 7
Joined: Wed Dec 26, 2012 11:58 am

Re: Sharing custom properties between scenes

Post by nikcain »

I was about to post a question on the same topic when I saw this one.

I've the same situation - lots of scenes and the problem of making a change that needs to be applied in all of them (ie I have a body called 'ball' and I want to change the angular damping).

I initially hoped that a script could load scenes and apply the changes on each one and resave it (and re-export it as well). Once I realised scripts were only run from the GUI and on the loaded scene my initial hope was that the editor could be run from a command line with a scene and a script, but that's not possible either.

So I don't mind how it would be achieved, but the end goal (for me) has to be all the exported raw info scenes to have the change implemented. So it needs to run through 100's of scenes, make a change, re-export each one (for me it's raw info).
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Sharing custom properties between scenes

Post by iforce2d »

Yes, I have run into exactly this requirement, and others have too. I am thinking to add a non-interactive mode so that the program can be called from the command line to do... pretty much what you say there, except that the script would be an external file loaded in to execute non-interactively, rather than running a script from the GUI. Unfortunately I am a bit short on time at the moment so I don't think I'll be able to get to this very soon.
nikcain
Posts: 7
Joined: Wed Dec 26, 2012 11:58 am

Re: Sharing custom properties between scenes

Post by nikcain »

ok, no problem - I'm happy to wait. I can probably figure out some kind of bash script with sed in the meantime...
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Sharing custom properties between scenes

Post by iforce2d »

You might find PHP or Javascript an easy way to deal with JSON for this kind of purpose, as they both have easy methods for parsing and dumping JSON. There may be even better options, but these are the ones I am familiar with and would use over sed if I was in your shoes.

I would go with PHP because it's a bit easier to deal with file io, although there are Javascript things (node.js?) that could probably do the job as well. PHP is most commonly used server-side to prepare webpages, but you can also easily run scripts as a standalone process, invoking like: php myfile.php

Perhaps you already know about all this, but I'll outline a few things for people coming along in future. You can use the file_get_contents and json_decode functions to quickly turn your .rube or .json file into an associative array in memory:

Code: Select all

$fileStr = file_get_contents( $argv[1] ); // argv[1] is the first parameter
$json = json_decode( $fileStr, true ); // true to create associative arrays
After this, the structure of the JSON can be quite intuitively accessed like this:

Code: Select all

$json['body'][3]['fixture'][1]['friction']
Of course the literal values there are not a practical example, you would need to loop over the contents of the array to find whatever it is you need to change, eg. in this case, something like:

Code: Select all

for ($i = 0; $i < count($json['body']); $i++) {
    if ( $json['body'][$i]['name'] == 'ball' )
         $json['body'][$i]['angularDamping'] = doubleval( $argv[2] ); 
}
echo $json_encode( $json );
This would dump the result JSON to stdout, which you could then direct into a file. Hmm... in fact, it seems the entire script would be no more than the first and third code blocks I have stated here, enclosed in <?php ?> tags of course. The code may be a bit of an eyesore, but what you can do with it in a few lines is really handy.

See also: Executing PHP files
Post Reply