new text file and data block ?

i know how to create a new text file in texte edtior space

but how do you pass a data text block to it ?

like i got a small OSL script that i want to pass to a new text file

how do you write organise that in python to pass each line one by one?

thanks

you mean insert a python string into an opened or new text datablock?

yes i have a few lines for an osl script
and need to add it to a newly opened text file in text editor

so how do you add these text lines?

#include “node_texture.h”
shader Crackle(
point p = P,
output float Fac1 = 0,
output float Fac2 = 0,
output float Fac3 = 0,
output float Fac4 = 0
){
float da[4];
point pa[4];
voronoi(p, “Distance Squared”, 0, da, pa);

Fac1 = da[0];
Fac2 = da[1];
Fac3 = da[2];
Fac4 = da[3];
}

thanks

Are you doing this from a file? If so, yo could do it this way:


import bpy

path = 'C://mypath//myfile.file' #filepath to your file
file = open(path, 'rt')
mytext = bpy.data.texts.new('myText')
line = file.readline()
while line:
    mytext.write(line)
    line = file.readline()
file.close()

these little osl are so small
i want to add it directly from the script

so what do i do make a list of items
and one item is one line
then write it to the new file in tet editor ?

thanks


mytext = bpy.data.texts.new('myText')
for item in oslList:
    myText.write(item+'
')

ok what is this command ?
mytext = bpy.data.texts.new(‘myText’)

never use it before
if i make a list why not use the list directly ?

and how do you add an empty line ?

thanks

It is just making the new text file to write to. If you already have that file created, it’s unnecessary.

ok i already created the file and named it

now it did add the list to file

but how do you add an empty line ?

ok i call it from node editor
now will have to test if it works as it should!

do you know how to add it in node editor from script
and select right name for OSL?

thanks

myText.write('
') # adds empty line

I don’t understand what you mean by ‘add it in node editor from script and select right name for OSL’.

sorry i was talking in the list of item
do i simply add
list1,append (’ ’ ) ?

for nodes script
i add whatever node i want with something like this

bright1 = m1.makeNode(‘ShaderNodeBrightContrast’,‘bright1’)
bright1.inputs[“Bright”].default_value = 1.1
bright1.inputs[“Contrast”].default_value = 2.2
bright1.inputs[“Color”].default_value = [1.0, 1.0, 1.0, 1] # White1
bright1.location =-600,300

but now if i want to activate this new script nodes is it same way ?

i can see this node thing

ShaderNodeScript
but then there must be a way to add the proper name for the script
may be with an input line

RGBmix3.blend_type=“MULTIPLY”?

i’ll add this new OSL function to my big script and try it later on today
and see if it can work as other nodes

thanks

ok added this to my big script and it crashes !
no error hard crash!

would there be something impossible to make a new OSL from inside a function in a script ?

bug ?

in 2.69 i sometime can reload file but it is refusing to run script
have to close file re open then i can run it
did you see that problem ?
is that a new bug for script

thanks

few examples how to deal with text:

import bpy

osl_str = """
#include "node_texture.h"
shader Crackle(
    point p = P,
    output float Fac1 = 0,
    output float Fac2 = 0,
    output float Fac3 = 0,
    output float Fac4 = 0
){
    float da[4];
    point pa[4];
    voronoi(p, "Distance Squared", 0, da, pa);
    
    Fac1 = da[0];
    Fac2 = da[1];
    Fac3 = da[2];
    Fac4 = da[3];
}
"""

# Write to new text datablock
t = bpy.data.texts.new("crackle.osl")
override = {'edit_text': t}
bpy.ops.text.insert(override, text="New text datablock,
text added with an operator.")

# Replace text
t.from_string("## foobar ##")

# Re-position cursor with operator and add text using low-level API
bpy.ops.text.move(override, type='FILE_TOP')
t.write(osl_str)

tried little script
and get errors

PyContext ‘window’ not found
PyContext ‘window’ not found
PyContext ‘area’ not found
PyContext ‘area’ not found
PyContext ‘area’ not found

what does the replace line does ?
and is there is a string where is it ?

thanks

tried little script and get errors

PyContext warnings are not errors. You can either ignore them, or pass the context members it asks for to suppress the warnings.

what does the replace line does ?

It does replace?! It replaces the entire text of the text datablock.

and is there is a string where is it ?

No clue what you mean.

for the string thing i was referring to the replace line
t.from_string("## foobar ##")

was not certain if this was a specail command or a string function!

for this method it should work when calling it from another script
will test this later on today

thanks

@cmomoney

i ran the littel snippet in a seperate file
and it works

now if i add this into another script inside a function
it will add the new text editor ols snippet but then i think it get stuck into that new text editor and then crashes

is there some command to bring back to the first script so that it can continue to run ?

thanks

did a test in a function
and it does add the new script node!

i tried to spec the name
but get this error
scriptmatlib4.blend\cyclesscriptmatlib4.py", line 9084, in OSLVoronoiCrackle2
TypeError: bpy_struct: item.attr = val: ShaderNodeScript.script expected a Text
type, not str

wiht

script1 = m1.makeNode(‘ShaderNodeScript’, ‘script1’)
script1.location =-600,0
script1.script=‘crackle.osl’

any idea waht text type can be ?

thanks

It is expecting an actual Text, you gave it the name of the Text in the form of a string.

was not certain if this was a specail command or a string function!

t is clearly a textblock reference, how would it be a string function?

It is expecting an actual Text, you gave it the name of the Text in the form of a string.

yep, you need to pass a textblock reference to point it to an internal script. Or switch node to external script?