The only thing I can see here that may be causing grief is bpy.context.object as opposed to context.scene.objects.active after the primitive add. It works fine for me but there are cases when the active scene object and the context object can differ.
I also suggest getting out of the habit of fully qualified paths like bpy.context.blah.blah.blah and use a context variable instead. When it comes to later and using these snippets in operators and panels the context is just about always passed. In handlers the scene is passed, hence using scene.objects.active.
Another tip is using get as both a setter and getter as shown below for the material. This way you don’t end up with a squillion material.001 .002 etc etc every time you run the test code.
import bpy
from bpy import context
scene = context.scene
material = bpy.data.materials.get("materialstar1", bpy.data.materials.new("materialstar1"))
material.use_nodes= True
material.node_tree.nodes.clear()
diffuse = material.node_tree.nodes.new(type = 'ShaderNodeBsdfDiffuse')
diffuse.inputs["Color"].default_value = (1.0,0.0,0.0,1.0)
diffuse.location = (-100,0)
output = material.node_tree.nodes.new(type = 'ShaderNodeOutputMaterial')
material.node_tree.links.new(diffuse.outputs['BSDF'], output.inputs['Surface'])
output.location = (100,0)
bpy.ops.mesh.primitive_cube_add()
cube = scene.objects.active
if cube is not None:
cube.data.materials.append(material)
do you first need to create a first material then use this ?
or can be use to make first mat
then when you test again it wont create new mat and use the same one !
that is interesting
the script is inside a function that is call from inside and operator
so possible the this scene thing can play tricks here
should l pass the scene var when I call the function for this new mat !
sorry for now but file is becoming very large
would have to remove 1/2 of it to upload and make it simpler
I have another way of making mat in same file which is working well
but I added this new way for experimenting
have to better understand more this bpy scene thing
did not think it was needed in this case !
did the modifications and now I get
AttributeError: ‘NoneType’ object has no attribute ‘active_material’
it does add the cube but cannot assign the material!
never add this problem before
may be there was some API change lately !
the script is inside a function that is call from inside and operator
so possible the this scene thing can play tricks here
should l pass the scene var when I call the function for this new mat !
The scene or the context. Run some tests, scene.objects.active vs context.object vs context.active_object etc. I have seen occasion when they differ.
sorry for now but file is becoming very large
would have to remove 1/2 of it to upload and make it simpler
I have another way of making mat in same file which is working well
but I added this new way for experimenting
have to better understand more this bpy scene thing
did not think it was needed in this case !
thanks
Quite possibly not. It is just good practice IMHO. 99.99% of the time scripts like
# some operator
def execute(self, context):
bpy.context.scene.objects.active = bpy.context.scene.objects.get(bpy.context.scene.myStringProp)
if bpy.context.active_object is not None:
bpy.context.scene.update()
which will lead you to your next error where you have pulled “ob” out of your hat on line 502 needs to be cube.copy() (I assume), then obs needs to be defined to obs = [], which then leads to sce not being defined, use scene instead… and whallah no more runtime errors for 1 Real Stars selection.
so the error had nothing to do with the indicated line it was after that part !
not funny loss a few hours on that one last night !
I was only beginning to add the part for adding stars
and it was not complete I had that working in another script
but thanks for indicating the changes
got a questions about that part
this copy thing is it link copy or not link
cause I tried to change color for each cube but it looks like they are all link mat
is there a way to have this with unlink mat ?
still wondering if the best shape is a cube
but have to think that with lots of stars verts count goes up fast
I got another table where count = 9000
but have to find way to limit that to visible stars only
so should be less and still not certain what verts count will be
and may have to change scale of cube function of MAG but not with this table which does not have this var!
if you have good sites for doing other things in the sky like nebulas or clouds or meteors shooting stars ect.
let me know
should make an interesting script for cycles
already got 1 / 2 examples for galaxies