Jump to content

Hi guys

please be considerate and disable AD blocker when you are on the C4D Cafe, because it helps us run this forum. We minimalized showing of the AD's all across the forums, so please, be so kind and at least help us by having AD blocker turned off.

 

Cheers,
Cafe Staff 



SolarPH

Phyton Script for Ordering Objects

Recommended Posts

SolarPH    6

Do anyone know how to make an order of values, I need a logic to work with python like this:

 

1st Lowest value to Output1

 

2nd Lowest Value to Output2

 

3rd Lowest Value to Output3

 

etc.

 

I have tried to make it as this:

 

     if A < B and C:

          Output1 = A

     elif A > B < C:

          Output1 = B

     elif A and B > C:

          Output1 = C

 

it will make the script long, and it was only for Output1.

 

I need a script that reads what is the 1st, 2nd, 3rd, etc. and output the object where it came from.

 

In the original project, I have 12 linked blocks, and I need to work on 12 effectors which is operated by the logic that I said earlier.

 

To make another example of the logic, I need it top work as this:

 

If "highest" is Av (which is "Av" the one from a float input port), the output in Output1 is "A" (which is an object linked from a user data of an object)

 

or

 

If "Highest" is (a value in tuple or the tuple ID), the output in Output1 is "A"

 

I have this order:

 

Av for A

Bv for B

Cv for C

etc.

 

I need a total of 12 Outputs for 1st to 12th lowest Value.

 

Also, I have a value which is the MAX value compared, which is 100000.

Share this post


Link to post
Share on other sites

jed    165

I think this works - not sure how, just pasted some code I found online. It takes the Y position of 5 cubes, sorts lo-hi and outputs the cube names as string.

 

import c4d
# sort cubes by Y position lo-hi
# cube name output as string

def main():
    global Output1, Output2, Output3, Output4, Output5
    
    mylist = [('cube1', Input1), ('cube2', Input2), ('cube3', Input3), ('cube4', Input4), ('cube5', Input5)]
    
    mylist.sort(key=lambda tup: tup[1]) # sort list of tuples by 2nd element lo-hi
    
    Output1 = mylist[0][0]
    Output2 = mylist[1][0]
    Output3 = mylist[2][0]
    Output4 = mylist[3][0]
    Output5 = mylist[4][0]

https://www.dropbox.com/s/pip4h9g0ef40us8/mylist.c4d?dl=1

 

not very elegant, but some ideas for you.

Share this post


Link to post
Share on other sites
SolarPH    6
16 hours ago, jed said:

I think this works - not sure how, just pasted some code I found online. It takes the Y position of 5 cubes, sorts lo-hi and outputs the cube names as string.

 


import c4d
# sort cubes by Y position lo-hi
# cube name output as string

def main():
    global Output1, Output2, Output3, Output4, Output5
    
    mylist = [('cube1', Input1), ('cube2', Input2), ('cube3', Input3), ('cube4', Input4), ('cube5', Input5)]
    
    mylist.sort(key=lambda tup: tup[1]) # sort list of tuples by 2nd element lo-hi
    
    Output1 = mylist[0][0]
    Output2 = mylist[1][0]
    Output3 = mylist[2][0]
    Output4 = mylist[3][0]
    Output5 = mylist[4][0]

https://www.dropbox.com/s/pip4h9g0ef40us8/mylist.c4d?dl=1

 

not very elegant, but some ideas for you.

yes, this might work.

 

I will test this right away

Share this post


Link to post
Share on other sites
SolarPH    6

the code you sent works.

 

if you also do know a script that works like a collision node but mixed with the script you sent?

Share this post


Link to post
Share on other sites
jed    165
1 hour ago, SolarPH said:

if you also do know a script that works like a collision node but mixed with the script you sent?

 

Can you be more specific - why not just use collision nodes ?

 

re lambda in above script, It's a new one for me - still not sure how it works, but I also found a similar method itemgetter that can reference the 2nd element of a tuple as a search criteria. Need to import the module -

 

import c4d
from operator import itemgetter

def main():
    
    mylist.sort(key = itemgetter(1)) # ascending sort
        
    mylist.sort(key = itemgetter(1), reverse = True) # decending sort

 

Share this post


Link to post
Share on other sites
SolarPH    6
On 8/4/2017 at 10:09 PM, jed said:

 

Can you be more specific - why not just use collision nodes ?

 

re lambda in above script, It's a new one for me - still not sure how it works, but I also found a similar method itemgetter that can reference the 2nd element of a tuple as a search criteria. Need to import the module -

 


import c4d
from operator import itemgetter

def main():
    
    mylist.sort(key = itemgetter(1)) # ascending sort
        
    mylist.sort(key = itemgetter(1), reverse = True) # decending sort

 

I just came back.

 

I think I will just reverse the order of the result reference by typing in 2 for 1st, 1 for 2nd, and 3 for third.

 

Also, I think changing the integer in "tup:tup[]" after the key changes the referenced number for sorting.

 

And oh, I used a pre-made 12-block collision detector which objects collide with the collision box.

Share this post


Link to post
Share on other sites
SolarPH    6

The reverse order works in lambda sort key too.

 

Now, I am having problems in color mixing, and one way to make the color mixing work the right way, is to make another layer of list with sorting. Sometimes it needs an ascending order, sometimes descending. What do you think the best way to order them for perfect color mixing?

 

Average RGB value used for sorting didn't help.

 

I have an idea of having the vector into order list, but comma would separate values. How can I input vector as the sorting value?

 

Any ideas?

Share this post


Link to post
Share on other sites
jed    165

Not sure how you would sort color - maybe by Hue from HSV. I just about understand colorspace in XPresso, but not in Python. When I've outputted a color from Python, it's just as c4d.Vector ie

 

def main():
    global color
    
    R, G, B = 0.75, 0.06, 0.8
    
    color = c4d.Vector(R, G, B)
    
    print color.x, color.y, color.z # get RGB

 

Share this post


Link to post
Share on other sites
SolarPH    6
On 8/11/2017 at 6:02 AM, jed said:

Not sure how you would sort color - maybe by Hue from HSV. I just about understand colorspace in XPresso, but not in Python. When I've outputted a color from Python, it's just as c4d.Vector ie

 


def main():
    global color
    
    R, G, B = 0.75, 0.06, 0.8
    
    color = c4d.Vector(R, G, B)
    
    print color.x, color.y, color.z # get RGB

 

I may test that sometime. For now, I will test it with normal Fusion Texture color assignment than my usual overlay texture method.

 

I do the texture overlay method to make the rig piece optimized. It makes the textures stay on low count (if the object needs to be multiplied by dragging an object while pressing Ctrl) because of the textures not modified, but as I see in my current project that have a mix range of 5 colors, it do not follow a smooth change in gradient, sometimes it just instantly changes black to red, or not having the correct division of colors. Sometimes it goes "Color1(100%) -> (3) Color1(50%) + Color2(50%) -> Color2(100%)" in a 5-block transition.

 

I can share you the project file if you want to see it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

×