node cycles mat not working ?

I can run this as a script and it works fine

but If I add this in a more complex script with menu panel

material = 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()
bpy.context.object.data.materials.append(material)

%%%%%%%%%%

error
les_Stars_SKy1.py", line 1006, in realstarsearth1
AttributeError: ‘NoneType’ object has no attribute ‘data’

any way to make this work and add new mat on new primitive object ?

thanks

17,000 posts and you still don’t use the code formatting option of the forum. What’s up Ricky?:smiley:

can you upload the larger python file so we can actually disect what is causing the error?

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)

about this get and set command

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 !

thanks

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 !

let me try to do a shorter script in file

thanks

No, Yes and Yes.

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

You can add zip files, or use pasteall.org.

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()

work fine, but reading them shits me no end.

ok I did a new shorter file for testing

run script in text editor

in propertie world panel at bottom new panel
select first menu1 =1
select menu2 = 1
function call is
def realstarsearth1(matname1)

the error now is that there is no active material !

strange cause this alone in a script run normally!

ztestmat1.blend (133 KB)

thanks

Hmmmmm…

Change line 494 from


bpy.context.object.active_material.diffuse_color = obcol

to


cube.active_material.diffuse_color = obcol

which is exactly the same error as OP.

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

thanks

when I upload new file in text editor I often get an error like
Error loading text, line lengths differ

any idea how not to have this

thanks

Hi Ricky,

I’m very new to cycles, and in my search to find a way to emulate object color (for drivers) I came across this http://blendersushi.blogspot.com.au/2012/12/cycles-object-info-node.html.

Otherwise set up multiple materials I suppose.

A cube with one subsurf gives a reasonable star, assuming you are rendering at some distance.

As for line lengths differ, never seen that, only inconsistent indent.