अजगर के साथ एक बनावट नक्शा बनाओ

Aug 15 2020

मैं 2.83 ब्लेंडर का उपयोग करता हूं। एक ऐडऑन (3 डी-व्यू से सुलभ) के लिए मैं बनावट के नक्शे बनाना चाहता हूं। मैं इसे स्क्रिप्टिंग के बिना पूरी तरह से करने में सक्षम हूं। लेकिन अजगर के साथ मैं सफ़र करने वाला नहीं था।

अपनी समस्या को सबसे कम करने के लिए न्यूनतम मैं मान्य यूवी के साथ एक चयनित वस्तु के साथ शुरू करता हूं। मैं फिर निम्नलिखित अजगर स्क्रिप्ट को निष्पादित करता हूं:

import bpy

obj = bpy.context.active_object

# Creating a texture node, linked with a new image
mat = obj.data.materials[0]
mat.use_nodes = True
texImage = mat.node_tree.nodes.new('ShaderNodeTexImage')
img = bpy.ops.image.new(name= obj.name + '_BakedTexture')
texImage = img

# !!! No image is linked to the texture node !!!

# Baking to the newly created image
# The following part works if I create the texture node and the assigning of the image by hand
bpy.context.view_layer.objects.active = obj
bpy.ops.object.bake(type='DIFFUSE', save_mode='EXTERNAL', filepath='C:\\TEMP\\baked.png', use_automatic_name=True, width=512, height=512)

मुझे लगता है कि जो मैं याद कर रहा हूं वह छवि को बनावट नोड से जोड़ने का सही तरीका है। मैंने ऐसे ही सवालों के जवाब दिए

अजगर के साथ सक्रिय छवि नोड सेट करें

लेकिन उनके जवाबों ने मदद नहीं की (ब्लेंडर 2.7 के लिए कोड जो अब संगत नहीं है, मुझे लगता है)।

जवाब

3 NoobCat Aug 22 2020 at 01:23

मुझे लगता है कि मैं आपको कुछ बुनियादी कदम दूँगा, अर्थात्, "TEX_IMAGE" प्रकार का एक नोड जोड़ें, अपनी छवि को इसे असाइन करें। इसलिए:

obj = bpy.context.active_object
# You can choose your texture size (This will be the de bake image)
image_name = obj.name + '_BakedTexture'
img = bpy.data.images.new(image_name,1024,1024)

   
#Due to the presence of any multiple materials, it seems necessary to iterate on all the materials, and assign them a node + the image to bake.
for mat in obj.data.materials:

    mat.use_nodes = True #Here it is assumed that the materials have been created with nodes, otherwise it would not be possible to assign a node for the Bake, so this step is a bit useless
    nodes = mat.node_tree.nodes
    texture_node =nodes.new('ShaderNodeTexImage')
    texture_node.name = 'Bake_node'
    texture_node.select = True
    nodes.active = texture_node
    texture_node.image = img #Assign the image to the node
    
bpy.context.view_layer.objects.active = obj
bpy.ops.object.bake(type='DIFFUSE', save_mode='EXTERNAL')

img.save_render(filepath='C:\\TEMP\\baked.png')
    
#In the last step, we are going to delete the nodes we created earlier
for mat in obj.data.materials:
    for n in mat.node_tree.nodes:
        if n.name == 'Bake_node':
            mat.node_tree.nodes.remove(n)

Bpy.ops का उपयोग करने के लिए जितना संभव हो उतना बचें, इस मामले में, जैसा कि आप देख सकते हैं, bpy.data.images.new () तरीकों के साथ एक छवि बनाना आसान है किसी भी ब्लेंडर ऑब्जेक्ट को उस पद्धति से बनाया जा सकता है।

bpy.data.objects.new()
bpy.data.materials.new()
bpy.data.scenes.new()
...
...

bpy.ops यह केवल तभी उपयोग करना अच्छा है जब आपके पास कोई विकल्प नहीं है, जैसा कि इस मामले में है bpy.ops.object.bake()

यह आपको कुछ बनाने की अनुमति देता है, और इसे सीधे एक चर के लिए असाइन करता है, जैसा कि img = bpy.data.images.new()सो के उदाहरण में आपको इसका उपयोग करने के लिए पागल होने की आवश्यकता नहीं है।