Dear list,
I wanted to edit some Pd files so that I can change the coordinates of
*some* lines. For example, if in a pd file there is the content:
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 474 838 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 293 207 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
I wanted:
a) in the lines beginning with "#X obj", to change the 2 fields afterwards
to a general value I'll give in the command (e.g. all lines are changed
"#X obj 100 100 ...")
b) to exclude from this operation the line numbers I supply, preferably in
an expression such as "5 10 20-30" etc.
To give an example, with the command "100 100 4-6", the text above would
be changed to
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 100 100 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 100 100 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
where all lines starting with #X obj were change, except for lines 4-6.
Further numbers or ranges after 4-6 would indicate other lines to exclude
from the replacement.
I know this isn't a task which can be done in Pd, but much better in a
language such as lua or python. As I don't know any of these languages, I
would appreciate some guidance on how to start, and maybe I could myself
continue the work. Preferably I would like to try it in lua, so that I
could process it in pdlua. But any suggestions are welcome. Except using
grep, as I only have access to an ms-dos console, no bash or similar ones.
Thanks,
jmmmp
I'd guess it probably _is_ doable in Pd, with some tangled use of the [text] (using "search" and "set"). When I do this type of manipulation, it's for mobile, so I'm limited to Objective-C and Java, but if you use Java you could adapt Chris' parser:
https://github.com/chr15m/PdDroidParty/blob/master/src/cx/mccormick/pddroidp...
to get a java data structure representation. Or, potentially simpler, port the parsing logic in that file to python or whatever language you want to use.
On Sat, Mar 19, 2016 at 12:36 PM, João Pais jmmmpais@gmail.com wrote:
Dear list,
I wanted to edit some Pd files so that I can change the coordinates of *some* lines. For example, if in a pd file there is the content:
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 474 838 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 293 207 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
I wanted: a) in the lines beginning with "#X obj", to change the 2 fields afterwards to a general value I'll give in the command (e.g. all lines are changed "#X obj 100 100 ...")
b) to exclude from this operation the line numbers I supply, preferably in an expression such as "5 10 20-30" etc.
To give an example, with the command "100 100 4-6", the text above would be changed to
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 100 100 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 100 100 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
where all lines starting with #X obj were change, except for lines 4-6. Further numbers or ranges after 4-6 would indicate other lines to exclude from the replacement.
I know this isn't a task which can be done in Pd, but much better in a language such as lua or python. As I don't know any of these languages, I would appreciate some guidance on how to start, and maybe I could myself continue the work. Preferably I would like to try it in lua, so that I could process it in pdlua. But any suggestions are welcome. Except using grep, as I only have access to an ms-dos console, no bash or similar ones.
Thanks,
jmmmp
Pd-list@lists.iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Hi João,
This functionality can be created in any proper language, but it is hard to create a generic solution based on your example. For instance, how do you know it will be all the lines except the ones you specify, and do you really want all the objects at 100,100 ?
Only if you have a large number of patches with the same structure and object order it is worthwhile to create a program like you propose to patch them. Otherwise a proper code-editor will be as time-efficient in changing the patches.
If in this case you just want the [expr] and [hradio] at 100,100 for a number of patches a sed* script will do...
Creating a script to convert patches costs effort, so you want it as generic as possible. If it can be used only once, a code editor is a better way.
Fred Jan
*) sed is a unix tool just like grep, only more flexible... Maybe you should try to install cygWin, then you get most unix tools on Windows. However, prepare for a steep learning curve...
Success,
Fred Jan
P.S. first non-optimized attempt: cat jmmmp.pd | sed -e 's/#X obj [1-9][0-9]* [1-9]expr/' -e 's/#X obj [1-9][0-9]* [1-9][0-9]* expr/#X obj 100 100 hradio/'
Dear list,
I wanted to edit some Pd files so that I can change the coordinates of *some* lines. For example, if in a pd file there is the content:
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 474 838 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 293 207 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
I wanted: a) in the lines beginning with "#X obj", to change the 2 fields afterwards to a general value I'll give in the command (e.g. all lines are changed "#X obj 100 100 ...")
b) to exclude from this operation the line numbers I supply, preferably in an expression such as "5 10 20-30" etc.
To give an example, with the command "100 100 4-6", the text above would be changed to
#N struct 1164-element float x float y float nr float index float nr-show float tick-show; #N canvas 168 80 670 664 gui 0; #X obj 186 548 textfile; #X obj 167 328 openpanel; #X obj 395 878 delay; #X obj 100 100 expr 60*$f1/$f2*1000; #X msg 166 668 stop; #X obj 100 100 hradio 17 1 0 6 $0-instructions $0-colord-i empty 0 -6 0 8 -134268 -1 -1 0;
where all lines starting with #X obj were change, except for lines 4-6. Further numbers or ranges after 4-6 would indicate other lines to exclude from the replacement.
I know this isn't a task which can be done in Pd, but much better in a language such as lua or python. As I don't know any of these languages, I would appreciate some guidance on how to start, and maybe I could myself continue the work. Preferably I would like to try it in lua, so that I could process it in pdlua. But any suggestions are welcome. Except using grep, as I only have access to an ms-dos console, no bash or similar ones.
Thanks,
jmmmp
Pd-list@lists.iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
Hi Fred,
This functionality can be created in any proper language, but it is hard
to create a generic solution based on your example. For instance, how do
you know it will be all the lines except the ones you specify, and do
you really want all the objects at 100,100 ?
Well, the purpose would be to know because the code makes it happen. Just
like you know that [expr if($f1 >= 0, 1, -1)] gives out a 1 for numbers
above and 0, and -1 for numbers below.
Only if you have a large number of patches with the same structure and
object order it is worthwhile to create a program like you propose to
patch them. Otherwise a proper code-editor will be as time-efficient in
changing the patches.If in this case you just want the [expr] and [hradio] at 100,100 for a
number of patches a sed* script will do...Creating a script to convert patches costs effort, so you want it as
generic as possible. If it can be used only once, a code editor is a
better way.
With one single patch I have here, there are 8805 lines of code. Is it
easier to change this by hand than to find a general solution? And if I
change a couple of objects and want to try this again, should I redo it
every time?
Fred Jan
*) sed is a unix tool just like grep, only more flexible... Maybe you
should try to install cygWin, then you get most unix tools on Windows.
However, prepare for a steep learning curve...Success,
Fred Jan
P.S. first non-optimized attempt: cat jmmmp.pd | sed -e 's/#X obj [1-9][0-9]* [1-9]expr/' -e 's/#X obj
[1-9][0-9]* [1-9][0-9]* expr/#X obj 100 100 hradio/'
I thank you for this, but I think you didn't understand the purpose: I'm
looking for a general tool. If I needed to edit 5 lines, it would have
been much faster to do it by hand than to write a mail bothering people
about it.
Best,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is done.
If you remove the #'s for the puts "..." lines, you can see some interim results (which will ruin the patch format).
Greetings,
Fred Jan
Hi Fred,
thanks, that looks promissing. But there is a problem, I'm on windows.
Does the first line in the patch means that I have to find a unix computer
to run it? Or can it be run from tcl?
Best,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is
done.If you remove the #'s for the puts "..." lines, you can see some interim results (which will ruin the patch format).
Greetings,
Fred Jan
On 2016-03-22 01:44, João Pais wrote:
Hi Fred,
thanks, that looks promissing. But there is a problem, I'm on windows. Does the first line in the patch means that I have to find a unix computer to run it? Or can it be run from tcl?
No, it should work on Windows too. The line is only relevant if you want to run the script directly as an executable program, without feeding it to tclsh. If you start tclsh with the script as the first argument, it should work on any platform.
No testing on Windows is done however. There might be some issues with the line ending convention (LF vs CR,LF).
But knowing where to find a unix computer is always a good idea ;-),
Fred Jan
Best,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is done.
If you remove the #'s for the puts "..." lines, you can see some interim results (which will ruin the patch format).
Greetings,
Fred Jan
Hi Fred,
I did some testing today when I had some more time. It works great, and if
I use it in a command like xxxx > new.pd, I get the new patch out of it.
If I may, I would ask for a small improvement: it would be great if the
processing would be done in the opposite way; that is, all lines with #X
obj are processed, *except* the ones given as arguments. I can also work
with the version you sent me, but for the exercises I'm thinking about,
only an exception of lines will be kept in the original way.
I tried to do it myself, but quickly I came to the conclusion that my lack
of experience with scripting languages doesn't really help me.
Thanks a lot,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is
done.If you remove the #'s for the puts "..." lines, you can see some interim results (which will ruin the patch format).
Greetings,
Fred Jan
Hi João,
Changed the behaviour and also added some code that prevents #A and #N lines from being changed. Make sure you proper test it, because I didn't. Consider this the price of free software :-).
@Dan: It is Tcl, but I should look into the gui plugin interface to make it work. And another way of selecting objects as there aren't any line numbers within Pd. It could be converted to some kind of rasterizer or align-to-grid option, but that may already exist...
Greetings & success,
Fred Jan
Hi Fred,
I did some testing today when I had some more time. It works great, and if I use it in a command like xxxx > new.pd, I get the new patch out of it.
If I may, I would ask for a small improvement: it would be great if the processing would be done in the opposite way; that is, all lines with #X obj are processed, *except* the ones given as arguments. I can also work with the version you sent me, but for the exercises I'm thinking about, only an exception of lines will be kept in the original way.
I tried to do it myself, but quickly I came to the conclusion that my lack of experience with scripting languages doesn't really help me.
Thanks a lot,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is done.
If you remove the #'s for the puts "..." lines, you can see some interim results (which will ruin the patch format).
Greetings,
Fred Jan
HI Fred Jan,
that sounds good. But I think you sent me the same file as before? They are exactly alike.
I don't want to push it, but I noticed 2 details that I could mention. But ignore them if you have better things to do:
noticed that it adds a first line with "lines to patch: 9 36 37 38 39..." etc. Anyway it is ignored by Pd when loading the file, I only noticed it by chance when I opened the pd file in a text editor
starting with "#X restore" and "#X msg" could also be processed, for a more coherent result.
But anyway, ignore all these if you have more interesting things to do. I can do any testing anytime for anything - since I can't program on anything else than Pd, at least it's my way of paying back the price of free software.
Afaik, there isn't any rasterizer or grid alignment option - you might ask
Jonathan Wilkes, since he's putting lots of new things into Pd2lork. Or
maybe the community might be interested in supporting your code to improve
patching quality - at least it could have an interesting result for you.
Best,
Joao
Hi João,
Changed the behaviour and also added some code that prevents #A and #N lines from being changed. Make sure you proper test it, because I didn't. Consider this the price of free software :-).
@Dan: It is Tcl, but I should look into the gui plugin interface to make it work. And another way of selecting objects as there aren't any line numbers within Pd. It could be converted to some kind of rasterizer or align-to-grid option, but that may already exist...
Greetings & success,
Fred Jan
Hi Fred,
I did some testing today when I had some more time. It works great, and if I use it in a command like xxxx > new.pd, I get the new patch out of
it.If I may, I would ask for a small improvement: it would be great if the processing would be done in the opposite way; that is, all lines with #X obj are processed, *except* the ones given as arguments. I can also work with the version you sent me, but for the exercises I'm thinking about, only an exception of lines will be kept in the original way.
I tried to do it myself, but quickly I came to the conclusion that my lack of experience with scripting languages doesn't really help me.
Thanks a lot,
Joao
Hi João,
It appeared to be a simple program in Tcl, which you should have, as it comes with Pd. The usage is: tclsh changeCoords.tcl patchName xcoord ycoord line1 ?line2? ... lineN arguments may be a number or a range, like 12-15.
The patch is currently dumped to the console. Only minimal checking is done.
If you remove the #'s for the puts "..." lines, you can see some
interim results (which will ruin the patch format).Greetings,
Fred Jan
Hi,
I tinkered around with tcl, until I think I found a version that suits my
needs exactly:
of patched lines doesn't get saved with the patch output.
It seems that the patch counts each line that begins with # as a real
line. But since the pd txt format splits lines, it turns out that there
are many lines that don't begin with #N. For example with the 2 objects
#N canvas 1066 230 670 662 gui 0; #X obj 0 0 cnv 12 400 260 empty $0-colorh-i empty 20 12 0 14 -220796 -1 0;
there are 3 lines in the text file, but the patch will only count 2.
Adding an incr lineCount after the last "puts $patchLine" does the job of
matching the number of lines in the text file with the line count.
Thanks very much, I think this is quite good already. I'll tinker a bit
more to see if I find out how to also process subpatches and messages. I
imagine that adding other if lines it could work.
Best,
Joao
that sounds good. But I think you sent me the same file as before? They are exactly alike.
Just checked, and I did send a newer version, I even added a 0.2 version number in the header. Here it is version 0.3.
On 2016-03-25 01:53 AM, João Pais wrote:
Hi,
I tinkered around with tcl, until I think I found a version that suits my needs exactly:
- I reversed the selection - if {[lsearch $lineList $lineCount] < 0} {
- I commented out line puts "lines to patch: $lineList", so that the
list of patched lines doesn't get saved with the patch output.
It seems that the patch counts each line that begins with # as a real line. But since the pd txt format splits lines, it turns out that there are many lines that don't begin with #N. For example with the 2 objects
#N canvas 1066 230 670 662 gui 0; #X obj 0 0 cnv 12 400 260 empty $0-colorh-i empty 20 12 0 14 -220796 -1 0;
there are 3 lines in the text file, but the patch will only count 2. Adding an incr lineCount after the last "puts $patchLine" does the job of matching the number of lines in the text file with the line count.
Sorry, the script was counting Pd-lines, not lines in the patch. Not much use if you look at the lines in a code-editor. If a line increment is done always, you could move it outside the if ...
Counting Pd-lines is useful if you want to correlate objects to the '#X connect' lines. But this would break down in case of sub-patches. I made another script for this...
Thanks very much, I think this is quite good already. I'll tinker a bit more to see if I find out how to also process subpatches and messages. I imagine that adding other if lines it could work.
The newer versions should be better as they prevent #A and #N from being corrupted.
Best,
Joao