Random colored assets. (Random colors, same material?)

More like bad practice, for the most part, unless you know exactly what you’re importing into the namespace and need to work with most of it. If you don’t have a clue though, it can be trouble – overwritting values assigned to names you didn’t know were taken may eff something up good without you even realizing. My two cents.

I’m getting confuse with who’s better then who conversation :slight_smile:
(Let’s move to a private Group PM if we are going to do this)

You’re trippin. We talk coding and how to do it more efficiently, nothing more, nothing less.
If one doesn’t want to consider themselves in need of learning that’s another story.

Ah ok.
I think it would be best now if all 3 of us shut-up . . . :slight_smile:

Back on topic, @Cotaks, I agree your solution is really good, but I’m so bad at python, I still can’t figure it out for my specific setup. I wanted to try to figure out your technique a little before I spammed this forum for help. (You guys shouldn’t have to do everything for me, right?)

So, here’s the problems I’m having. I’m spawning an armature with a series of child meshes. If I just add an object, how do I make it start with the correct armature modifier?

Also, I’ve tried everything, and random numerical generation doesn’t seem to work when you add objects, and the random code is within the spawned object.

If you want to take a look, I’ve included another blend.

The snakes are supposed to spawn with random color within a range.
The head should be a the same color as the body.
The snakes should spawn with a random hat.
The hat should be a random color as well.

The event that spawns the snakes needs to be from from logic brick sensors, not from python controllers. As you see, I may want for example one snake to spawn after a delay, and another to spawn after an event, such as the press of the space-bar for this example, and writing every possible event is not efficient.

Here’s the blend.
Nope_Rope.blend (741.5 KB)

I think you are looking for :arrow_down:

Armature Logic Brick --> Constrain Type --> Set Target

If you need this function in python & we are on the right track just tell me.

Something like this?

Nope_Rope_2.blend (745.1 KB)

So, I’m going to do some more testing as I don’t want to jump to conclusions, but I think this is exactly what I was trying to do. Thanks a lot.

My problem was I didn’t understand you had to include the sensor in the python code, even if you have it hooked up to a logic brick sensor. I think i get it now.

Imma test this some more. Preemptive-thank you, and thank you for your time already!

Late reply, but If I wanted to add a controller object, with the snake armature parented to the controller object, and the snake geometry parented to the snake armature, how would I still change their color? How can I make the hat a child of the child of the added object? EG…

(added_object = own.scene.addObject(‘New_controller_object’, own,0)

Where “New_controller_object” has “Nope_Rope_Armature” as a child, and “Nope_Rope” as a child of that armature.

So far so good, but now how do I add the hat parented to “Nope_Rope_Armature”, as well as how do I make the “Nope_Rope” object change color when it
s parent is not the (added_object)?

For all the objects that you spawn.
You can access them.
For instance. You want parents & colors changed right after spawn :arrow_down: :question:

#This can be any color here
COLOR = [1,0,0,1]
CONTROLLER_OBJ = own.scene.addObject(‘New_controller_object’, own,0)
ARMATURE_BONES_OBJ = own.scene.addObject(‘Nope_Rope_Armature’, own,0)
ARMATURE_BONES_OBJ.parent = CONTROLLER_OBJ
ARMATURE_MESH_OBJ = own.scene.addObject(‘Nope_Rope’, own,0)
ARMATURE_MESH_OBJ .parent = ARMATURE_BONES_OBJ
ARMATURE_MESH_OBJ .color = COLOR

this should do the trick:

        for child in added_object.children:
            
            if child.children:
                for sub_child in child.children:
                    sub_child.color = [random.uniform(0.00,1.0) ,random.uniform(0.00,1.0) ,random.uniform(0.0,1.0) ,1]

            child.color = [random.uniform(0.00,1.0) ,random.uniform(0.00,1.0) ,random.uniform(0.0,1.0) ,1]

hat.setParent(Nope_Rope_Armature)

Thank you for being patient.

Regarding “hat.setParent(Nope_Rope_Armature)”
How does that parent the added hat to the added armature? It changes the parent, but not to the armature that was added with the controller object.

Like, do I put it here somehow? (Nope_rope_ARMATURE)
if sensor.positive:

    added_object = own.scene.addObject('CONTROLER_OBJECT', own,0)
    added_hat = own.scene.addObject(random.choice(hat_list), own,0)
    added_hat.setParent(added_object.Nope_rope_ARMATURE)

Confused. It either spawns the object parented to the original object, not the spawned one, or it doesn’t spawn the object at all.

What am I missing?

oh right.

i suggest to add an empty to the place where you need the hat. Parent that empty to the armature. Now we can grab the position as well.

put a property on that empty ‘hat_spawner’.

    added_object = own.scene.addObject('CONTROLER_OBJECT', own,0)
    
    #check if the added object has things perented to it
    if added_object.children:
        
        #loop trough the objects that are parented to the hit_box
        for child in added_object.children:
            
            #check if the the object parented to the hitbox has its own objects parented to it (armature)
            if child.children:
                
                #loop trough the objects that are parented to it
                for sub_child in child.children:
                
                    #if property is in the object
                    if 'hat_spawner' in sub_child: 
                        hat = own.scene.addObject(random.choice(hat_list), sub_child,0)
                        #set position and orientation
                        hat.worldPosition = sub_child.worldPosition
                        hat.worldOrientation = sub_child.worldOrientation
                        hat.color = [random.uniform(0.00,1.0) ,random.uniform(0.00,1.0) ,random.uniform(0.0,1.0) ,1]
                        #parent the hat
                        hat.setParent(sub_child)
                       

whole loop mechanism xD

but if you are sure that only the empty is parented to the armature and only the armature is parented to the hitbox then you can do:

empty = added_object.children[0].children[0]
added_hat.setParent(empty)
#set position and orientation as well here

Your second code example is closer to my setup, as position isn’t important. (All objects share the same origin.) but the problem still is, how does it know to parent the added_hat to the spawned instance/object/armature rather than any other?

If I input “added_hat.setParent(empty)” it will parent it to the original instance of the empty that was parented to the original controller_object, instead of the newly spawned instance of “empty”.

Also, If I parent the hat to the empty, it still won’t be deformed by the armature.

In case I wasn’t clear, here’s another blend so you can see exactly the issue. Notice when armatures move, the hats don’t deform with the armature, but do in fact move with the spawned instance.
Nope_Rope_3.blend (751.3 KB)

(This is the old code, but I followed your last post and It had the same issue.)

well in that case you need to parent the empty to the head bone.
orgin should always be on/near the center of the object itself.

here you go:
Nope_Rope_3.blend (744.3 KB)

(no idea what you had done with the hats so i created new ones)

#edit forgot a line, add this line at line 25

                        sub_child.color = [random.uniform(0.00,1.0) ,random.uniform(0.00,1.0) ,random.uniform(0.0,1.0) ,1]
          

Well, I finally understood your instructions, and got it working. I literally doubled my knowledge of Python in the process, and even figured out how to take your last file, and properly make the sub-children sneks random colors too!

Thank you for taking the time to help me with this, and for being patient. I wanted to understand the code, not just copy-paste, and you helped me learn just that. Thanks again, and good luck!

P.S.Also, sorry you had to endure those macabre, horrifying monkey snake things in the example demos. The more I used them as examples, the more they scared me, especially at the end when they were gesticulating with hats. XD

2 Likes