Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a bug ? #1

Closed
sexfio opened this issue Oct 7, 2022 · 4 comments
Closed

a bug ? #1

sexfio opened this issue Oct 7, 2022 · 4 comments

Comments

@sexfio
Copy link

sexfio commented Oct 7, 2022

import bpy
import bmesh
import random as rnd

def deleteall():
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False, confirm=False)
for m in bpy.data.materials:
bpy.data.materials.remove(m)

def RndColor():
return [rnd.uniform(0,1.0),rnd.uniform(0,1.0),rnd.uniform(0,1.0),1.0]

def SetMaterial(obj,color):
material = bpy.data.materials.new(name=f'rnd color material') # maybe repeated
material.use_nodes = True
#rnd_material.diffuse_color = RndColor()
#bpy.data.materials[rnd_material.name].node_tree.nodes["Emission"].inputs[0].default_value = (0.0441256, 0.57093, 1, 1)
material.node_tree.nodes["Principled BSDF"].inputs["Metallic"].default_value = rnd.uniform(0,1)
material.node_tree.nodes["Principled BSDF"].inputs["Roughness"].default_value = rnd.uniform(0,1)
material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = color
obj.data.materials.append(material)

def go_1():
deleteall()

a = []
for _ in range(9):
    x = rnd.uniform(-5,5)
    y = rnd.uniform(-5,5)
    z = rnd.uniform(-5,5)
    a.append([x,y,z])
    print(x,y,z)
    
for aa in a:
    if aa[2] <= -2:
        bpy.ops.mesh.primitive_ico_sphere_add(radius=1, location=[aa[0],aa[1],aa[2]])
    elif aa[2] > -2 and aa[2] <= 1:
        bpy.ops.mesh.primitive_cube_add(size=1,location=[aa[0],aa[1],aa[2]])
    else:        
        bpy.ops.mesh.primitive_cone_add(location=[aa[0],aa[1],aa[2]])
    obj = bpy.context.active_object
    SetMaterial(obj,RndColor())

cubes = []
for obj in bpy.data.objects:
    if "Cube" in obj.name:
        cubes.append(obj)

for cube in cubes:
    location = cube.location
    bpy.data.objects.remove(cube)
    bpy.ops.mesh.primitive_monkey_add(location=location)
    obj = bpy.context.active_object
    SetMaterial(obj,RndColor())

#----------------------------------------------------------------------------------------
if name == "main":
go_1()

@sexfio
Copy link
Author

sexfio commented Oct 7, 2022

@sexfio
Copy link
Author

sexfio commented Oct 7, 2022

Repeat the code for several times. There is about a 5% chance that an object cannot be displayed. The Z coordinate is - nan m
: )
sorry, youtube blocked the codes and URL I sent, I dunno >.<

@CGArtPython
Copy link
Owner

Thanks for sharing your code!

So the issue seems to be in the order of operations in your code.

On line 52 from your example, you are removing the cube before you use the location reference you got on line 51
https://github.com/sexfio/blender/blob/main/test.py

Basically, there is a good chance that the reference to the location vector object will be invalid.

Notice that I delete the object after using it in my example
https://gist.github.com/CGArtPython/cb53598625fd6dd3f9d1b0f2b49aecb8

This also can be solved by making a copy of the location vector.
You would need to import
import copy
and then
do this
location = copy.copy(cube.location)

So your code would look like this

 for cube in cubes:
	 location = copy.copy(cube.location)
	 bpy.data.objects.remove(cube)
	 bpy.ops.mesh.primitive_monkey_add(location=location)

@sexfio
Copy link
Author

sexfio commented Oct 10, 2022

thank you very much, I made a mistake :)

@sexfio sexfio closed this as completed Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants