Use a list or array for data?
-
I've created a bunch of user data and want to insert it into several sound tracks. The issue is that it gets extremely tedious writing this. Is there a more efficient way to write all this using arrays or lists?
I have attached an example oh how the user data looks within Cinema 4D.
track = obj.GetCTracks() obj[c4d.ID_USERDATA,4] = track[0].GetName() track[0][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,6] track[0][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,7] track[0][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,14] obj[c4d.ID_USERDATA,10] = track[1].GetName() track[1][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,11] track[1][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,12] track[1][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,9] obj[c4d.ID_USERDATA,15] = track[2].GetName() track[2][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,16] track[2][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,17] track[2][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,18] obj[c4d.ID_USERDATA,3] = track[3].GetName() track[3][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,20] track[3][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,21] track[3][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,22] obj[c4d.ID_USERDATA,24] = track[4].GetName() track[4][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,25] track[4][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,26] track[4][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,27] obj[c4d.ID_USERDATA,29] = track[5].GetName() track[5][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,30] track[5][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,31] track[5][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,32] obj[c4d.ID_USERDATA,34] = track[6].GetName() track[6][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,35] track[6][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,36] track[6][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,37] obj[c4d.ID_USERDATA,39] = track[7].GetName() track[7][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,40] track[7][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,41] track[7][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,42] obj[c4d.ID_USERDATA,44] = track[8].GetName() track[8][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,45] track[8][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,46] track[8][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,47] obj[c4d.ID_USERDATA,49] = track[9].GetName() track[9][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,50] track[9][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,51] track[9][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,52] obj[c4d.ID_USERDATA,54] = track[10].GetName() track[10][c4d.CID_SOUND_ONOFF] = obj[c4d.ID_USERDATA,55] track[10][c4d.CID_SOUND_START] = obj[c4d.ID_USERDATA,56] track[10][c4d.CID_SOUND_NAME] = obj[c4d.ID_USERDATA,57]
-
Hello,
The first thing that come to mind is a for loop. But you need to get informations in userData that are stored in your obj in a chaotic way.
What i suggest here, is to create those userdata in a more organized way (maybe with a script) so can easily create a loop in that case.
in pseudo code it could be something like this at the end.
tracks = obj.GetCTracks() for index, track in enumerate(tracks): obj[c4d.ID_USERDATA, index * 4 + 0] = track.GetName() track[c4d.CID_SOUND_ONOFF] = obj[index*4 + 1] track[c4d.CID_SOUND_START] = obj[index*4 + 2] track[c4d.CID_SOUND_NAME] = obj[index*4 + 3]
Let me know what do you think about it ?
Cheers
-
@m_magalhaes Yeah since the ID’s of my userdata are all over the place, maybe I better just stick with writing them all out
I was trying to make it so it started with just one track, and have an add/remove button at the top that would create additional tracks. But this starts to get extremely confusing for me to wrap my head around, as I’m only just a beginner.
-
hello,
this topic is really related with this one So you have maybe now a better idea on how add or remove userdata.
Because you changed your idea (by adding more tracks), the solution you came with is not good anymore. That's the perfect moment for stepping back. Think again about what you want to achieve and how you could do it. It's an iteration.
It's not a problem to do that, it's even what you should do. You could be totally blocked in few days and your code will be a nightmare to manage.
When you are managing data, you should think about array, dictionary and things like that or array of array.
You could probably run through all your user data (as long as they are visually organised) and build an array.
After that, you could just iterate thought that array.Don't be afraid to step back, think how you can organise your data. Most of the time your code will be shorter, easy to maintain, easy to update.
Cheers
Manuel -
hello,
if you don't have anything else to add, i'll mark this topic as "solved"
Cheers
Manuel